/ck/protected/modules/rbam/components/behaviors/RbamPhpAuthManagerBehavior.php

https://github.com/moravianlibrary/Projekt-Ceska-knihovna · PHP · 230 lines · 116 code · 28 blank · 86 comment · 11 complexity · ff587d3f20a45552e69aecb4df4edde3 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id: RbamPhpAuthManagerBehavior.php 15 2010-12-20 09:01:13Z Chris $*/
  3. /**
  4. * RBAM PhpAuthManager Behavior class file.
  5. * Provides additional features used by RBAM to CPhpAuthmanager.
  6. *
  7. * @copyright Copyright &copy; 2010 PBM Web Development - All Rights Reserved
  8. * @package RBAM
  9. * @since V1.0.0
  10. * @version $Revision: 15 $
  11. * @license BSD License (see documentation)
  12. */
  13. /**
  14. * RBAM PhpAuthManager Behavior class
  15. * @package RBAM
  16. */
  17. class RbamPhpAuthManagerBehavior extends RbamAuthManagerBehavior {
  18. /**
  19. * Returns the assignments authorised for the specified item.
  20. * @param string the item name.
  21. * @return array CAuthAssignments authorised for the item
  22. */
  23. public function getItemEAuthAssignments($name) {
  24. $owner = $this->getOwner();
  25. $roles = array();
  26. if ($owner->getAuthItem($name)->getType() == CAuthItem::TYPE_ROLE)
  27. $roles[] = $name;
  28. $authItems = $this->getEAncestors($name);
  29. foreach ($authItems as $authItem)
  30. if ($authItem->getType() == CAuthItem::TYPE_ROLE)
  31. $roles[] = $authItem->getName();
  32. $assignments = array();
  33. if (!empty($roles)) {
  34. // get users, then all their assignments, then filter those by name
  35. $userIdAttribute = $this->module->userIdAttribute;
  36. $criteria = new CDbCriteria($this->module->userCriteria);
  37. $criteria->mergeWith(new CDbCriteria(array('select'=>$userIdAttribute)));
  38. $userAssignments = array();
  39. foreach (CActiveRecord::model($this->module->userClass)->findAll($criteria) as $user) {
  40. $usersAssignments[$user->$userIdAttribute] = $owner->getAuthAssignments($user->$userIdAttribute);
  41. }
  42. foreach ($roles as $role)
  43. foreach ($usersAssignments as $userAssignments)
  44. if (array_key_exists($role, $userAssignments))
  45. $assignments[] = $userAssignments[$role];
  46. foreach($assignments as &$assignment)
  47. $assignment->attachBehavior('RbamAuthAssignmentBehavior', array(
  48. 'class'=>'RbamAuthAssignmentBehavior',
  49. 'module'=>$this->module
  50. ));
  51. }
  52. return $assignments;
  53. }
  54. /**
  55. * Returns the parents of the specified item.
  56. * If type is not given all parents are returned.
  57. * @param mixed name(s) of the child item(s).
  58. * This can be either a string or an array.
  59. * The latter represents a list of item names.
  60. * @param integer type of parents to return.
  61. * @return array all parent items of the child
  62. */
  63. public function getItemEParents($names, $type=null) {
  64. if (is_string($names))
  65. $names = array($names);
  66. $owner = $this->getOwner();
  67. $parents = array();
  68. foreach ($owner->getAuthItems() as $authItem)
  69. foreach ($names as $name)
  70. if ($authItem->hasChild($name))
  71. $parents[] = $authItem;
  72. $parents = $this->filterByType($parents, $type);
  73. foreach($parents as &$parent)
  74. $parent->attachBehavior('RbamAuthItemBehavior', 'RbamAuthItemBehavior');
  75. return $parents;
  76. }
  77. /**
  78. * Returns items of the specified type that are children of the specified parent item.
  79. * If type is not given all children are returned.
  80. * @param mixed name(s) of the parent item(s).
  81. * This can be either a string or an array.
  82. * The latter represents a list of item names.
  83. * @param integer type of children to return.
  84. * @return array items of the specified type that are children of the parent.
  85. */
  86. public function getItemEChildren($names, $type=null) {
  87. $children = $this->getOwner()->getItemChildren($names);
  88. $children = $this->filterByType($children, $type);
  89. foreach($children as &$child)
  90. $child->attachBehavior('RbamAuthItemBehavior', 'RbamAuthItemBehavior');
  91. return $children;
  92. }
  93. /**
  94. * Returns items of the specified type that are unrelated to the specified item.
  95. * If type is not given all unrelated items are returned.
  96. * @param string name of the item.
  97. * @param integer type of item to return
  98. * @return array items of the specified type that are unrelated to the item.
  99. */
  100. public function getItemEUnrelated($name, $type=null) {
  101. $unrelated = $this->filterOutRelated($name, $type, $this->getOwner()->getAuthItems($type));
  102. foreach($unrelated as &$item)
  103. $item->attachBehavior('RbamAuthItemBehavior', 'RbamAuthItemBehavior');
  104. $related = array_merge($this->getEAncestors($name), $this->getEDescendants($name));
  105. return array_diff_key($unrelated, $related);
  106. }
  107. /**
  108. * Returns the number of children of the specified item.
  109. * @param string $name the parent item name.
  110. * @return int the number of child items of the parent
  111. */
  112. public function getItemChildCount($name) {
  113. return count($this->getOwner()->getItemChildren($name));
  114. }
  115. /**
  116. * Returns the number of parents of the specified item.
  117. * @param string $name the child item name.
  118. * @return int the number of parent items of the child
  119. */
  120. public function getItemParentCount($name) {
  121. return count($this->getItemEParents($name));
  122. }
  123. /**
  124. * Returns roles not assigned to the user, either directly or via inheritance.
  125. * @param mixed the user id.
  126. * @return array roles not assigned to the user.
  127. */
  128. public function getEUnassignedRoles($uid) {
  129. $owner = $this->getOwner();
  130. $unassignedRoles = array();
  131. foreach ($owner->getAuthItems(CAuthItem::TYPE_ROLE) as $role)
  132. if (!$owner->isAssigned($role->name, $uid))
  133. $unassignedRoles[] = $role;
  134. foreach ($owner->defaultRoles as $defaultRole)
  135. unset($unassignedRoles[$defaultRole]);
  136. foreach($unassignedRoles as &$unassignedRole)
  137. $unassignedRole->attachBehavior('RbamAuthItemBehavior', 'RbamAuthItemBehavior');
  138. $assignedRoles = array();
  139. foreach ($owner->getAuthAssignments($uid) as $assignment)
  140. $assignedRoles[] = $assignment->itemName;
  141. $childRoles = $this->getItemEChildren($assignedRoles, CAuthItem::TYPE_ROLE);
  142. return array_diff_key($unassignedRoles, $childRoles);
  143. }
  144. /**
  145. * Filter items by type
  146. * @param array items to filter
  147. * @param integer type of auth items required
  148. * @return filtered items
  149. */
  150. private function filterByType($items, $type) {
  151. if (!is_null($type)) {
  152. switch ($type) {
  153. case CAuthItem::TYPE_OPERATION:
  154. $items = array_filter($items, array($this,'isOperation'));
  155. break;
  156. case CAuthItem::TYPE_TASK:
  157. $items = array_filter($items, array($this,'isTask'));
  158. break;
  159. case CAuthItem::TYPE_ROLE:
  160. $items = array_filter($items, array($this,'isRole'));
  161. break;
  162. }
  163. }
  164. return $items;
  165. }
  166. /**
  167. * Filters out relations of an item from the list of items
  168. * @param string name of the item
  169. * @param integer type of items
  170. * @param array list of items to filter
  171. * @return array filtered items
  172. */
  173. private function filterOutRelated($name, $type, $items) {
  174. foreach ($this->getItemEChildren($name, $type) as $child)
  175. unset($items[$child->getName()]);
  176. foreach ($this->getItemEParents($name, $type) as $child)
  177. unset($items[$child->getName()]);
  178. unset($items[$name]);
  179. return $items;
  180. }
  181. /**
  182. * Callback to filter auth items that are operations
  183. * @param CAuthItem the item to test
  184. */
  185. public function isOperation($item) {
  186. return $item->type==CAuthItem::TYPE_OPERATION;
  187. }
  188. /**
  189. * Callback to filter auth items that are tasks
  190. * @param CAuthItem the item to test
  191. */
  192. public function isTask($item) {
  193. return $item->type==CAuthItem::TYPE_TASK;
  194. }
  195. /**
  196. * Callback to filter auth items that are roles
  197. * @param CAuthItem the item to test
  198. */
  199. public function isRole($item) {
  200. return $item->type==CAuthItem::TYPE_ROLE;
  201. }
  202. }