modusergroup.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Represents a group of users with common attributes.
  12. *
  13. * @property string $name The name of the User Group
  14. * @property string $description A user-specified description of the User Group
  15. * @property int $parent The parent group for this User Group. If none, will be 0
  16. * @property int $rank The rank of this group, used when sorting the groups
  17. *
  18. * @see modUser
  19. * @see modUserGroupRole
  20. * @see modUserGroupMember
  21. * @package modx
  22. */
  23. class modUserGroup extends xPDOSimpleObject {
  24. /**
  25. * Overrides xPDOObject::save to fire modX-specific events.
  26. *
  27. * {@inheritDoc}
  28. */
  29. public function save($cacheFlag= null) {
  30. $isNew = $this->isNew();
  31. if ($this->xpdo instanceof modX) {
  32. $this->xpdo->invokeEvent('OnUserGroupBeforeSave',array(
  33. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  34. 'usergroup' => &$this,
  35. 'cacheFlag' => $cacheFlag,
  36. ));
  37. }
  38. $saved = parent :: save($cacheFlag);
  39. if ($saved && $this->xpdo instanceof modX) {
  40. $this->xpdo->invokeEvent('OnUserGroupSave',array(
  41. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  42. 'usergroup' => &$this,
  43. 'cacheFlag' => $cacheFlag,
  44. ));
  45. }
  46. return $saved;
  47. }
  48. /**
  49. * Overrides xPDOObject::remove to fire modX-specific events
  50. *
  51. * {@inheritDoc}
  52. */
  53. public function remove(array $ancestors= array ()) {
  54. if ($this->xpdo instanceof modX) {
  55. $this->xpdo->invokeEvent('OnUserGroupBeforeRemove',array(
  56. 'usergroup' => &$this,
  57. 'ancestors' => $ancestors,
  58. ));
  59. }
  60. $removed = parent :: remove($ancestors);
  61. // delete ACLs for this group
  62. $targets = explode(',', $this->xpdo->getOption('principal_targets', null, 'modAccessContext,modAccessResourceGroup,modAccessCategory'));
  63. array_walk($targets, 'trim');
  64. foreach($targets as $target) {
  65. $fields = $this->xpdo->getFields($target);
  66. if(array_key_exists('principal_class', $fields) && array_key_exists('principal', $fields)) {
  67. $tablename = $this->xpdo->getTableName($target);
  68. $principal_class_field = $this->xpdo->escape('principal_class');
  69. $principal_field = $this->xpdo->escape('principal');
  70. if(!empty($tablename)) {
  71. $this->xpdo->query("DELETE FROM {$tablename} WHERE {$principal_class_field} = 'modUserGroup' AND {$principal_field} = {$this->_fields['id']}");
  72. }
  73. }
  74. }
  75. if ($this->xpdo instanceof modX) {
  76. $this->xpdo->invokeEvent('OnUserGroupRemove',array(
  77. 'usergroup' => &$this,
  78. 'ancestors' => $ancestors,
  79. ));
  80. }
  81. return $removed;
  82. }
  83. /**
  84. * Get all users in a user group.
  85. *
  86. * @access public
  87. * @param array $criteria
  88. * @return array An array of {@link modUser} objects.
  89. */
  90. public function getUsersIn(array $criteria = array()) {
  91. $c = $this->xpdo->newQuery('modUser');
  92. $c->select($this->xpdo->getSelectColumns('modUser','modUser'));
  93. $c->select(array(
  94. 'role' => 'UserGroupRole.name',
  95. 'role_name' => 'UserGroupRole.name',
  96. ));
  97. $c->innerJoin('modUserGroupMember','UserGroupMembers');
  98. $c->leftJoin('modUserGroupRole','UserGroupRole','UserGroupMembers.role = UserGroupRole.id');
  99. $c->where(array(
  100. 'UserGroupMembers.user_group' => $this->get('id'),
  101. ));
  102. $sort = !empty($criteria['sort']) ? $criteria['sort'] : 'modUser.username';
  103. $dir = !empty($criteria['dir']) ? $criteria['dir'] : 'DESC';
  104. $c->sortby($sort,$dir);
  105. if (isset($criteria['limit'])) {
  106. $start = !empty($criteria['start']) ? $criteria['start'] : 0;
  107. $c->limit($criteria['limit'],$start);
  108. }
  109. return $this->xpdo->getCollection('modUser',$c);
  110. }
  111. /**
  112. * Get all resource groups related to the user group.
  113. *
  114. * @access public
  115. * @param boolean $limit The number of Resource Groups to grab. Defaults to 0, which
  116. * grabs all Groups.
  117. * @param int $start The starting index for the limit query.
  118. * @return array An array of resource groups.
  119. */
  120. public function getResourceGroups($limit = false,$start = 0) {
  121. $c = $this->xpdo->newQuery('modResourceGroup');
  122. $c->innerJoin('modAccessResourceGroup','Acls',array(
  123. 'Acls.principal_class' => 'modUserGroup',
  124. 'Acls.principal' => $this->get('id'),
  125. ));
  126. if ($limit) $c->limit($limit,$start);
  127. return $this->xpdo->getCollection('modResourceGroup',$c);
  128. }
  129. }