0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-03-14 16:33:21 +00:00
nextcloud_server/lib/private/Group/MetaData.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

201 lines
5.9 KiB
PHP
Raw Normal View History

2014-04-17 16:25:54 +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>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Daniel Kesselberg <mail@danielkesselberg.de>
2016-07-21 15:07:57 +00:00
* @author Joas Schilling <coding@schilljs.com>
* @author John Molakvoæ <skjnldsv@protonmail.com>
2016-05-26 17:56:05 +00:00
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Roeland Jago Douma <roeland@famdouma.nl>
2015-03-26 10:44:34 +00:00
* @author Stephan Peijnik <speijnik@anexia-it.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @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 10:44:34 +00:00
*
2014-04-17 16:25:54 +00:00
*/
namespace OC\Group;
use OC\Group\Manager as GroupManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserSession;
2015-10-27 13:09:45 +00:00
2014-04-17 16:25:54 +00:00
class MetaData {
public const SORT_NONE = 0;
public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
public const SORT_GROUPNAME = 2;
2014-04-17 16:25:54 +00:00
2015-10-27 13:09:45 +00:00
/** @var string */
2014-04-17 16:25:54 +00:00
protected $user;
2015-10-27 13:09:45 +00:00
/** @var bool */
2014-04-17 16:25:54 +00:00
protected $isAdmin;
2015-10-27 13:09:45 +00:00
/** @var array */
protected $metaData = [];
/** @var GroupManager */
2014-04-17 16:25:54 +00:00
protected $groupManager;
/** @var int */
protected $sorting = self::SORT_NONE;
2015-10-27 13:09:45 +00:00
/** @var IUserSession */
protected $userSession;
2014-04-17 16:25:54 +00:00
/**
2014-07-14 19:19:08 +00:00
* @param string $user the uid of the current user
* @param bool $isAdmin whether the current users is an admin
2014-04-17 16:25:54 +00:00
*/
public function __construct(
string $user,
bool $isAdmin,
IGroupManager $groupManager,
2015-10-27 13:09:45 +00:00
IUserSession $userSession
2014-04-17 16:25:54 +00:00
) {
$this->user = $user;
$this->isAdmin = $isAdmin;
2014-04-17 16:25:54 +00:00
$this->groupManager = $groupManager;
2015-10-27 13:09:45 +00:00
$this->userSession = $userSession;
2014-04-17 16:25:54 +00:00
}
/**
* 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
2014-04-17 16:25:54 +00:00
*/
public function get(string $groupSearch = '', string $userSearch = ''): array {
$key = $groupSearch . '::' . $userSearch;
if (isset($this->metaData[$key])) {
return $this->metaData[$key];
2014-04-17 16:25:54 +00:00
}
$adminGroups = [];
$groups = [];
2014-04-17 16:25:54 +00:00
$sortGroupsIndex = 0;
$sortGroupsKeys = [];
2014-04-17 16:25:54 +00:00
$sortAdminGroupsIndex = 0;
$sortAdminGroupsKeys = [];
2014-04-17 16:25:54 +00:00
foreach ($this->getGroups($groupSearch) as $group) {
$groupMetaData = $this->generateGroupMetaData($group, $userSearch);
2014-04-22 17:41:35 +00:00
if (strtolower($group->getGID()) !== 'admin') {
2014-04-17 16:25:54 +00:00
$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] = [$adminGroups, $groups];
return $this->metaData[$key];
2014-04-17 16:25:54 +00:00
}
/**
* sets the sort mode, see SORT_* constants for supported modes
2014-04-17 16:25:54 +00:00
*/
public function setSorting(int $sortMode): void {
switch ($sortMode) {
case self::SORT_USERCOUNT:
case self::SORT_GROUPNAME:
$this->sorting = $sortMode;
break;
default:
$this->sorting = self::SORT_NONE;
2014-04-17 16:25:54 +00:00
}
}
/**
2014-07-14 19:19:08 +00:00
* 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()
2014-04-17 16:25:54 +00:00
*/
private function addEntry(array &$entries, array &$sortKeys, int &$sortIndex, array $data): void {
2014-04-17 16:25:54 +00:00
$entries[] = $data;
if ($this->sorting === self::SORT_USERCOUNT) {
2014-04-17 16:25:54 +00:00
$sortKeys[$sortIndex] = $data['usercount'];
$sortIndex++;
} elseif ($this->sorting === self::SORT_GROUPNAME) {
$sortKeys[$sortIndex] = $data['name'];
$sortIndex++;
2014-04-17 16:25:54 +00:00
}
}
/**
2014-07-14 19:19:08 +00:00
* creates an array containing the group meta data
* @return array with the keys 'id', 'name', 'usercount' and 'disabled'
2014-04-17 16:25:54 +00:00
*/
private function generateGroupMetaData(IGroup $group, string $userSearch): array {
return [
'id' => $group->getGID(),
'name' => $group->getDisplayName(),
'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
'disabled' => $group->countDisabled(),
'canAdd' => $group->canAddUser(),
'canRemove' => $group->canRemoveUser(),
];
2014-04-17 16:25:54 +00:00
}
/**
2014-07-14 19:19:08 +00:00
* sorts the result array, if applicable
* @param array $entries the result array, by reference
* @param array $sortKeys the array containing the sort keys
2014-04-17 16:25:54 +00:00
*/
private function sort(array &$entries, array $sortKeys): void {
if ($this->sorting === self::SORT_USERCOUNT) {
2014-04-17 16:25:54 +00:00
array_multisort($sortKeys, SORT_DESC, $entries);
} elseif ($this->sorting === self::SORT_GROUPNAME) {
array_multisort($sortKeys, SORT_ASC, $entries);
2014-04-17 16:25:54 +00:00
}
}
/**
2014-07-14 19:19:08 +00:00
* returns the available groups
* @return IGroup[]
2014-04-17 16:25:54 +00:00
*/
public function getGroups(string $search = ''): array {
if ($this->isAdmin) {
return $this->groupManager->search($search);
2014-04-17 16:25:54 +00:00
} else {
2015-10-27 13:09:45 +00:00
$userObject = $this->userSession->getUser();
if ($userObject !== null) {
2015-10-27 13:09:45 +00:00
$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
} else {
$groups = [];
}
return $groups;
2014-04-17 16:25:54 +00:00
}
}
2014-05-09 17:03:05 +00:00
}