Skip to content
Snippets Groups Projects

Issue #3347873: Centralize permission checks in a service

All threads resolved!
All threads resolved!
Files
6
<?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(protected 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());
}
}
Loading