Merge pull request #3259 from nextcloud/add-profile-data-to-provisioning-api-stable11

Add profile data to provisioning api stable11
This commit is contained in:
Lukas Reschke 2017-02-10 13:00:04 +01:00 committed by GitHub
commit 592af1fff1
6 changed files with 234 additions and 48 deletions

View File

@ -45,6 +45,7 @@ return [
['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'],
['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'],
['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'],
['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'],
['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'],

View File

@ -29,6 +29,7 @@
namespace OCA\Provisioning_API\Controller;
use OC\Accounts\AccountManager;
use \OC_Helper;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
@ -53,6 +54,8 @@ class UsersController extends OCSController {
private $groupManager;
/** @var IUserSession */
private $userSession;
/** @var AccountManager */
private $accountManager;
/** @var ILogger */
private $logger;
@ -63,6 +66,7 @@ class UsersController extends OCSController {
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IUserSession $userSession
* @param AccountManager $accountManager
* @param ILogger $logger
*/
public function __construct($appName,
@ -71,6 +75,7 @@ class UsersController extends OCSController {
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
AccountManager $accountManager,
ILogger $logger) {
parent::__construct($appName, $request);
@ -78,6 +83,7 @@ class UsersController extends OCSController {
$this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->accountManager = $accountManager;
$this->logger = $logger;
}
@ -107,7 +113,7 @@ class UsersController extends OCSController {
}
if($offset === null) {
$offset = 0;
$offset = 0;
}
$users = [];
@ -159,7 +165,7 @@ class UsersController extends OCSController {
throw new OCSException('no group specified (required for subadmins)', 106);
}
}
try {
$newUser = $this->userManager->createUser($userid, $password);
$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
@ -188,6 +194,42 @@ class UsersController extends OCSController {
* @throws OCSException
*/
public function getUser($userId) {
$data = $this->getUserData($userId);
return new DataResponse($data);
}
/**
* @NoAdminRequired
* @NoSubAdminRequired
*
* gets user info from the currently logged in user
*
* @return DataResponse
* @throws OCSException
*/
public function getCurrentUser() {
$user = $this->userSession->getUser();
if ($user) {
$data = $this->getUserData($user->getUID());
// rename "displayname" to "display-name" only for this call to keep
// the API stable.
$data['display-name'] = $data['displayname'];
unset($data['displayname']);
return new DataResponse($data);
}
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
/**
* creates a array with all user data
*
* @param $userId
* @return array
* @throws OCSException
*/
protected function getUserData($userId) {
$currentLoggedInUser = $this->userSession->getUser();
$data = [];
@ -209,12 +251,19 @@ class UsersController extends OCSController {
}
}
$userAccount = $this->accountManager->getUser($targetUserObject);
// Find the data
$data['id'] = $targetUserObject->getUID();
$data['quota'] = $this->fillStorageInfo($userId);
$data['email'] = $targetUserObject->getEMailAddress();
$data['displayname'] = $targetUserObject->getDisplayName();
$data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
$data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
$data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
$data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
return new DataResponse($data);
return $data;
}
/**
@ -435,7 +484,7 @@ class UsersController extends OCSController {
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
}
}
/**

View File

@ -29,7 +29,10 @@
namespace OCA\Provisioning_API\Tests\Controller;
use OC\Accounts\AccountManager;
use OCA\Provisioning_API\Controller\UsersController;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\IUserSession;
@ -38,7 +41,7 @@ use Test\TestCase as OriginalTest;
use OCP\ILogger;
class UsersControllerTest extends OriginalTest {
/** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */
protected $userManager;
/** @var IConfig | PHPUnit_Framework_MockObject_MockObject */
@ -51,6 +54,10 @@ class UsersControllerTest extends OriginalTest {
protected $logger;
/** @var UsersController | PHPUnit_Framework_MockObject_MockObject */
protected $api;
/** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */
protected $accountManager;
/** @var IRequest | PHPUnit_Framework_MockObject_MockObject */
protected $request;
protected function tearDown() {
parent::tearDown();
@ -74,17 +81,21 @@ class UsersControllerTest extends OriginalTest {
$this->logger = $this->getMockBuilder('OCP\ILogger')
->disableOriginalConstructor()
->getMock();
$request = $this->getMockBuilder('OCP\IRequest')
$this->request = $this->getMockBuilder('OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->accountManager = $this->getMockBuilder(AccountManager::class)
->disableOriginalConstructor()
->getMock();
$this->api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
->setConstructorArgs([
'provisioning_api',
$request,
$this->request,
$this->userManager,
$this->config,
$this->groupManager,
$this->userSession,
$this->accountManager,
$this->logger,
])
->setMethods(['fillStorageInfo'])
@ -621,7 +632,7 @@ class UsersControllerTest extends OriginalTest {
$this->api->getUser('UserToGet');
}
public function testGetUserAsAdmin() {
public function testGetUserDataAsAdmin() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@ -649,6 +660,16 @@ class UsersControllerTest extends OriginalTest {
->method('isAdmin')
->with('admin')
->will($this->returnValue(true));
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser)
->willReturn(
[
AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
$this->config
->expects($this->at(0))
->method('getUserValue')
@ -663,17 +684,26 @@ class UsersControllerTest extends OriginalTest {
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('Demo User'));
$targetUser
->expects($this->once())
->method('getUID')
->will($this->returnValue('UID'));
$expected = [
'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'displayname' => 'Demo User',
'phone' => 'phone',
'address' => 'address',
'webpage' => 'website',
'twitter' => 'twitter'
];
$this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
$this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UserToGet']));
}
public function testGetUserAsSubAdminAndUserIsAccessible() {
public function testGetUserDataAsSubAdminAndUserIsAccessible() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@ -728,14 +758,33 @@ class UsersControllerTest extends OriginalTest {
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('Demo User'));
$targetUser
->expects($this->once())
->method('getUID')
->will($this->returnValue('UID'));
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser)
->willReturn(
[
AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
$expected = [
'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'displayname' => 'Demo User',
'phone' => 'phone',
'address' => 'address',
'webpage' => 'website',
'twitter' => 'twitter'
];
$this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
$this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UserToGet']));
}
@ -743,7 +792,7 @@ class UsersControllerTest extends OriginalTest {
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 997
*/
public function testGetUserAsSubAdminAndUserIsNotAccessible() {
public function testGetUserDataAsSubAdminAndUserIsNotAccessible() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@ -781,10 +830,10 @@ class UsersControllerTest extends OriginalTest {
->method('getSubAdmin')
->will($this->returnValue($subAdminManager));
$this->api->getUser('UserToGet');
$this->invokePrivate($this->api, 'getUserData', ['UserToGet']);
}
public function testGetUserAsSubAdminSelfLookup() {
public function testGetUserDataAsSubAdminSelfLookup() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@ -834,13 +883,32 @@ class UsersControllerTest extends OriginalTest {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('subadmin@owncloud.org'));
$targetUser
->expects($this->once())
->method('getUID')
->will($this->returnValue('UID'));
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser)
->willReturn(
[
AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
$expected = [
'id' => 'UID',
'quota' => ['DummyValue'],
'email' => 'subadmin@owncloud.org',
'displayname' => 'Subadmin User',
'phone' => 'phone',
'address' => 'address',
'webpage' => 'website',
'twitter' => 'twitter'
];
$this->assertEquals($expected, $this->api->getUser('subadmin')->getData());
$this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['subadmin']));
}
public function testEditUserRegularUserSelfEditChangeDisplayName() {
@ -2293,4 +2361,105 @@ class UsersControllerTest extends OriginalTest {
$this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
}
public function testGetCurrentUserLoggedIn() {
$user = $this->getMock(IUser::class);
$user->expects($this->once())->method('getUID')->willReturn('UID');
$this->userSession->expects($this->once())->method('getUser')
->willReturn($user);
/** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
->setConstructorArgs([
'provisioning_api',
$this->request,
$this->userManager,
$this->config,
$this->groupManager,
$this->userSession,
$this->accountManager,
$this->logger,
])
->setMethods(['getUserData'])
->getMock();
$api->expects($this->once())->method('getUserData')->with('UID')
->willReturn(
[
'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'displayname' => 'Demo User',
'phone' => 'phone',
'address' => 'address',
'webpage' => 'website',
'twitter' => 'twitter'
]
);
$expected = [
'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'phone' => 'phone',
'address' => 'address',
'webpage' => 'website',
'twitter' => 'twitter',
'display-name' => 'Demo User'
];
$this->assertSame($expected, $api->getCurrentUser()->getData());
}
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
*/
public function testGetCurrentUserNotLoggedIn() {
$this->userSession->expects($this->once())->method('getUser')
->willReturn(null);
$this->api->getCurrentUser();
}
public function testGetUser() {
/** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
->setConstructorArgs([
'provisioning_api',
$this->request,
$this->userManager,
$this->config,
$this->groupManager,
$this->userSession,
$this->accountManager,
$this->logger,
])
->setMethods(['getUserData'])
->getMock();
$expected = [
'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'phone' => 'phone',
'address' => 'address',
'webpage' => 'website',
'twitter' => 'twitter',
'displayname' => 'Demo User'
];
$api->expects($this->once())->method('getUserData')
->with('uid')
->willReturn($expected);
$this->assertSame($expected, $api->getUser('uid')->getData());
}
}

View File

@ -105,20 +105,6 @@ class OCSController extends \OCP\AppFramework\OCSController {
return new DataResponse($result);
}
/**
* @NoAdminRequired
* @return DataResponse
*/
public function getCurrentUser() {
$userObject = $this->userSession->getUser();
$data = [
'id' => $userObject->getUID(),
'display-name' => $userObject->getDisplayName(),
'email' => $userObject->getEMailAddress(),
];
return new DataResponse($data);
}
/**
* @PublicPage
*

View File

@ -58,7 +58,6 @@ $application->registerRoutes($this, [
],
'ocs' => [
['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
['root' => '/cloud', 'name' => 'OCS#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],

View File

@ -116,24 +116,6 @@ class OCSControllerTest extends TestCase {
$this->assertEquals($expected, $this->controller->getCapabilities());
}
public function testGetCurrentUser() {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('uid');
$user->method('getDisplayName')->willReturn('displayName');
$user->method('getEMailAddress')->willReturn('e@mail.com');
$this->userSession->method('getUser')
->willReturn($user);
$expected = new DataResponse([
'id' => 'uid',
'display-name' => 'displayName',
'email' => 'e@mail.com',
]);
$this->assertEquals($expected, $this->controller->getCurrentUser());
}
public function testPersonCheckValid() {
$this->request->method('getRemoteAddress')
->willReturn('1.2.3.4');