You are here

protected function ViewProfilesPermsAccessHandler::checkAccess in View profiles permissions 2.0.x

Same name and namespace in other branches
  1. 8 src/ViewProfilesPermsAccessHandler.php \Drupal\view_profiles_perms\ViewProfilesPermsAccessHandler::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 UserAccessControlHandler::checkAccess

File

src/ViewProfilesPermsAccessHandler.php, line 18

Class

ViewProfilesPermsAccessHandler
Defines an access control handler for the user entity type.

Namespace

Drupal\view_profiles_perms

Code

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

  // Parent class allowing access takes precedence.

  /* @var $entity \Drupal\user\UserInterface */
  $access = parent::checkAccess($entity, $operation, $account);
  if ($access
    ->isAllowed()) {
    return $access;
  }

  // Respect the conditions on view of the parent class.
  // @see \Drupal\user\UserAccessControlHandler::checkAccess()
  if ($operation == 'view' && $entity
    ->isActive() && $account
    ->id() !== $entity
    ->id()) {
    foreach ($entity
      ->getRoles(TRUE) as $role) {
      $permission_access = AccessResult::allowedIfHasPermission($account, "access {$role} users profiles");
      if ($permission_access
        ->isAllowed()) {
        return $permission_access;
      }
    }
  }
  return $access;
}