diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2016-05-01 13:10:07 +0200 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-05-01 13:10:07 +0200 |
commit | 0caa09e8452cebd55fc1ce5ffeb588d499c8e094 (patch) | |
tree | 76faffcf66f1e60e12f7ba5cc34764d3dd8b77c2 /lib/private/Group | |
parent | d89b533fa56cd3f1f4add2a60894c98c78650a09 (diff) | |
download | nextcloud-server-0caa09e8452cebd55fc1ce5ffeb588d499c8e094.tar.gz nextcloud-server-0caa09e8452cebd55fc1ce5ffeb588d499c8e094.zip |
Move \OC\Group to PSR-4
Diffstat (limited to 'lib/private/Group')
-rw-r--r-- | lib/private/Group/Group.php | 289 | ||||
-rw-r--r-- | lib/private/Group/Manager.php | 351 | ||||
-rw-r--r-- | lib/private/Group/MetaData.php | 200 |
3 files changed, 840 insertions, 0 deletions
diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php new file mode 100644 index 00000000000..064b9f899e6 --- /dev/null +++ b/lib/private/Group/Group.php @@ -0,0 +1,289 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Bart Visscher <bartv@thisnet.nl> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Lukas Reschke <lukas@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin Appelman <icewind@owncloud.com> + * @author Robin McCorkell <robin@mccorkell.me.uk> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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/> + * + */ + +namespace OC\Group; + +use OCP\IGroup; + +class Group implements IGroup { + /** + * @var string $id + */ + private $gid; + + /** + * @var \OC\User\User[] $users + */ + private $users = array(); + + /** + * @var bool $usersLoaded + */ + private $usersLoaded; + + /** + * @var \OC_Group_Backend[]|\OC_Group_Database[] $backend + */ + private $backends; + + /** + * @var \OC\Hooks\PublicEmitter $emitter + */ + private $emitter; + + /** + * @var \OC\User\Manager $userManager + */ + private $userManager; + + /** + * @param string $gid + * @param \OC_Group_Backend[] $backends + * @param \OC\User\Manager $userManager + * @param \OC\Hooks\PublicEmitter $emitter + */ + public function __construct($gid, $backends, $userManager, $emitter = null) { + $this->gid = $gid; + $this->backends = $backends; + $this->userManager = $userManager; + $this->emitter = $emitter; + } + + public function getGID() { + return $this->gid; + } + + /** + * get all users in the group + * + * @return \OC\User\User[] + */ + public function getUsers() { + if ($this->usersLoaded) { + return $this->users; + } + + $userIds = array(); + foreach ($this->backends as $backend) { + $diff = array_diff( + $backend->usersInGroup($this->gid), + $userIds + ); + if ($diff) { + $userIds = array_merge($userIds, $diff); + } + } + + $this->users = $this->getVerifiedUsers($userIds); + $this->usersLoaded = true; + return $this->users; + } + + /** + * check if a user is in the group + * + * @param \OC\User\User $user + * @return bool + */ + public function inGroup($user) { + if (isset($this->users[$user->getUID()])) { + return true; + } + foreach ($this->backends as $backend) { + if ($backend->inGroup($user->getUID(), $this->gid)) { + $this->users[$user->getUID()] = $user; + return true; + } + } + return false; + } + + /** + * add a user to the group + * + * @param \OC\User\User $user + */ + public function addUser($user) { + if ($this->inGroup($user)) { + return; + } + + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(\OC_Group_Backend::ADD_TO_GROUP)) { + $backend->addToGroup($user->getUID(), $this->gid); + if ($this->users) { + $this->users[$user->getUID()] = $user; + } + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); + } + return; + } + } + } + + /** + * remove a user from the group + * + * @param \OC\User\User $user + */ + public function removeUser($user) { + $result = false; + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(\OC_Group_Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { + $backend->removeFromGroup($user->getUID(), $this->gid); + $result = true; + } + } + if ($result) { + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); + } + if ($this->users) { + foreach ($this->users as $index => $groupUser) { + if ($groupUser->getUID() === $user->getUID()) { + unset($this->users[$index]); + return; + } + } + } + } + } + + /** + * search for users in the group by userid + * + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\User\User[] + */ + public function searchUsers($search, $limit = null, $offset = null) { + $users = array(); + foreach ($this->backends as $backend) { + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); + $users += $this->getVerifiedUsers($userIds); + if (!is_null($limit) and $limit <= 0) { + return array_values($users); + } + } + return array_values($users); + } + + /** + * returns the number of users matching the search string + * + * @param string $search + * @return int|bool + */ + public function count($search = '') { + $users = false; + foreach ($this->backends as $backend) { + if($backend->implementsActions(\OC_Group_Backend::COUNT_USERS)) { + if($users === false) { + //we could directly add to a bool variable, but this would + //be ugly + $users = 0; + } + $users += $backend->countUsersInGroup($this->gid, $search); + } + } + return $users; + } + + /** + * search for users in the group by displayname + * + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\User\User[] + */ + public function searchDisplayName($search, $limit = null, $offset = null) { + $users = array(); + foreach ($this->backends as $backend) { + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); + $users = $this->getVerifiedUsers($userIds); + if (!is_null($limit) and $limit <= 0) { + return array_values($users); + } + } + return array_values($users); + } + + /** + * delete the group + * + * @return bool + */ + public function delete() { + // Prevent users from deleting group admin + if ($this->getGID() === 'admin') { + return false; + } + + $result = false; + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(\OC_Group_Backend::DELETE_GROUP)) { + $result = true; + $backend->deleteGroup($this->gid); + } + } + if ($result and $this->emitter) { + $this->emitter->emit('\OC\Group', 'postDelete', array($this)); + } + return $result; + } + + /** + * returns all the Users from an array that really exists + * @param string[] $userIds an array containing user IDs + * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value + */ + private function getVerifiedUsers($userIds) { + if (!is_array($userIds)) { + return array(); + } + $users = array(); + foreach ($userIds as $userId) { + $user = $this->userManager->get($userId); + if (!is_null($user)) { + $users[$userId] = $user; + } + } + return $users; + } +} diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php new file mode 100644 index 00000000000..e82a1d4f2e6 --- /dev/null +++ b/lib/private/Group/Manager.php @@ -0,0 +1,351 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Bart Visscher <bartv@thisnet.nl> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Jörn Friedrich Dreyer <jfd@butonic.de> + * @author Lukas Reschke <lukas@owncloud.com> + * @author macjohnny <estebanmarin@gmx.ch> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin Appelman <icewind@owncloud.com> + * @author Robin McCorkell <robin@mccorkell.me.uk> + * @author Roeland Jago Douma <rullzer@owncloud.com> + * @author Roman Kreisel <mail@romankreisel.de> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * @author voxsim <Simon Vocella> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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/> + * + */ + +namespace OC\Group; + +use OC\Hooks\PublicEmitter; +use OCP\GroupInterface; +use OCP\IGroupManager; + +/** + * Class Manager + * + * Hooks available in scope \OC\Group: + * - preAddUser(\OC\Group\Group $group, \OC\User\User $user) + * - postAddUser(\OC\Group\Group $group, \OC\User\User $user) + * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user) + * - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user) + * - preDelete(\OC\Group\Group $group) + * - postDelete(\OC\Group\Group $group) + * - preCreate(string $groupId) + * - postCreate(\OC\Group\Group $group) + * + * @package OC\Group + */ +class Manager extends PublicEmitter implements IGroupManager { + /** + * @var GroupInterface[] $backends + */ + private $backends = array(); + + /** + * @var \OC\User\Manager $userManager + */ + private $userManager; + + /** + * @var \OC\Group\Group[] + */ + private $cachedGroups = array(); + + /** + * @var \OC\Group\Group[] + */ + private $cachedUserGroups = array(); + + /** @var \OC\SubAdmin */ + private $subAdmin = null; + + /** + * @param \OC\User\Manager $userManager + */ + public function __construct(\OC\User\Manager $userManager) { + $this->userManager = $userManager; + $cachedGroups = & $this->cachedGroups; + $cachedUserGroups = & $this->cachedUserGroups; + $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { + /** + * @var \OC\Group\Group $group + */ + unset($cachedGroups[$group->getGID()]); + $cachedUserGroups = array(); + }); + $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { + /** + * @var \OC\Group\Group $group + */ + $cachedUserGroups = array(); + }); + $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { + /** + * @var \OC\Group\Group $group + */ + $cachedUserGroups = array(); + }); + } + + /** + * 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; + } + + /** + * @param \OCP\GroupInterface $backend + */ + public function addBackend($backend) { + $this->backends[] = $backend; + $this->clearCaches(); + } + + public function clearBackends() { + $this->backends = array(); + $this->clearCaches(); + } + + protected function clearCaches() { + $this->cachedGroups = array(); + $this->cachedUserGroups = array(); + } + + /** + * @param string $gid + * @return \OC\Group\Group + */ + public function get($gid) { + if (isset($this->cachedGroups[$gid])) { + return $this->cachedGroups[$gid]; + } + return $this->getGroupObject($gid); + } + + /** + * @param string $gid + * @return \OCP\IGroup + */ + protected function getGroupObject($gid) { + $backends = array(); + foreach ($this->backends as $backend) { + if ($backend->groupExists($gid)) { + $backends[] = $backend; + } + } + if (count($backends) === 0) { + return null; + } + $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this); + return $this->cachedGroups[$gid]; + } + + /** + * @param string $gid + * @return bool + */ + public function groupExists($gid) { + return !is_null($this->get($gid)); + } + + /** + * @param string $gid + * @return \OC\Group\Group + */ + public function createGroup($gid) { + if ($gid === '' || is_null($gid)) { + return false; + } else if ($group = $this->get($gid)) { + return $group; + } else { + $this->emit('\OC\Group', 'preCreate', array($gid)); + foreach ($this->backends as $backend) { + if ($backend->implementsActions(\OC_Group_Backend::CREATE_GROUP)) { + $backend->createGroup($gid); + $group = $this->getGroupObject($gid); + $this->emit('\OC\Group', 'postCreate', array($group)); + return $group; + } + } + return null; + } + } + + /** + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\Group\Group[] + */ + public function search($search, $limit = null, $offset = null) { + $groups = array(); + foreach ($this->backends as $backend) { + $groupIds = $backend->getGroups($search, $limit, $offset); + foreach ($groupIds as $groupId) { + $groups[$groupId] = $this->get($groupId); + } + if (!is_null($limit) and $limit <= 0) { + return array_values($groups); + } + } + return array_values($groups); + } + + /** + * @param \OC\User\User|null $user + * @return \OC\Group\Group[] + */ + public function getUserGroups($user) { + if (is_null($user)) { + return []; + } + return $this->getUserIdGroups($user->getUID()); + } + + /** + * @param string $uid the user id + * @return \OC\Group\Group[] + */ + public function getUserIdGroups($uid) { + if (isset($this->cachedUserGroups[$uid])) { + return $this->cachedUserGroups[$uid]; + } + $groups = array(); + foreach ($this->backends as $backend) { + $groupIds = $backend->getUserGroups($uid); + if (is_array($groupIds)) { + foreach ($groupIds as $groupId) { + $groups[$groupId] = $this->get($groupId); + } + } + } + $this->cachedUserGroups[$uid] = $groups; + return $this->cachedUserGroups[$uid]; + } + + /** + * 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 + * @param string $group + * @return bool if in group + */ + public function isInGroup($userId, $group) { + return array_key_exists($group, $this->getUserIdGroups($userId)); + } + + /** + * get a list of group ids for a user + * @param \OC\User\User $user + * @return array with group ids + */ + public function getUserGroupIds($user) { + return array_map(function($value) { + return (string) $value; + }, array_keys($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 + * @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 array(); + } + + $search = trim($search); + $groupUsers = array(); + + 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 = array(); + foreach($groupUsers as $groupUser) { + $matchingUsers[$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; + } +} diff --git a/lib/private/Group/MetaData.php b/lib/private/Group/MetaData.php new file mode 100644 index 00000000000..8e0866479c1 --- /dev/null +++ b/lib/private/Group/MetaData.php @@ -0,0 +1,200 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Lukas Reschke <lukas@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Stephan Peijnik <speijnik@anexia-it.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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/> + * + */ + +namespace OC\Group; + +use OCP\IUserSession; + +class MetaData { + const SORT_NONE = 0; + const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends + const SORT_GROUPNAME = 2; + + /** @var string */ + protected $user; + /** @var bool */ + protected $isAdmin; + /** @var array */ + protected $metaData = array(); + /** @var \OCP\IGroupManager */ + protected $groupManager; + /** @var bool */ + protected $sorting = false; + /** @var IUserSession */ + protected $userSession; + + /** + * @param string $user the uid of the current user + * @param bool $isAdmin whether the current users is an admin + * @param \OCP\IGroupManager $groupManager + * @param IUserSession $userSession + */ + public function __construct( + $user, + $isAdmin, + \OCP\IGroupManager $groupManager, + IUserSession $userSession + ) { + $this->user = $user; + $this->isAdmin = (bool)$isAdmin; + $this->groupManager = $groupManager; + $this->userSession = $userSession; + } + + /** + * returns an array with meta data about all available groups + * the array is structured as follows: + * [0] array containing meta data about admin groups + * [1] array containing meta data about unprivileged groups + * @param string $groupSearch only effective when instance was created with + * isAdmin being true + * @param string $userSearch the pattern users are search for + * @return array + */ + public function get($groupSearch = '', $userSearch = '') { + $key = $groupSearch . '::' . $userSearch; + if(isset($this->metaData[$key])) { + return $this->metaData[$key]; + } + + $adminGroups = array(); + $groups = array(); + $sortGroupsIndex = 0; + $sortGroupsKeys = array(); + $sortAdminGroupsIndex = 0; + $sortAdminGroupsKeys = array(); + + foreach($this->getGroups($groupSearch) as $group) { + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); + if (strtolower($group->getGID()) !== 'admin') { + $this->addEntry( + $groups, + $sortGroupsKeys, + $sortGroupsIndex, + $groupMetaData); + } else { + //admin group is hard coded to 'admin' for now. In future, + //backends may define admin groups too. Then the if statement + //has to be adjusted accordingly. + $this->addEntry( + $adminGroups, + $sortAdminGroupsKeys, + $sortAdminGroupsIndex, + $groupMetaData); + } + } + + //whether sorting is necessary is will be checked in sort() + $this->sort($groups, $sortGroupsKeys); + $this->sort($adminGroups, $sortAdminGroupsKeys); + + $this->metaData[$key] = array($adminGroups, $groups); + return $this->metaData[$key]; + } + + /** + * sets the sort mode, see SORT_* constants for supported modes + * + * @param int $sortMode + */ + public function setSorting($sortMode) { + switch ($sortMode) { + case self::SORT_USERCOUNT: + case self::SORT_GROUPNAME: + $this->sorting = $sortMode; + break; + + default: + $this->sorting = self::SORT_NONE; + } + } + + /** + * adds an group entry to the resulting array + * @param array $entries the resulting array, by reference + * @param array $sortKeys the sort key array, by reference + * @param int $sortIndex the sort key index, by reference + * @param array $data the group's meta data as returned by generateGroupMetaData() + */ + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { + $entries[] = $data; + if ($this->sorting === self::SORT_USERCOUNT) { + $sortKeys[$sortIndex] = $data['usercount']; + $sortIndex++; + } else if ($this->sorting === self::SORT_GROUPNAME) { + $sortKeys[$sortIndex] = $data['name']; + $sortIndex++; + } + } + + /** + * creates an array containing the group meta data + * @param \OCP\IGroup $group + * @param string $userSearch + * @return array with the keys 'id', 'name' and 'usercount' + */ + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { + return array( + 'id' => $group->getGID(), + 'name' => $group->getGID(), + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, + ); + } + + /** + * sorts the result array, if applicable + * @param array $entries the result array, by reference + * @param array $sortKeys the array containing the sort keys + * @param return null + */ + private function sort(&$entries, $sortKeys) { + if ($this->sorting === self::SORT_USERCOUNT) { + array_multisort($sortKeys, SORT_DESC, $entries); + } else if ($this->sorting === self::SORT_GROUPNAME) { + array_multisort($sortKeys, SORT_ASC, $entries); + } + } + + /** + * returns the available groups + * @param string $search a search string + * @return \OCP\IGroup[] + */ + protected function getGroups($search = '') { + if($this->isAdmin) { + return $this->groupManager->search($search); + } else { + $userObject = $this->userSession->getUser(); + if($userObject !== null) { + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); + } else { + $groups = []; + } + + return $groups; + } + } +} |