Role.php in Drupal 9
File
core/modules/user/src/Entity/Role.php
View source
<?php
namespace Drupal\user\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\user\RoleInterface;
class Role extends ConfigEntityBase implements RoleInterface {
protected $id;
protected $label;
protected $weight;
protected $permissions = [];
protected $is_admin;
public function getPermissions() {
if ($this
->isAdmin()) {
return [];
}
return $this->permissions;
}
public function getWeight() {
return $this
->get('weight');
}
public function setWeight($weight) {
$this
->set('weight', $weight);
return $this;
}
public function hasPermission($permission) {
if ($this
->isAdmin()) {
return TRUE;
}
return in_array($permission, $this->permissions);
}
public function grantPermission($permission) {
if ($this
->isAdmin()) {
return $this;
}
if (!$this
->hasPermission($permission)) {
$this->permissions[] = $permission;
}
return $this;
}
public function revokePermission($permission) {
if ($this
->isAdmin()) {
return $this;
}
$this->permissions = array_diff($this->permissions, [
$permission,
]);
return $this;
}
public function isAdmin() {
return (bool) $this->is_admin;
}
public function setIsAdmin($is_admin) {
$this->is_admin = $is_admin;
return $this;
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
parent::postLoad($storage, $entities);
uasort($entities, 'static::sort');
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!isset($this->weight) && ($roles = $storage
->loadMultiple())) {
$max = array_reduce($roles, function ($max, $role) {
return $max > $role->weight ? $max : $role->weight;
});
$this->weight = $max + 1;
}
if (!$this
->isSyncing()) {
sort($this->permissions);
}
}
public function calculateDependencies() {
parent::calculateDependencies();
$permission_definitions = \Drupal::service('user.permissions')
->getPermissions();
$valid_permissions = array_intersect($this->permissions, array_keys($permission_definitions));
$invalid_permissions = array_diff($this->permissions, $valid_permissions);
if (!empty($invalid_permissions) && !$this
->get('skip_missing_permission_deprecation')) {
@trigger_error('Adding non-existent permissions to a role is deprecated in drupal:9.3.0 and triggers a runtime exception before drupal:10.0.0. The incorrect permissions are "' . implode('", "', $invalid_permissions) . '". Permissions should be defined in a permissions.yml file or a permission callback. See https://www.drupal.org/node/3193348', E_USER_DEPRECATED);
}
foreach ($valid_permissions as $permission) {
$this
->addDependency('module', $permission_definitions[$permission]['provider']);
if (!empty($permission_definitions[$permission]['dependencies'])) {
$this
->addDependencies($permission_definitions[$permission]['dependencies']);
}
}
return $this;
}
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
$permission_definitions = \Drupal::service('user.permissions')
->getPermissions();
foreach ([
'content',
'config',
] as $type) {
$dependencies[$type] = array_keys($dependencies[$type]);
}
foreach ($this->permissions as $key => $permission) {
if (!isset($permission_definitions[$permission])) {
continue;
}
if (in_array($permission_definitions[$permission]['provider'], $dependencies['module'], TRUE)) {
unset($this->permissions[$key]);
$changed = TRUE;
continue;
}
if (isset($permission_definitions[$permission]['dependencies'])) {
foreach ($permission_definitions[$permission]['dependencies'] as $type => $list) {
if (array_intersect($list, $dependencies[$type])) {
unset($this->permissions[$key]);
$changed = TRUE;
continue 2;
}
}
}
}
return $changed;
}
}
Classes
Name |
Description |
Role |
Defines the user role entity class. |