Merge pull request #12711 from owncloud/add-backend-to-rest-index

Expose backend type via REST API
This commit is contained in:
Lukas Reschke 2014-12-10 11:56:45 +01:00
commit 5398bbdc00
7 changed files with 155 additions and 89 deletions

View File

@ -219,6 +219,15 @@ class User implements IUser {
return $this->home; return $this->home;
} }
/**
* Get the name of the backend class the user is connected with
*
* @return string
*/
public function getBackendClassName() {
return get_class($this->backend);
}
/** /**
* check if the backend allows the user to change his avatar on Personal page * check if the backend allows the user to change his avatar on Personal page
* *

View File

@ -68,6 +68,13 @@ interface IUser {
*/ */
public function getHome(); public function getHome();
/**
* Get the name of the backend class the user is connected with
*
* @return string
*/
public function getBackendClassName();
/** /**
* check if the backend allows the user to change his avatar on Personal page * check if the backend allows the user to change his avatar on Personal page
* *

View File

@ -18,6 +18,7 @@ use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
@ -65,9 +66,27 @@ class UsersController extends Controller {
$this->l10n = $l10n; $this->l10n = $l10n;
} }
/**
* @param IUser $user
* @param array $userGroups
* @return array
*/
private function formatUserForIndex(IUser $user, array $userGroups = null) {
return array(
'name' => $user->getUID(),
'displayname' => $user->getDisplayName(),
'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($user->getUID()),
'quota' => $this->config->getUserValue($user->getUID(), 'files', 'quota', 'default'),
'storageLocation' => $user->getHome(),
'lastLogin' => $user->getLastLogin(),
'backend' => $user->getBackendClassName()
);
}
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired *
* @param int $offset * @param int $offset
* @param int $limit * @param int $limit
* @param string $gid * @param string $gid
@ -91,16 +110,7 @@ class UsersController extends Controller {
} }
foreach ($batch as $uid => $displayname) { foreach ($batch as $uid => $displayname) {
$user = $this->userManager->get($uid); $users[] = $this->formatUserForIndex($this->userManager->get($uid));
$users[] = array(
'name' => $uid,
'displayname' => $displayname,
'groups' => $this->groupManager->getUserGroupIds($user),
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($uid),
'quota' => $this->config->getUserValue($uid, 'files', 'quota', 'default'),
'storageLocation' => $user->getHome(),
'lastLogin' => $user->getLastLogin(),
);
} }
} else { } else {
$groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()); $groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID());
@ -115,20 +125,13 @@ class UsersController extends Controller {
$user = $this->userManager->get($uid); $user = $this->userManager->get($uid);
// Only add the groups, this user is a subadmin of // Only add the groups, this user is a subadmin of
$userGroups = array_intersect($this->groupManager->getUserGroupIds($user), \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID())); $userGroups = array_intersect($this->groupManager->getUserGroupIds($user),
$users[] = array( \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()));
'name' => $uid, $users[] = $this->formatUserForIndex($user, $userGroups);
'displayname' => $user->getDisplayName(),
'groups' => $userGroups,
'quota' => $this->config->getUserValue($uid, 'files', 'quota', 'default'),
'storageLocation' => $user->getHome(),
'lastLogin' => $user->getLastLogin(),
);
} }
} }
// FIXME: That assignment on "data" is uneeded here - JS should be adjusted return new DataResponse($users);
return new DataResponse(array('data' => $users, 'status' => 'success'));
} }
/** /**
@ -247,7 +250,6 @@ class UsersController extends Controller {
), ),
Http::STATUS_FORBIDDEN Http::STATUS_FORBIDDEN
); );
} }
} }

View File

@ -331,11 +331,10 @@ var UserList = {
function (result) { function (result) {
var loadedUsers = 0; var loadedUsers = 0;
var trs = []; var trs = [];
if (result.status === 'success') {
//The offset does not mirror the amount of users available, //The offset does not mirror the amount of users available,
//because it is backend-dependent. For correct retrieval, //because it is backend-dependent. For correct retrieval,
//always the limit(requested amount of users) needs to be added. //always the limit(requested amount of users) needs to be added.
$.each(result.data, function (index, user) { $.each(result, function (index, user) {
if(UserList.has(user.name)) { if(UserList.has(user.name)) {
return true; return true;
} }
@ -344,7 +343,7 @@ var UserList = {
trs.push($tr); trs.push($tr);
loadedUsers++; loadedUsers++;
}); });
if (result.data.length > 0) { if (result.length > 0) {
UserList.doSort(); UserList.doSort();
$userList.siblings('.loading').css('visibility', 'hidden'); $userList.siblings('.loading').css('visibility', 'hidden');
} }
@ -359,7 +358,7 @@ var UserList = {
trs[i].removeClass('transparent'); trs[i].removeClass('transparent');
} }
}, 0); }, 0);
} }).always(function() {
UserList.updating = false; UserList.updating = false;
}); });
}, },

View File

@ -78,6 +78,9 @@ class SimpleUserForTesting implements \OCP\IUser {
public function getHome() { public function getHome() {
} }
public function getBackendClassName() {
}
public function canChangeAvatar() { public function canChangeAvatar() {
} }

View File

@ -215,6 +215,13 @@ class User extends \Test\TestCase {
$this->assertEquals('/home/foo', $user->getHome()); $this->assertEquals('/home/foo', $user->getHome());
} }
public function testGetBackendClassName() {
$user = new \OC\User\User('foo', new \OC_User_Dummy());
$this->assertEquals('OC_User_Dummy', $user->getBackendClassName());
$user = new \OC\User\User('foo', new \OC_User_Database());
$this->assertEquals('OC_User_Database', $user->getBackendClassName());
}
public function testGetHomeNotSupported() { public function testGetHomeNotSupported() {
/** /**
* @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend

View File

@ -54,30 +54,68 @@ class UsersControllerTest extends \Test\TestCase {
* to test for subadmins. Thus the test always assumes you have admin permissions... * to test for subadmins. Thus the test always assumes you have admin permissions...
*/ */
public function testIndex() { public function testIndex() {
$admin = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$admin
->method('getLastLogin')
->will($this->returnValue(12));
$admin
->method('getHome')
->will($this->returnValue('/home/admin'));
$foo = $this->getMockBuilder('\OC\User\User') $foo = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$foo
->expects($this->exactly(3))
->method('getUID')
->will($this->returnValue('foo'));
$foo
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('M. Foo'));
$foo $foo
->method('getLastLogin') ->method('getLastLogin')
->will($this->returnValue(500)); ->will($this->returnValue(500));
$foo $foo
->method('getHome') ->method('getHome')
->will($this->returnValue('/home/foo')); ->will($this->returnValue('/home/foo'));
$foo
->expects($this->once())
->method('getBackendClassName')
->will($this->returnValue('OC_User_Database'));
$admin = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$admin
->expects($this->exactly(3))
->method('getUID')
->will($this->returnValue('admin'));
$admin
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('S. Admin'));
$admin
->expects($this->once())
->method('getLastLogin')
->will($this->returnValue(12));
$admin
->expects($this->once())
->method('getHome')
->will($this->returnValue('/home/admin'));
$admin
->expects($this->once())
->method('getBackendClassName')
->will($this->returnValue('OC_User_Dummy'));
$bar = $this->getMockBuilder('\OC\User\User') $bar = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$bar
->expects($this->exactly(3))
->method('getUID')
->will($this->returnValue('bar'));
$bar
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('B. Ar'));
$bar $bar
->method('getLastLogin') ->method('getLastLogin')
->will($this->returnValue(3999)); ->will($this->returnValue(3999));
$bar $bar
->method('getHome') ->method('getHome')
->will($this->returnValue('/home/bar')); ->will($this->returnValue('/home/bar'));
$bar
->expects($this->once())
->method('getBackendClassName')
->will($this->returnValue('OC_User_Dummy'));
$this->container['GroupManager'] $this->container['GroupManager']
->expects($this->once()) ->expects($this->once())
@ -98,8 +136,6 @@ class UsersControllerTest extends \Test\TestCase {
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
array( array(
'status' => 'success',
'data' => array(
0 => array( 0 => array(
'name' => 'foo', 'name' => 'foo',
'displayname' => 'M. Foo', 'displayname' => 'M. Foo',
@ -107,7 +143,8 @@ class UsersControllerTest extends \Test\TestCase {
'subadmin' => array(), 'subadmin' => array(),
'quota' => 1024, 'quota' => 1024,
'storageLocation' => '/home/foo', 'storageLocation' => '/home/foo',
'lastLogin' => 500 'lastLogin' => 500,
'backend' => 'OC_User_Database'
), ),
1 => array( 1 => array(
'name' => 'admin', 'name' => 'admin',
@ -116,7 +153,8 @@ class UsersControllerTest extends \Test\TestCase {
'subadmin' => array(), 'subadmin' => array(),
'quota' => 404, 'quota' => 404,
'storageLocation' => '/home/admin', 'storageLocation' => '/home/admin',
'lastLogin' => 12 'lastLogin' => 12,
'backend' => 'OC_User_Dummy'
), ),
2 => array( 2 => array(
'name' => 'bar', 'name' => 'bar',
@ -125,10 +163,10 @@ class UsersControllerTest extends \Test\TestCase {
'subadmin' => array(), 'subadmin' => array(),
'quota' => 2323, 'quota' => 2323,
'storageLocation' => '/home/bar', 'storageLocation' => '/home/bar',
'lastLogin' => 3999 'lastLogin' => 3999,
'backend' => 'OC_User_Dummy'
), ),
) )
)
); );
$response = $this->usersController->index(0, 10, 'pattern'); $response = $this->usersController->index(0, 10, 'pattern');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
@ -341,4 +379,5 @@ class UsersControllerTest extends \Test\TestCase {
$response = $this->usersController->destroy('UserToDelete'); $response = $this->usersController->destroy('UserToDelete');
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }
} }