Skip to content

Commit 4c13918

Browse files
committed
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for #12620. For example: ````json [{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}] ```
1 parent 25a87d4 commit 4c13918

File tree

6 files changed

+152
-89
lines changed

6 files changed

+152
-89
lines changed

lib/private/user/user.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ public function getHome() {
217217
return $this->home;
218218
}
219219

220+
/**
221+
* Get the name of the backend class the user is connected with
222+
*
223+
* @return string
224+
*/
225+
public function getBackendClassName() {
226+
return get_class($this->backend);
227+
}
228+
220229
/**
221230
* check if the backend allows the user to change his avatar on Personal page
222231
*

lib/public/iuser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ public function setPassword($password, $recoveryPassword = null);
6868
*/
6969
public function getHome();
7070

71+
/**
72+
* Get the name of the backend class the user is connected with
73+
*
74+
* @return string
75+
*/
76+
public function getBackendClassName();
77+
7178
/**
7279
* check if the backend allows the user to change his avatar on Personal page
7380
*

settings/controller/userscontroller.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\IGroupManager;
1919
use OCP\IL10N;
2020
use OCP\IRequest;
21+
use OCP\IUser;
2122
use OCP\IUserManager;
2223
use OCP\IUserSession;
2324

@@ -65,9 +66,27 @@ public function __construct($appName,
6566
$this->l10n = $l10n;
6667
}
6768

69+
/**
70+
* @param IUser $user
71+
* @param array $userGroups
72+
* @return array
73+
*/
74+
private function formatUserForIndex(IUser $user, array $userGroups = null) {
75+
return array(
76+
'name' => $user->getUID(),
77+
'displayname' => $user->getDisplayName(),
78+
'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
79+
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($user->getUID()),
80+
'quota' => $this->config->getUserValue($user->getUID(), 'files', 'quota', 'default'),
81+
'storageLocation' => $user->getHome(),
82+
'lastLogin' => $user->getLastLogin(),
83+
'backend' => $user->getBackendClassName()
84+
);
85+
}
86+
6887
/**
6988
* @NoAdminRequired
70-
* @NoCSRFRequired
89+
*
7190
* @param int $offset
7291
* @param int $limit
7392
* @param string $gid
@@ -91,16 +110,7 @@ public function index($offset = 0, $limit = 10, $gid = '', $pattern = '') {
91110
}
92111

93112
foreach ($batch as $uid => $displayname) {
94-
$user = $this->userManager->get($uid);
95-
$users[] = array(
96-
'name' => $uid,
97-
'displayname' => $displayname,
98-
'groups' => $this->groupManager->getUserGroupIds($user),
99-
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($uid),
100-
'quota' => $this->config->getUserValue($uid, 'files', 'quota', 'default'),
101-
'storageLocation' => $user->getHome(),
102-
'lastLogin' => $user->getLastLogin(),
103-
);
113+
$users[] = $this->formatUserForIndex($this->userManager->get($uid));
104114
}
105115
} else {
106116
$groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID());
@@ -115,20 +125,13 @@ public function index($offset = 0, $limit = 10, $gid = '', $pattern = '') {
115125
$user = $this->userManager->get($uid);
116126

117127
// Only add the groups, this user is a subadmin of
118-
$userGroups = array_intersect($this->groupManager->getUserGroupIds($user), \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()));
119-
$users[] = array(
120-
'name' => $uid,
121-
'displayname' => $user->getDisplayName(),
122-
'groups' => $userGroups,
123-
'quota' => $this->config->getUserValue($uid, 'files', 'quota', 'default'),
124-
'storageLocation' => $user->getHome(),
125-
'lastLogin' => $user->getLastLogin(),
126-
);
128+
$userGroups = array_intersect($this->groupManager->getUserGroupIds($user),
129+
\OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()));
130+
$users[] = $this->formatUserForIndex($user, $userGroups);
127131
}
128132
}
129133

130-
// FIXME: That assignment on "data" is uneeded here - JS should be adjusted
131-
return new DataResponse(array('data' => $users, 'status' => 'success'));
134+
return new DataResponse($users);
132135
}
133136

134137
/**
@@ -142,7 +145,7 @@ public function index($offset = 0, $limit = 10, $gid = '', $pattern = '') {
142145
* TODO: Tidy up and write unit tests - code is mainly static method calls
143146
*/
144147
public function create($username, $password, array $groups) {
145-
148+
146149
if (!$this->isAdmin) {
147150
if (!empty($groups)) {
148151
foreach ($groups as $key => $group) {
@@ -247,7 +250,6 @@ public function destroy($id) {
247250
),
248251
Http::STATUS_FORBIDDEN
249252
);
250-
251253
}
252254

253255
}

settings/js/users/users.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -331,35 +331,34 @@ var UserList = {
331331
function (result) {
332332
var loadedUsers = 0;
333333
var trs = [];
334-
if (result.status === 'success') {
335-
//The offset does not mirror the amount of users available,
336-
//because it is backend-dependent. For correct retrieval,
337-
//always the limit(requested amount of users) needs to be added.
338-
$.each(result.data, function (index, user) {
339-
if(UserList.has(user.name)) {
340-
return true;
341-
}
342-
var $tr = UserList.add(user.name, user.displayname, user.groups, user.subadmin, user.quota, user.storageLocation, user.lastLogin, false);
343-
$tr.addClass('appear transparent');
344-
trs.push($tr);
345-
loadedUsers++;
346-
});
347-
if (result.data.length > 0) {
348-
UserList.doSort();
349-
$userList.siblings('.loading').css('visibility', 'hidden');
350-
}
351-
else {
352-
UserList.noMoreEntries = true;
353-
$userList.siblings('.loading').remove();
334+
//The offset does not mirror the amount of users available,
335+
//because it is backend-dependent. For correct retrieval,
336+
//always the limit(requested amount of users) needs to be added.
337+
$.each(result, function (index, user) {
338+
if(UserList.has(user.name)) {
339+
return true;
354340
}
355-
UserList.offset += loadedUsers;
356-
// animate
357-
setTimeout(function() {
358-
for (var i = 0; i < trs.length; i++) {
359-
trs[i].removeClass('transparent');
360-
}
361-
}, 0);
341+
var $tr = UserList.add(user.name, user.displayname, user.groups, user.subadmin, user.quota, user.storageLocation, user.lastLogin, false);
342+
$tr.addClass('appear transparent');
343+
trs.push($tr);
344+
loadedUsers++;
345+
});
346+
if (result.length > 0) {
347+
UserList.doSort();
348+
$userList.siblings('.loading').css('visibility', 'hidden');
362349
}
350+
else {
351+
UserList.noMoreEntries = true;
352+
$userList.siblings('.loading').remove();
353+
}
354+
UserList.offset += loadedUsers;
355+
// animate
356+
setTimeout(function() {
357+
for (var i = 0; i < trs.length; i++) {
358+
trs[i].removeClass('transparent');
359+
}
360+
}, 0);
361+
}).always(function() {
363362
UserList.updating = false;
364363
});
365364
},

tests/lib/user/user.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ public function testGetHome() {
216216
$this->assertEquals('/home/foo', $user->getHome());
217217
}
218218

219+
public function testGetBackendClassName() {
220+
$user = new \OC\User\User('foo', new \OC_User_Dummy());
221+
$this->assertEquals('OC_User_Dummy', $user->getBackendClassName());
222+
$user = new \OC\User\User('foo', new \OC_User_Database());
223+
$this->assertEquals('OC_User_Database', $user->getBackendClassName());
224+
}
225+
219226
public function testGetHomeNotSupported() {
220227
/**
221228
* @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend

tests/settings/controller/userscontrollertest.php

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,68 @@ protected function setUp() {
5454
* to test for subadmins. Thus the test always assumes you have admin permissions...
5555
*/
5656
public function testIndex() {
57-
$admin = $this->getMockBuilder('\OC\User\User')
58-
->disableOriginalConstructor()->getMock();
59-
$admin
60-
->method('getLastLogin')
61-
->will($this->returnValue(12));
62-
$admin
63-
->method('getHome')
64-
->will($this->returnValue('/home/admin'));
6557
$foo = $this->getMockBuilder('\OC\User\User')
6658
->disableOriginalConstructor()->getMock();
59+
$foo
60+
->expects($this->exactly(3))
61+
->method('getUID')
62+
->will($this->returnValue('foo'));
63+
$foo
64+
->expects($this->once())
65+
->method('getDisplayName')
66+
->will($this->returnValue('M. Foo'));
6767
$foo
6868
->method('getLastLogin')
6969
->will($this->returnValue(500));
7070
$foo
7171
->method('getHome')
7272
->will($this->returnValue('/home/foo'));
73+
$foo
74+
->expects($this->once())
75+
->method('getBackendClassName')
76+
->will($this->returnValue('OC_User_Database'));
77+
$admin = $this->getMockBuilder('\OC\User\User')
78+
->disableOriginalConstructor()->getMock();
79+
$admin
80+
->expects($this->exactly(3))
81+
->method('getUID')
82+
->will($this->returnValue('admin'));
83+
$admin
84+
->expects($this->once())
85+
->method('getDisplayName')
86+
->will($this->returnValue('S. Admin'));
87+
$admin
88+
->expects($this->once())
89+
->method('getLastLogin')
90+
->will($this->returnValue(12));
91+
$admin
92+
->expects($this->once())
93+
->method('getHome')
94+
->will($this->returnValue('/home/admin'));
95+
$admin
96+
->expects($this->once())
97+
->method('getBackendClassName')
98+
->will($this->returnValue('OC_User_Dummy'));
7399
$bar = $this->getMockBuilder('\OC\User\User')
74100
->disableOriginalConstructor()->getMock();
101+
$bar
102+
->expects($this->exactly(3))
103+
->method('getUID')
104+
->will($this->returnValue('bar'));
105+
$bar
106+
->expects($this->once())
107+
->method('getDisplayName')
108+
->will($this->returnValue('B. Ar'));
75109
$bar
76110
->method('getLastLogin')
77111
->will($this->returnValue(3999));
78112
$bar
79113
->method('getHome')
80114
->will($this->returnValue('/home/bar'));
115+
$bar
116+
->expects($this->once())
117+
->method('getBackendClassName')
118+
->will($this->returnValue('OC_User_Dummy'));
81119

82120
$this->container['GroupManager']
83121
->expects($this->once())
@@ -98,36 +136,36 @@ public function testIndex() {
98136

99137
$expectedResponse = new DataResponse(
100138
array(
101-
'status' => 'success',
102-
'data' => array(
103-
0 => array(
104-
'name' => 'foo',
105-
'displayname' => 'M. Foo',
106-
'groups' => array('Users', 'Support'),
107-
'subadmin' => array(),
108-
'quota' => 1024,
109-
'storageLocation' => '/home/foo',
110-
'lastLogin' => 500
111-
),
112-
1 => array(
113-
'name' => 'admin',
114-
'displayname' => 'S. Admin',
115-
'groups' => array('admins', 'Support'),
116-
'subadmin' => array(),
117-
'quota' => 404,
118-
'storageLocation' => '/home/admin',
119-
'lastLogin' => 12
120-
),
121-
2 => array(
122-
'name' => 'bar',
123-
'displayname' => 'B. Ar',
124-
'groups' => array('External Users'),
125-
'subadmin' => array(),
126-
'quota' => 2323,
127-
'storageLocation' => '/home/bar',
128-
'lastLogin' => 3999
129-
),
130-
)
139+
0 => array(
140+
'name' => 'foo',
141+
'displayname' => 'M. Foo',
142+
'groups' => array('Users', 'Support'),
143+
'subadmin' => array(),
144+
'quota' => 1024,
145+
'storageLocation' => '/home/foo',
146+
'lastLogin' => 500,
147+
'backend' => 'OC_User_Database'
148+
),
149+
1 => array(
150+
'name' => 'admin',
151+
'displayname' => 'S. Admin',
152+
'groups' => array('admins', 'Support'),
153+
'subadmin' => array(),
154+
'quota' => 404,
155+
'storageLocation' => '/home/admin',
156+
'lastLogin' => 12,
157+
'backend' => 'OC_User_Dummy'
158+
),
159+
2 => array(
160+
'name' => 'bar',
161+
'displayname' => 'B. Ar',
162+
'groups' => array('External Users'),
163+
'subadmin' => array(),
164+
'quota' => 2323,
165+
'storageLocation' => '/home/bar',
166+
'lastLogin' => 3999,
167+
'backend' => 'OC_User_Dummy'
168+
),
131169
)
132170
);
133171
$response = $this->usersController->index(0, 10, 'pattern');
@@ -341,4 +379,5 @@ public function testDestroyUnsuccessful() {
341379
$response = $this->usersController->destroy('UserToDelete');
342380
$this->assertEquals($expectedResponse, $response);
343381
}
382+
344383
}

0 commit comments

Comments
 (0)