Expose subadmin to OCP

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2019-01-29 10:38:05 +01:00
parent 754932c756
commit 15abcff2a1
No known key found for this signature in database
GPG Key ID: F941078878347C0C
10 changed files with 156 additions and 135 deletions

View File

@ -28,6 +28,9 @@ use OCA\DAV\CalDAV\Activity\Filter\Todo;
use OCP\Activity\IFilter;
use Test\TestCase;
/**
* @group DB
*/
class GenericTest extends TestCase {
public function dataFilters() {

View File

@ -809,11 +809,8 @@ class UsersController extends AUserData {
return new DataResponse();
}
// Go
if ($subAdminManager->createSubAdmin($user, $group)) {
return new DataResponse();
} else {
throw new OCSException('Unknown error occurred', 103);
}
$subAdminManager->createSubAdmin($user, $group);
return new DataResponse();
}
/**
@ -845,11 +842,8 @@ class UsersController extends AUserData {
}
// Go
if ($subAdminManager->deleteSubAdmin($user, $group)) {
return new DataResponse();
} else {
throw new OCSException('Unknown error occurred', 103);
}
$subAdminManager->deleteSubAdmin($user, $group);
return new DataResponse();
}
/**

View File

@ -2736,44 +2736,6 @@ class UsersControllerTest extends TestCase {
$this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
}
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 103
* @expectedExceptionMessage Unknown error occurred
*/
public function testAddSubAdminUnsuccessful() {
$targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
$targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
$this->userManager
->expects($this->once())
->method('get')
->with('ExistingUser')
->will($this->returnValue($targetUser));
$this->groupManager
->expects($this->once())
->method('get')
->with('TargetGroup')
->will($this->returnValue($targetGroup));
$subAdminManager = $this->getMockBuilder('OC\SubAdmin')
->disableOriginalConstructor()->getMock();
$subAdminManager
->expects($this->once())
->method('isSubAdminOfGroup')
->with($targetUser, $targetGroup)
->will($this->returnValue(false));
$subAdminManager
->expects($this->once())
->method('createSubAdmin')
->with($targetUser, $targetGroup)
->will($this->returnValue(false));
$this->groupManager
->expects($this->once())
->method('getSubAdmin')
->will($this->returnValue($subAdminManager));
$this->api->addSubAdmin('ExistingUser', 'TargetGroup');
}
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 101
@ -2877,44 +2839,6 @@ class UsersControllerTest extends TestCase {
$this->assertEquals([], $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom')->getData());
}
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 103
* @expectedExceptionMessage Unknown error occurred
*/
public function testRemoveSubAdminUnsuccessful() {
$targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
$targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
$this->userManager
->expects($this->once())
->method('get')
->with('ExistingUser')
->will($this->returnValue($targetUser));
$this->groupManager
->expects($this->once())
->method('get')
->with('GroupToDeleteFrom')
->will($this->returnValue($targetGroup));
$subAdminManager = $this->getMockBuilder('OC\SubAdmin')
->disableOriginalConstructor()->getMock();
$subAdminManager
->expects($this->once())
->method('isSubAdminOfGroup')
->with($targetUser, $targetGroup)
->will($this->returnValue(true));
$subAdminManager
->expects($this->once())
->method('deleteSubAdmin')
->with($targetUser, $targetGroup)
->will($this->returnValue(false));
$this->groupManager
->expects($this->once())
->method('getSubAdmin')
->will($this->returnValue($subAdminManager));
$this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
}
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 404

View File

@ -263,6 +263,7 @@ return array(
'OCP\\Group\\Backend\\IGroupDetailsBackend' => $baseDir . '/lib/public/Group/Backend/IGroupDetailsBackend.php',
'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php',
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
'OCP\\Group\\ISubAdmin' => $baseDir . '/lib/public/Group/ISubAdmin.php',
'OCP\\Http\\Client\\IClient' => $baseDir . '/lib/public/Http/Client/IClient.php',
'OCP\\Http\\Client\\IClientService' => $baseDir . '/lib/public/Http/Client/IClientService.php',
'OCP\\Http\\Client\\IResponse' => $baseDir . '/lib/public/Http/Client/IResponse.php',

View File

@ -293,6 +293,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Group\\Backend\\IGroupDetailsBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IGroupDetailsBackend.php',
'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php',
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
'OCP\\Group\\ISubAdmin' => __DIR__ . '/../../..' . '/lib/public/Group/ISubAdmin.php',
'OCP\\Http\\Client\\IClient' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClient.php',
'OCP\\Http\\Client\\IClientService' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClientService.php',
'OCP\\Http\\Client\\IResponse' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IResponse.php',

View File

@ -141,6 +141,7 @@ use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
use OCP\FullTextSearch\IFullTextSearchManager;
use OCP\GlobalScale\IConfig;
use OCP\Group\ISubAdmin;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IL10N;
@ -1201,6 +1202,8 @@ class Server extends ServerContainer implements IServerContainer {
);
});
$this->registerAlias(ISubAdmin::class, SubAdmin::class);
$this->connectDispatcher();
}

