Newer
Older
<?php
/**
* @file
* Contains Drupal\user\Entity\Role.
*/
namespace Drupal\user\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\user\RoleInterface;
/**
* Defines the user role entity class.
*
Alex Pott
committed
* @ConfigEntityType(
* id = "user_role",
* label = @Translation("Role"),
* controllers = {
Alex Pott
committed
* "storage" = "Drupal\user\RoleStorageController",
* "access" = "Drupal\user\RoleAccessController",
Alex Pott
committed
* "list" = "Drupal\user\RoleListController",
* "form" = {
* "default" = "Drupal\user\RoleFormController",
* "delete" = "Drupal\user\Form\UserRoleDelete"
Alex Pott
committed
* }
* admin_permission = "administer permissions",
* config_prefix = "role",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "weight" = "weight",
* "label" = "label"
Alex Pott
committed
* },
* links = {
Alex Pott
committed
* "delete-form" = "user.role_delete",
* "edit-form" = "user.role_edit",
* "edit-permissions-form" = "user.admin_permission"
* }
* )
*/
class Role extends ConfigEntityBase implements RoleInterface {
/**
* The machine name of this role.
*
* @var string
*/
public $id;
/**
* The UUID of this role.
*
* @var string
*/
public $uuid;
/**
* The human-readable label of this role.
*
* @var string
*/
public $label;
/**
* The weight of this role in administrative listings.
*
* @var int
*/
public $weight;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* The permissions belonging to this role.
*
* @var array
*/
public $permissions = array();
/**
* {@inheritdoc}
*/
public function getPermissions() {
return $this->permissions;
}
/**
* {@inheritdoc}
*/
public function hasPermission($permission) {
return in_array($permission, $this->permissions);
}
/**
* {@inheritdoc}
*/
public function grantPermission($permission) {
if (!$this->hasPermission($permission)) {
$this->permissions[] = $permission;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function revokePermission($permission) {
$this->permissions = array_diff($this->permissions, array($permission));
return $this;
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public static function postLoad(EntityStorageControllerInterface $storage_controller, array &$entities) {
parent::postLoad($storage_controller, $entities);
// Sort the queried roles by their weight.
// See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
uasort($entities, 'static::sort');
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
Angie Byron
committed
parent::preSave($storage_controller);
Dries Buytaert
committed
if (!isset($this->weight) && ($roles = $storage_controller->loadMultiple())) {
// Set a role weight to make this new role last.
$max = array_reduce($roles, function($max, $role) {
return $max > $role->weight ? $max : $role->weight;
});
$this->weight = $max + 1;
}
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
parent::postSave($storage_controller, $update);
Cache::invalidateTags(array('role' => $this->id()));
// Clear render cache.
entity_render_cache_clear();
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
Angie Byron
committed
parent::postDelete($storage_controller, $entities);
$ids = array_keys($entities);
$storage_controller->deleteRoleReferences($ids);
Cache::invalidateTags(array('role' => $ids));
}
Angie Byron
committed