PermissionChecker.php

Same filename and directory in other branches
  1. 11.x core/lib/Drupal/Core/Session/PermissionChecker.php

Namespace

Drupal\Core\Session

File

core/lib/Drupal/Core/Session/PermissionChecker.php

View source
<?php

namespace Drupal\Core\Session;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Checks permissions for an account.
 */
class PermissionChecker implements PermissionCheckerInterface {
    
    /**
     * Constructs a PermissionChecker object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
     *   The entity type manager.
     */
    public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasPermission(string $permission, AccountInterface $account) : bool {
        // User #1 has all privileges.
        if ((int) $account->id() === 1) {
            return TRUE;
        }
        return $this->entityTypeManager
            ->getStorage('user_role')
            ->isPermissionInRoles($permission, $account->getRoles());
    }

}

Classes

Title Deprecated Summary
PermissionChecker Checks permissions for an account.

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