You are here

protected function IndividualTypeAccessControlHandler::checkAccess in CRM Core 8.3

Same name and namespace in other branches
  1. 8 modules/crm_core_contact/src/IndividualTypeAccessControlHandler.php \Drupal\crm_core_contact\IndividualTypeAccessControlHandler::checkAccess()
  2. 8.2 modules/crm_core_contact/src/IndividualTypeAccessControlHandler.php \Drupal\crm_core_contact\IndividualTypeAccessControlHandler::checkAccess()

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides EntityAccessControlHandler::checkAccess

File

modules/crm_core_contact/src/IndividualTypeAccessControlHandler.php, line 18

Class

IndividualTypeAccessControlHandler
Access control handler for CRM Core Individual type entities.

Namespace

Drupal\crm_core_contact

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\crm_core_contact\Entity\IndividualType $entity */

  // First check permission.
  if (parent::checkAccess($entity, $operation, $account)
    ->isForbidden()) {
    return AccessResult::forbidden();
  }
  switch ($operation) {
    case 'delete':

      // If individual instance of this individual type exist, you can't
      // delete it.
      $results = \Drupal::entityQuery('crm_core_individual')
        ->condition('type', $entity
        ->id())
        ->execute();
      return AccessResult::allowedIf(empty($results));

    // @todo Which is it?
    case 'view':
    case 'edit':
    case 'update':

      // If the individual type is locked, you can't edit it.
      return AccessResult::allowed();
  }
}