public function PermissionsHashGenerator::generate

public PermissionsHashGenerator::generate(AccountInterface $account)

Cached by role, invalidated whenever permissions change.

Overrides PermissionsHashGeneratorInterface::generate

File

core/lib/Drupal/Core/Session/PermissionsHashGenerator.php, line 57

Class

PermissionsHashGenerator
Generates and caches the permissions hash for a user.

Namespace

Drupal\Core\Session

Code

public function generate(AccountInterface $account) {
  // User 1 is the super user, and can always access all permissions. Use a
  // different, unique identifier for the hash.
  if ($account->id() == 1) {
    return $this->hash('is-super-user');
  }

  $sorted_roles = $account->getRoles();
  sort($sorted_roles);
  $role_list = implode(',', $sorted_roles);
  $cid = "user_permissions_hash:$role_list";
  if ($static_cache = $this->static->get($cid)) {
    return $static_cache->data;
  }
  else {
    $tags = Cache::buildTags('config:user.role', $sorted_roles, '.');
    if ($cache = $this->cache->get($cid)) {
      $permissions_hash = $cache->data;
    }
    else {
      $permissions_hash = $this->doGenerate($sorted_roles);
      $this->cache->set($cid, $permissions_hash, Cache::PERMANENT, $tags);
    }
    $this->static->set($cid, $permissions_hash, Cache::PERMANENT, $tags);
  }

  return $permissions_hash;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Session!PermissionsHashGenerator.php/function/PermissionsHashGenerator::generate/8.1.x