nextcloud/lib/private/Group/Manager.php

426 lines
11 KiB
PHP
Raw Normal View History

2013-07-10 02:06:22 +04:00
<?php
/**
2016-07-21 18:07:57 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
2016-05-26 20:56:05 +03:00
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
2015-03-26 13:44:34 +03:00
* @author Bart Visscher <bartv@thisnet.nl>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
2016-07-21 18:07:57 +03:00
* @author Joas Schilling <coding@schilljs.com>
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Knut Ahlers <knut@ahlers.me>
2016-05-26 20:56:05 +03:00
* @author Lukas Reschke <lukas@statuscode.ch>
2015-03-26 13:44:34 +03:00
* @author macjohnny <estebanmarin@gmx.ch>
* @author Morris Jobke <hey@morrisjobke.de>
2016-07-21 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
2016-01-12 17:02:16 +03:00
* @author Robin McCorkell <robin@mccorkell.me.uk>
2016-07-21 18:07:57 +03:00
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Roman Kreisel <mail@romankreisel.de>
2015-06-25 12:43:55 +03:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
* @author Vinicius Cubas Brand <vinicius@eita.org.br>
* @author voxsim "Simon Vocella"
2015-03-26 13:44:34 +03:00
*
* @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/>
2015-03-26 13:44:34 +03:00
*
2013-07-10 02:06:22 +04:00
*/
2013-07-10 02:06:22 +04:00
namespace OC\Group;
use OC\Hooks\PublicEmitter;
use OCP\GroupInterface;
use OCP\IGroup;
2014-07-16 16:25:31 +04:00
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2013-07-10 02:06:22 +04:00
/**
* Class Manager
*
* Hooks available in scope \OC\Group:
* - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
2013-07-10 03:59:59 +04:00
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
2013-07-10 02:06:22 +04:00
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
2013-07-10 03:59:59 +04:00
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
2013-07-10 02:06:22 +04:00
* - preDelete(\OC\Group\Group $group)
2013-07-10 03:59:59 +04:00
* - postDelete(\OC\Group\Group $group)
2013-07-10 02:06:22 +04:00
* - preCreate(string $groupId)
2013-07-10 03:59:59 +04:00
* - postCreate(\OC\Group\Group $group)
2013-07-10 02:06:22 +04:00
*
* @package OC\Group
*/
2014-07-16 16:25:31 +04:00
class Manager extends PublicEmitter implements IGroupManager {
/** @var GroupInterface[] */
private $backends = [];
2013-07-10 02:06:22 +04:00
/** @var \OC\User\Manager */
2013-07-10 02:06:22 +04:00
private $userManager;
/** @var EventDispatcherInterface */
private $dispatcher;
/** @var ILogger */
private $logger;
2013-07-10 02:06:22 +04:00
/** @var \OC\Group\Group[] */
private $cachedGroups = [];
2014-02-22 01:52:48 +04:00
/** @var (string[])[] */
private $cachedUserGroups = [];
2013-07-10 02:06:22 +04:00
/** @var \OC\SubAdmin */
private $subAdmin = null;
2014-06-19 21:35:10 +04:00
2013-07-10 02:06:22 +04:00
/**
* @param \OC\User\Manager $userManager
* @param EventDispatcherInterface $dispatcher
* @param ILogger $logger
2013-07-10 02:06:22 +04:00
*/
public function __construct(\OC\User\Manager $userManager,
EventDispatcherInterface $dispatcher,
ILogger $logger) {
2013-07-10 02:06:22 +04:00
$this->userManager = $userManager;
$this->dispatcher = $dispatcher;
$this->logger = $logger;
$cachedGroups = &$this->cachedGroups;
$cachedUserGroups = &$this->cachedUserGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
2013-07-10 02:06:22 +04:00
/**
* @var \OC\Group\Group $group
*/
unset($cachedGroups[$group->getGID()]);
$cachedUserGroups = [];
2013-07-10 02:06:22 +04:00
});
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
2013-07-10 02:06:22 +04:00
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = [];
});
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = [];
2013-07-10 02:06:22 +04:00
});
}
/**
* Checks whether a given backend is used
*
* @param string $backendClass Full classname including complete namespace
* @return bool
*/
public function isBackendUsed($backendClass) {
$backendClass = strtolower(ltrim($backendClass, '\\'));
foreach ($this->backends as $backend) {
if (strtolower(get_class($backend)) === $backendClass) {
return true;
}
}
return false;
}
2013-07-10 02:06:22 +04:00
/**
* @param \OCP\GroupInterface $backend
2013-07-10 02:06:22 +04:00
*/
public function addBackend($backend) {
$this->backends[] = $backend;
2016-01-21 19:29:14 +03:00
$this->clearCaches();
2013-07-10 02:06:22 +04:00
}
public function clearBackends() {
$this->backends = [];
2016-01-21 19:29:14 +03:00
$this->clearCaches();
}
/**
* Get the active backends
*
* @return \OCP\GroupInterface[]
*/
public function getBackends() {
return $this->backends;
}
2016-01-21 19:29:14 +03:00
protected function clearCaches() {
$this->cachedGroups = [];
$this->cachedUserGroups = [];
2013-07-10 02:06:22 +04:00
}
/**
* @param string $gid
* @return IGroup|null
2013-07-10 02:06:22 +04:00
*/
public function get($gid) {
if (isset($this->cachedGroups[$gid])) {
return $this->cachedGroups[$gid];
}
2014-02-14 17:25:45 +04:00
return $this->getGroupObject($gid);
2013-07-10 02:06:22 +04:00
}
/**
* @param string $gid
2016-11-30 22:21:44 +03:00
* @param string $displayName
* @return \OCP\IGroup|null
*/
2016-11-30 22:21:44 +03:00
protected function getGroupObject($gid, $displayName = null) {
$backends = [];
foreach ($this->backends as $backend) {
if ($backend->implementsActions(Backend::GROUP_DETAILS)) {
2016-11-30 22:21:44 +03:00
$groupData = $backend->getGroupDetails($gid);
if (is_array($groupData) && !empty($groupData)) {
2016-11-30 22:21:44 +03:00
// 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;
}
} elseif ($backend->groupExists($gid)) {
$backends[] = $backend;
}
}
2014-02-14 17:25:45 +04:00
if (count($backends) === 0) {
return null;
}
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName);
2013-07-10 02:06:22 +04:00
return $this->cachedGroups[$gid];
}
/**
* @param string $gid
* @return bool
*/
2013-07-10 04:17:24 +04:00
public function groupExists($gid) {
return $this->get($gid) instanceof IGroup;
2013-07-10 02:06:22 +04:00
}
/**
* @param string $gid
* @return IGroup|null
2013-07-10 02:06:22 +04:00
*/
2013-07-10 04:17:24 +04:00
public function createGroup($gid) {
if ($gid === '' || $gid === null) {
return null;
} elseif ($group = $this->get($gid)) {
2014-02-14 18:07:08 +04:00
return $group;
2013-07-10 02:06:22 +04:00
} else {
$this->emit('\OC\Group', 'preCreate', [$gid]);
2013-07-10 02:06:22 +04:00
foreach ($this->backends as $backend) {
if ($backend->implementsActions(Backend::CREATE_GROUP)) {
if ($backend->createGroup($gid)) {
$group = $this->getGroupObject($gid);
$this->emit('\OC\Group', 'postCreate', [$group]);
return $group;
}
2013-07-10 02:06:22 +04:00
}
}
return null;
}
}
/**
* @param string $search
* @param int $limit
* @param int $offset
* @return \OC\Group\Group[]
*/
2013-07-10 04:17:24 +04:00
public function search($search, $limit = null, $offset = null) {
$groups = [];
2013-07-10 02:06:22 +04:00
foreach ($this->backends as $backend) {
$groupIds = $backend->getGroups($search, $limit, $offset);
foreach ($groupIds as $groupId) {
$aGroup = $this->get($groupId);
if ($aGroup instanceof IGroup) {
$groups[$groupId] = $aGroup;
} else {
$this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']);
}
2013-07-10 02:06:22 +04:00
}
if (!is_null($limit) and $limit <= 0) {
return array_values($groups);
}
}
return array_values($groups);
}
/**
* @param IUser|null $user
2013-07-10 02:06:22 +04:00
* @return \OC\Group\Group[]
*/
public function getUserGroups(IUser $user = null) {
if (!$user instanceof IUser) {
return [];
}
return $this->getUserIdGroups($user->getUID());
}
/**
* @param string $uid the user id
* @return \OC\Group\Group[]
*/
public function getUserIdGroups($uid) {
$groups = [];
foreach ($this->getUserIdGroupIds($uid) as $groupId) {
$aGroup = $this->get($groupId);
if ($aGroup instanceof IGroup) {
$groups[$groupId] = $aGroup;
} else {
$this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
2013-07-10 02:06:22 +04:00
}
}
return $groups;
2013-07-10 02:06:22 +04:00
}
/**
* Checks if a userId is in the admin group
*
* @param string $userId
* @return bool if admin
*/
public function isAdmin($userId) {
foreach ($this->backends as $backend) {
if ($backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
return true;
}
}
return $this->isInGroup($userId, 'admin');
}
/**
* Checks if a userId is in a group
*
* @param string $userId
2015-06-19 22:00:42 +03:00
* @param string $group
* @return bool if in group
*/
public function isInGroup($userId, $group) {
return array_search($group, $this->getUserIdGroupIds($userId)) !== false;
}
/**
* get a list of group ids for a user
*
* @param IUser $user
2014-06-19 21:35:10 +04:00
* @return array with group ids
*/
public function getUserGroupIds(IUser $user) {
return $this->getUserIdGroupIds($user->getUID());
}
/**
* @param string $uid the user id
* @return GroupInterface[]
*/
private function getUserIdGroupIds($uid) {
if (!isset($this->cachedUserGroups[$uid])) {
$groups = [];
foreach ($this->backends as $backend) {
if ($groupIds = $backend->getUserGroups($uid)) {
$groups = array_merge($groups, $groupIds);
}
}
$this->cachedUserGroups[$uid] = $groups;
}
return $this->cachedUserGroups[$uid];
}
/**
* get an array of groupid and displayName for a user
*
* @param IUser $user
* @return array ['displayName' => displayname]
*/
public function getUserGroupNames(IUser $user) {
return array_map(function ($group) {
return ['displayName' => $group->getDisplayName()];
}, $this->getUserGroups($user));
}
/**
* get a list of all display names in a group
*
* @param string $gid
* @param string $search
* @param int $limit
* @param int $offset
2014-05-11 21:13:51 +04:00
* @return array an array of display names (value) and user ids (key)
*/
public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
$group = $this->get($gid);
if (is_null($group)) {
return [];
}
$search = trim($search);
$groupUsers = [];
if (!empty($search)) {
// only user backends have the capability to do a complex search for users
$searchOffset = 0;
$searchLimit = $limit * 100;
if ($limit === -1) {
$searchLimit = 500;
}
do {
$filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
foreach ($filteredUsers as $filteredUser) {
if ($group->inGroup($filteredUser)) {
$groupUsers[] = $filteredUser;
}
}
$searchOffset += $searchLimit;
} while (count($groupUsers) < $searchLimit + $offset && count($filteredUsers) >= $searchLimit);
if ($limit === -1) {
$groupUsers = array_slice($groupUsers, $offset);
} else {
$groupUsers = array_slice($groupUsers, $offset, $limit);
}
} else {
$groupUsers = $group->searchUsers('', $limit, $offset);
}
$matchingUsers = [];
foreach ($groupUsers as $groupUser) {
$matchingUsers[(string) $groupUser->getUID()] = $groupUser->getDisplayName();
}
return $matchingUsers;
}
/**
* @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-10 02:06:22 +04:00
}