Skip to content

Commit

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

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Nov 19, 2018
1 parent e607745 commit 42379f9
Show file tree
Hide file tree
Showing 4 changed files with 376 additions and 0 deletions.
9 changes: 9 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\GroupsController;
use OC\Settings\Controller\SettingsPageController;
use OC\Settings\Controller\AppSettingsController;
use OC\Settings\Controller\AuthSettingsController;
Expand Down Expand Up @@ -152,6 +153,14 @@ public function __construct(array $urlParams=[]) {
$c->query('Config')
);
});
$container->registerService('GroupsController', function (IContainer $c) {
return new GroupsController(
$c->query('AppName'),
$c->query('Request'),
$c->query('GroupManager'),
$c->query('UserSession')
);
});

/**
* Core class wrappers
Expand Down
124 changes: 124 additions & 0 deletions settings/Controller/GroupsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* @author Sujith Haridasan <sharidasan@owncloud.com>
*
* @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 OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;

/**
* Class GroupsController
*
* @package OC\Settings\Controller
*/
class GroupsController extends Controller {
/** @var IGroupManager */
private $groupManager;

/** @var IUserSession */
private $userSession;

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

/**
* Get the groups for the user
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return DataResponse
*/
public function getGroupsForUser() {
$user = $this->userSession->getUser();

if ($user === null) {
return new DataResponse(
[
'status' => 'error',
'data' => [
'message' => 'User not logged in'
]
],
Http::STATUS_NOT_FOUND
);
}

$adminGroups = [];
$userGroups = [];
$isAdmin = $this->groupManager->isAdmin($user->getUID());
$groups = $this->getGroups($isAdmin, $user, '');
foreach ($groups as $group) {
if ($group->getGID() === 'admin') {
$adminGroup['id'] = $group->getGID();
$adminGroup['name'] = $group->getDisplayName();
$adminGroup['userCount'] = $group->count('');
$adminGroups[] = $adminGroup;
} else {
$userGroup['id'] = $group->getGID();
$userGroup['name'] = $group->getDisplayName();
$userGroup['userCount'] = $group->count('');
$userGroups[] = $userGroup;
}
}
return new DataResponse(
[
'data' => ['adminGroups' => $adminGroups, 'groups' => $userGroups]
], Http::STATUS_OK);
}

/**
* @param $isAdmin
* @param IUser $user
* @param string $search
* @return IGroup[]
*/
private function getGroups($isAdmin, IUser $user, $search = '') {
if ($isAdmin === true) {
return $this->groupManager->search($search, null, null, 'management');
}

if ($user !== null) {
return $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
}
return [];
}
}
1 change: 1 addition & 0 deletions settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
['name' => 'Cors#removeDomain', 'url' => '/settings/domains/{id}', 'verb' => 'DELETE'],
['name' => 'LegalSettings#setImprintUrl', 'url' => '/settings/admin/legal/imprint', 'verb' => 'POST'],
['name' => 'LegalSettings#setPrivacyPolicyUrl', 'url' => '/settings/admin/legal/privacypolicy', 'verb' => 'POST'],
['name' => 'Groups#getGroupsForUser', 'url' => '/settings/users/groups', 'verb' => 'GET'],
]
]);

