1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\RbacModule\models; |
4
|
|
|
|
5
|
|
|
use yii\helpers\ArrayHelper; |
6
|
|
|
use yii\rbac\{Item, Permission as BasePermission, ManagerInterface}; |
7
|
|
|
use Itstructure\RbacModule\interfaces\ModelInterface; |
8
|
|
|
use Itstructure\RbacModule\Module; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Permission |
12
|
|
|
* |
13
|
|
|
* @property BasePermission $permission |
14
|
|
|
* @property array $permissions |
15
|
|
|
* @property ManagerInterface $authManager |
16
|
|
|
* |
17
|
|
|
* @package Itstructure\RbacModule\models |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class Permission extends Rbac implements ModelInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Permission object. |
23
|
|
|
* |
24
|
|
|
* @var BasePermission |
25
|
|
|
*/ |
26
|
|
|
public $permission; |
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
|
|
|
'safe', |
50
|
|
|
], |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function attributeLabels() |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
'name' => Module::t('permissions', 'Name'), |
62
|
|
|
'description' => Module::t('permissions', 'Description'), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get current child permissions assigned to current permission. |
68
|
|
|
* |
69
|
|
|
* @return \yii\rbac\Permission[] |
70
|
|
|
*/ |
71
|
|
|
protected function getCurrentChildren(): array |
72
|
|
|
{ |
73
|
|
|
$permissions = $this->authManager->getChildren($this->getOldName()); |
74
|
|
|
|
75
|
|
|
return array_keys($permissions); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get new child items. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function getNewChildren(): array |
84
|
|
|
{ |
85
|
|
|
return empty($this->permissions) ? [] : $this->permissions; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create new Permission. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* |
93
|
|
|
* @return Item |
94
|
|
|
*/ |
95
|
|
|
protected function createItem(string $name): Item |
96
|
|
|
{ |
97
|
|
|
return $this->authManager->createPermission($name); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create self 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->permission ? null : $this->permission->name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns Permission object. |
124
|
|
|
* |
125
|
|
|
* @param string $key |
126
|
|
|
* |
127
|
|
|
* @return Item|null |
128
|
|
|
*/ |
129
|
|
|
protected function getItem(string $key) |
130
|
|
|
{ |
131
|
|
|
return $this->authManager->getPermission($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set permission field for child instance of Rbac - Permission. |
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->permission = $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.