You are here

public function ContactPermissionsContactPageAccess::access in Contact Permissions 8

Same name and namespace in other branches
  1. 8.2 src/Access/ContactPermissionsContactPageAccess.php \Drupal\contact_permissions\Access\ContactPermissionsContactPageAccess::access()

Checks access to the given user's contact page.

Parameters

\Drupal\user\UserInterface $user: The user being contacted.

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

Return value

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

Overrides ContactPageAccess::access

File

src/Access/ContactPermissionsContactPageAccess.php, line 18

Class

ContactPermissionsContactPageAccess
Access check for contact_personal_page route.

Namespace

Drupal\contact_permissions\Access

Code

public function access(UserInterface $user, AccountInterface $account) {

  /* \Drupal\Core\Access\AccessResult $access */
  $access = parent::access($user, $account);
  if (!$access
    ->isAllowed() && $access
    ->getReason() == "The 'access user contact forms' permission is required.") {
    foreach ($user
      ->getRoles() as $role_id) {
      $permission_access = AccessResult::allowedIfHasPermission($account, "use {$role_id} personal contact forms");
      if ($permission_access
        ->isAllowed()) {
        $access = $permission_access;
        break;
      }
    }
  }
  return $access
    ->andif(AccessResult::allowedIfHasPermission($user, 'have a personal contact form'));
}