Skip to content

Commit

Permalink
Fix the group selector drop down
Browse files Browse the repository at this point in the history
This change gets the group selector drop
down

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Nov 6, 2018
1 parent 5ce2e87 commit f3d289f
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 0 deletions.
200 changes: 200 additions & 0 deletions lib/private/MetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php
/**
* @author Arthur Schiwon <[email protected]>
* @author Joas Schilling <[email protected]>
* @author Lukas Reschke <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Stephan Peijnik <[email protected]>
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @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;

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 = [];
/** @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 = [];
$groups = [];
$sortGroupsIndex = 0;
$sortGroupsKeys = [];
$sortAdminGroupsIndex = 0;
$sortAdminGroupsKeys = [];

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] = [$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++;
} elseif ($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 [
'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);
} elseif ($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, null, null, 'management');
} else {
$userObject = $this->userSession->getUser();
if ($userObject !== null) {
$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
} else {
$groups = [];
}

return $groups;
}
}
}
10 changes: 10 additions & 0 deletions settings/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OC\Server;
use OC\AppFramework\Utility\TimeFactory;
use OC\Settings\Controller\CorsController;
use OC\Settings\Controller\GroupsSelectController;
use OC\Settings\Controller\SettingsPageController;
use OC\Settings\Controller\AppSettingsController;
use OC\Settings\Controller\AuthSettingsController;
Expand Down Expand Up @@ -152,6 +153,15 @@ public function __construct(array $urlParams=[]) {
$c->query('Config')
);
});
$container->registerService('GroupsSelectController', function (IContainer $c) {
return new GroupsSelectController(
$c->query('AppName'),
$c->query('Request'),
$c->query('GroupManager'),
$c->query('UserSession'),
$c->query('L10N')
);
});

/**
* Core class wrappers
Expand Down
98 changes: 98 additions & 0 deletions settings/Controller/GroupsSelectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* @author Sujith Haridasan "[email protected]"
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @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\Settings\Controller;

use OC\MetaData;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;

class GroupsSelectController extends Controller {
/** @var IGroupManager */
private $groupManager;
/** @var IL10N */
private $l10n;
/** @var IUserSession */
private $userSession;

/**
* @param string $appName
* @param IRequest $request
* @param IGroupManager $groupManager
* @param IUserSession $userSession
* @param IL10N $l10n
*/
public function __construct($appName,
IRequest $request,
IGroupManager $groupManager,
IUserSession $userSession,
IL10N $l10n) {
parent::__construct($appName, $request);
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->l10n = $l10n;
}

/**
* @NoAdminRequired
*
* @param string $pattern
* @param bool $filterGroups
* @param int $sortGroups
* @return DataResponse
*/
public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
$groupPattern = $filterGroups ? $pattern : '';

$groupsInfo = new MetaData(
$this->userSession->getUser()->getUID(),
$this->isAdmin(),
$this->groupManager,
$this->userSession
);
$groupsInfo->setSorting($sortGroups);
list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);

return new DataResponse(
[
'data' => ['adminGroups' => $adminGroups, 'groups' => $groups]
]
);
}

/**
* Check if current user (active and not in incognito mode)
* is an admin
*
* @return bool
*/
private function isAdmin() {
// Get current user (active and not in incognito mode)
$user = $this->userSession->getUser();
if ($user !== null) {
return $this->groupManager->isAdmin($user->getUID());
}
return false;
}
}
1 change: 1 addition & 0 deletions settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
$application->registerRoutes($this, [
'resources' => [
'auth_settings' => ['url' => '/settings/personal/authtokens'],
'groups_select' => ['url' => '/settings/users/groups'],
],
'routes' => [
['name' => 'MailSettings#setMailSettings', 'url' => '/settings/admin/mailsettings', 'verb' => 'POST'],
Expand Down

0 comments on commit f3d289f

Please sign in to comment.