|
1
|
|
|
<?php |
|
2
|
|
|
namespace Drupal\Driver\Plugin\DriverEntity; |
|
3
|
|
|
|
|
4
|
|
|
use Drupal\Driver\Plugin\DriverEntityPluginDrupal8Base; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A driver field plugin used to test selecting an arbitrary plugin. |
|
8
|
|
|
* |
|
9
|
|
|
* @DriverEntity( |
|
10
|
|
|
* id = "role8", |
|
11
|
|
|
* version = 8, |
|
12
|
|
|
* weight = -100, |
|
13
|
|
|
* entityTypes = { |
|
14
|
|
|
* "user_role", |
|
15
|
|
|
* }, |
|
16
|
|
|
* ) |
|
17
|
|
|
*/ |
|
18
|
|
|
class RoleDrupal8 extends DriverEntityPluginDrupal8Base |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The id of the attached user. |
|
23
|
|
|
* |
|
24
|
|
|
* @var integer; |
|
25
|
|
|
* |
|
26
|
|
|
* @deprecated Use id() instead. |
|
27
|
|
|
*/ |
|
28
|
|
|
public $uid; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function delete() |
|
34
|
|
|
{ |
|
35
|
|
|
user_cancel(array(), $this->id(), 'user_cancel_delete'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function load($entityId) |
|
42
|
|
|
{ |
|
43
|
|
|
$entity = parent::load($entityId); |
|
44
|
|
|
$this->uid = $this->id(); |
|
|
|
|
|
|
45
|
|
|
return $entity; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function save() |
|
52
|
|
|
{ |
|
53
|
|
|
parent::save(); |
|
54
|
|
|
$this->uid = $this->id(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getNewEntity() |
|
61
|
|
|
{ |
|
62
|
|
|
$entity = parent::getNewEntity(); |
|
63
|
|
|
$entity->set('status', 1); |
|
64
|
|
|
return $entity; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Grant permissions to role by permission machine name or label. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string|array $permissions |
|
71
|
|
|
* The permissions to be granted, identifed by string machine nam or label. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function grantPermissions($permissions) |
|
74
|
|
|
{ |
|
75
|
|
|
// Allow single string value, as Role::grantPermission does. |
|
76
|
|
|
if (is_string($permissions)) { |
|
77
|
|
|
$permissions = [$permissions]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Convert labels to machine names. |
|
81
|
|
|
$this->convertPermissions($permissions); |
|
82
|
|
|
// Check the all the permissions strings are valid. |
|
83
|
|
|
$this->checkPermissions($permissions); |
|
84
|
|
|
|
|
85
|
|
|
$this->set('permissions', $permissions); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Retrieve all permissions. |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
* Array of all defined permissions. |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getAllPermissions() |
|
95
|
|
|
{ |
|
96
|
|
|
$permissions = &drupal_static(__FUNCTION__); |
|
97
|
|
|
|
|
98
|
|
|
if (!isset($permissions)) { |
|
99
|
|
|
$permissions = \Drupal::service('user.permissions')->getPermissions(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $permissions; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Convert any permission labels to machine name. |
|
107
|
|
|
* |
|
108
|
|
|
* @param array &$permissions |
|
109
|
|
|
* Array of permission names. |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function convertPermissions(array &$permissions) |
|
112
|
|
|
{ |
|
113
|
|
|
$all_permissions = $this->getAllPermissions(); |
|
114
|
|
|
|
|
115
|
|
|
foreach ($all_permissions as $name => $definition) { |
|
116
|
|
|
$key = array_search($definition['title'], $permissions); |
|
117
|
|
|
if (false !== $key) { |
|
118
|
|
|
$permissions[$key] = $name; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Check to make sure that the array of permissions are valid. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $permissions |
|
127
|
|
|
* Permissions to check. |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function checkPermissions(array &$permissions) |
|
130
|
|
|
{ |
|
131
|
|
|
$available = array_keys($this->getAllPermissions()); |
|
132
|
|
|
|
|
133
|
|
|
foreach ($permissions as $permission) { |
|
134
|
|
|
if (!in_array($permission, $available)) { |
|
135
|
|
|
throw new \RuntimeException(sprintf('Invalid permission "%s".', $permission)); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.