You are here

class SwitchAccessCheck in Masquerade 8.2

Checks access for any masquerade permissions.

Hierarchy

Expanded class hierarchy of SwitchAccessCheck

1 string reference to 'SwitchAccessCheck'
masquerade.services.yml in ./masquerade.services.yml
masquerade.services.yml
1 service uses SwitchAccessCheck
access_check.masquerade.switch in ./masquerade.services.yml
Drupal\masquerade\Access\SwitchAccessCheck

File

src/Access/SwitchAccessCheck.php, line 13

Namespace

Drupal\masquerade\Access
View source
class SwitchAccessCheck implements AccessInterface {

  /**
   * The masquerade service.
   *
   * @var \Drupal\masquerade\Masquerade
   */
  protected $masquerade;

  /**
   * Constructs a new UnmasqueradeAccessCheck object.
   *
   * @param \Drupal\masquerade\Masquerade $masquerade
   *   The masquerade service.
   */
  public function __construct(Masquerade $masquerade) {
    $this->masquerade = $masquerade;
  }

  /**
   * Check to see if user has any permissions to masquerade.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account) {
    if ($this->masquerade
      ->isMasquerading()) {

      // Do now allow to masquerade when already masquerading.
      $result = AccessResult::forbidden();
    }
    elseif ($account
      ->id() == 1) {

      // Uid 1 may masquerade as anyone.
      $result = AccessResult::allowed();
    }
    else {

      // Ability to masquerade defined by permissions.
      $permissions = $this->masquerade
        ->getPermissions();
      $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
    }
    return $result
      ->addCacheContexts([
      'session.is_masquerading',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SwitchAccessCheck::$masquerade protected property The masquerade service.
SwitchAccessCheck::access public function Check to see if user has any permissions to masquerade.
SwitchAccessCheck::__construct public function Constructs a new UnmasqueradeAccessCheck object.