BaseManager.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\rbac;
  8. use yii\base\Component;
  9. use yii\base\InvalidArgumentException;
  10. use yii\base\InvalidConfigException;
  11. use yii\base\InvalidValueException;
  12. /**
  13. * BaseManager is a base class implementing [[ManagerInterface]] for RBAC management.
  14. *
  15. * For more details and usage information on DbManager, see the [guide article on security authorization](guide:security-authorization).
  16. *
  17. * @property Role[] $defaultRoleInstances Default roles. The array is indexed by the role names. This property
  18. * is read-only.
  19. * @property string[] $defaultRoles Default roles. Note that the type of this property differs in getter and
  20. * setter. See [[getDefaultRoles()]] and [[setDefaultRoles()]] for details.
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @since 2.0
  24. */
  25. abstract class BaseManager extends Component implements ManagerInterface
  26. {
  27. /**
  28. * @var array a list of role names that are assigned to every user automatically without calling [[assign()]].
  29. * Note that these roles are applied to users, regardless of their state of authentication.
  30. */
  31. protected $defaultRoles = [];
  32. /**
  33. * Returns the named auth item.
  34. * @param string $name the auth item name.
  35. * @return Item the auth item corresponding to the specified name. Null is returned if no such item.
  36. */
  37. abstract protected function getItem($name);
  38. /**
  39. * Returns the items of the specified type.
  40. * @param int $type the auth item type (either [[Item::TYPE_ROLE]] or [[Item::TYPE_PERMISSION]]
  41. * @return Item[] the auth items of the specified type.
  42. */
  43. abstract protected function getItems($type);
  44. /**
  45. * Adds an auth item to the RBAC system.
  46. * @param Item $item the item to add
  47. * @return bool whether the auth item is successfully added to the system
  48. * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
  49. */
  50. abstract protected function addItem($item);
  51. /**
  52. * Adds a rule to the RBAC system.
  53. * @param Rule $rule the rule to add
  54. * @return bool whether the rule is successfully added to the system
  55. * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
  56. */
  57. abstract protected function addRule($rule);
  58. /**
  59. * Removes an auth item from the RBAC system.
  60. * @param Item $item the item to remove
  61. * @return bool whether the role or permission is successfully removed
  62. * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
  63. */
  64. abstract protected function removeItem($item);
  65. /**
  66. * Removes a rule from the RBAC system.
  67. * @param Rule $rule the rule to remove
  68. * @return bool whether the rule is successfully removed
  69. * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
  70. */
  71. abstract protected function removeRule($rule);
  72. /**
  73. * Updates an auth item in the RBAC system.
  74. * @param string $name the name of the item being updated
  75. * @param Item $item the updated item
  76. * @return bool whether the auth item is successfully updated
  77. * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
  78. */
  79. abstract protected function updateItem($name, $item);
  80. /**
  81. * Updates a rule to the RBAC system.
  82. * @param string $name the name of the rule being updated
  83. * @param Rule $rule the updated rule
  84. * @return bool whether the rule is successfully updated
  85. * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
  86. */
  87. abstract protected function updateRule($name, $rule);
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function createRole($name)
  92. {
  93. $role = new Role();
  94. $role->name = $name;
  95. return $role;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function createPermission($name)
  101. {
  102. $permission = new Permission();
  103. $permission->name = $name;
  104. return $permission;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function add($object)
  110. {
  111. if ($object instanceof Item) {
  112. if ($object->ruleName && $this->getRule($object->ruleName) === null) {
  113. $rule = \Yii::createObject($object->ruleName);
  114. $rule->name = $object->ruleName;
  115. $this->addRule($rule);
  116. }
  117. return $this->addItem($object);
  118. } elseif ($object instanceof Rule) {
  119. return $this->addRule($object);
  120. }
  121. throw new InvalidArgumentException('Adding unsupported object type.');
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function remove($object)
  127. {
  128. if ($object instanceof Item) {
  129. return $this->removeItem($object);
  130. } elseif ($object instanceof Rule) {
  131. return $this->removeRule($object);
  132. }
  133. throw new InvalidArgumentException('Removing unsupported object type.');
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function update($name, $object)
  139. {
  140. if ($object instanceof Item) {
  141. if ($object->ruleName && $this->getRule($object->ruleName) === null) {
  142. $rule = \Yii::createObject($object->ruleName);
  143. $rule->name = $object->ruleName;
  144. $this->addRule($rule);
  145. }
  146. return $this->updateItem($name, $object);
  147. } elseif ($object instanceof Rule) {
  148. return $this->updateRule($name, $object);
  149. }
  150. throw new InvalidArgumentException('Updating unsupported object type.');
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getRole($name)
  156. {
  157. $item = $this->getItem($name);
  158. return $item instanceof Item && $item->type == Item::TYPE_ROLE ? $item : null;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function getPermission($name)
  164. {
  165. $item = $this->getItem($name);
  166. return $item instanceof Item && $item->type == Item::TYPE_PERMISSION ? $item : null;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getRoles()
  172. {
  173. return $this->getItems(Item::TYPE_ROLE);
  174. }
  175. /**
  176. * Set default roles
  177. * @param string[]|\Closure $roles either array of roles or a callable returning it
  178. * @throws InvalidArgumentException when $roles is neither array nor Closure
  179. * @throws InvalidValueException when Closure return is not an array
  180. * @since 2.0.14
  181. */
  182. public function setDefaultRoles($roles)
  183. {
  184. if (is_array($roles)) {
  185. $this->defaultRoles = $roles;
  186. } elseif ($roles instanceof \Closure) {
  187. $roles = call_user_func($roles);
  188. if (!is_array($roles)) {
  189. throw new InvalidValueException('Default roles closure must return an array');
  190. }
  191. $this->defaultRoles = $roles;
  192. } else {
  193. throw new InvalidArgumentException('Default roles must be either an array or a callable');
  194. }
  195. }
  196. /**
  197. * Get default roles
  198. * @return string[] default roles
  199. * @since 2.0.14
  200. */
  201. public function getDefaultRoles()
  202. {
  203. return $this->defaultRoles;
  204. }
  205. /**
  206. * Returns defaultRoles as array of Role objects.
  207. * @since 2.0.12
  208. * @return Role[] default roles. The array is indexed by the role names
  209. */
  210. public function getDefaultRoleInstances()
  211. {
  212. $result = [];
  213. foreach ($this->defaultRoles as $roleName) {
  214. $result[$roleName] = $this->createRole($roleName);
  215. }
  216. return $result;
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function getPermissions()
  222. {
  223. return $this->getItems(Item::TYPE_PERMISSION);
  224. }
  225. /**
  226. * Executes the rule associated with the specified auth item.
  227. *
  228. * If the item does not specify a rule, this method will return true. Otherwise, it will
  229. * return the value of [[Rule::execute()]].
  230. *
  231. * @param string|int $user the user ID. This should be either an integer or a string representing
  232. * the unique identifier of a user. See [[\yii\web\User::id]].
  233. * @param Item $item the auth item that needs to execute its rule
  234. * @param array $params parameters passed to [[CheckAccessInterface::checkAccess()]] and will be passed to the rule
  235. * @return bool the return value of [[Rule::execute()]]. If the auth item does not specify a rule, true will be returned.
  236. * @throws InvalidConfigException if the auth item has an invalid rule.
  237. */
  238. protected function executeRule($user, $item, $params)
  239. {
  240. if ($item->ruleName === null) {
  241. return true;
  242. }
  243. $rule = $this->getRule($item->ruleName);
  244. if ($rule instanceof Rule) {
  245. return $rule->execute($user, $item, $params);
  246. }
  247. throw new InvalidConfigException("Rule not found: {$item->ruleName}");
  248. }
  249. /**
  250. * Checks whether array of $assignments is empty and [[defaultRoles]] property is empty as well.
  251. *
  252. * @param Assignment[] $assignments array of user's assignments
  253. * @return bool whether array of $assignments is empty and [[defaultRoles]] property is empty as well
  254. * @since 2.0.11
  255. */
  256. protected function hasNoAssignments(array $assignments)
  257. {
  258. return empty($assignments) && empty($this->defaultRoles);
  259. }
  260. }