comparison core/modules/user/src/Entity/Role.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\user\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\user\RoleInterface;
8
9 /**
10 * Defines the user role entity class.
11 *
12 * @ConfigEntityType(
13 * id = "user_role",
14 * label = @Translation("Role"),
15 * handlers = {
16 * "storage" = "Drupal\user\RoleStorage",
17 * "access" = "Drupal\user\RoleAccessControlHandler",
18 * "list_builder" = "Drupal\user\RoleListBuilder",
19 * "form" = {
20 * "default" = "Drupal\user\RoleForm",
21 * "delete" = "Drupal\Core\Entity\EntityDeleteForm"
22 * }
23 * },
24 * admin_permission = "administer permissions",
25 * config_prefix = "role",
26 * static_cache = TRUE,
27 * entity_keys = {
28 * "id" = "id",
29 * "weight" = "weight",
30 * "label" = "label"
31 * },
32 * links = {
33 * "delete-form" = "/admin/people/roles/manage/{user_role}/delete",
34 * "edit-form" = "/admin/people/roles/manage/{user_role}",
35 * "edit-permissions-form" = "/admin/people/permissions/{user_role}",
36 * "collection" = "/admin/people/roles",
37 * },
38 * config_export = {
39 * "id",
40 * "label",
41 * "weight",
42 * "is_admin",
43 * "permissions",
44 * }
45 * )
46 */
47 class Role extends ConfigEntityBase implements RoleInterface {
48
49 /**
50 * The machine name of this role.
51 *
52 * @var string
53 */
54 protected $id;
55
56 /**
57 * The human-readable label of this role.
58 *
59 * @var string
60 */
61 protected $label;
62
63 /**
64 * The weight of this role in administrative listings.
65 *
66 * @var int
67 */
68 protected $weight;
69
70 /**
71 * The permissions belonging to this role.
72 *
73 * @var array
74 */
75 protected $permissions = [];
76
77 /**
78 * An indicator whether the role has all permissions.
79 *
80 * @var bool
81 */
82 protected $is_admin;
83
84 /**
85 * {@inheritdoc}
86 */
87 public function getPermissions() {
88 if ($this->isAdmin()) {
89 return [];
90 }
91 return $this->permissions;
92 }
93
94 /**
95 * {@inheritdoc}
96 */
97 public function getWeight() {
98 return $this->get('weight');
99 }
100
101 /**
102 * {@inheritdoc}
103 */
104 public function setWeight($weight) {
105 $this->set('weight', $weight);
106 return $this;
107 }
108
109 /**
110 * {@inheritdoc}
111 */
112 public function hasPermission($permission) {
113 if ($this->isAdmin()) {
114 return TRUE;
115 }
116 return in_array($permission, $this->permissions);
117 }
118
119 /**
120 * {@inheritdoc}
121 */
122 public function grantPermission($permission) {
123 if ($this->isAdmin()) {
124 return $this;
125 }
126 if (!$this->hasPermission($permission)) {
127 $this->permissions[] = $permission;
128 }
129 return $this;
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 public function revokePermission($permission) {
136 if ($this->isAdmin()) {
137 return $this;
138 }
139 $this->permissions = array_diff($this->permissions, [$permission]);
140 return $this;
141 }
142
143 /**
144 * {@inheritdoc}
145 */
146 public function isAdmin() {
147 return (bool) $this->is_admin;
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function setIsAdmin($is_admin) {
154 $this->is_admin = $is_admin;
155 return $this;
156 }
157
158 /**
159 * {@inheritdoc}
160 */
161 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
162 parent::postLoad($storage, $entities);
163 // Sort the queried roles by their weight.
164 // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
165 uasort($entities, 'static::sort');
166 }
167
168 /**
169 * {@inheritdoc}
170 */
171 public function preSave(EntityStorageInterface $storage) {
172 parent::preSave($storage);
173
174 if (!isset($this->weight) && ($roles = $storage->loadMultiple())) {
175 // Set a role weight to make this new role last.
176 $max = array_reduce($roles, function ($max, $role) {
177 return $max > $role->weight ? $max : $role->weight;
178 });
179 $this->weight = $max + 1;
180 }
181
182 if (!$this->isSyncing()) {
183 // Permissions are always ordered alphabetically to avoid conflicts in the
184 // exported configuration.
185 sort($this->permissions);
186 }
187 }
188
189 }