Skip to content

Commit 5dc6406

Browse files
committed
Add filter for 'backend' to user REST route
This adds a "backend" type filter to the index REST route which is a pre-requisite for #12620 For example when calling `index.php/settings/users/users?offset=0&limit=10&gid=&pattern=&backend=OC_User_Database` only users within the backend `OC_User_Database` would be shown. (requires sending a CSRF token as well) Depends upon #12711
1 parent 5398bbd commit 5dc6406

File tree

6 files changed

+121
-17
lines changed

6 files changed

+121
-17
lines changed

lib/private/user.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ public static function getUsers($search = '', $limit = null, $offset = null) {
546546
* @return array associative array with all display names (value) and corresponding uids (key)
547547
*
548548
* Get a list of all display names and user ids.
549+
* @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
549550
*/
550551
public static function getDisplayNames($search = '', $limit = null, $offset = null) {
551552
$displayNames = array();

lib/private/user/manager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public function __construct(IConfig $config = null) {
6262
});
6363
}
6464

65+
/**
66+
* Get the active backends
67+
* @return \OC_User_Interface[]
68+
*/
69+
public function getBackends() {
70+
return $this->backends;
71+
}
72+
6573
/**
6674
* register a user backend
6775
*

lib/public/iusermanager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ interface IUserManager {
3131
*/
3232
public function registerBackend($backend);
3333

34+
/**
35+
* Get the active backends
36+
* @return \OC_User_Interface[]
37+
*/
38+
public function getBackends();
39+
3440
/**
3541
* remove a user backend
3642
*

settings/controller/userscontroller.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace OC\Settings\Controller;
1212

1313
use OC\AppFramework\Http;
14+
use OC\User\Manager;
1415
use OC\User\User;
1516
use \OCP\AppFramework\Controller;
1617
use OCP\AppFramework\Http\DataResponse;
@@ -84,46 +85,67 @@ private function formatUserForIndex(IUser $user, array $userGroups = null) {
8485
);
8586
}
8687

88+
/**
89+
* @param array $userIDs
90+
* @return IUser[]
91+
*/
92+
private function getUsersForUID(array $userIDs) {
93+
$users = [];
94+
foreach ($userIDs as $uid) {
95+
$users[] = $this->userManager->get($uid);
96+
}
97+
return $users;
98+
}
99+
87100
/**
88101
* @NoAdminRequired
89102
*
90103
* @param int $offset
91104
* @param int $limit
92-
* @param string $gid
93-
* @param string $pattern
105+
* @param string $gid GID to filter for
106+
* @param string $pattern Pattern to search for in the username
107+
* @param string $backend Backend to filter for (class-name)
94108
* @return DataResponse
95109
*
96110
* TODO: Tidy up and write unit tests - code is mainly static method calls
97111
*/
98-
public function index($offset = 0, $limit = 10, $gid = '', $pattern = '') {
112+
public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
99113
// FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
100114
if($gid === '_everyone') {
101115
$gid = '';
102116
}
117+
118+
// Remove backends
119+
if(!empty($backend)) {
120+
$activeBackends = $this->userManager->getBackends();
121+
$this->userManager->clearBackends();
122+
foreach($activeBackends as $singleActiveBackend) {
123+
if($backend === get_class($singleActiveBackend)) {
124+
$this->userManager->registerBackend($singleActiveBackend);
125+
}
126+
}
127+
}
128+
103129
$users = array();
104130
if ($this->isAdmin) {
131+
105132
if($gid !== '') {
106-
$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
133+
$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
107134
} else {
108-
// FIXME: Remove static method call
109-
$batch = \OC_User::getDisplayNames($pattern, $limit, $offset);
135+
$batch = $this->userManager->search('');
110136
}
111137

112-
foreach ($batch as $uid => $displayname) {
113-
$users[] = $this->formatUserForIndex($this->userManager->get($uid));
138+
foreach ($batch as $user) {
139+
$users[] = $this->formatUserForIndex($user);
114140
}
141+
115142
} else {
116-
$groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID());
117-
if($gid !== '' && in_array($gid, $groups)) {
118-
$groups = array($gid);
119-
} elseif($gid !== '') {
120-
//don't you try to investigate loops you must not know about
121-
$groups = array();
143+
if($gid !== '' && !in_array($gid, \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()))) {
144+
$gid = '';
122145
}
123-
$batch = \OC_Group::usersInGroups($groups, $pattern, $limit, $offset);
124-
foreach ($batch as $uid) {
125-
$user = $this->userManager->get($uid);
126146

147+
$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
148+
foreach ($batch as $user) {
127149
// Only add the groups, this user is a subadmin of
128150
$userGroups = array_intersect($this->groupManager->getUserGroupIds($user),
129151
\OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()));

tests/lib/user/manager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
namespace Test\User;
1111

1212
class Manager extends \Test\TestCase {
13+
public function testGetBackends() {
14+
$userDummyBackend = $this->getMock('\OC_User_Dummy');
15+
$manager = new \OC\User\Manager();
16+
$manager->registerBackend($userDummyBackend);
17+
$this->assertEquals([$userDummyBackend], $manager->getBackends());
18+
$dummyDatabaseBackend = $this->getMock('\OC_User_Database');
19+
$manager->registerBackend($dummyDatabaseBackend);
20+
$this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends());
21+
}
22+
23+
1324
public function testUserExistsSingleBackendExists() {
1425
/**
1526
* @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend

tests/settings/controller/userscontrollertest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,62 @@ public function testIndex() {
172172
$this->assertEquals($expectedResponse, $response);
173173
}
174174

175+
public function testIndexWithBackend() {
176+
$user = $this->getMockBuilder('\OC\User\User')
177+
->disableOriginalConstructor()->getMock();
178+
$user
179+
->expects($this->exactly(3))
180+
->method('getUID')
181+
->will($this->returnValue('foo'));
182+
$user
183+
->expects($this->once())
184+
->method('getDisplayName')
185+
->will($this->returnValue('M. Foo'));
186+
$user
187+
->method('getLastLogin')
188+
->will($this->returnValue(500));
189+
$user
190+
->method('getHome')
191+
->will($this->returnValue('/home/foo'));
192+
$user
193+
->expects($this->once())
194+
->method('getBackendClassName')
195+
->will($this->returnValue('OC_User_Database'));
196+
$this->container['UserManager']
197+
->expects($this->once())
198+
->method('getBackends')
199+
->will($this->returnValue([new \OC_User_Dummy(), new \OC_User_Database()]));
200+
$this->container['UserManager']
201+
->expects($this->once())
202+
->method('clearBackends');
203+
$this->container['UserManager']
204+
->expects($this->once())
205+
->method('registerBackend')
206+
->with(new \OC_User_Dummy());
207+
$this->container['UserManager']
208+
->expects($this->once())
209+
->method('search')
210+
->with('')
211+
->will($this->returnValue([$user]));
212+
213+
$expectedResponse = new DataResponse(
214+
array(
215+
0 => array(
216+
'name' => 'foo',
217+
'displayname' => 'M. Foo',
218+
'groups' => null,
219+
'subadmin' => array(),
220+
'quota' => null,
221+
'storageLocation' => '/home/foo',
222+
'lastLogin' => 500,
223+
'backend' => 'OC_User_Database'
224+
)
225+
)
226+
);
227+
$response = $this->usersController->index(0, 10, '','', 'OC_User_Dummy');
228+
$this->assertEquals($expectedResponse, $response);
229+
}
230+
175231
/**
176232
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked
177233
* to test for subadmins. Thus the test always assumes you have admin permissions...

0 commit comments

Comments
 (0)