CAuthItem.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * CAuthItem class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CAuthItem 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 {@link CAuthAssignment assignments}.
  16. * He can perform an operation only when it is among his assigned items.
  17. *
  18. * @property IAuthManager $authManager The authorization manager.
  19. * @property integer $type The authorization item type. This could be 0 (operation), 1 (task) or 2 (role).
  20. * @property string $name The item name.
  21. * @property string $description The item description.
  22. * @property string $bizRule The business rule associated with this item.
  23. * @property mixed $data The additional data associated with this item.
  24. * @property array $children All child items of this item.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @package system.web.auth
  28. * @since 1.0
  29. */
  30. class CAuthItem extends CComponent
  31. {
  32. const TYPE_OPERATION=0;
  33. const TYPE_TASK=1;
  34. const TYPE_ROLE=2;
  35. private $_auth;
  36. private $_type;
  37. private $_name;
  38. private $_description;
  39. private $_bizRule;
  40. private $_data;
  41. /**
  42. * Constructor.
  43. * @param IAuthManager $auth authorization manager
  44. * @param string $name authorization item name
  45. * @param integer $type authorization item type. This can be 0 (operation), 1 (task) or 2 (role).
  46. * @param string $description the description
  47. * @param string $bizRule the business rule associated with this item
  48. * @param mixed $data additional data for this item
  49. */
  50. public function __construct($auth,$name,$type,$description='',$bizRule=null,$data=null)
  51. {
  52. $this->_type=(int)$type;
  53. $this->_auth=$auth;
  54. $this->_name=$name;
  55. $this->_description=$description;
  56. $this->_bizRule=$bizRule;
  57. $this->_data=$data;
  58. }
  59. /**
  60. * Checks to see if the specified item is within the hierarchy starting from this item.
  61. * This method is expected to be internally used by the actual implementations
  62. * of the {@link IAuthManager::checkAccess}.
  63. * @param string $itemName the name of the item to be checked
  64. * @param array $params the parameters to be passed to business rule evaluation
  65. * @return boolean whether the specified item is within the hierarchy starting from this item.
  66. */
  67. public function checkAccess($itemName,$params=array())
  68. {
  69. Yii::trace('Checking permission "'.$this->_name.'"','system.web.auth.CAuthItem');
  70. if($this->_auth->executeBizRule($this->_bizRule,$params,$this->_data))
  71. {
  72. if($this->_name==$itemName)
  73. return true;
  74. foreach($this->_auth->getItemChildren($this->_name) as $item)
  75. {
  76. if($item->checkAccess($itemName,$params))
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. /**
  83. * @return IAuthManager the authorization manager
  84. */
  85. public function getAuthManager()
  86. {
  87. return $this->_auth;
  88. }
  89. /**
  90. * @return integer the authorization item type. This could be 0 (operation), 1 (task) or 2 (role).
  91. */
  92. public function getType()
  93. {
  94. return $this->_type;
  95. }
  96. /**
  97. * @return string the item name
  98. */
  99. public function getName()
  100. {
  101. return $this->_name;
  102. }
  103. /**
  104. * @param string $value the item name
  105. */
  106. public function setName($value)
  107. {
  108. if($this->_name!==$value)
  109. {
  110. $oldName=$this->_name;
  111. $this->_name=$value;
  112. $this->_auth->saveAuthItem($this,$oldName);
  113. }
  114. }
  115. /**
  116. * @return string the item description
  117. */
  118. public function getDescription()
  119. {
  120. return $this->_description;
  121. }
  122. /**
  123. * @param string $value the item description
  124. */
  125. public function setDescription($value)
  126. {
  127. if($this->_description!==$value)
  128. {
  129. $this->_description=$value;
  130. $this->_auth->saveAuthItem($this);
  131. }
  132. }
  133. /**
  134. * @return string the business rule associated with this item
  135. */
  136. public function getBizRule()
  137. {
  138. return $this->_bizRule;
  139. }
  140. /**
  141. * @param string $value the business rule associated with this item
  142. */
  143. public function setBizRule($value)
  144. {
  145. if($this->_bizRule!==$value)
  146. {
  147. $this->_bizRule=$value;
  148. $this->_auth->saveAuthItem($this);
  149. }
  150. }
  151. /**
  152. * @return mixed the additional data associated with this item
  153. */
  154. public function getData()
  155. {
  156. return $this->_data;
  157. }
  158. /**
  159. * @param mixed $value the additional data associated with this item
  160. */
  161. public function setData($value)
  162. {
  163. if($this->_data!==$value)
  164. {
  165. $this->_data=$value;
  166. $this->_auth->saveAuthItem($this);
  167. }
  168. }
  169. /**
  170. * Adds a child item.
  171. * @param string $name the name of the child item
  172. * @return boolean whether the item is added successfully
  173. * @throws CException if either parent or child doesn't exist or if a loop has been detected.
  174. * @see IAuthManager::addItemChild
  175. */
  176. public function addChild($name)
  177. {
  178. return $this->_auth->addItemChild($this->_name,$name);
  179. }
  180. /**
  181. * Removes a child item.
  182. * Note, the child item is not deleted. Only the parent-child relationship is removed.
  183. * @param string $name the child item name
  184. * @return boolean whether the removal is successful
  185. * @see IAuthManager::removeItemChild
  186. */
  187. public function removeChild($name)
  188. {
  189. return $this->_auth->removeItemChild($this->_name,$name);
  190. }
  191. /**
  192. * Returns a value indicating whether a child exists
  193. * @param string $name the child item name
  194. * @return boolean whether the child exists
  195. * @see IAuthManager::hasItemChild
  196. */
  197. public function hasChild($name)
  198. {
  199. return $this->_auth->hasItemChild($this->_name,$name);
  200. }
  201. /**
  202. * Returns the children of this item.
  203. * @return array all child items of this item.
  204. * @see IAuthManager::getItemChildren
  205. */
  206. public function getChildren()
  207. {
  208. return $this->_auth->getItemChildren($this->_name);
  209. }
  210. /**
  211. * Assigns this item to a user.
  212. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  213. * @param string $bizRule the business rule to be executed when {@link checkAccess} is called
  214. * for this particular authorization item.
  215. * @param mixed $data additional data associated with this assignment
  216. * @return CAuthAssignment the authorization assignment information.
  217. * @throws CException if the item has already been assigned to the user
  218. * @see IAuthManager::assign
  219. */
  220. public function assign($userId,$bizRule=null,$data=null)
  221. {
  222. return $this->_auth->assign($this->_name,$userId,$bizRule,$data);
  223. }
  224. /**
  225. * Revokes an authorization assignment from a user.
  226. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  227. * @return boolean whether removal is successful
  228. * @see IAuthManager::revoke
  229. */
  230. public function revoke($userId)
  231. {
  232. return $this->_auth->revoke($this->_name,$userId);
  233. }
  234. /**
  235. * Returns a value indicating whether this item has been assigned to the user.
  236. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  237. * @return boolean whether the item has been assigned to the user.
  238. * @see IAuthManager::isAssigned
  239. */
  240. public function isAssigned($userId)
  241. {
  242. return $this->_auth->isAssigned($this->_name,$userId);
  243. }
  244. /**
  245. * Returns the item assignment information.
  246. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  247. * @return CAuthAssignment the item assignment information. Null is returned if
  248. * this item is not assigned to the user.
  249. * @see IAuthManager::getAuthAssignment
  250. */
  251. public function getAssignment($userId)
  252. {
  253. return $this->_auth->getAuthAssignment($this->_name,$userId);
  254. }
  255. }