1 | <?php |
||
27 | abstract class BaseManager extends Component implements ManagerInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var array a list of role names that are assigned to every user automatically without calling [[assign()]]. |
||
31 | * Note that these roles are applied to users, regardless of their state of authentication. |
||
32 | */ |
||
33 | protected $defaultRoles = []; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Returns the named auth item. |
||
38 | * @param string $name the auth item name. |
||
39 | * @return Item the auth item corresponding to the specified name. Null is returned if no such item. |
||
40 | */ |
||
41 | abstract protected function getItem($name); |
||
42 | |||
43 | /** |
||
44 | * Returns the items of the specified type. |
||
45 | * @param int $type the auth item type (either [[Item::TYPE_ROLE]] or [[Item::TYPE_PERMISSION]] |
||
46 | * @return Item[] the auth items of the specified type. |
||
47 | */ |
||
48 | abstract protected function getItems($type); |
||
49 | |||
50 | /** |
||
51 | * Adds an auth item to the RBAC system. |
||
52 | * @param Item $item the item to add |
||
53 | * @return bool whether the auth item is successfully added to the system |
||
54 | * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique) |
||
55 | */ |
||
56 | abstract protected function addItem($item); |
||
57 | |||
58 | /** |
||
59 | * Adds a rule to the RBAC system. |
||
60 | * @param Rule $rule the rule to add |
||
61 | * @return bool whether the rule is successfully added to the system |
||
62 | * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique) |
||
63 | */ |
||
64 | abstract protected function addRule($rule); |
||
65 | |||
66 | /** |
||
67 | * Removes an auth item from the RBAC system. |
||
68 | * @param Item $item the item to remove |
||
69 | * @return bool whether the role or permission is successfully removed |
||
70 | * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique) |
||
71 | */ |
||
72 | abstract protected function removeItem($item); |
||
73 | |||
74 | /** |
||
75 | * Removes a rule from the RBAC system. |
||
76 | * @param Rule $rule the rule to remove |
||
77 | * @return bool whether the rule is successfully removed |
||
78 | * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique) |
||
79 | */ |
||
80 | abstract protected function removeRule($rule); |
||
81 | |||
82 | /** |
||
83 | * Updates an auth item in the RBAC system. |
||
84 | * @param string $name the name of the item being updated |
||
85 | * @param Item $item the updated item |
||
86 | * @return bool whether the auth item is successfully updated |
||
87 | * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique) |
||
88 | */ |
||
89 | abstract protected function updateItem($name, $item); |
||
90 | |||
91 | /** |
||
92 | * Updates a rule to the RBAC system. |
||
93 | * @param string $name the name of the rule being updated |
||
94 | * @param Rule $rule the updated rule |
||
95 | * @return bool whether the rule is successfully updated |
||
96 | * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique) |
||
97 | */ |
||
98 | abstract protected function updateRule($name, $rule); |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 264 | public function createRole($name) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 263 | public function createPermission($name) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 276 | public function add($object) |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 7 | public function remove($object) |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 22 | public function update($name, $object) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 62 | public function getRole($name) |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 27 | public function getPermission($name) |
|
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | 24 | public function getRoles() |
|
199 | |||
200 | /** |
||
201 | * Set default roles. |
||
202 | * @param array|\Closure $roles either array of roles or a callable returning it |
||
203 | * @since 2.0.14 |
||
204 | */ |
||
205 | 268 | public function setDefaultRoles($roles) |
|
219 | |||
220 | /** |
||
221 | * Get default roles. |
||
222 | * @return array default roles |
||
223 | * @since 2.0.14 |
||
224 | */ |
||
225 | public function getDefaultRoles() |
||
229 | |||
230 | /** |
||
231 | * Returns defaultRoles as array of Role objects. |
||
232 | * @since 2.0.12 |
||
233 | * @return Role[] default roles. The array is indexed by the role names |
||
234 | */ |
||
235 | 22 | public function getDefaultRoleInstances() |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 18 | public function getPermissions() |
|
252 | |||
253 | /** |
||
254 | * Executes the rule associated with the specified auth item. |
||
255 | * |
||
256 | * If the item does not specify a rule, this method will return true. Otherwise, it will |
||
257 | * return the value of [[Rule::execute()]]. |
||
258 | * |
||
259 | * @param string|int $user the user ID. This should be either an integer or a string representing |
||
260 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
261 | * @param Item $item the auth item that needs to execute its rule |
||
262 | * @param array $params parameters passed to [[CheckAccessInterface::checkAccess()]] and will be passed to the rule |
||
263 | * @return bool the return value of [[Rule::execute()]]. If the auth item does not specify a rule, true will be returned. |
||
264 | * @throws InvalidConfigException if the auth item has an invalid rule. |
||
265 | */ |
||
266 | 47 | protected function executeRule($user, $item, $params) |
|
278 | |||
279 | /** |
||
280 | * Checks whether array of $assignments is empty and [[defaultRoles]] property is empty as well. |
||
281 | * |
||
282 | * @param Assignment[] $assignments array of user's assignments |
||
283 | * @return bool whether array of $assignments is empty and [[defaultRoles]] property is empty as well |
||
284 | * @since 2.0.11 |
||
285 | */ |
||
286 | 55 | protected function hasNoAssignments(array $assignments) |
|
290 | } |
||
291 |