View source
<?php
namespace Drupal\crm_core_contact;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\crm_core_contact\Entity\OrganizationType;
class OrganizationAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermissions($account, [
'administer crm_core_organization entities',
'view any crm_core_organization entity',
'view any crm_core_organization entity of bundle ' . $entity
->bundle(),
], 'OR');
case 'update':
return AccessResult::allowedIfHasPermissions($account, [
'administer crm_core_organization entities',
'edit any crm_core_organization entity',
'edit any crm_core_organization entity of bundle ' . $entity
->bundle(),
], 'OR');
case 'delete':
return AccessResult::allowedIfHasPermissions($account, [
'administer crm_core_organization entities',
'delete any crm_core_organization entity',
'delete any crm_core_organization entity of bundle ' . $entity
->bundle(),
], 'OR');
case 'revert':
return AccessResult::allowedIfHasPermissions($account, [
'administer crm_core_organization entities',
'revert organization record',
], 'OR');
}
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
$organization_type_is_active = empty($entity_bundle);
if (!empty($entity_bundle)) {
$organization_type_entity = OrganizationType::load($entity_bundle);
$organization_type_is_active = $organization_type_entity
->status();
}
return AccessResult::allowedIf($organization_type_is_active)
->andIf(AccessResult::allowedIfHasPermissions($account, [
'administer crm_core_organization entities',
'create crm_core_organization entities',
'create crm_core_organization entities of bundle ' . $entity_bundle,
], 'OR'));
}
}