2015-08-13 19:31:38 +00:00
|
|
|
<?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;
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2016-05-20 13:38:20 +00:00
|
|
|
class SubAdminTest extends \Test\TestCase {
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
/** @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');
|
|
|
|
}
|
2015-11-05 10:50:57 +00:00
|
|
|
|
|
|
|
// Create "orphaned" users and groups (scenario: temporarily disabled
|
|
|
|
// backend)
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->insert('group_admin')
|
|
|
|
->values([
|
|
|
|
'gid' => $qb->createNamedParameter($this->groups[0]->getGID()),
|
|
|
|
'uid' => $qb->createNamedParameter('orphanedUser')
|
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
$qb->insert('group_admin')
|
|
|
|
->values([
|
|
|
|
'gid' => $qb->createNamedParameter('orphanedGroup'),
|
|
|
|
'uid' => $qb->createNamedParameter('orphanedUser')
|
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
$qb->insert('group_admin')
|
|
|
|
->values([
|
|
|
|
'gid' => $qb->createNamedParameter('orphanedGroup'),
|
|
|
|
'uid' => $qb->createNamedParameter($this->users[0]->getUID())
|
|
|
|
])
|
|
|
|
->execute();
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
foreach($this->users as $user) {
|
|
|
|
$user->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->groups as $group) {
|
|
|
|
$group->delete();
|
|
|
|
}
|
2015-11-05 10:50:57 +00:00
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->delete('group_admin')
|
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser')))
|
|
|
|
->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup')))
|
|
|
|
->execute();
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateSubAdmin() {
|
|
|
|
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[1]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
$result = $subAdmin->getSubAdminsGroups($this->users[0]);
|
|
|
|
|
|
|
|
$this->assertContains($this->groups[0], $result);
|
|
|
|
$this->assertContains($this->groups[1], $result);
|
|
|
|
$this->assertNotContains($this->groups[2], $result);
|
2015-11-05 10:50:57 +00:00
|
|
|
$this->assertNotContains(null, $result);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetGroupsSubAdmins() {
|
|
|
|
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->createSubAdmin($this->users[1], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
$result = $subAdmin->getGroupsSubAdmins($this->groups[0]);
|
|
|
|
|
|
|
|
$this->assertContains($this->users[0], $result);
|
|
|
|
$this->assertContains($this->users[1], $result);
|
|
|
|
$this->assertNotContains($this->users[2], $result);
|
2015-11-05 10:50:57 +00:00
|
|
|
$this->assertNotContains(null, $result);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllSubAdmin() {
|
|
|
|
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->createSubAdmin($this->users[1], $this->groups[1]);
|
|
|
|
$subAdmin->createSubAdmin($this->users[2], $this->groups[1]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
$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);
|
2015-11-05 10:50:57 +00:00
|
|
|
$this->assertNotContains(['user' => null, 'group' => null], $result);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsSubAdminofGroup() {
|
|
|
|
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
$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]));
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsSubAdmin() {
|
|
|
|
$subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
|
|
|
|
$this->assertFalse($subAdmin->isSubAdmin($this->users[1]));
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->createSubAdmin($this->users[2], $this->groups[2]);
|
2015-08-13 19:31:38 +00:00
|
|
|
|
|
|
|
$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]));
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
|
|
|
|
$subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
|
2015-08-13 19:31:38 +00:00
|
|
|
$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) {
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($user, $group);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($user, $group);
|
2015-08-13 19:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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++;
|
|
|
|
});
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->createSubAdmin($u, $g);
|
2015-08-13 19:31:38 +00:00
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdmin->deleteSubAdmin($u, $g);
|
2015-08-13 19:31:38 +00:00
|
|
|
$this->assertEquals(2, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|