2014-04-17 12:25:54 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2014-04-17 12:25:54 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-04-17 12:25:54 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Group;
|
|
|
|
|
|
2020-07-08 07:49:11 -04:00
|
|
|
use OC\Group\Manager as GroupManager;
|
2022-03-08 10:44:22 -05:00
|
|
|
use OCP\IGroup;
|
2018-05-16 06:32:56 -04:00
|
|
|
use OCP\IGroupManager;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IUserSession;
|
2015-10-27 09:09:45 -04:00
|
|
|
|
2014-04-17 12:25:54 -04:00
|
|
|
class MetaData {
|
2020-04-10 10:54:27 -04:00
|
|
|
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 12:25:54 -04:00
|
|
|
|
2015-10-27 09:09:45 -04:00
|
|
|
/** @var array */
|
2020-03-26 04:30:18 -04:00
|
|
|
protected $metaData = [];
|
2022-03-08 09:49:29 -05:00
|
|
|
/** @var int */
|
|
|
|
|
protected $sorting = self::SORT_NONE;
|
2014-04-17 12:25:54 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-07-14 15:19:08 -04:00
|
|
|
* @param string $user the uid of the current user
|
|
|
|
|
* @param bool $isAdmin whether the current users is an admin
|
2014-04-17 12:25:54 -04:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2024-07-11 06:09:39 -04:00
|
|
|
private string $user,
|
|
|
|
|
private bool $isAdmin,
|
|
|
|
|
private bool $isDelegatedAdmin,
|
|
|
|
|
private IGroupManager $groupManager,
|
2024-09-19 05:10:31 -04:00
|
|
|
private IUserSession $userSession,
|
2023-11-23 04:22:34 -05:00
|
|
|
) {
|
2014-04-17 12:25:54 -04: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
|
2014-07-09 06:19:50 -04:00
|
|
|
* @param string $groupSearch only effective when instance was created with
|
2024-08-23 09:10:27 -04:00
|
|
|
* isAdmin being true
|
2014-07-09 06:19:50 -04:00
|
|
|
* @param string $userSearch the pattern users are search for
|
2014-04-17 12:25:54 -04:00
|
|
|
*/
|
2022-03-08 10:44:22 -05:00
|
|
|
public function get(string $groupSearch = '', string $userSearch = ''): array {
|
2014-07-09 06:19:50 -04:00
|
|
|
$key = $groupSearch . '::' . $userSearch;
|
2020-04-10 08:19:56 -04:00
|
|
|
if (isset($this->metaData[$key])) {
|
2014-07-09 06:19:50 -04:00
|
|
|
return $this->metaData[$key];
|
2014-04-17 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$adminGroups = [];
|
|
|
|
|
$groups = [];
|
2014-04-17 12:25:54 -04:00
|
|
|
$sortGroupsIndex = 0;
|
2020-03-26 04:30:18 -04:00
|
|
|
$sortGroupsKeys = [];
|
2014-04-17 12:25:54 -04:00
|
|
|
$sortAdminGroupsIndex = 0;
|
2020-03-26 04:30:18 -04:00
|
|
|
$sortAdminGroupsKeys = [];
|
2014-04-17 12:25:54 -04:00
|
|
|
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($this->getGroups($groupSearch) as $group) {
|
2014-07-09 06:19:50 -04:00
|
|
|
$groupMetaData = $this->generateGroupMetaData($group, $userSearch);
|
2014-04-22 13:41:35 -04:00
|
|
|
if (strtolower($group->getGID()) !== 'admin') {
|
2014-04-17 12:25:54 -04: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);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->metaData[$key] = [$adminGroups, $groups];
|
2014-07-09 06:19:50 -04:00
|
|
|
return $this->metaData[$key];
|
2014-04-17 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-18 06:23:12 -04:00
|
|
|
* sets the sort mode, see SORT_* constants for supported modes
|
2014-04-17 12:25:54 -04:00
|
|
|
*/
|
2022-03-08 10:44:22 -05:00
|
|
|
public function setSorting(int $sortMode): void {
|
2015-05-18 06:23:12 -04:00
|
|
|
switch ($sortMode) {
|
|
|
|
|
case self::SORT_USERCOUNT:
|
|
|
|
|
case self::SORT_GROUPNAME:
|
|
|
|
|
$this->sorting = $sortMode;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
$this->sorting = self::SORT_NONE;
|
2014-04-17 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-14 15:19:08 -04: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 12:25:54 -04:00
|
|
|
*/
|
2022-03-08 10:44:22 -05:00
|
|
|
private function addEntry(array &$entries, array &$sortKeys, int &$sortIndex, array $data): void {
|
2014-04-17 12:25:54 -04:00
|
|
|
$entries[] = $data;
|
2015-05-18 06:23:12 -04:00
|
|
|
if ($this->sorting === self::SORT_USERCOUNT) {
|
2014-04-17 12:25:54 -04:00
|
|
|
$sortKeys[$sortIndex] = $data['usercount'];
|
|
|
|
|
$sortIndex++;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($this->sorting === self::SORT_GROUPNAME) {
|
2015-05-18 06:23:12 -04:00
|
|
|
$sortKeys[$sortIndex] = $data['name'];
|
|
|
|
|
$sortIndex++;
|
2014-04-17 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-14 15:19:08 -04:00
|
|
|
* creates an array containing the group meta data
|
2018-05-16 06:32:56 -04:00
|
|
|
* @return array with the keys 'id', 'name', 'usercount' and 'disabled'
|
2014-04-17 12:25:54 -04:00
|
|
|
*/
|
2022-03-08 10:44:22 -05:00
|
|
|
private function generateGroupMetaData(IGroup $group, string $userSearch): array {
|
2020-03-26 04:30:18 -04:00
|
|
|
return [
|
2020-04-09 03:22:29 -04:00
|
|
|
'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 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-14 15:19:08 -04: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 12:25:54 -04:00
|
|
|
*/
|
2022-03-08 10:44:22 -05:00
|
|
|
private function sort(array &$entries, array $sortKeys): void {
|
2015-05-18 06:23:12 -04:00
|
|
|
if ($this->sorting === self::SORT_USERCOUNT) {
|
2014-04-17 12:25:54 -04:00
|
|
|
array_multisort($sortKeys, SORT_DESC, $entries);
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($this->sorting === self::SORT_GROUPNAME) {
|
2015-05-18 06:23:12 -04:00
|
|
|
array_multisort($sortKeys, SORT_ASC, $entries);
|
2014-04-17 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-14 15:19:08 -04:00
|
|
|
* returns the available groups
|
2022-03-08 10:44:22 -05:00
|
|
|
* @return IGroup[]
|
2014-04-17 12:25:54 -04:00
|
|
|
*/
|
2022-03-08 10:44:22 -05:00
|
|
|
public function getGroups(string $search = ''): array {
|
2024-07-11 06:09:39 -04:00
|
|
|
if ($this->isAdmin || $this->isDelegatedAdmin) {
|
2014-07-09 06:19:50 -04:00
|
|
|
return $this->groupManager->search($search);
|
2014-04-17 12:25:54 -04:00
|
|
|
} else {
|
2015-10-27 09:09:45 -04:00
|
|
|
$userObject = $this->userSession->getUser();
|
2024-07-11 06:09:39 -04:00
|
|
|
if ($userObject !== null && $this->groupManager instanceof GroupManager) {
|
2015-10-27 09:09:45 -04:00
|
|
|
$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
|
|
|
|
|
} else {
|
|
|
|
|
$groups = [];
|
2014-07-11 01:12:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $groups;
|
2014-04-17 12:25:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 13:03:05 -04:00
|
|
|
}
|