BaseManager.php 8.2 KB

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