2013-07-09 22:06:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Group;
|
|
|
|
|
|
|
|
use OC\Hooks\PublicEmitter;
|
2014-07-16 12:25:31 +00:00
|
|
|
use OCP\IGroupManager;
|
2013-07-09 22:06:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Manager
|
|
|
|
*
|
|
|
|
* Hooks available in scope \OC\Group:
|
|
|
|
* - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-09 23:59:59 +00:00
|
|
|
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-09 22:06:22 +00:00
|
|
|
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-09 23:59:59 +00:00
|
|
|
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-09 22:06:22 +00:00
|
|
|
* - preDelete(\OC\Group\Group $group)
|
2013-07-09 23:59:59 +00:00
|
|
|
* - postDelete(\OC\Group\Group $group)
|
2013-07-09 22:06:22 +00:00
|
|
|
* - preCreate(string $groupId)
|
2013-07-09 23:59:59 +00:00
|
|
|
* - postCreate(\OC\Group\Group $group)
|
2013-07-09 22:06:22 +00:00
|
|
|
*
|
|
|
|
* @package OC\Group
|
|
|
|
*/
|
2014-07-16 12:25:31 +00:00
|
|
|
class Manager extends PublicEmitter implements IGroupManager {
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
2014-05-11 17:28:45 +00:00
|
|
|
* @var \OC_Group_Backend[]|\OC_Group_Database[] $backends
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
|
|
|
private $backends = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\User\Manager $userManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group[]
|
|
|
|
*/
|
2014-02-21 21:52:48 +00:00
|
|
|
private $cachedGroups = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group[]
|
|
|
|
*/
|
|
|
|
private $cachedUserGroups = array();
|
2013-07-09 22:06:22 +00:00
|
|
|
|
2014-06-19 17:35:10 +00:00
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* @param \OC\User\Manager $userManager
|
|
|
|
*/
|
|
|
|
public function __construct($userManager) {
|
|
|
|
$this->userManager = $userManager;
|
2014-02-27 18:12:03 +00:00
|
|
|
$cachedGroups = & $this->cachedGroups;
|
|
|
|
$cachedUserGroups = & $this->cachedUserGroups;
|
|
|
|
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group $group
|
|
|
|
*/
|
2014-02-27 18:12:03 +00:00
|
|
|
unset($cachedGroups[$group->getGID()]);
|
|
|
|
$cachedUserGroups = array();
|
2013-07-09 22:06:22 +00:00
|
|
|
});
|
2014-02-27 19:09:07 +00:00
|
|
|
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group $group
|
|
|
|
*/
|
2014-02-27 19:09:07 +00:00
|
|
|
$cachedUserGroups = array();
|
|
|
|
});
|
|
|
|
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
|
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group $group
|
|
|
|
*/
|
|
|
|
$cachedUserGroups = array();
|
2013-07-09 22:06:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC_Group_Backend $backend
|
|
|
|
*/
|
|
|
|
public function addBackend($backend) {
|
|
|
|
$this->backends[] = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearBackends() {
|
|
|
|
$this->backends = array();
|
|
|
|
$this->cachedGroups = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $gid
|
|
|
|
* @return \OC\Group\Group
|
|
|
|
*/
|
|
|
|
public function get($gid) {
|
|
|
|
if (isset($this->cachedGroups[$gid])) {
|
|
|
|
return $this->cachedGroups[$gid];
|
|
|
|
}
|
2014-02-14 13:25:45 +00:00
|
|
|
return $this->getGroupObject($gid);
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 00:09:42 +00:00
|
|
|
protected function getGroupObject($gid) {
|
2013-07-18 10:15:34 +00:00
|
|
|
$backends = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend->groupExists($gid)) {
|
|
|
|
$backends[] = $backend;
|
|
|
|
}
|
|
|
|
}
|
2014-02-14 13:25:45 +00:00
|
|
|
if (count($backends) === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-18 10:15:34 +00:00
|
|
|
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
|
2013-07-09 22:06:22 +00:00
|
|
|
return $this->cachedGroups[$gid];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $gid
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-10 00:17:24 +00:00
|
|
|
public function groupExists($gid) {
|
2013-07-09 22:06:22 +00:00
|
|
|
return !is_null($this->get($gid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $gid
|
|
|
|
* @return \OC\Group\Group
|
|
|
|
*/
|
2013-07-10 00:17:24 +00:00
|
|
|
public function createGroup($gid) {
|
2014-08-04 11:46:53 +00:00
|
|
|
if ($gid === '' || is_null($gid)) {
|
2013-07-09 22:06:22 +00:00
|
|
|
return false;
|
2014-02-14 14:07:08 +00:00
|
|
|
} else if ($group = $this->get($gid)) {
|
|
|
|
return $group;
|
2013-07-09 22:06:22 +00:00
|
|
|
} else {
|
|
|
|
$this->emit('\OC\Group', 'preCreate', array($gid));
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) {
|
|
|
|
$backend->createGroup($gid);
|
2013-07-10 00:09:42 +00:00
|
|
|
$group = $this->getGroupObject($gid);
|
2013-07-09 23:59:59 +00:00
|
|
|
$this->emit('\OC\Group', 'postCreate', array($group));
|
2013-07-09 22:06:22 +00:00
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return \OC\Group\Group[]
|
|
|
|
*/
|
2013-07-10 00:17:24 +00:00
|
|
|
public function search($search, $limit = null, $offset = null) {
|
2013-07-09 22:06:22 +00:00
|
|
|
$groups = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
$groupIds = $backend->getGroups($search, $limit, $offset);
|
|
|
|
foreach ($groupIds as $groupId) {
|
2014-02-21 21:52:48 +00:00
|
|
|
$groups[$groupId] = $this->get($groupId);
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
if (!is_null($limit) and $limit <= 0) {
|
|
|
|
return array_values($groups);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values($groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\User\User $user
|
|
|
|
* @return \OC\Group\Group[]
|
|
|
|
*/
|
|
|
|
public function getUserGroups($user) {
|
2014-02-21 21:52:48 +00:00
|
|
|
$uid = $user->getUID();
|
|
|
|
if (isset($this->cachedUserGroups[$uid])) {
|
|
|
|
return $this->cachedUserGroups[$uid];
|
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
$groups = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
2014-02-21 21:52:48 +00:00
|
|
|
$groupIds = $backend->getUserGroups($uid);
|
2013-07-09 22:06:22 +00:00
|
|
|
foreach ($groupIds as $groupId) {
|
2014-02-21 21:52:48 +00:00
|
|
|
$groups[$groupId] = $this->get($groupId);
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-23 13:59:27 +00:00
|
|
|
$this->cachedUserGroups[$uid] = $groups;
|
2014-02-21 21:52:48 +00:00
|
|
|
return $this->cachedUserGroups[$uid];
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
2014-06-19 09:41:29 +00:00
|
|
|
|
2014-06-12 07:41:23 +00:00
|
|
|
/**
|
2014-06-19 09:41:29 +00:00
|
|
|
* get a list of group ids for a user
|
2014-06-12 07:41:23 +00:00
|
|
|
* @param \OC\User\User $user
|
2014-06-19 17:35:10 +00:00
|
|
|
* @return array with group ids
|
2014-06-12 07:41:23 +00:00
|
|
|
*/
|
|
|
|
public function getUserGroupIds($user) {
|
|
|
|
$groupIds = array();
|
2014-06-19 17:35:10 +00:00
|
|
|
$userId = $user->getUID();
|
|
|
|
if (isset($this->cachedUserGroups[$userId])) {
|
2014-06-23 13:59:27 +00:00
|
|
|
return array_keys($this->cachedUserGroups[$userId]);
|
2014-06-23 12:47:19 +00:00
|
|
|
} else {
|
2014-06-19 17:35:10 +00:00
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
$groupIds = array_merge($groupIds, $backend->getUserGroups($userId));
|
|
|
|
}
|
2014-06-12 07:41:23 +00:00
|
|
|
}
|
|
|
|
return $groupIds;
|
|
|
|
}
|
2014-03-14 12:51:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get a list of all display names in a group
|
2014-03-14 12:51:17 +00:00
|
|
|
* @param string $gid
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array of display names (value) and user ids (key)
|
2014-03-14 12:51:17 +00:00
|
|
|
*/
|
|
|
|
public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
|
|
|
|
$group = $this->get($gid);
|
|
|
|
if(is_null($group)) {
|
|
|
|
return array();
|
|
|
|
}
|
2014-08-28 11:51:48 +00:00
|
|
|
|
2014-03-14 12:51:17 +00:00
|
|
|
$search = trim($search);
|
2014-08-28 11:51:48 +00:00
|
|
|
$groupUsers = array();
|
|
|
|
|
2014-03-14 12:51:17 +00:00
|
|
|
if(!empty($search)) {
|
2014-08-28 11:51:48 +00:00
|
|
|
// only user backends have the capability to do a complex search for users
|
2014-09-15 16:37:54 +00:00
|
|
|
$searchOffset = 0;
|
2014-10-10 19:29:11 +00:00
|
|
|
$searchLimit = $limit * 100;
|
2014-09-15 16:37:54 +00:00
|
|
|
if($limit === -1) {
|
2014-10-10 19:29:11 +00:00
|
|
|
$searchLimit = 500;
|
2014-08-28 11:51:48 +00:00
|
|
|
}
|
2014-09-15 16:37:54 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
$filteredUsers = $this->userManager->search($search, $searchLimit, $searchOffset);
|
|
|
|
foreach($filteredUsers as $filteredUser) {
|
|
|
|
if($group->inGroup($filteredUser)) {
|
2014-09-18 15:50:19 +00:00
|
|
|
$groupUsers[]= $filteredUser;
|
2014-09-15 16:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$searchOffset += $searchLimit;
|
2014-10-10 16:58:39 +00:00
|
|
|
} while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit);
|
2014-09-15 16:37:54 +00:00
|
|
|
|
2014-08-28 11:51:48 +00:00
|
|
|
if($limit === -1) {
|
|
|
|
$groupUsers = array_slice($groupUsers, $offset);
|
|
|
|
} else {
|
|
|
|
$groupUsers = array_slice($groupUsers, $offset, $limit);
|
|
|
|
}
|
2014-03-14 12:51:17 +00:00
|
|
|
} else {
|
2014-08-28 11:51:48 +00:00
|
|
|
$groupUsers = $group->searchUsers('', $limit, $offset);
|
2014-03-14 12:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$matchingUsers = array();
|
2014-08-28 11:51:48 +00:00
|
|
|
foreach($groupUsers as $groupUser) {
|
|
|
|
$matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName();
|
2014-03-14 12:51:17 +00:00
|
|
|
}
|
|
|
|
return $matchingUsers;
|
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|