1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\RbacModule\models; |
4
|
|
|
|
5
|
|
|
use yii\helpers\ArrayHelper; |
6
|
|
|
use yii\rbac\{Item, Role as BaseRole, ManagerInterface}; |
7
|
|
|
use Itstructure\RbacModule\interfaces\ModelInterface; |
8
|
|
|
use Itstructure\RbacModule\Module; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Role |
12
|
|
|
* |
13
|
|
|
* @property BaseRole $role |
14
|
|
|
* @property array $permissions |
15
|
|
|
* @property ManagerInterface $authManager |
16
|
|
|
* |
17
|
|
|
* @package Itstructure\RbacModule\models |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class Role extends Rbac implements ModelInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Role object. |
23
|
|
|
* |
24
|
|
|
* @var BaseRole |
25
|
|
|
*/ |
26
|
|
|
public $role; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Child permissions. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public $permissions = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validate rules. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function rules() |
41
|
|
|
{ |
42
|
|
|
return ArrayHelper::merge( |
43
|
|
|
parent::rules(), |
44
|
|
|
[ |
45
|
|
|
[ |
46
|
|
|
[ |
47
|
|
|
'permissions', |
48
|
|
|
], |
49
|
|
|
'required', |
50
|
|
|
], |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function attributeLabels() |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
'name' => Module::t('roles', 'Name'), |
62
|
|
|
'description' => Module::t('roles', 'Description'), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get current child permissions assigned to current role. |
68
|
|
|
* |
69
|
|
|
* @return \yii\rbac\Permission[] |
70
|
|
|
*/ |
71
|
|
|
protected function getCurrentChildren(): array |
72
|
|
|
{ |
73
|
|
|
$permissions = $this->authManager->getPermissionsByRole($this->getOldName()); |
74
|
|
|
|
75
|
|
|
return array_keys($permissions); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get new child permissions. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function getNewChildren(): array |
84
|
|
|
{ |
85
|
|
|
return empty($this->permissions) ? [] : $this->permissions; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create new role. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* |
93
|
|
|
* @return Item |
94
|
|
|
*/ |
95
|
|
|
protected function createItem(string $name): Item |
96
|
|
|
{ |
97
|
|
|
return $this->authManager->createRole($name); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create child permission. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* |
105
|
|
|
* @return Item |
106
|
|
|
*/ |
107
|
|
|
protected function createChild(string $name): Item |
108
|
|
|
{ |
109
|
|
|
return $this->authManager->createPermission($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns old name in current object, which is set before new value. |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
protected function getOldName() |
118
|
|
|
{ |
119
|
|
|
return null === $this->role ? null : $this->role->name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns role object. |
124
|
|
|
* |
125
|
|
|
* @param string $key |
126
|
|
|
* |
127
|
|
|
* @return Item|null |
128
|
|
|
*/ |
129
|
|
|
protected function getItem(string $key) |
130
|
|
|
{ |
131
|
|
|
return $this->authManager->getRole($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set role field for child instance of Rbac - Role. |
136
|
|
|
* |
137
|
|
|
* @param Rbac $instance |
138
|
|
|
* @param Item $item |
139
|
|
|
* |
140
|
|
|
* @return Rbac |
141
|
|
|
*/ |
142
|
|
|
protected function setItemForInstance(Rbac $instance, Item $item): Rbac |
143
|
|
|
{ |
144
|
|
|
$instance->role = $item; |
|
|
|
|
145
|
|
|
return $instance; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set children permissions for instance of Role. |
150
|
|
|
* |
151
|
|
|
* @param Rbac $instance |
152
|
|
|
* |
153
|
|
|
* @return Rbac |
154
|
|
|
*/ |
155
|
|
|
protected function setChildrenForInstance(Rbac $instance): Rbac |
156
|
|
|
{ |
157
|
|
|
$instance->permissions = $instance->getCurrentChildren(); |
|
|
|
|
158
|
|
|
return $instance; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.