2013-07-09 22:06:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-03-01 16:25:15 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author macjohnny <estebanmarin@gmx.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-01 16:25:15 +00:00
|
|
|
* @author Roman Kreisel <mail@romankreisel.de>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author voxsim <Simon Vocella>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
namespace OC\Group;
|
|
|
|
|
|
|
|
use OC\Hooks\PublicEmitter;
|
2016-01-14 12:58:45 +00:00
|
|
|
use OCP\GroupInterface;
|
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
|
|
|
/**
|
2016-01-14 12:58:45 +00:00
|
|
|
* @var GroupInterface[] $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
|
|
|
|
2015-08-13 19:31:38 +00:00
|
|
|
/** @var \OC\SubAdmin */
|
|
|
|
private $subAdmin = null;
|
2014-06-19 17:35:10 +00:00
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* @param \OC\User\Manager $userManager
|
|
|
|
*/
|
2015-08-13 19:31:38 +00:00
|
|
|
public function __construct(\OC\User\Manager $userManager) {
|
2013-07-09 22:06:22 +00:00
|
|
|
$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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-11 15:57:00 +00:00
|
|
|
/**
|
2015-06-16 09:31:04 +00:00
|
|
|
* Checks whether a given backend is used
|
|
|
|
*
|
|
|
|
* @param string $backendClass Full classname including complete namespace
|
|
|
|
* @return bool
|
2015-06-11 15:57:00 +00:00
|
|
|
*/
|
2015-06-16 09:31:04 +00:00
|
|
|
public function isBackendUsed($backendClass) {
|
|
|
|
$backendClass = strtolower(ltrim($backendClass, '\\'));
|
|
|
|
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if (strtolower(get_class($backend)) === $backendClass) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-06-11 15:57:00 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
2016-01-14 12:58:45 +00:00
|
|
|
* @param \OCP\GroupInterface $backend
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
|
|
|
public function addBackend($backend) {
|
|
|
|
$this->backends[] = $backend;
|
2016-01-21 16:29:14 +00:00
|
|
|
$this->clearCaches();
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function clearBackends() {
|
|
|
|
$this->backends = array();
|
2016-01-21 16:29:14 +00:00
|
|
|
$this->clearCaches();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function clearCaches() {
|
2013-07-09 22:06:22 +00:00
|
|
|
$this->cachedGroups = array();
|
2015-02-25 13:54:25 +00:00
|
|
|
$this->cachedUserGroups = array();
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
2016-02-17 13:21:07 +00:00
|
|
|
/**
|
|
|
|
* @param string $gid
|
2016-11-30 19:21:44 +00:00
|
|
|
* @param string $displayName
|
2016-02-18 09:50:00 +00:00
|
|
|
* @return \OCP\IGroup
|
2016-02-17 13:21:07 +00:00
|
|
|
*/
|
2016-11-30 19:21:44 +00:00
|
|
|
protected function getGroupObject($gid, $displayName = null) {
|
2013-07-18 10:15:34 +00:00
|
|
|
$backends = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
2016-11-30 19:21:44 +00:00
|
|
|
if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) {
|
|
|
|
$groupData = $backend->getGroupDetails($gid);
|
|
|
|
if (is_array($groupData)) {
|
|
|
|
// take the display name from the first backend that has a non-null one
|
|
|
|
if (is_null($displayName) && isset($groupData['displayName'])) {
|
|
|
|
$displayName = $groupData['displayName'];
|
|
|
|
}
|
|
|
|
$backends[] = $backend;
|
|
|
|
}
|
|
|
|
} else if ($backend->groupExists($gid)) {
|
2013-07-18 10:15:34 +00:00
|
|
|
$backends[] = $backend;
|
|
|
|
}
|
|
|
|
}
|
2014-02-14 13:25:45 +00:00
|
|
|
if (count($backends) === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-30 19:21:44 +00:00
|
|
|
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName);
|
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) {
|
2016-05-17 14:06:44 +00:00
|
|
|
if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) {
|
2013-07-09 22:06:22 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-20 13:38:29 +00:00
|
|
|
* @param \OC\User\User|null $user
|
2013-07-09 22:06:22 +00:00
|
|
|
* @return \OC\Group\Group[]
|
|
|
|
*/
|
|
|
|
public function getUserGroups($user) {
|
2015-11-19 14:35:58 +00:00
|
|
|
if (is_null($user)) {
|
2016-03-07 10:57:05 +00:00
|
|
|
return [];
|
2015-11-19 14:35:58 +00:00
|
|
|
}
|
2014-12-16 19:33:13 +00:00
|
|
|
return $this->getUserIdGroups($user->getUID());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uid the user id
|
|
|
|
* @return \OC\Group\Group[]
|
|
|
|
*/
|
|
|
|
public function getUserIdGroups($uid) {
|
2014-02-21 21:52:48 +00:00
|
|
|
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);
|
2015-04-14 09:00:20 +00:00
|
|
|
if (is_array($groupIds)) {
|
|
|
|
foreach ($groupIds as $groupId) {
|
|
|
|
$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-12-16 19:33:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a userId is in the admin group
|
|
|
|
* @param string $userId
|
|
|
|
* @return bool if admin
|
|
|
|
*/
|
|
|
|
public function isAdmin($userId) {
|
|
|
|
return $this->isInGroup($userId, 'admin');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a userId is in a group
|
|
|
|
* @param string $userId
|
2015-06-19 19:00:42 +00:00
|
|
|
* @param string $group
|
2014-12-16 19:33:13 +00:00
|
|
|
* @return bool if in group
|
|
|
|
*/
|
|
|
|
public function isInGroup($userId, $group) {
|
|
|
|
return array_key_exists($group, $this->getUserIdGroups($userId));
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-11-06 09:40:09 +00:00
|
|
|
return array_map(function($value) {
|
|
|
|
return (string) $value;
|
|
|
|
}, array_keys($this->getUserGroups($user)));
|
2014-06-12 07:41:23 +00:00
|
|
|
}
|
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 {
|
2015-03-24 20:35:44 +00:00
|
|
|
$filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
|
2014-09-15 16:37:54 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OC\SubAdmin
|
|
|
|
*/
|
|
|
|
public function getSubAdmin() {
|
|
|
|
if (!$this->subAdmin) {
|
|
|
|
$this->subAdmin = new \OC\SubAdmin(
|
|
|
|
$this->userManager,
|
|
|
|
$this,
|
|
|
|
\OC::$server->getDatabaseConnection()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->subAdmin;
|
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|