AccessRule.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\web;
  8. use yii\base\Component;
  9. use yii\base\Action;
  10. /**
  11. * This class represents an access rule defined by the [[AccessControl]] action filter
  12. *
  13. * @author Qiang Xue <[email protected]>
  14. * @since 2.0
  15. */
  16. class AccessRule extends Component
  17. {
  18. /**
  19. * @var boolean whether this is an 'allow' rule or 'deny' rule.
  20. */
  21. public $allow;
  22. /**
  23. * @var array list of action IDs that this rule applies to. The comparison is case-sensitive.
  24. * If not set or empty, it means this rule applies to all actions.
  25. */
  26. public $actions;
  27. /**
  28. * @var array list of controller IDs that this rule applies to. The comparison is case-sensitive.
  29. * If not set or empty, it means this rule applies to all controllers.
  30. */
  31. public $controllers;
  32. /**
  33. * @var array list of roles that this rule applies to. Two special roles are recognized, and
  34. * they are checked via [[User::isGuest]]:
  35. *
  36. * - `?`: matches a guest user (not authenticated yet)
  37. * - `@`: matches an authenticated user
  38. *
  39. * Using additional role names requires RBAC (Role-Based Access Control), and
  40. * [[User::checkAccess()]] will be called.
  41. *
  42. * If this property is not set or empty, it means this rule applies to all roles.
  43. */
  44. public $roles;
  45. /**
  46. * @var array list of user IP addresses that this rule applies to. An IP address
  47. * can contain the wildcard `*` at the end so that it matches IP addresses with the same prefix.
  48. * For example, '192.168.*' matches all IP addresses in the segment '192.168.'.
  49. * If not set or empty, it means this rule applies to all IP addresses.
  50. * @see Request::userIP
  51. */
  52. public $ips;
  53. /**
  54. * @var array list of request methods (e.g. `GET`, `POST`) that this rule applies to.
  55. * The request methods must be specified in uppercase.
  56. * If not set or empty, it means this rule applies to all request methods.
  57. * @see Request::requestMethod
  58. */
  59. public $verbs;
  60. /**
  61. * @var callback a callback that will be called to determine if the rule should be applied.
  62. * The signature of the callback should be as follows:
  63. *
  64. * ~~~
  65. * function ($rule, $action)
  66. * ~~~
  67. *
  68. * where `$rule` is this rule, and `$action` is the current [[Action|action]] object.
  69. * The callback should return a boolean value indicating whether this rule should be applied.
  70. */
  71. public $matchCallback;
  72. /**
  73. * @var callback a callback that will be called if this rule determines the access to
  74. * the current action should be denied. If not set, the behavior will be determined by
  75. * [[AccessControl]].
  76. *
  77. * The signature of the callback should be as follows:
  78. *
  79. * ~~~
  80. * function ($rule, $action)
  81. * ~~~
  82. *
  83. * where `$rule` is this rule, and `$action` is the current [[Action|action]] object.
  84. */
  85. public $denyCallback;
  86. /**
  87. * Checks whether the Web user is allowed to perform the specified action.
  88. * @param Action $action the action to be performed
  89. * @param User $user the user object
  90. * @param Request $request
  91. * @return boolean|null true if the user is allowed, false if the user is denied, null if the rule does not apply to the user
  92. */
  93. public function allows($action, $user, $request)
  94. {
  95. if ($this->matchAction($action)
  96. && $this->matchRole($user)
  97. && $this->matchIP($request->getUserIP())
  98. && $this->matchVerb($request->getMethod())
  99. && $this->matchController($action->controller)
  100. && $this->matchCustom($action)
  101. ) {
  102. return $this->allow ? true : false;
  103. } else {
  104. return null;
  105. }
  106. }
  107. /**
  108. * @param Action $action the action
  109. * @return boolean whether the rule applies to the action
  110. */
  111. protected function matchAction($action)
  112. {
  113. return empty($this->actions) || in_array($action->id, $this->actions, true);
  114. }
  115. /**
  116. * @param Controller $controller the controller
  117. * @return boolean whether the rule applies to the controller
  118. */
  119. protected function matchController($controller)
  120. {
  121. return empty($this->controllers) || in_array($controller->uniqueId, $this->controllers, true);
  122. }
  123. /**
  124. * @param User $user the user object
  125. * @return boolean whether the rule applies to the role
  126. */
  127. protected function matchRole($user)
  128. {
  129. if (empty($this->roles)) {
  130. return true;
  131. }
  132. foreach ($this->roles as $role) {
  133. if ($role === '?' && $user->getIsGuest()) {
  134. return true;
  135. } elseif ($role === '@' && !$user->getIsGuest()) {
  136. return true;
  137. } elseif ($user->checkAccess($role)) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * @param string $ip the IP address
  145. * @return boolean whether the rule applies to the IP address
  146. */
  147. protected function matchIP($ip)
  148. {
  149. if (empty($this->ips)) {
  150. return true;
  151. }
  152. foreach ($this->ips as $rule) {
  153. if ($rule === '*' || $rule === $ip || (($pos = strpos($rule, '*')) !== false && !strncmp($ip, $rule, $pos))) {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. /**
  160. * @param string $verb the request method
  161. * @return boolean whether the rule applies to the request
  162. */
  163. protected function matchVerb($verb)
  164. {
  165. return empty($this->verbs) || in_array($verb, $this->verbs, true);
  166. }
  167. /**
  168. * @param Action $action the action to be performed
  169. * @return boolean whether the rule should be applied
  170. */
  171. protected function matchCustom($action)
  172. {
  173. return empty($this->matchCallback) || call_user_func($this->matchCallback, $this, $action);
  174. }
  175. }