2014-04-17 20:25:54 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@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) 2015, 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/>
|
|
|
|
*
|
2014-04-17 20:25:54 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-04-17 20:25:54 +04:00
|
|
|
namespace OC\Group;
|
|
|
|
|
|
|
|
class MetaData {
|
|
|
|
const SORT_NONE = 0;
|
2015-05-18 13:23:12 +03:00
|
|
|
const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
|
|
|
|
const SORT_GROUPNAME = 2;
|
2014-04-17 20:25:54 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $user
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool $isAdmin
|
|
|
|
*/
|
|
|
|
protected $isAdmin;
|
|
|
|
|
|
|
|
/**
|
2014-07-09 14:19:50 +04:00
|
|
|
* @var array $metaData
|
2014-04-17 20:25:54 +04:00
|
|
|
*/
|
2014-07-09 14:19:50 +04:00
|
|
|
protected $metaData = array();
|
2014-04-17 20:25:54 +04:00
|
|
|
|
|
|
|
/**
|
2014-12-04 16:15:55 +03:00
|
|
|
* @var \OCP\IGroupManager $groupManager
|
2014-04-17 20:25:54 +04:00
|
|
|
*/
|
|
|
|
protected $groupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int $sorting
|
|
|
|
*/
|
|
|
|
protected $sorting = false;
|
|
|
|
|
|
|
|
/**
|
2014-07-14 23:19:08 +04:00
|
|
|
* @param string $user the uid of the current user
|
|
|
|
* @param bool $isAdmin whether the current users is an admin
|
2014-12-04 16:15:55 +03:00
|
|
|
* @param \OCP\IGroupManager $groupManager
|
2014-04-17 20:25:54 +04:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$user,
|
|
|
|
$isAdmin,
|
2014-12-04 16:15:55 +03:00
|
|
|
\OCP\IGroupManager $groupManager
|
2014-04-17 20:25:54 +04:00
|
|
|
) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->isAdmin = (bool)$isAdmin;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 14:19:50 +04:00
|
|
|
* @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 20:25:54 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-07-09 14:19:50 +04:00
|
|
|
public function get($groupSearch = '', $userSearch = '') {
|
|
|
|
$key = $groupSearch . '::' . $userSearch;
|
|
|
|
if(isset($this->metaData[$key])) {
|
|
|
|
return $this->metaData[$key];
|
2014-04-17 20:25:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$adminGroups = array();
|
|
|
|
$groups = array();
|
|
|
|
$sortGroupsIndex = 0;
|
|
|
|
$sortGroupsKeys = array();
|
|
|
|
$sortAdminGroupsIndex = 0;
|
|
|
|
$sortAdminGroupsKeys = array();
|
|
|
|
|
2014-07-09 14:19:50 +04:00
|
|
|
foreach($this->getGroups($groupSearch) as $group) {
|
|
|
|
$groupMetaData = $this->generateGroupMetaData($group, $userSearch);
|
2014-04-22 21:41:35 +04:00
|
|
|
if (strtolower($group->getGID()) !== 'admin') {
|
2014-04-17 20: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);
|
|
|
|
|
2014-07-09 14:19:50 +04:00
|
|
|
$this->metaData[$key] = array($adminGroups, $groups);
|
|
|
|
return $this->metaData[$key];
|
2014-04-17 20:25:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-18 13:23:12 +03:00
|
|
|
* sets the sort mode, see SORT_* constants for supported modes
|
|
|
|
*
|
|
|
|
* @param int $sortMode
|
2014-04-17 20:25:54 +04:00
|
|
|
*/
|
|
|
|
public function setSorting($sortMode) {
|
2015-05-18 13:23:12 +03:00
|
|
|
switch ($sortMode) {
|
|
|
|
case self::SORT_USERCOUNT:
|
|
|
|
case self::SORT_GROUPNAME:
|
|
|
|
$this->sorting = $sortMode;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$this->sorting = self::SORT_NONE;
|
2014-04-17 20:25:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-14 23: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 20:25:54 +04:00
|
|
|
*/
|
|
|
|
private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) {
|
|
|
|
$entries[] = $data;
|
2015-05-18 13:23:12 +03:00
|
|
|
if ($this->sorting === self::SORT_USERCOUNT) {
|
2014-04-17 20:25:54 +04:00
|
|
|
$sortKeys[$sortIndex] = $data['usercount'];
|
|
|
|
$sortIndex++;
|
2015-05-18 13:23:12 +03:00
|
|
|
} else if ($this->sorting === self::SORT_GROUPNAME) {
|
|
|
|
$sortKeys[$sortIndex] = $data['name'];
|
|
|
|
$sortIndex++;
|
2014-04-17 20:25:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-14 23:19:08 +04:00
|
|
|
* creates an array containing the group meta data
|
2015-05-18 13:23:12 +03:00
|
|
|
* @param \OCP\IGroup $group
|
2014-07-09 14:19:50 +04:00
|
|
|
* @param string $userSearch
|
2014-04-17 20:25:54 +04:00
|
|
|
* @return array with the keys 'id', 'name' and 'usercount'
|
|
|
|
*/
|
2015-05-18 13:23:12 +03:00
|
|
|
private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) {
|
2014-04-17 20:25:54 +04:00
|
|
|
return array(
|
2014-07-09 14:19:50 +04:00
|
|
|
'id' => $group->getGID(),
|
2014-04-17 20:25:54 +04:00
|
|
|
'name' => $group->getGID(),
|
2015-05-18 13:23:12 +03:00
|
|
|
'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
|
2014-04-17 20:25:54 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-14 23: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 20:25:54 +04:00
|
|
|
* @param return null
|
|
|
|
*/
|
|
|
|
private function sort(&$entries, $sortKeys) {
|
2015-05-18 13:23:12 +03:00
|
|
|
if ($this->sorting === self::SORT_USERCOUNT) {
|
2014-04-17 20:25:54 +04:00
|
|
|
array_multisort($sortKeys, SORT_DESC, $entries);
|
2015-05-18 13:23:12 +03:00
|
|
|
} else if ($this->sorting === self::SORT_GROUPNAME) {
|
|
|
|
array_multisort($sortKeys, SORT_ASC, $entries);
|
2014-04-17 20:25:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-14 23:19:08 +04:00
|
|
|
* returns the available groups
|
|
|
|
* @param string $search a search string
|
2015-05-18 13:23:12 +03:00
|
|
|
* @return \OCP\IGroup[]
|
2014-04-17 20:25:54 +04:00
|
|
|
*/
|
|
|
|
private function getGroups($search = '') {
|
|
|
|
if($this->isAdmin) {
|
2014-07-09 14:19:50 +04:00
|
|
|
return $this->groupManager->search($search);
|
2014-04-17 20:25:54 +04:00
|
|
|
} else {
|
2014-12-04 16:15:55 +03:00
|
|
|
// FIXME: Remove static method call
|
2014-07-11 09:12:04 +04:00
|
|
|
$groupIds = \OC_SubAdmin::getSubAdminsGroups($this->user);
|
|
|
|
|
|
|
|
/* \OC_SubAdmin::getSubAdminsGroups() returns an array of GIDs, but this
|
|
|
|
* method is expected to return an array with the GIDs as keys and group objects as
|
|
|
|
* values, so we need to convert this information.
|
|
|
|
*/
|
|
|
|
$groups = array();
|
|
|
|
foreach($groupIds as $gid) {
|
|
|
|
$group = $this->groupManager->get($gid);
|
|
|
|
if (!is_null($group)) {
|
|
|
|
$groups[$gid] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
2014-04-17 20:25:54 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-09 21:03:05 +04:00
|
|
|
}
|