View File

@ -29,13 +29,14 @@
namespace OC;
use OC\Hooks\PublicEmitter;
use OCP\Group\ISubAdmin;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IDBConnection;
class SubAdmin extends PublicEmitter {
class SubAdmin extends PublicEmitter implements ISubAdmin {
/** @var IUserManager */
private $userManager;
@ -70,9 +71,8 @@ class SubAdmin extends PublicEmitter {
* add a SubAdmin
* @param IUser $user user to be SubAdmin
* @param IGroup $group group $user becomes subadmin of
* @return bool
*/
public function createSubAdmin(IUser $user, IGroup $group) {
public function createSubAdmin(IUser $user, IGroup $group): void {
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('group_admin')
@ -84,16 +84,14 @@ class SubAdmin extends PublicEmitter {
$this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
\OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]);
return true;
}
/**
* delete a SubAdmin
* @param IUser $user the user that is the SubAdmin
* @param IGroup $group the group
* @return bool
*/
public function deleteSubAdmin(IUser $user, IGroup $group) {
public function deleteSubAdmin(IUser $user, IGroup $group): void {
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_admin')
@ -103,7 +101,6 @@ class SubAdmin extends PublicEmitter {
$this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]);
\OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]);
return true;
}
/**
@ -111,7 +108,7 @@ class SubAdmin extends PublicEmitter {
* @param IUser $user the SubAdmin
* @return IGroup[]
*/
public function getSubAdminsGroups(IUser $user) {
public function getSubAdminsGroups(IUser $user): array {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->select('gid')
@ -136,7 +133,7 @@ class SubAdmin extends PublicEmitter {
* @param IUser $user
* @return array ['displayName' => displayname]
*/
public function getSubAdminsGroupsName(IUser $user) {
public function getSubAdminsGroupsName(IUser $user): array {
return array_map(function($group) {
return array('displayName' => $group->getDisplayName());
}, $this->getSubAdminsGroups($user));
@ -147,7 +144,7 @@ class SubAdmin extends PublicEmitter {
* @param IGroup $group the group
* @return IUser[]
*/
public function getGroupsSubAdmins(IGroup $group) {
public function getGroupsSubAdmins(IGroup $group): array {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->select('uid')
@ -171,7 +168,7 @@ class SubAdmin extends PublicEmitter {
* get all SubAdmins
* @return array
*/
public function getAllSubAdmins() {
public function getAllSubAdmins(): array {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->select('*')
@ -200,7 +197,7 @@ class SubAdmin extends PublicEmitter {
* @param IGroup $group
* @return bool
*/
public function isSubAdminOfGroup(IUser $user, IGroup $group) {
public function isSubAdminOfGroup(IUser $user, IGroup $group): bool {
$qb = $this->dbConn->getQueryBuilder();
/*
@ -224,7 +221,7 @@ class SubAdmin extends PublicEmitter {
* @param IUser $user
* @return bool
*/
public function isSubAdmin(IUser $user) {
public function isSubAdmin(IUser $user): bool {
// Check if the user is already an admin
if ($this->groupManager->isAdmin($user->getUID())) {
return true;
@ -250,7 +247,7 @@ class SubAdmin extends PublicEmitter {
* @param IUser $user
* @return bool
*/
public function isUserAccessible($subadmin, $user) {
public function isUserAccessible(IUser $subadmin, IUser $user): bool {
if(!$this->isSubAdmin($subadmin)) {
return false;
}
@ -269,30 +266,24 @@ class SubAdmin extends PublicEmitter {
/**
* delete all SubAdmins by $user
* @param IUser $user
* @return boolean
*/
private function post_deleteUser($user) {
private function post_deleteUser(IUser $user) {
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_admin')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
->execute();
return true;
}
/**
* delete all SubAdmins by $group
* @param IGroup $group
* @return boolean
*/
private function post_deleteGroup($group) {
private function post_deleteGroup(IGroup $group) {
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
->execute();
return true;
}
}

View File

@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Group;
use OCP\IGroup;
use OCP\IUser;
/**
* @since 16.0.0
*/
interface ISubAdmin {
/**
* add a SubAdmin
* @param IUser $user user to be SubAdmin
* @param IGroup $group group $user becomes subadmin of
*
* @since 16.0.0
*/
public function createSubAdmin(IUser $user, IGroup $group): void;
/**
* delete a SubAdmin
* @param IUser $user the user that is the SubAdmin
* @param IGroup $group the group
*
* @since 16.0.0
*/
public function deleteSubAdmin(IUser $user, IGroup $group): void;
/**
* get groups of a SubAdmin
* @param IUser $user the SubAdmin
* @return IGroup[]
*
* @since 16.0.0
*/
public function getSubAdminsGroups(IUser $user): array;
/**
* get SubAdmins of a group
* @param IGroup $group the group
* @return IUser[]
*
* @since 16.0.0
*/
public function getGroupsSubAdmins(IGroup $group): array;
/**
* checks if a user is a SubAdmin of a group
* @param IUser $user
* @param IGroup $group
* @return bool
*
* @since 16.0.0
*/
public function isSubAdminOfGroup(IUser $user, IGroup $group): bool;
/**
* checks if a user is a SubAdmin
* @param IUser $user
* @return bool
*
* @since 16.0.0
*/
public function isSubAdmin(IUser $user): bool;
/**
* checks if a user is a accessible by a subadmin
* @param IUser $subadmin
* @param IUser $user
* @return bool
*
* @since 16.0.0
*/
public function isUserAccessible(IUser $subadmin, IUser $user): bool;
}

View File

@ -44,6 +44,8 @@ use OCP\App\IAppManager;
* Class AppSettingsControllerTest
*
* @package Tests\Settings\Controller
*
* @group DB
*/
class AppSettingsControllerTest extends TestCase {
/** @var AppSettingsController */

View File

@ -20,6 +20,9 @@
*/
namespace Test;
/**
* @group DB
*/
class SubAdminTest extends \Test\TestCase {
/** @var \OCP\IUserManager */
@ -97,7 +100,7 @@ class SubAdminTest extends \Test\TestCase {
public function testCreateSubAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
// Look for subadmin in the database
$qb = $this->dbConn->getQueryBuilder();
@ -122,8 +125,8 @@ class SubAdminTest extends \Test\TestCase {
public function testDeleteSubAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
// DB query should be empty
$qb = $this->dbConn->getQueryBuilder();
@ -138,8 +141,8 @@ class SubAdminTest extends \Test\TestCase {
public function testGetSubAdminsGroups() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[1]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->createSubAdmin($this->users[0], $this->groups[1]);
$result = $subAdmin->getSubAdminsGroups($this->users[0]);
@ -148,14 +151,14 @@ class SubAdminTest extends \Test\TestCase {
$this->assertNotContains($this->groups[2], $result);
$this->assertNotContains(null, $result);
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]));
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]);
}
public function testGetGroupsSubAdmins() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->createSubAdmin($this->users[1], $this->groups[0]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->createSubAdmin($this->users[1], $this->groups[0]);
$result = $subAdmin->getGroupsSubAdmins($this->groups[0]);
@ -164,16 +167,16 @@ class SubAdminTest extends \Test\TestCase {
$this->assertNotContains($this->users[2], $result);
$this->assertNotContains(null, $result);
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]));
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]);
}
public function testGetAllSubAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->createSubAdmin($this->users[1], $this->groups[1]));
$this->assertTrue($subAdmin->createSubAdmin($this->users[2], $this->groups[1]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->createSubAdmin($this->users[1], $this->groups[1]);
$subAdmin->createSubAdmin($this->users[2], $this->groups[1]);
$result = $subAdmin->getAllSubAdmins();
@ -185,23 +188,23 @@ class SubAdminTest extends \Test\TestCase {
public function testIsSubAdminofGroup() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0]));
$this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[1]));
$this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[1], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
}
public function testIsSubAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
$this->assertFalse($subAdmin->isSubAdmin($this->users[1]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
}
public function testIsSubAdminAsAdmin() {
@ -216,15 +219,15 @@ class SubAdminTest extends \Test\TestCase {
$this->groups[0]->addUser($this->users[1]);
$this->groups[1]->addUser($this->users[1]);
$this->groups[1]->addUser($this->users[2]);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->createSubAdmin($this->users[2], $this->groups[2]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->createSubAdmin($this->users[2], $this->groups[2]);
$this->assertTrue($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
$this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[2]));
$this->assertFalse($subAdmin->isUserAccessible($this->users[2], $this->users[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]));
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
$subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]);
}
public function testIsUserAccessibleAsUser() {
@ -234,7 +237,7 @@ class SubAdminTest extends \Test\TestCase {
public function testIsUserAccessibleAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
$this->groupManager->get('admin')->addUser($this->users[1]);
$this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
@ -246,7 +249,7 @@ class SubAdminTest extends \Test\TestCase {
$user = array_shift($this->users);
foreach($this->groups as $group) {
$this->assertTrue($subAdmin->createSubAdmin($user, $group));
$subAdmin->createSubAdmin($user, $group);
}
$user->delete();
@ -258,7 +261,7 @@ class SubAdminTest extends \Test\TestCase {
$group = array_shift($this->groups);
foreach($this->users as $user) {
$this->assertTrue($subAdmin->createSubAdmin($user, $group));
$subAdmin->createSubAdmin($user, $group);
}
$group->delete();
@ -285,10 +288,10 @@ class SubAdminTest extends \Test\TestCase {
$count++;
});
$this->assertTrue($subAdmin->createSubAdmin($u, $g));
$subAdmin->createSubAdmin($u, $g);
$this->assertEquals(1, $count);
$this->assertTrue($subAdmin->deleteSubAdmin($u, $g));
$subAdmin->deleteSubAdmin($u, $g);
$this->assertEquals(2, $count);
}