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 | 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 | 'permissions' => Module::t('permissions', 'Permissions'), |
||
64 | ]; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get current child permissions assigned to current permission. |
||
69 | * |
||
70 | * @return \yii\rbac\Permission[] |
||
71 | */ |
||
72 | protected function getCurrentChildren(): array |
||
73 | { |
||
74 | $permissions = $this->authManager->getChildren($this->getOldName()); |
||
75 | |||
76 | return array_keys($permissions); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get new child items. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | protected function getNewChildren(): array |
||
85 | { |
||
86 | return empty($this->permissions) ? [] : $this->permissions; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Create new Permission. |
||
91 | * |
||
92 | * @param string $name |
||
93 | * |
||
94 | * @return Item |
||
95 | */ |
||
96 | protected function createItem(string $name): Item |
||
97 | { |
||
98 | return $this->authManager->createPermission($name); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Create self child permission. |
||
103 | * |
||
104 | * @param string $name |
||
105 | * |
||
106 | * @return Item |
||
107 | */ |
||
108 | protected function createChild(string $name): Item |
||
109 | { |
||
110 | return $this->authManager->createPermission($name); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns old name in current object, which is set before new value. |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | protected function getOldName() |
||
119 | { |
||
120 | return null === $this->permission ? null : $this->permission->name; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Returns Permission object. |
||
125 | * |
||
126 | * @param string $key |
||
127 | * |
||
128 | * @return Item|null |
||
129 | */ |
||
130 | protected function getItem(string $key) |
||
131 | { |
||
132 | return $this->authManager->getPermission($key); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Set permission field for child instance of Rbac - Permission. |
||
137 | * |
||
138 | * @param Rbac $instance |
||
139 | * @param Item $item |
||
140 | * |
||
141 | * @return Rbac |
||
142 | */ |
||
143 | protected function setItemForInstance(Rbac $instance, Item $item): Rbac |
||
144 | { |
||
145 | $instance->permission = $item; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
146 | return $instance; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Set children permissions for instance of Role. |
||
151 | * |
||
152 | * @param Rbac $instance |
||
153 | * |
||
154 | * @return Rbac |
||
155 | */ |
||
156 | protected function setChildrenForInstance(Rbac $instance): Rbac |
||
157 | { |
||
158 | $instance->permissions = $instance->getCurrentChildren(); |
||
0 ignored issues
–
show
The property
permissions does not exist on Itstructure\RbacModule\models\Rbac . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
159 | return $instance; |
||
160 | } |
||
161 | } |
||
162 |