BaseManager.php 7.6 KB

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