CAuthManager.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * CAuthManager 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. * CAuthManager is the base class for authorization manager classes.
  12. *
  13. * CAuthManager extends {@link CApplicationComponent} and implements some methods
  14. * that are common among authorization manager classes.
  15. *
  16. * CAuthManager together with its concrete child classes implement the Role-Based
  17. * Access Control (RBAC).
  18. *
  19. * The main idea is that permissions are organized as a hierarchy of
  20. * {@link CAuthItem authorization items}. Items on higher level inherit the permissions
  21. * represented by items on lower level. And roles are simply top-level authorization items
  22. * that may be assigned to individual users. A user is said to have a permission
  23. * to do something if the corresponding authorization item is inherited by one of his roles.
  24. *
  25. * Using authorization manager consists of two aspects. First, the authorization hierarchy
  26. * and assignments have to be established. CAuthManager and its child classes
  27. * provides APIs to accomplish this task. Developers may need to develop some GUI
  28. * so that it is more intuitive to end-users. Second, developers call {@link IAuthManager::checkAccess}
  29. * at appropriate places in the application code to check if the current user
  30. * has the needed permission for an operation.
  31. *
  32. * @property array $roles Roles (name=>CAuthItem).
  33. * @property array $tasks Tasks (name=>CAuthItem).
  34. * @property array $operations Operations (name=>CAuthItem).
  35. *
  36. * @author Qiang Xue <qiang.xue@gmail.com>
  37. * @package system.web.auth
  38. * @since 1.0
  39. */
  40. abstract class CAuthManager extends CApplicationComponent implements IAuthManager
  41. {
  42. /**
  43. * @var boolean Enable error reporting for bizRules.
  44. * @since 1.1.3
  45. */
  46. public $showErrors = false;
  47. /**
  48. * @var array list of role names that are assigned to all users implicitly.
  49. * These roles do not need to be explicitly assigned to any user.
  50. * When calling {@link checkAccess}, these roles will be checked first.
  51. * For performance reason, you should minimize the number of such roles.
  52. * A typical usage of such roles is to define an 'authenticated' role and associate
  53. * it with a biz rule which checks if the current user is authenticated.
  54. * And then declare 'authenticated' in this property so that it can be applied to
  55. * every authenticated user.
  56. */
  57. public $defaultRoles=array();
  58. /**
  59. * Creates a role.
  60. * This is a shortcut method to {@link IAuthManager::createAuthItem}.
  61. * @param string $name the item name
  62. * @param string $description the item description.
  63. * @param string $bizRule the business rule associated with this item
  64. * @param mixed $data additional data to be passed when evaluating the business rule
  65. * @return CAuthItem the authorization item
  66. */
  67. public function createRole($name,$description='',$bizRule=null,$data=null)
  68. {
  69. return $this->createAuthItem($name,CAuthItem::TYPE_ROLE,$description,$bizRule,$data);
  70. }
  71. /**
  72. * Creates a task.
  73. * This is a shortcut method to {@link IAuthManager::createAuthItem}.
  74. * @param string $name the item name
  75. * @param string $description the item description.
  76. * @param string $bizRule the business rule associated with this item
  77. * @param mixed $data additional data to be passed when evaluating the business rule
  78. * @return CAuthItem the authorization item
  79. */
  80. public function createTask($name,$description='',$bizRule=null,$data=null)
  81. {
  82. return $this->createAuthItem($name,CAuthItem::TYPE_TASK,$description,$bizRule,$data);
  83. }
  84. /**
  85. * Creates an operation.
  86. * This is a shortcut method to {@link IAuthManager::createAuthItem}.
  87. * @param string $name the item name
  88. * @param string $description the item description.
  89. * @param string $bizRule the business rule associated with this item
  90. * @param mixed $data additional data to be passed when evaluating the business rule
  91. * @return CAuthItem the authorization item
  92. */
  93. public function createOperation($name,$description='',$bizRule=null,$data=null)
  94. {
  95. return $this->createAuthItem($name,CAuthItem::TYPE_OPERATION,$description,$bizRule,$data);
  96. }
  97. /**
  98. * Returns roles.
  99. * This is a shortcut method to {@link IAuthManager::getAuthItems}.
  100. * @param mixed $userId the user ID. If not null, only the roles directly assigned to the user
  101. * will be returned. Otherwise, all roles will be returned.
  102. * @return array roles (name=>CAuthItem)
  103. */
  104. public function getRoles($userId=null)
  105. {
  106. return $this->getAuthItems(CAuthItem::TYPE_ROLE,$userId);
  107. }
  108. /**
  109. * Returns tasks.
  110. * This is a shortcut method to {@link IAuthManager::getAuthItems}.
  111. * @param mixed $userId the user ID. If not null, only the tasks directly assigned to the user
  112. * will be returned. Otherwise, all tasks will be returned.
  113. * @return array tasks (name=>CAuthItem)
  114. */
  115. public function getTasks($userId=null)
  116. {
  117. return $this->getAuthItems(CAuthItem::TYPE_TASK,$userId);
  118. }
  119. /**
  120. * Returns operations.
  121. * This is a shortcut method to {@link IAuthManager::getAuthItems}.
  122. * @param mixed $userId the user ID. If not null, only the operations directly assigned to the user
  123. * will be returned. Otherwise, all operations will be returned.
  124. * @return array operations (name=>CAuthItem)
  125. */
  126. public function getOperations($userId=null)
  127. {
  128. return $this->getAuthItems(CAuthItem::TYPE_OPERATION,$userId);
  129. }
  130. /**
  131. * Executes the specified business rule.
  132. * @param string $bizRule the business rule to be executed.
  133. * @param array $params parameters passed to {@link IAuthManager::checkAccess}.
  134. * @param mixed $data additional data associated with the authorization item or assignment.
  135. * @return boolean whether the business rule returns true.
  136. * If the business rule is empty, it will still return true.
  137. */
  138. public function executeBizRule($bizRule,$params,$data)
  139. {
  140. return $bizRule==='' || $bizRule===null || ($this->showErrors ? eval($bizRule)!=0 : @eval($bizRule)!=0);
  141. }
  142. /**
  143. * Checks the item types to make sure a child can be added to a parent.
  144. * @param integer $parentType parent item type
  145. * @param integer $childType child item type
  146. * @throws CException if the item cannot be added as a child due to its incompatible type.
  147. */
  148. protected function checkItemChildType($parentType,$childType)
  149. {
  150. static $types=array('operation','task','role');
  151. if($parentType < $childType)
  152. throw new CException(Yii::t('yii','Cannot add an item of type "{child}" to an item of type "{parent}".',
  153. array('{child}'=>$types[$childType], '{parent}'=>$types[$parentType])));
  154. }
  155. }