You are here

class MasqueradePermissions in Masquerade 8.2

Provides dynamic permissions of the masquerade module.

Hierarchy

Expanded class hierarchy of MasqueradePermissions

File

src/MasqueradePermissions.php, line 12

Namespace

Drupal\masquerade
View source
class MasqueradePermissions {
  use StringTranslationTrait;

  /**
   * Returns an array of masquerade permissions.
   *
   * @todo Allow permissions for each role to masquerade as as subset of roles
   *   https://drupal.org/node/1171500
   *
   * @return array
   *   The permissions array.
   */
  public function permissions() {
    $permissions = [];

    // Anonymous was intentionally left out. Logout instead.
    $roles = $this
      ->getUserRoles();
    foreach ($roles as $role) {
      $permissions['masquerade as ' . $role
        ->id()] = [
        'title' => $this
          ->t('Masquerade as @role', [
          '@role' => $role
            ->label(),
        ]),
        'restrict access' => TRUE,
      ];
    }
    return $permissions;
  }

  /**
   * Returns role entities allowed to masquerade as.
   *
   * @return \Drupal\user\RoleInterface[]
   *   An associative array with the role id as the key and the role object as
   *   value.
   */
  protected function getUserRoles() {
    $roles = Role::loadMultiple();

    // Do not allow masquerade as anonymous user, use private browsing.
    unset($roles[RoleInterface::ANONYMOUS_ID]);
    return $roles;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MasqueradePermissions::getUserRoles protected function Returns role entities allowed to masquerade as.
MasqueradePermissions::permissions public function Returns an array of masquerade permissions.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.