/lib/AccessControlList.php

https://github.com/Alex8452/Kurogo-Mobile-Web-Doc-Translation · PHP · 327 lines · 195 code · 43 blank · 89 comment · 30 complexity · 4910d9b0b81238ac52c5e3fda618af6b MD5 · raw file

  1. <?php
  2. /**
  3. * @package Authentication
  4. * @subpackage AccessControlList
  5. */
  6. /**
  7. * AccessControlList
  8. * Used to encapsulate a rule used for access control
  9. * @see Module::getAccessControlLists()
  10. * @see AuthenticationAuthority
  11. * @package Authentication
  12. * @subpackage AccessControlList
  13. */
  14. class AccessControlList
  15. {
  16. /* enums for rule types */
  17. /** Rule for user access */
  18. const RULE_TYPE_ACCESS='U';
  19. /** Rule for admin access */
  20. const RULE_TYPE_ADMIN='A';
  21. /* enums for rule scopes */
  22. /** matches any user within the specified authority */
  23. const RULE_SCOPE_USER='U';
  24. /** matches a group/authority combo */
  25. const RULE_SCOPE_GROUP='G';
  26. /** matches everyone (including anonymous) */
  27. const RULE_SCOPE_EVERYONE='E';
  28. /** special constant to match all values of a particular scope */
  29. const RULE_VALUE_ALL='*';
  30. /* enums for rule actions */
  31. /** allow */
  32. const RULE_ACTION_ALLOW='A';
  33. /** deny */
  34. const RULE_ACTION_DENY='D';
  35. /**
  36. * Rule Action
  37. * @var string
  38. */
  39. protected $ruleType;
  40. /**
  41. * Rule Action
  42. * @var string
  43. */
  44. protected $ruleAction;
  45. /**
  46. * Rule scope
  47. * @var string
  48. */
  49. protected $ruleScope;
  50. /**
  51. * Rule authority
  52. * @var string
  53. */
  54. protected $ruleAuthority;
  55. /**
  56. * Rule value
  57. * @var string
  58. */
  59. protected $ruleValue;
  60. /**
  61. * Returns a list of valid rule types
  62. * @return array
  63. */
  64. public static function ruleTypes()
  65. {
  66. return array(
  67. AccessControlList::RULE_TYPE_ACCESS,
  68. AccessControlList::RULE_TYPE_ADMIN
  69. );
  70. }
  71. /**
  72. * Returns a list of valid rule scopes
  73. * @return array
  74. */
  75. public static function ruleScopes()
  76. {
  77. return array(
  78. AccessControlList::RULE_SCOPE_USER,
  79. AccessControlList::RULE_SCOPE_GROUP,
  80. AccessControlList::RULE_SCOPE_EVERYONE
  81. );
  82. }
  83. /**
  84. * Returns a list of valid rule actions
  85. * @return array
  86. */
  87. public static function ruleActions()
  88. {
  89. return array(
  90. AccessControlList::RULE_ACTION_ALLOW,
  91. AccessControlList::RULE_ACTION_DENY
  92. );
  93. }
  94. /**
  95. * Sees if the given user matches the rule
  96. * @param User $user a valid user object
  97. * @return mixed, the action if the user matches the rule or false if the rule did not match
  98. */
  99. public function evaluateForUser(User $user)
  100. {
  101. switch ($this->ruleScope)
  102. {
  103. case self::RULE_SCOPE_USER:
  104. /* if the value is all then see if the userID is set
  105. this will NOT match an anonymous user
  106. */
  107. if ($this->ruleAuthority) {
  108. if ($user->getAuthenticationAuthorityIndex()==$this->ruleAuthority) {
  109. /* can match either userID or email */
  110. if ($this->ruleValue==self::RULE_VALUE_ALL) {
  111. if ($user->getUserID()) {
  112. return $this->ruleAction;
  113. }
  114. } else if ($user->getUserID()==$this->ruleValue ||
  115. (Validator::isValidEmail($this->ruleValue) && $user->getEmail()==$this->ruleValue)) {
  116. return $this->ruleAction;
  117. }
  118. }
  119. } elseif ($this->ruleValue==self::RULE_VALUE_ALL) {
  120. if ($user->getUserID()) {
  121. return $this->ruleAction;
  122. }
  123. } else if ($user->getUserID()==$this->ruleValue ||
  124. (Validator::isValidEmail($this->ruleValue) && $user->getEmail()==$this->ruleValue)) {
  125. return $this->ruleAction;
  126. }
  127. break;
  128. case self::RULE_SCOPE_GROUP:
  129. /* Note: a group value of ALL is not valid */
  130. if ($authority = AuthenticationAuthority::getAuthenticationAuthority($this->ruleAuthority)) {
  131. if ($group = $authority->getGroup($this->ruleValue)) {
  132. /* see if the user is a member of the group */
  133. if ($group->userIsMember($user)) {
  134. return $this->ruleAction;
  135. }
  136. }
  137. }
  138. break;
  139. case self::RULE_SCOPE_EVERYONE:
  140. /* always matches */
  141. return $this->ruleAction;
  142. break;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Returns an access control list based on a string
  148. * @param string Should be in format ACTION:RULE:VALUE
  149. * @return AccessControlList or false if the string is invalid
  150. */
  151. public static function createFromArray($aclArray) {
  152. $aclArray = array_merge(array(
  153. 'type'=>'','action'=>'','scope'=>'','authority'=>'','value'=>''),
  154. $aclArray
  155. );
  156. $acl = self::factory($aclArray['type'], $aclArray['action'], $aclArray['scope'], $aclArray['authority'], $aclArray['value']);
  157. return $acl;
  158. }
  159. public function getType() {
  160. return $this->ruleType;
  161. }
  162. public function __toString() {
  163. $str = '(';
  164. switch($this->ruleType)
  165. {
  166. case self::RULE_TYPE_ACCESS:
  167. $str .= 'Access';
  168. break;
  169. case self::RULE_TYPE_ADMIN:
  170. $str .= 'Admin';
  171. break;
  172. }
  173. $str .= ") ";
  174. switch($this->ruleAction)
  175. {
  176. case self::RULE_ACTION_ALLOW:
  177. $str .= 'Allow ';
  178. break;
  179. case self::RULE_ACTION_DENY:
  180. $str .= 'Deny ';
  181. break;
  182. }
  183. switch($this->ruleScope)
  184. {
  185. case self::RULE_SCOPE_USER:
  186. if ($this->ruleValue == self::RULE_VALUE_ALL) {
  187. if ($this->ruleAuthority) {
  188. $str .= " All Users";
  189. } else {
  190. $str .= " All Logged In Users";
  191. }
  192. } else {
  193. $str .= " User \"$this->ruleValue\"";
  194. }
  195. break;
  196. case self::RULE_SCOPE_GROUP:
  197. $str .= " Group \"$this->ruleValue\"";
  198. break;
  199. case self::RULE_SCOPE_EVERYONE:
  200. $str .= ' Everyone ';
  201. break;
  202. }
  203. if ($this->ruleAuthority) {
  204. $str .= " from \"$this->ruleAuthority\"";
  205. }
  206. return $str;
  207. }
  208. public function toArray() {
  209. return array(
  210. 'TITLE'=>strval($this),
  211. 'type'=>$this->ruleType,
  212. 'action'=>$this->ruleAction,
  213. 'scope'=>$this->ruleScope,
  214. 'authority'=>$this->ruleAuthority,
  215. 'value'=>$this->ruleValue
  216. );
  217. }
  218. public static function allAccess() {
  219. return self::factory(
  220. self::RULE_TYPE_ACCESS,
  221. self::RULE_ACTION_ALLOW,
  222. self::RULE_SCOPE_EVERYONE,
  223. null,
  224. null
  225. );
  226. }
  227. public static function validateACL($key, $aclArray) {
  228. $aclArray = array_merge(array(
  229. 'type'=>'','action'=>'','scope'=>'','authority'=>'','value'=>''),
  230. $aclArray
  231. );
  232. $acl = new AccessControlList($aclArray['type'], $aclArray['action'], $aclArray['scope'], $aclArray['authority'], $aclArray['value']);
  233. return true;
  234. }
  235. /**
  236. * Instantiates an AccessControlList
  237. * @param $ruleType string @see AccessControlList::ruleTypes()
  238. * @param $ruleAction string @see AccessControlList::ruleActions()
  239. * @param $ruleScope string @see AccessControlList::ruleScopes()
  240. * @param $ruleValue string
  241. * @return AccessControlList or false if there was an error
  242. */
  243. public static function factory($ruleType, $ruleAction, $ruleScope, $ruleAuthority, $ruleValue)
  244. {
  245. try {
  246. $AccessControlList = new AccessControlList($ruleType, $ruleAction, $ruleScope, $ruleAuthority, $ruleValue);
  247. } catch (Exception $e) {
  248. $AccessControlList = false;
  249. }
  250. return $AccessControlList;
  251. }
  252. /**
  253. * Constructor
  254. * @param $ruleType string @see AccessControlList::ruleTypes()
  255. * @param $ruleAction string @see AccessControlList::ruleActions()
  256. * @param $ruleScope string @see AccessControlList::ruleScopes()
  257. * @param $ruleValue string
  258. *
  259. * Will throw an exception if invalid values are present
  260. */
  261. public function __construct($ruleType, $ruleAction, $ruleScope, $ruleAuthority, $ruleValue)
  262. {
  263. if (!in_array($ruleType, self::ruleTypes())) {
  264. throw new Exception("Invalid rule type $ruleType");
  265. }
  266. if (!in_array($ruleAction, self::ruleActions())) {
  267. throw new Exception("Invalid rule action $ruleAction");
  268. }
  269. if (!in_array($ruleScope, self::ruleScopes())) {
  270. throw new Exception("Invalid rule scope $ruleScope");
  271. }
  272. if ($ruleScope==self::RULE_SCOPE_GROUP && $ruleValue == self::RULE_VALUE_ALL) {
  273. throw new Exception("Rule of type Group cannot contain ALL");
  274. }
  275. if ($ruleScope != self::RULE_SCOPE_EVERYONE && empty($ruleValue)) {
  276. throw new Exception("Rule value cannot be empty");
  277. }
  278. $this->ruleType = $ruleType;
  279. $this->ruleAction = $ruleAction;
  280. $this->ruleScope = $ruleScope;
  281. $this->ruleAuthority = $ruleAuthority;
  282. $this->ruleValue = $ruleValue;
  283. }
  284. }