function PermissionsHashGenerator::generate

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

Overrides PermissionsHashGeneratorInterface::generate

File

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

Class

PermissionsHashGenerator
Generates and caches the permissions hash for a user.

Namespace

Drupal\Core\Session

Code

public function generate(AccountInterface $account) {
    // We can use a simple per-user static cache here because we already cache
    // the permissions more efficiently in the access policy processor. On top
    // of that, there is only a tiny chance of a hash being generated for more
    // than one account during a single request.
    $cid = 'permissions_hash_' . $account->id();
    // Retrieve the hash from the static cache if available.
    if ($static_cache = $this->static
        ->get($cid)) {
        return $static_cache->data;
    }
    // Otherwise hash the permissions and store them in the static cache.
    $calculated_permissions = $this->processor
        ->processAccessPolicies($account);
    $item = $calculated_permissions->getItem();
    // This should never happen, but in case nothing defined permissions for the
    // current user, even if empty, we need to have _some_ hash too.
    if ($item === FALSE) {
        $hash = 'no-access-policies';
    }
    elseif ($item->isAdmin()) {
        $hash = 'is-admin';
    }
    else {
        $permissions = $item->getPermissions();
        sort($permissions);
        $hash = $this->hash(serialize($permissions));
    }
    $this->static
        ->set($cid, $hash, Cache::PERMANENT, $calculated_permissions->getCacheTags());
    return $hash;
}

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