| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /*
- * This file is part of MODX Revolution.
- *
- * Copyright (c) MODX, LLC. All Rights Reserved.
- *
- * For complete copyright and license information, see the COPYRIGHT and LICENSE
- * files found in the top-level directory of this distribution.
- */
- /**
- * Represents a group of users with common attributes.
- *
- * @property string $name The name of the User Group
- * @property string $description A user-specified description of the User Group
- * @property int $parent The parent group for this User Group. If none, will be 0
- * @property int $rank The rank of this group, used when sorting the groups
- *
- * @see modUser
- * @see modUserGroupRole
- * @see modUserGroupMember
- * @package modx
- */
- class modUserGroup extends xPDOSimpleObject {
- /**
- * Overrides xPDOObject::save to fire modX-specific events.
- *
- * {@inheritDoc}
- */
- public function save($cacheFlag= null) {
- $isNew = $this->isNew();
- if ($this->xpdo instanceof modX) {
- $this->xpdo->invokeEvent('OnUserGroupBeforeSave',array(
- 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
- 'usergroup' => &$this,
- 'cacheFlag' => $cacheFlag,
- ));
- }
- $saved = parent :: save($cacheFlag);
- if ($saved && $this->xpdo instanceof modX) {
- $this->xpdo->invokeEvent('OnUserGroupSave',array(
- 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
- 'usergroup' => &$this,
- 'cacheFlag' => $cacheFlag,
- ));
- }
- return $saved;
- }
- /**
- * Overrides xPDOObject::remove to fire modX-specific events
- *
- * {@inheritDoc}
- */
- public function remove(array $ancestors= array ()) {
- if ($this->xpdo instanceof modX) {
- $this->xpdo->invokeEvent('OnUserGroupBeforeRemove',array(
- 'usergroup' => &$this,
- 'ancestors' => $ancestors,
- ));
- }
- $removed = parent :: remove($ancestors);
- // delete ACLs for this group
- $targets = explode(',', $this->xpdo->getOption('principal_targets', null, 'modAccessContext,modAccessResourceGroup,modAccessCategory'));
- array_walk($targets, 'trim');
- foreach($targets as $target) {
- $fields = $this->xpdo->getFields($target);
- if(array_key_exists('principal_class', $fields) && array_key_exists('principal', $fields)) {
- $tablename = $this->xpdo->getTableName($target);
- $principal_class_field = $this->xpdo->escape('principal_class');
- $principal_field = $this->xpdo->escape('principal');
- if(!empty($tablename)) {
- $this->xpdo->query("DELETE FROM {$tablename} WHERE {$principal_class_field} = 'modUserGroup' AND {$principal_field} = {$this->_fields['id']}");
- }
- }
- }
- if ($this->xpdo instanceof modX) {
- $this->xpdo->invokeEvent('OnUserGroupRemove',array(
- 'usergroup' => &$this,
- 'ancestors' => $ancestors,
- ));
- }
- return $removed;
- }
- /**
- * Get all users in a user group.
- *
- * @access public
- * @param array $criteria
- * @return array An array of {@link modUser} objects.
- */
- public function getUsersIn(array $criteria = array()) {
- $c = $this->xpdo->newQuery('modUser');
- $c->select($this->xpdo->getSelectColumns('modUser','modUser'));
- $c->select(array(
- 'role' => 'UserGroupRole.name',
- 'role_name' => 'UserGroupRole.name',
- ));
- $c->innerJoin('modUserGroupMember','UserGroupMembers');
- $c->leftJoin('modUserGroupRole','UserGroupRole','UserGroupMembers.role = UserGroupRole.id');
- $c->where(array(
- 'UserGroupMembers.user_group' => $this->get('id'),
- ));
- $sort = !empty($criteria['sort']) ? $criteria['sort'] : 'modUser.username';
- $dir = !empty($criteria['dir']) ? $criteria['dir'] : 'DESC';
- $c->sortby($sort,$dir);
- if (isset($criteria['limit'])) {
- $start = !empty($criteria['start']) ? $criteria['start'] : 0;
- $c->limit($criteria['limit'],$start);
- }
- return $this->xpdo->getCollection('modUser',$c);
- }
- /**
- * Get all resource groups related to the user group.
- *
- * @access public
- * @param boolean $limit The number of Resource Groups to grab. Defaults to 0, which
- * grabs all Groups.
- * @param int $start The starting index for the limit query.
- * @return array An array of resource groups.
- */
- public function getResourceGroups($limit = false,$start = 0) {
- $c = $this->xpdo->newQuery('modResourceGroup');
- $c->innerJoin('modAccessResourceGroup','Acls',array(
- 'Acls.principal_class' => 'modUserGroup',
- 'Acls.principal' => $this->get('id'),
- ));
- if ($limit) $c->limit($limit,$start);
- return $this->xpdo->getCollection('modResourceGroup',$c);
- }
- }
|