Skip to content

Commit

Permalink
Merge pull request #18399 from owncloud/api-getusers-for-subadmins
Browse files Browse the repository at this point in the history
enable api getUsers for subadmins
  • Loading branch information
DeepDiver1975 committed Oct 20, 2015
2 parents 474f34e + 23db51f commit 60abc27
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/provisioning_api/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
\OC::$server->getGroupManager(),
\OC::$server->getUserSession()
);
API::register('get', '/cloud/users', [$users, 'getUsers'], 'provisioning_api', API::ADMIN_AUTH);
API::register('get', '/cloud/users', [$users, 'getUsers'], 'provisioning_api', API::SUBADMIN_AUTH);
API::register('post', '/cloud/users', [$users, 'addUser'], 'provisioning_api', API::ADMIN_AUTH);
API::register('get', '/cloud/users/{userid}', [$users, 'getUser'], 'provisioning_api', API::USER_AUTH);
API::register('put', '/cloud/users/{userid}', [$users, 'editUser'], 'provisioning_api', API::USER_AUTH);
Expand Down
27 changes: 26 additions & 1 deletion apps/provisioning_api/lib/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use \OC_OCS_Result;
use \OC_SubAdmin;
use \OC_Helper;
use \OC_Group;
use OCP\Files\NotFoundException;

class Users {
Expand Down Expand Up @@ -71,7 +72,31 @@ public function getUsers() {
$limit = !empty($_GET['limit']) ? $_GET['limit'] : null;
$offset = !empty($_GET['offset']) ? $_GET['offset'] : null;

$users = $this->userManager->search($search, $limit, $offset);
// Check if user is logged in
$user = $this->userSession->getUser();
if ($user === null) {
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED);
}

// Admin? Or SubAdmin?
if($this->groupManager->isAdmin($user->getUID())){
$users = $this->userManager->search($search, $limit, $offset);
} else if (\OC_SubAdmin::isSubAdmin($user->getUID())) {
$subAdminOfGroups = \OC_SubAdmin::getSubAdminsGroups($user->getUID());

if($offset === null) {
$offset = 0;
}

$users = [];
foreach ($subAdminOfGroups as $group) {
$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
}

$users = array_slice($users, $offset, $limit);
} else {
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED);
}
$users = array_keys($users);

return new OC_OCS_Result([
Expand Down
78 changes: 77 additions & 1 deletion apps/provisioning_api/tests/userstest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ protected function setup() {
$this->groupManager,
$this->userSession
);

$this->userSession->setUser(null);
}

// Test getting the list of users
public function testGetUsers() {
public function testGetUsersAsAdmin() {
$user = $this->generateUsers();
$this->groupManager->get('admin')->addUser($user);
$this->userSession->setUser($user);

$result = $this->api->getUsers();
$this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded());
Expand Down Expand Up @@ -103,6 +109,70 @@ public function testGetUsers() {
$this->assertEquals(array_keys($this->userManager->search('', 1, 1)), $data['users']);
}

public function testGetUsersAsSubAdmin() {
$user = $this->generateUsers(10);
$this->userSession->setUser($user[0]);
$group = $this->groupManager->createGroup($this->getUniqueID());
\OC_SubAdmin::createSubAdmin($user[0]->getUID(), $group->getGID());

//Empty list
$result = $this->api->getUsers([]);
$this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded());
$this->assertEquals(['users' => []], $result->getData());

//Some users in group
$group->addUser($user[1]);
$group->addUser($user[2]);
$group->addUser($user[3]);
$group->addUser($user[4]);

$result = $this->api->getUsers([]);
$this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded());
$this->assertArrayHasKey('users', $result->getData());

$this->assertContains($user[1]->getUID(), $result->getData()['users']);
$this->assertContains($user[2]->getUID(), $result->getData()['users']);
$this->assertContains($user[3]->getUID(), $result->getData()['users']);
$this->assertContains($user[4]->getUID(), $result->getData()['users']);

$uids = [
$user[1]->getUID(),
$user[2]->getUID(),
$user[3]->getUID(),
$user[4]->getUID()
];
sort($uids);

$_GET['limit'] = 2;
$_GET['offset'] = 1;
$result = $this->api->getUsers([]);

$this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded());
$this->assertEquals(['users' => array_slice($uids, 1, 2)], $result->getData());
}

public function testGetUsersNoUser() {
$result = $this->api->getUsers([]);

$this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());
}

public function testGetUsersAsUser() {
$user = $this->generateUsers();
$this->userSession->setUser($user);

$result = $this->api->getUsers();
$this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());

}

public function testAddUser() {
$this->resetParams();
$_POST['userid'] = $this->getUniqueID();
Expand Down Expand Up @@ -794,6 +864,9 @@ public function testAddToGroupAsIrelevantSubAdmin() {
}

public function testAddToGroupNoGroupId() {
$user = $this->generateUsers();
$this->userSession->setUser($user);

$_POST['groupid'] = '';
$result = $this->api->addToGroup([
'userid' => $this->getUniqueID(),
Expand Down Expand Up @@ -935,6 +1008,9 @@ public function testRemoveFromGroupAsIrelevantSubAdmin() {
}

public function testRemoveFromGroupNoGroupId() {
$user = $this->generateUsers();
$this->userSession->setUser($user);

$result = $this->api->removeFromGroup([
'_delete' => [
'groupid' => ''
Expand Down

0 comments on commit 60abc27

Please sign in to comment.