Expand Down
242 changes: 242 additions & 0 deletions tests/Settings/Controller/GroupsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?php
/**
* @author Sujith Haridasan <sharidasan@owncloud.com>
*
* @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 Test;

use OC\AppFramework\Http;
use OC\Settings\Controller\GroupsController;
use OCP\AppFramework\Http\DataResponse;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\ISubAdminManager;
use OCP\IUser;
use OCP\IUserSession;
use Test\Traits\UserTrait;

/**
* Class GroupsControllerTest
*
* @group DB
* @package Test
*/
class GroupsControllerTest extends TestCase {
use UserTrait;
/** @var string */
private $appName;

/** @var IRequest | \PHPUnit_Framework_MockObject_MockObject */
private $request;

/** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
private $groupManager;

/** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
private $userSession;

/** @var GroupsController */
private $groupsController;

public function setUp() {
parent::setUp();
$this->appName = 'settings';
$this->request = $this->createMock(IRequest::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->groupsController = new GroupsController($this->appName, $this->request, $this->groupManager, $this->userSession);
}

public function provideAdminGroups() {
return [
[['admin', 'group1', 'group2']]
];
}

/**
* @dataProvider provideAdminGroups
* @param $groups
*/
public function testGetGroupsForAdmin($groups) {
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('admin');

$this->userSession->method('getUser')
->willReturn($user);
$this->groupManager->method('isAdmin')
->willReturn(true);

$groupsForAdmin = [];
foreach ($groups as $group) {
$groupObject = $this->createMock(IGroup::class);
$groupObject->method('getGID')
->willReturn($group);
$groupObject->method('getDisplayName')
->willReturn($group);
$groupObject->method('count')
->willReturn(1);
$groupsForAdmin[] = $groupObject;
}

$this->groupManager->method('search')
->willReturn($groupsForAdmin);

$expectedResult = new DataResponse(
[
'data' => [
'adminGroups' => [['id' => 'admin', 'name' => 'admin', 'userCount' => 1]],
'groups' => [
['id' => 'group1', 'name' => 'group1', 'userCount' => 1],
['id' => 'group2', 'name' => 'group2', 'userCount' => 1]
]
]

]
);
$result = $this->groupsController->getGroupsForUser();
$this->assertEquals($expectedResult, $result);
}

public function testGetGroupsForSubadmin() {
$groups = [
['groupName' => 'group1','userCount' => 3],
['groupName' => 'group2', 'userCount' => 2],
['groupName' => 'group3', 'userCount' => 1]
];
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('admin');

$this->userSession->method('getUser')
->willReturn($user);
$this->groupManager->method('isAdmin')
->willReturn(false);

$groupsForSubAdmin = [];
foreach ($groups as $group) {
$groupObject = $this->createMock(IGroup::class);
$groupObject->method('getGID')
->willReturn($group['groupName']);
$groupObject->method('getDisplayName')
->willReturn($group['groupName']);
$groupObject->method('count')
->willReturn($group['userCount']);
$groupsForSubAdmin[] = $groupObject;
}

$subAdminManager = $this->createMock(ISubAdminManager::class);
$subAdminManager->method('getSubAdminsGroups')
->with($user)
->willReturn($groupsForSubAdmin);
$this->groupManager->method('getSubAdmin')
->willReturn($subAdminManager);

$expectedResult = new DataResponse(
[
'data' => [
'adminGroups' => [],
'groups' => [
['id' => 'group1', 'name' => 'group1', 'userCount' => 3],
['id' => 'group2', 'name' => 'group2', 'userCount' => 2],
['id' => 'group3', 'name' => 'group3', 'userCount' => 1]
]
]

]
);
$result = $this->groupsController->getGroupsForUser();
$this->assertEquals($expectedResult, $result);
}

/**
* A test which adds users to regular groups and admin group
*/
public function testMultipleUsersInGroup() {
$user1 = $this->createUser('user1');
$user2 = $this->createUser('user2');
$user3 = $this->createUser('user3');
$user4 = $this->createUser('user4');
$user5 = $this->createUser('user5');
$user6 = $this->createUser('user6');
$user7 = $this->createUser('user7');
$this->loginAsUser('user6');

$groupManager = \OC::$server->getGroupManager();
$group1 = $groupManager->createGroup('testGroup1');
$group2 = $groupManager->createGroup('testGroup2');
$adminGroup = $groupManager->get('admin');

$group1->addUser($user1);
$group1->addUser($user2);
$group1->addUser($user3);
$group2->addUser($user4);
$group2->addUser($user5);

if ($adminGroup !== null) {
$adminGroup->addUser($user6);
$adminGroup->addUser($user7);
$adminUserCount = $adminGroup->count('');
}

$group1Count = $group1->count('');
$group2Count = $group2->count('');

$groupController = new GroupsController('settings', \OC::$server->getRequest(), $groupManager, \OC::$server->getUserSession());

if ($adminGroup !== null) {
$adminGroupsExpect = ['id' => 'admin', 'name' => 'admin', 'userCount' => $adminUserCount];
} else {
$adminGroupsExpect = [];
}
$expectedResult = new DataResponse(
[
'data' => [
'adminGroups' => [
$adminGroupsExpect
],
'groups' => [
['id' => 'testGroup1', 'name' => 'testGroup1', 'userCount' => $group1Count],
['id' => 'testGroup2', 'name' => 'testGroup2', 'userCount' => $group2Count]
]
]
]
);

$result = $groupController->getGroupsForUser();
$this->assertEquals($expectedResult, $result);
}

public function testGetGroupForNullUser() {
$this->userSession->method('getUser')
->willReturn(null);

$expectedResult = new DataResponse(
[
'status' => 'error',
'data' => [
'message' => 'User not logged in'
]
], Http::STATUS_NOT_FOUND
);
$result = $this->groupsController->getGroupsForUser();
$this->assertEquals($expectedResult, $result);
}
}

0 comments on commit 42379f9

Please sign in to comment.