Merge pull request #19827 from owncloud/subadmin_non_static

Make OC_SubAdmin non static
This commit is contained in:
Thomas Müller 2015-10-21 14:43:09 +02:00
commit 33144a5d80
6 changed files with 705 additions and 111 deletions

View File

@ -71,11 +71,13 @@ class Manager extends PublicEmitter implements IGroupManager {
*/ */
private $cachedUserGroups = array(); private $cachedUserGroups = array();
/** @var \OC\SubAdmin */
private $subAdmin = null;
/** /**
* @param \OC\User\Manager $userManager * @param \OC\User\Manager $userManager
*/ */
public function __construct($userManager) { public function __construct(\OC\User\Manager $userManager) {
$this->userManager = $userManager; $this->userManager = $userManager;
$cachedGroups = & $this->cachedGroups; $cachedGroups = & $this->cachedGroups;
$cachedUserGroups = & $this->cachedUserGroups; $cachedUserGroups = & $this->cachedUserGroups;
@ -314,4 +316,19 @@ class Manager extends PublicEmitter implements IGroupManager {
} }
return $matchingUsers; return $matchingUsers;
} }
/**
* @return \OC\SubAdmin
*/
public function getSubAdmin() {
if (!$this->subAdmin) {
$this->subAdmin = new \OC\SubAdmin(
$this->userManager,
$this,
\OC::$server->getDatabaseConnection()
);
}
return $this->subAdmin;
}
} }

View File

@ -0,0 +1,172 @@
<?php
/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Georg Ehrke <georg@owncloud.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Lukas Reschke <lukas@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
* @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/>
*
*/
/**
* This class provides all methods needed for managing groups.
*
* Hooks provided:
* post_createSubAdmin($gid)
* post_deleteSubAdmin($gid)
*/
class OC_SubAdmin{
/**
* add a SubAdmin
* @param string $uid uid of the SubAdmin
* @param string $gid gid of the group
* @return boolean
*/
public static function createSubAdmin($uid, $gid) {
$groupManager = \OC::$server->getGroupManager();
$userManager = \OC::$server->getUserManager();
$subAdmin = $groupManager->getSubAdmin();
return $subAdmin->createSubAdmin($userManager->get($uid), $groupManager->get($gid));
}
/**
* delete a SubAdmin
* @param string $uid uid of the SubAdmin
* @param string $gid gid of the group
* @return boolean
*/
public static function deleteSubAdmin($uid, $gid) {
$groupManager = \OC::$server->getGroupManager();
$userManager = \OC::$server->getUserManager();
$subAdmin = $groupManager->getSubAdmin();
return $subAdmin->deleteSubAdmin($userManager->get($uid), $groupManager->get($gid));
}
/**
* get groups of a SubAdmin
* @param string $uid uid of the SubAdmin
* @return array
*/
public static function getSubAdminsGroups($uid) {
$groupManager = \OC::$server->getGroupManager();
$userManager = \OC::$server->getUserManager();
$subAdmin = $groupManager->getSubAdmin();
$groups = $subAdmin->getSubAdminsGroups($userManager->get($uid));
// New class returns IGroup[] so convert back
$gids = [];
foreach ($groups as $group) {
$gids[] = $group->getGID();
}
return $gids;
}
/**
* get SubAdmins of a group
* @param string $gid gid of the group
* @return array
*/
public static function getGroupsSubAdmins($gid) {
$groupManager = \OC::$server->getGroupManager();
$subAdmin = $groupManager->getSubAdmin();
$users = $subAdmin->getGroupsSubAdmins($groupManager->get($gid));
// New class returns IUser[] so convert back
$uids = [];
foreach ($users as $user) {
$uids[] = $user->getUID();
}
return $uids;
}
/**
* get all SubAdmins
* @return array
*/
public static function getAllSubAdmins() {
$groupManager = \OC::$server->getGroupManager();
$subAdmin = $groupManager->getSubAdmin();
$subAdmins = $subAdmin->getAllSubAdmins();
// New class returns IUser[] so convert back
$result = [];
foreach ($subAdmins as $subAdmin) {
$result[] = [
'gid' => $subAdmin['group']->getGID(),
'uid' => $subAdmin['user']->getUID(),
];
}
return $result;
}
/**
* checks if a user is a SubAdmin of a group
* @param string $uid uid of the subadmin
* @param string $gid gid of the group
* @return bool
*/
public static function isSubAdminofGroup($uid, $gid) {
$groupManager = \OC::$server->getGroupManager();
$userManager = \OC::$server->getUserManager();
$subAdmin = $groupManager->getSubAdmin();
return $subAdmin->isSubAdminOfGroup($userManager->get($uid), $groupManager->get($gid));
}
/**
* checks if a user is a SubAdmin
* @param string $uid uid of the subadmin
* @return bool
*/
public static function isSubAdmin($uid) {
$groupManager = \OC::$server->getGroupManager();
$userManager = \OC::$server->getUserManager();
$subAdmin = $groupManager->getSubAdmin();
return $subAdmin->isSubAdmin($userManager->get($uid));
}
/**
* checks if a user is a accessible by a subadmin
* @param string $subadmin uid of the subadmin
* @param string $user uid of the user
* @return bool
*/
public static function isUserAccessible($subadmin, $user) {
$groupManager = \OC::$server->getGroupManager();
$userManager = \OC::$server->getUserManager();
$subAdmin = $groupManager->getSubAdmin();
return $subAdmin->isUserAccessible($userManager->get($subadmin), $userManager->get($user));
}
/*
* alias for self::isSubAdminofGroup()
*/
public static function isGroupAccessible($subadmin, $group) {
return self::isSubAdminofGroup($subadmin, $group);
}
}

