Item.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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;
  9. use yii\base\Object;
  10. /**
  11. * Item represents an authorization item.
  12. * An authorization item can be an operation, a task or a role.
  13. * They form an authorization hierarchy. Items on higher levels of the hierarchy
  14. * inherit the permissions represented by items on lower levels.
  15. * A user may be assigned one or several authorization items (called [[Assignment]] assignments).
  16. * He can perform an operation only when it is among his assigned items.
  17. *
  18. * @property Item[] $children All child items of this item. This property is read-only.
  19. * @property string $name The item name.
  20. *
  21. * @author Qiang Xue <[email protected]>
  22. * @author Alexander Kochetov <[email protected]>
  23. * @since 2.0
  24. */
  25. class Item extends Object
  26. {
  27. const TYPE_OPERATION = 0;
  28. const TYPE_TASK = 1;
  29. const TYPE_ROLE = 2;
  30. /**
  31. * @var Manager the auth manager of this item
  32. */
  33. public $manager;
  34. /**
  35. * @var string the item description
  36. */
  37. public $description;
  38. /**
  39. * @var string the business rule associated with this item
  40. */
  41. public $bizRule;
  42. /**
  43. * @var mixed the additional data associated with this item
  44. */
  45. public $data;
  46. /**
  47. * @var integer the authorization item type. This could be 0 (operation), 1 (task) or 2 (role).
  48. */
  49. public $type;
  50. private $_name;
  51. private $_oldName;
  52. /**
  53. * Checks to see if the specified item is within the hierarchy starting from this item.
  54. * This method is expected to be internally used by the actual implementations
  55. * of the [[Manager::checkAccess()]].
  56. * @param string $itemName the name of the item to be checked
  57. * @param array $params the parameters to be passed to business rule evaluation
  58. * @return boolean whether the specified item is within the hierarchy starting from this item.
  59. */
  60. public function checkAccess($itemName, $params = [])
  61. {
  62. Yii::trace('Checking permission: ' . $this->_name, __METHOD__);
  63. if ($this->manager->executeBizRule($this->bizRule, $params, $this->data)) {
  64. if ($this->_name == $itemName) {
  65. return true;
  66. }
  67. foreach ($this->manager->getItemChildren($this->_name) as $item) {
  68. if ($item->checkAccess($itemName, $params)) {
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * @return string the item name
  77. */
  78. public function getName()
  79. {
  80. return $this->_name;
  81. }
  82. /**
  83. * @param string $value the item name
  84. */
  85. public function setName($value)
  86. {
  87. if ($this->_name !== $value) {
  88. $this->_oldName = $this->_name;
  89. $this->_name = $value;
  90. }
  91. }
  92. /**
  93. * Adds a child item.
  94. * @param string $name the name of the child item
  95. * @return boolean whether the item is added successfully
  96. * @throws \yii\base\Exception if either parent or child doesn't exist or if a loop has been detected.
  97. * @see Manager::addItemChild
  98. */
  99. public function addChild($name)
  100. {
  101. return $this->manager->addItemChild($this->_name, $name);
  102. }
  103. /**
  104. * Removes a child item.
  105. * Note, the child item is not deleted. Only the parent-child relationship is removed.
  106. * @param string $name the child item name
  107. * @return boolean whether the removal is successful
  108. * @see Manager::removeItemChild
  109. */
  110. public function removeChild($name)
  111. {
  112. return $this->manager->removeItemChild($this->_name, $name);
  113. }
  114. /**
  115. * Returns a value indicating whether a child exists
  116. * @param string $name the child item name
  117. * @return boolean whether the child exists
  118. * @see Manager::hasItemChild
  119. */
  120. public function hasChild($name)
  121. {
  122. return $this->manager->hasItemChild($this->_name, $name);
  123. }
  124. /**
  125. * Returns the children of this item.
  126. * @return Item[] all child items of this item.
  127. * @see Manager::getItemChildren
  128. */
  129. public function getChildren()
  130. {
  131. return $this->manager->getItemChildren($this->_name);
  132. }
  133. /**
  134. * Assigns this item to a user.
  135. * @param mixed $userId the user ID (see [[User::id]])
  136. * @param string $bizRule the business rule to be executed when [[checkAccess()]] is called
  137. * for this particular authorization item.
  138. * @param mixed $data additional data associated with this assignment
  139. * @return Assignment the authorization assignment information.
  140. * @throws \yii\base\Exception if the item has already been assigned to the user
  141. * @see Manager::assign
  142. */
  143. public function assign($userId, $bizRule = null, $data = null)
  144. {
  145. return $this->manager->assign($userId, $this->_name, $bizRule, $data);
  146. }
  147. /**
  148. * Revokes an authorization assignment from a user.
  149. * @param mixed $userId the user ID (see [[User::id]])
  150. * @return boolean whether removal is successful
  151. * @see Manager::revoke
  152. */
  153. public function revoke($userId)
  154. {
  155. return $this->manager->revoke($userId, $this->_name);
  156. }
  157. /**
  158. * Returns a value indicating whether this item has been assigned to the user.
  159. * @param mixed $userId the user ID (see [[User::id]])
  160. * @return boolean whether the item has been assigned to the user.
  161. * @see Manager::isAssigned
  162. */
  163. public function isAssigned($userId)
  164. {
  165. return $this->manager->isAssigned($userId, $this->_name);
  166. }
  167. /**
  168. * Returns the item assignment information.
  169. * @param mixed $userId the user ID (see [[User::id]])
  170. * @return Assignment the item assignment information. Null is returned if
  171. * this item is not assigned to the user.
  172. * @see Manager::getAssignment
  173. */
  174. public function getAssignment($userId)
  175. {
  176. return $this->manager->getAssignment($userId, $this->_name);
  177. }
  178. /**
  179. * Saves an authorization item to persistent storage.
  180. */
  181. public function save()
  182. {
  183. $this->manager->saveItem($this, $this->_oldName);
  184. $this->_oldName = null;
  185. }
  186. }