Skip to content

Commit ffe2543

Browse files
authored
Merge pull request #3232 from nextcloud/add-profile-data-to-provisioning-api
add data from the users profile to the provisioning api
2 parents 5873a0a + 5086335 commit ffe2543

6 files changed

Lines changed: 177 additions & 39 deletions

File tree

apps/provisioning_api/appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'],
4646
['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'],
4747
['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'],
48+
['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
4849
['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'],
4950
['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'],
5051
['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'],

apps/provisioning_api/lib/Controller/UsersController.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
namespace OCA\Provisioning_API\Controller;
3131

32+
use OC\Accounts\AccountManager;
3233
use \OC_Helper;
3334
use OCP\AppFramework\Http\DataResponse;
3435
use OCP\AppFramework\OCS\OCSException;
@@ -53,6 +54,8 @@ class UsersController extends OCSController {
5354
private $groupManager;
5455
/** @var IUserSession */
5556
private $userSession;
57+
/** @var AccountManager */
58+
private $accountManager;
5659
/** @var ILogger */
5760
private $logger;
5861

@@ -63,6 +66,7 @@ class UsersController extends OCSController {
6366
* @param IConfig $config
6467
* @param IGroupManager $groupManager
6568
* @param IUserSession $userSession
69+
* @param AccountManager $accountManager
6670
* @param ILogger $logger
6771
*/
6872
public function __construct($appName,
@@ -71,13 +75,15 @@ public function __construct($appName,
7175
IConfig $config,
7276
IGroupManager $groupManager,
7377
IUserSession $userSession,
78+
AccountManager $accountManager,
7479
ILogger $logger) {
7580
parent::__construct($appName, $request);
7681

7782
$this->userManager = $userManager;
7883
$this->config = $config;
7984
$this->groupManager = $groupManager;
8085
$this->userSession = $userSession;
86+
$this->accountManager = $accountManager;
8187
$this->logger = $logger;
8288
}
8389

@@ -107,7 +113,7 @@ public function getUsers($search = '', $limit = null, $offset = null) {
107113
}
108114

109115
if($offset === null) {
110-
$offset = 0;
116+
$offset = 0;
111117
}
112118

113119
$users = [];
@@ -159,7 +165,7 @@ public function addUser($userid, $password, $groups = null) {
159165
throw new OCSException('no group specified (required for subadmins)', 106);
160166
}
161167
}
162-
168+
163169
try {
164170
$newUser = $this->userManager->createUser($userid, $password);
165171
$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
@@ -209,14 +215,45 @@ public function getUser($userId) {
209215
}
210216
}
211217

218+
$userAccount = $this->accountManager->getUser($targetUserObject);
219+
212220
// Find the data
221+
$data['id'] = $targetUserObject->getUID();
213222
$data['quota'] = $this->fillStorageInfo($userId);
214223
$data['email'] = $targetUserObject->getEMailAddress();
215224
$data['displayname'] = $targetUserObject->getDisplayName();
225+
$data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
226+
$data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
227+
$data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
228+
$data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
216229

217230
return new DataResponse($data);
218231
}
219232

233+
/**
234+
* @NoAdminRequired
235+
* @NoSubAdminRequired
236+
*
237+
* gets user info from the currently logged in user
238+
*
239+
* @return DataResponse
240+
* @throws OCSException
241+
*/
242+
public function getCurrentUser() {
243+
$user = $this->userSession->getUser();
244+
if ($user) {
245+
$result = $this->getUser($user->getUID());
246+
// rename "displayname" to "display-name" only for this call to keep
247+
// the API stable.
248+
$result['display-name'] = $result['displayname'];
249+
unset($result['displayname']);
250+
return $result;
251+
252+
}
253+
254+
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
255+
}
256+
220257
/**
221258
* @NoAdminRequired
222259
* @NoSubAdminRequired
@@ -436,7 +473,7 @@ public function getUsersGroups($userId) {
436473
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
437474
}
438475
}
439-
476+
440477
}
441478

442479
/**

apps/provisioning_api/tests/Controller/UsersControllerTest.php

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929

3030
namespace OCA\Provisioning_API\Tests\Controller;
3131

32+
use OC\Accounts\AccountManager;
3233
use OCA\Provisioning_API\Controller\UsersController;
3334
use OCP\AppFramework\Http\DataResponse;
3435
use OCP\IGroup;
36+
use OCP\IRequest;
3537
use OCP\IUser;
3638
use OCP\IUserManager;
3739
use OCP\IConfig;
@@ -41,7 +43,7 @@
4143
use OCP\ILogger;
4244

4345
class UsersControllerTest extends OriginalTest {
44-
46+
4547
/** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */
4648
protected $userManager;
4749
/** @var IConfig | PHPUnit_Framework_MockObject_MockObject */
@@ -54,6 +56,10 @@ class UsersControllerTest extends OriginalTest {
5456
protected $logger;
5557
/** @var UsersController | PHPUnit_Framework_MockObject_MockObject */
5658
protected $api;
59+
/** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */
60+
protected $accountManager;
61+
/** @var IRequest | PHPUnit_Framework_MockObject_MockObject */
62+
protected $request;
5763

5864
protected function tearDown() {
5965
parent::tearDown();
@@ -77,17 +83,21 @@ protected function setUp() {
7783
$this->logger = $this->getMockBuilder('OCP\ILogger')
7884
->disableOriginalConstructor()
7985
->getMock();
80-
$request = $this->getMockBuilder('OCP\IRequest')
86+
$this->request = $this->getMockBuilder('OCP\IRequest')
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->accountManager = $this->getMockBuilder(AccountManager::class)
8190
->disableOriginalConstructor()
8291
->getMock();
8392
$this->api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
8493
->setConstructorArgs([
8594
'provisioning_api',
86-
$request,
95+
$this->request,
8796
$this->userManager,
8897
$this->config,
8998
$this->groupManager,
9099
$this->userSession,
100+
$this->accountManager,
91101
$this->logger,
92102
])
93103
->setMethods(['fillStorageInfo'])
@@ -652,6 +662,16 @@ public function testGetUserAsAdmin() {
652662
->method('isAdmin')
653663
->with('admin')
654664
->will($this->returnValue(true));
665+
$this->accountManager->expects($this->any())->method('getUser')
666+
->with($targetUser)
667+
->willReturn(
668+
[
669+
AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
670+
AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
671+
AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
672+
AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
673+
]
674+
);
655675
$this->config
656676
->expects($this->at(0))
657677
->method('getUserValue')
@@ -666,12 +686,21 @@ public function testGetUserAsAdmin() {
666686
->expects($this->once())
667687
->method('getDisplayName')
668688
->will($this->returnValue('Demo User'));
689+
$targetUser
690+
->expects($this->once())
691+
->method('getUID')
692+
->will($this->returnValue('UID'));
669693

670694
$expected = [
695+
'id' => 'UID',
671696
'enabled' => 'true',
672697
'quota' => ['DummyValue'],
673698
'email' => 'demo@owncloud.org',
674699
'displayname' => 'Demo User',
700+
'phone' => 'phone',
701+
'address' => 'address',
702+
'webpage' => 'website',
703+
'twitter' => 'twitter'
675704
];
676705
$this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
677706
}
@@ -731,12 +760,31 @@ public function testGetUserAsSubAdminAndUserIsAccessible() {
731760
->expects($this->once())
732761
->method('getDisplayName')
733762
->will($this->returnValue('Demo User'));
763+
$targetUser
764+
->expects($this->once())
765+
->method('getUID')
766+
->will($this->returnValue('UID'));
767+
$this->accountManager->expects($this->any())->method('getUser')
768+
->with($targetUser)
769+
->willReturn(
770+
[
771+
AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
772+
AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
773+
AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
774+
AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
775+
]
776+
);
734777

735778
$expected = [
779+
'id' => 'UID',
736780
'enabled' => 'true',
737781
'quota' => ['DummyValue'],
738782
'email' => 'demo@owncloud.org',
739783
'displayname' => 'Demo User',
784+
'phone' => 'phone',
785+
'address' => 'address',
786+
'webpage' => 'website',
787+
'twitter' => 'twitter'
740788
];
741789
$this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
742790
}
@@ -837,11 +885,30 @@ public function testGetUserAsSubAdminSelfLookup() {
837885
->expects($this->once())
838886
->method('getEMailAddress')
839887
->will($this->returnValue('subadmin@owncloud.org'));
888+
$targetUser
889+
->expects($this->once())
890+
->method('getUID')
891+
->will($this->returnValue('UID'));
892+
$this->accountManager->expects($this->any())->method('getUser')
893+
->with($targetUser)
894+
->willReturn(
895+
[
896+
AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
897+
AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
898+
AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
899+
AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
900+
]
901+
);
840902

841903
$expected = [
904+
'id' => 'UID',
842905
'quota' => ['DummyValue'],
843906
'email' => 'subadmin@owncloud.org',
844907
'displayname' => 'Subadmin User',
908+
'phone' => 'phone',
909+
'address' => 'address',
910+
'webpage' => 'website',
911+
'twitter' => 'twitter'
845912
];
846913
$this->assertEquals($expected, $this->api->getUser('subadmin')->getData());
847914
}
@@ -2485,4 +2552,70 @@ public function testDisableUser() {
24852552

24862553
$this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
24872554
}
2555+
2556+
public function testGetCurrentUserLoggedIn() {
2557+
2558+
$user = $this->getMock(IUser::class);
2559+
$user->expects($this->once())->method('getUID')->willReturn('UID');
2560+
2561+
$this->userSession->expects($this->once())->method('getUser')
2562+
->willReturn($user);
2563+
2564+
/** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
2565+
$api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
2566+
->setConstructorArgs([
2567+
'provisioning_api',
2568+
$this->request,
2569+
$this->userManager,
2570+
$this->config,
2571+
$this->groupManager,
2572+
$this->userSession,
2573+
$this->accountManager,
2574+
$this->logger,
2575+
])
2576+
->setMethods(['getUser'])
2577+
->getMock();
2578+
2579+
$api->expects($this->once())->method('getUser')->with('UID')
2580+
->willReturn(
2581+
[
2582+
'id' => 'UID',
2583+
'enabled' => 'true',
2584+
'quota' => ['DummyValue'],
2585+
'email' => 'demo@owncloud.org',
2586+
'displayname' => 'Demo User',
2587+
'phone' => 'phone',
2588+
'address' => 'address',
2589+
'webpage' => 'website',
2590+
'twitter' => 'twitter'
2591+
]
2592+
);
2593+
2594+
$expected = [
2595+
'id' => 'UID',
2596+
'enabled' => 'true',
2597+
'quota' => ['DummyValue'],
2598+
'email' => 'demo@owncloud.org',
2599+
'phone' => 'phone',
2600+
'address' => 'address',
2601+
'webpage' => 'website',
2602+
'twitter' => 'twitter',
2603+
'display-name' => 'Demo User'
2604+
];
2605+
2606+
$this->assertSame($expected, $api->getCurrentUser());
2607+
}
2608+
2609+
/**
2610+
* @expectedException \OCP\AppFramework\OCS\OCSException
2611+
*/
2612+
public function testGetCurrentUserNotLoggedIn() {
2613+
2614+
$this->userSession->expects($this->once())->method('getUser')
2615+
->willReturn(null);
2616+
2617+
$this->api->getCurrentUser();
2618+
}
2619+
2620+
24882621
}

core/Controller/OCSController.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,6 @@ public function getCapabilities() {
105105
return new DataResponse($result);
106106
}
107107

108-
/**
109-
* @NoAdminRequired
110-
* @return DataResponse
111-
*/
112-
public function getCurrentUser() {
113-
$userObject = $this->userSession->getUser();
114-
$data = [
115-
'id' => $userObject->getUID(),
116-
'display-name' => $userObject->getDisplayName(),
117-
'email' => $userObject->getEMailAddress(),
118-
];
119-
return new DataResponse($data);
120-
}
121-
122108
/**
123109
* @PublicPage
124110
*

core/routes.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
],
6060
'ocs' => [
6161
['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
62-
['root' => '/cloud', 'name' => 'OCS#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
6362
['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
6463
['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
6564
['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],

0 commit comments

Comments
 (0)