View File

@ -2,10 +2,9 @@
/** /**
* @author Bart Visscher <bartv@thisnet.nl> * @author Bart Visscher <bartv@thisnet.nl>
* @author Georg Ehrke <georg@owncloud.com> * @author Georg Ehrke <georg@owncloud.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Lukas Reschke <lukas@owncloud.com> * @author Lukas Reschke <lukas@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de> * @author Morris Jobke <hey@morrisjobke.de>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk> * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu> * @author Thomas Müller <thomas.mueller@tmit.eu>
* *
* @copyright Copyright (c) 2015, ownCloud, Inc. * @copyright Copyright (c) 2015, ownCloud, Inc.
@ -24,171 +23,235 @@
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* *
*/ */
OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_SubAdmin', 'post_deleteUser');
OC_Hook::connect('OC_User', 'post_deleteGroup', 'OC_SubAdmin', 'post_deleteGroup'); namespace OC;
/**
* This class provides all methods needed for managing groups. use OC\Hooks\PublicEmitter;
* use OCP\IUser;
* Hooks provided: use OCP\IUserManager;
* post_createSubAdmin($gid) use OCP\IGroup;
* post_deleteSubAdmin($gid) use OCP\IGroupManager;
*/ use OCP\IDBConnection;
class OC_SubAdmin{
class SubAdmin extends PublicEmitter {
/** @var IUserManager */
private $userManager;
/** @var IGroupManager */
private $groupManager;
/** @var IDBConnection */
private $dbConn;
public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
IDBConnection $dbConn) {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->dbConn = $dbConn;
$this->userManager->listen('\OC\User', 'postDelete', function($user) {
$this->post_deleteUser($user);
});
$this->groupManager->listen('\OC\Group', 'postDelete', function($group) {
$this->post_deleteGroup($group);
});
}
/** /**
* add a SubAdmin * add a SubAdmin
* @param string $uid uid of the SubAdmin * @param IUser $user user to be SubAdmin
* @param string $gid gid of the group * @param IGroup $group group $user becomes subadmin of
* @return boolean * @return bool
*/ */
public static function createSubAdmin($uid, $gid) { public function createSubAdmin(IUser $user, IGroup $group) {
$stmt = OC_DB::prepare('INSERT INTO `*PREFIX*group_admin` (`gid`,`uid`) VALUES(?,?)'); $qb = $this->dbConn->getQueryBuilder();
$stmt->execute(array($gid, $uid));
OC_Hook::emit( "OC_SubAdmin", "post_createSubAdmin", array( "gid" => $gid )); $result = $qb->insert('group_admin')
->values([
'gid' => $qb->createNamedParameter($group->getGID()),
'uid' => $qb->createNamedParameter($user->getUID())
])
->execute();
$this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
\OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]);
return true; return true;
} }
/** /**
* delete a SubAdmin * delete a SubAdmin
* @param string $uid uid of the SubAdmin * @param IUser $user the user that is the SubAdmin
* @param string $gid gid of the group * @param IGroup $group the group
* @return boolean * @return bool
*/ */
public static function deleteSubAdmin($uid, $gid) { public function deleteSubAdmin(IUser $user, IGroup $group) {
$stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ? AND `uid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$stmt->execute(array($gid, $uid));
OC_Hook::emit( "OC_SubAdmin", "post_deleteSubAdmin", array( "gid" => $gid )); $result = $qb->delete('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
->execute();
$this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]);
\OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]);
return true; return true;
} }
/** /**
* get groups of a SubAdmin * get groups of a SubAdmin
* @param string $uid uid of the SubAdmin * @param IUser $user the SubAdmin
* @return array * @return IGroup[]
*/ */
public static function getSubAdminsGroups($uid) { public function getSubAdminsGroups(IUser $user) {
$stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*group_admin` WHERE `uid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$result = $stmt->execute(array($uid));
$gids = array(); $result = $qb->select('gid')
while($row = $result->fetchRow()) { ->from('group_admin')
$gids[] = $row['gid']; ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
->execute();
$groups = [];
while($row = $result->fetch()) {
$groups[] = $this->groupManager->get($row['gid']);
} }
return $gids;
return $groups;
} }
/** /**
* get SubAdmins of a group * get SubAdmins of a group
* @param string $gid gid of the group * @param IGroup $group the group
* @return array * @return IUser[]
*/ */
public static function getGroupsSubAdmins($gid) { public function getGroupsSubAdmins(IGroup $group) {
$stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_admin` WHERE `gid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$result = $stmt->execute(array($gid));
$uids = array(); $result = $qb->select('uid')
while($row = $result->fetchRow()) { ->from('group_admin')
$uids[] = $row['uid']; ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
->execute();
$users = [];
while($row = $result->fetch()) {
$users[] = $this->userManager->get($row['uid']);
} }
return $uids;
return $users;
} }
/** /**
* get all SubAdmins * get all SubAdmins
* @return array * @return array
*/ */
public static function getAllSubAdmins() { public function getAllSubAdmins() {
$stmt = OC_DB::prepare('SELECT * FROM `*PREFIX*group_admin`'); $qb = $this->dbConn->getQueryBuilder();
$result = $stmt->execute();
$subadmins = array(); $result = $qb->select('*')
while($row = $result->fetchRow()) { ->from('group_admin')
$subadmins[] = $row; ->execute();
$subadmins = [];
while($row = $result->fetch()) {
$subadmins[] = [
'user' => $this->userManager->get($row['uid']),
'group' => $this->groupManager->get($row['gid'])
];
} }
return $subadmins; return $subadmins;
} }
/** /**
* checks if a user is a SubAdmin of a group * checks if a user is a SubAdmin of a group
* @param string $uid uid of the subadmin * @param IUser $user
* @param string $gid gid of the group * @param IGroup $group
* @return bool * @return bool
*/ */
public static function isSubAdminofGroup($uid, $gid) { public function isSubAdminofGroup(IUser $user, IGroup $group) {
$stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ? AND `gid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$result = $stmt->execute(array($uid, $gid));
$result = $result->fetchRow(); $result = $qb->select('*')
if($result['count'] >= 1) { ->from('group_admin')
return true; ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
} ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
return false; ->execute();
return !empty($result->fetch()) ? true : false;
} }
/** /**
* checks if a user is a SubAdmin * checks if a user is a SubAdmin
* @param string $uid uid of the subadmin * @param IUser $user
* @return bool * @return bool
*/ */
public static function isSubAdmin($uid) { public function isSubAdmin(IUser $user) {
// Check if the user is already an admin // Check if the user is already an admin
if(OC_Group::inGroup($uid, 'admin' )) { if ($this->groupManager->isAdmin($user->getUID())) {
return true; return true;
} }
$stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$result = $stmt->execute(array($uid));
$result = $result->fetchRow(); $result = $qb->select('gid')
if($result['count'] > 0) { ->from('group_admin')
return true; ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
} ->setMaxResults(1)
return false; ->execute()
->fetch();
return $result === false ? false : true;
} }
/** /**
* checks if a user is a accessible by a subadmin * checks if a user is a accessible by a subadmin
* @param string $subadmin uid of the subadmin * @param IUser $subadmin
* @param string $user uid of the user * @param IUser $user
* @return bool * @return bool
*/ */
public static function isUserAccessible($subadmin, $user) { public function isUserAccessible($subadmin, $user) {
if(!self::isSubAdmin($subadmin)) { if(!$this->isSubAdmin($subadmin)) {
return false; return false;
} }
if(OC_User::isAdminUser($user)) { if($this->groupManager->isAdmin($user->getUID())) {
return false; return false;
} }
$accessiblegroups = self::getSubAdminsGroups($subadmin); $accessiblegroups = $this->getSubAdminsGroups($subadmin);
foreach($accessiblegroups as $accessiblegroup) { foreach($accessiblegroups as $accessiblegroup) {
if(OC_Group::inGroup($user, $accessiblegroup)) { if($accessiblegroup->inGroup($user)) {
return true; return true;
} }
} }
return false; return false;
} }
/*
* alias for self::isSubAdminofGroup()
*/
public static function isGroupAccessible($subadmin, $group) {
return self::isSubAdminofGroup($subadmin, $group);
}
/** /**
* delete all SubAdmins by uid * delete all SubAdmins by $user
* @param array $parameters * @param IUser $user
* @return boolean * @return boolean
*/ */
public static function post_deleteUser($parameters) { private function post_deleteUser($user) {
$stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `uid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$stmt->execute(array($parameters['uid']));
$result = $qb->delete('group_admin')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
->execute();
return true; return true;
} }
/** /**
* delete all SubAdmins by gid * delete all SubAdmins by $group
* @param array $parameters * @param IGroup $group
* @return boolean * @return boolean
*/ */
public static function post_deleteGroup($parameters) { private function post_deleteGroup($group) {
$stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?'); $qb = $this->dbConn->getQueryBuilder();
$stmt->execute(array($parameters['gid']));
$result = $qb->delete('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
->execute();
return true; return true;
} }
} }

View File

@ -167,7 +167,7 @@ class UsersController extends Controller {
'name' => $user->getUID(), 'name' => $user->getUID(),
'displayname' => $user->getDisplayName(), 'displayname' => $user->getDisplayName(),
'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups, 'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($user->getUID()), 'subadmin' => $this->subAdminFactory->getSubAdminsOfGroups($user->getUID()),
'quota' => $this->config->getUserValue($user->getUID(), 'files', 'quota', 'default'), 'quota' => $this->config->getUserValue($user->getUID(), 'files', 'quota', 'default'),
'storageLocation' => $user->getHome(), 'storageLocation' => $user->getHome(),
'lastLogin' => $user->getLastLogin() * 1000, 'lastLogin' => $user->getLastLogin() * 1000,

264
tests/lib/subadmin.php Normal file
View File

@ -0,0 +1,264 @@
<?php
/**
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @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/>
*
*/
namespace Test;
class SubAdmin extends \Test\TestCase {
/** @var \OCP\IUserManager */
private $userManager;
/** @var \OCP\IGroupManager */
private $groupManager;
/** @var \OCP\IDBConnection */
private $dbConn;
/** @var \OCP\IUser[] */
private $users;
/** @var \OCP\IGroup[] */
private $groups;
public function setup() {
$this->users = [];
$this->groups = [];
$this->userManager = \OC::$server->getUserManager();
$this->groupManager = \OC::$server->getGroupManager();
$this->dbConn = \OC::$server->getDatabaseConnection();
// Create 3 users and 3 groups
for ($i = 0; $i < 3; $i++) {
$this->users[] = $this->userManager->createUser('user'.$i, 'user');
$this->groups[] = $this->groupManager->createGroup('group'.$i);
}
// Create admin group
if (!$this->groupManager->groupExists('admin')) {
$this->groupManager->createGroup('admin');
}
}
public function tearDown() {
foreach($this->users as $user) {
$user->delete();
}
foreach($this->groups as $group) {
$group->delete();
}
}
public function testCreateSubAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($subAdmin->createSubAdmin($this->users[0], $this->groups[0]));
// Look for subadmin in the database
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->select(['gid', 'uid'])
->from('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
->execute()
->fetch();
$this->assertEquals(
[
'gid' => $this->groups[0]->getGID(),
'uid' => $this->users[0]->getUID()
], $result);
// Delete subadmin
$result = $qb->delete('*PREFIX*group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
->execute();
}
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]));
// DB query should be empty
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->select(['gid', 'uid'])
->from('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
->execute()
->fetch();
$this->assertEmpty($result);
}
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]));
$result = $subAdmin->getSubAdminsGroups($this->users[0]);
$this->assertContains($this->groups[0], $result);
$this->assertContains($this->groups[1], $result);
$this->assertNotContains($this->groups[2], $result);
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($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]));
$result = $subAdmin->getGroupsSubAdmins($this->groups[0]);
$this->assertContains($this->users[0], $result);
$this->assertContains($this->users[1], $result);
$this->assertNotContains($this->users[2], $result);
$this->assertTrue($subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]));
$this->assertTrue($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]));
$result = $subAdmin->getAllSubAdmins();
$this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result);
$this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result);
$this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result);
}
public function testIsSubAdminofGroup() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($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]));
}
public function testIsSubAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($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]));
}
public function testIsSubAdminAsAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->groupManager->get('admin')->addUser($this->users[0]);
$this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
}
public function testIsUserAccessible() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$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]));
$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]));
}
public function testIsUserAccessibleAsUser() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
}
public function testIsUserAccessibleAdmin() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$this->assertTrue($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]));
}
public function testPostDeleteUser() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$user = array_shift($this->users);
foreach($this->groups as $group) {
$this->assertTrue($subAdmin->createSubAdmin($user, $group));
}
$user->delete();
$this->assertEmpty($subAdmin->getAllSubAdmins());
}
public function testPostDeleteGroup() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$group = array_shift($this->groups);
foreach($this->users as $user) {
$this->assertTrue($subAdmin->createSubAdmin($user, $group));
}
$group->delete();
$this->assertEmpty($subAdmin->getAllSubAdmins());
}
public function testHooks() {
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
$test = $this;
$u = $this->users[0];
$g = $this->groups[0];
$count = 0;
$subAdmin->listen('\OC\SubAdmin', 'postCreateSubAdmin', function ($user, $group) use ($test, $u, $g, &$count) {
$test->assertEquals($u->getUID(), $user->getUID());
$test->assertEquals($g->getGID(), $group->getGID());
$count++;
});
$subAdmin->listen('\OC\SubAdmin', 'postDeleteSubAdmin', function ($user, $group) use ($test, $u, $g, &$count) {
$test->assertEquals($u->getUID(), $user->getUID());
$test->assertEquals($g->getGID(), $group->getGID());
$count++;
});
$this->assertTrue($subAdmin->createSubAdmin($u, $g));
$this->assertEquals(1, $count);
$this->assertTrue($subAdmin->deleteSubAdmin($u, $g));
$this->assertEquals(2, $count);
}
}

View File

@ -153,6 +153,22 @@ class UsersControllerTest extends \Test\TestCase {
404, 'admin@bar.com', 404, 'admin@bar.com',
2323, 'bar@dummy.com')); 2323, 'bar@dummy.com'));
$this->container['SubAdminFactory']
->expects($this->at(0))
->method('getSubAdminsOfGroups')
->with('foo')
->will($this->returnValue([]));
$this->container['SubAdminFactory']
->expects($this->at(1))
->method('getSubAdminsOfGroups')
->with('admin')
->will($this->returnValue([]));
$this->container['SubAdminFactory']
->expects($this->at(2))
->method('getSubAdminsOfGroups')
->with('bar')
->will($this->returnValue([]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
0 => array( 0 => array(
@ -199,11 +215,6 @@ class UsersControllerTest extends \Test\TestCase {
public function testIndexSubAdmin() { public function testIndexSubAdmin() {
$this->container['IsAdmin'] = false; $this->container['IsAdmin'] = false;
$this->container['SubAdminFactory']
->expects($this->once())
->method('getSubAdminsOfGroups')
->with('username')
->will($this->returnValue(['SubGroup1', 'SubGroup2']));
$user = $this->getMockBuilder('\OC\User\User') $user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
@ -321,6 +332,15 @@ class UsersControllerTest extends \Test\TestCase {
2323, 'bar@dummy.com' 2323, 'bar@dummy.com'
)); ));
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->will($this->returnValueMap([
['username' , ['SubGroup1', 'SubGroup2']],
['foo', []],
['admin', []],
['bar', []],
]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
[ [
0 => [ 0 => [
@ -452,6 +472,23 @@ class UsersControllerTest extends \Test\TestCase {
404, 'admin@bar.com', 404, 'admin@bar.com',
2323, 'bar@dummy.com')); 2323, 'bar@dummy.com'));
$this->container['SubAdminFactory']
->expects($this->at(0))
->method('getSubAdminsOfGroups')
->with('foo')
->will($this->returnValue([]));
$this->container['SubAdminFactory']
->expects($this->at(1))
->method('getSubAdminsOfGroups')
->with('admin')
->will($this->returnValue([]));
$this->container['SubAdminFactory']
->expects($this->at(2))
->method('getSubAdminsOfGroups')
->with('bar')
->will($this->returnValue([]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
0 => array( 0 => array(
@ -532,6 +569,12 @@ class UsersControllerTest extends \Test\TestCase {
->with('') ->with('')
->will($this->returnValue([$user])); ->will($this->returnValue([$user]));
$this->container['SubAdminFactory']
->expects($this->once())
->method('getSubAdminsOfGroups')
->with('foo')
->will($this->returnValue([]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
0 => array( 0 => array(
@ -591,6 +634,11 @@ class UsersControllerTest extends \Test\TestCase {
->method('createUser') ->method('createUser')
->will($this->onConsecutiveCalls($user)); ->will($this->onConsecutiveCalls($user));
$this->container['SubAdminFactory']
->expects($this->once())
->method('getSubAdminsOfGroups')
->with('foo')
->will($this->returnValue([]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
@ -613,11 +661,6 @@ class UsersControllerTest extends \Test\TestCase {
public function testCreateSuccessfulWithoutGroupSubAdmin() { public function testCreateSuccessfulWithoutGroupSubAdmin() {
$this->container['IsAdmin'] = false; $this->container['IsAdmin'] = false;
$this->container['SubAdminFactory']
->expects($this->once())
->method('getSubAdminsOfGroups')
->with('username')
->will($this->returnValue(['SubGroup1', 'SubGroup2']));
$user = $this->getMockBuilder('\OC\User\User') $user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$user $user
@ -671,6 +714,13 @@ class UsersControllerTest extends \Test\TestCase {
->with($user) ->with($user)
->will($this->onConsecutiveCalls(['SubGroup1', 'SubGroup2'])); ->will($this->onConsecutiveCalls(['SubGroup1', 'SubGroup2']));
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->will($this->returnValueMap([
['username', ['SubGroup1', 'SubGroup2']],
['foo', []],
]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
'name' => 'foo', 'name' => 'foo',
@ -740,6 +790,12 @@ class UsersControllerTest extends \Test\TestCase {
->with($user) ->with($user)
->will($this->onConsecutiveCalls(array('NewGroup', 'ExistingGroup'))); ->will($this->onConsecutiveCalls(array('NewGroup', 'ExistingGroup')));
$this->container['SubAdminFactory']
->expects($this->once())
->method('getSubAdminsOfGroups')
->with('foo')
->will($this->returnValue([]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
'name' => 'foo', 'name' => 'foo',
@ -761,11 +817,6 @@ class UsersControllerTest extends \Test\TestCase {
public function testCreateSuccessfulWithGroupSubAdmin() { public function testCreateSuccessfulWithGroupSubAdmin() {
$this->container['IsAdmin'] = false; $this->container['IsAdmin'] = false;
$this->container['SubAdminFactory']
->expects($this->once())
->method('getSubAdminsOfGroups')
->with('username')
->will($this->returnValue(['SubGroup1', 'SubGroup2']));
$user = $this->getMockBuilder('\OC\User\User') $user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$user $user
@ -819,6 +870,13 @@ class UsersControllerTest extends \Test\TestCase {
->with($user) ->with($user)
->will($this->onConsecutiveCalls(['SubGroup1'])); ->will($this->onConsecutiveCalls(['SubGroup1']));
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->will($this->returnValueMap([
['username', ['SubGroup1', 'SubGroup2']],
['foo', []],
]));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
'name' => 'foo', 'name' => 'foo',
@ -1286,6 +1344,11 @@ class UsersControllerTest extends \Test\TestCase {
list($user, $expectedResult) = $this->mockUser(); list($user, $expectedResult) = $this->mockUser();
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->with($user->getUID())
->will($this->returnValue([]));
$result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
@ -1323,6 +1386,11 @@ class UsersControllerTest extends \Test\TestCase {
) )
->will($this->returnValue('1')); ->will($this->returnValue('1'));
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->with($user->getUID())
->will($this->returnValue([]));
$result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
@ -1341,6 +1409,11 @@ class UsersControllerTest extends \Test\TestCase {
$expectedResult['isRestoreDisabled'] = true; $expectedResult['isRestoreDisabled'] = true;
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->with($user->getUID())
->will($this->returnValue([]));
$result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
@ -1380,6 +1453,11 @@ class UsersControllerTest extends \Test\TestCase {
$expectedResult['isRestoreDisabled'] = true; $expectedResult['isRestoreDisabled'] = true;
$this->container['SubAdminFactory']
->method('getSubAdminsOfGroups')
->with($user->getUID())
->will($this->returnValue([]));
$result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }