function PermissionsHashGenerator::generate

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Session/PermissionsHashGenerator.php \Drupal\Core\Session\PermissionsHashGenerator::generate()
  2. 10 core/lib/Drupal/Core/Session/PermissionsHashGenerator.php \Drupal\Core\Session\PermissionsHashGenerator::generate()
  3. 11.x core/lib/Drupal/Core/Session/PermissionsHashGenerator.php \Drupal\Core\Session\PermissionsHashGenerator::generate()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.