Skip to content

Commit

Permalink
Update provisioning api for new user registration with email
Browse files Browse the repository at this point in the history
Update the provisioning api for new user registration with
email.

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Sep 26, 2018
1 parent ae91774 commit 718bc05
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 32 deletions.
10 changes: 10 additions & 0 deletions apps/provisioning_api/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace OCA\Provisioning_API\AppInfo;

use OC\User\Service\SigninWithEmail;
use OCA\Provisioning_API\Apps;
use OCA\Provisioning_API\Groups;
use OCA\Provisioning_API\Users;
Expand All @@ -37,6 +38,15 @@
\OC::$server->getUserManager(),
\OC::$server->getGroupManager(),
\OC::$server->getUserSession(),
new SigninWithEmail(
\OC::$server->getUserSession(), \OC::$server->getGroupManager(),
\OC::$server->getURLGenerator(), \OC::$server->getUserManager(),
\OC::$server->getSecureRandom(), new \OC_Defaults(),
\OC::$server->getTimeFactory(), \OC::$server->getMailer(),
\OC::$server->getL10N('settings'), \OC::$server->getLogger(),
\OC::$server->getConfig(), \OC::$server->getAppManager(),
\OC::$server->getAvatarManager(), \OC::$server->getEventDispatcher()
),
\OC::$server->getLogger(),
\OC::$server->getTwoFactorAuthManager()
);
Expand Down
27 changes: 23 additions & 4 deletions apps/provisioning_api/lib/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

namespace OCA\Provisioning_API;

use OC\AppFramework\Http;
use OC\OCS\Result;
use OC\User\Service\SigninWithEmail;
use OC_Helper;
use OCP\API;
use OCP\Files\FileInfo;
Expand All @@ -50,6 +52,8 @@ class Users {
private $groupManager;
/** @var IUserSession */
private $userSession;
/** @var SigninWithEmail */
private $signinWithEmail;
/** @var ILogger */
private $logger;
/** @var \OC\Authentication\TwoFactorAuth\Manager */
Expand All @@ -64,11 +68,13 @@ class Users {
public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
IUserSession $userSession,
SigninWithEmail $signinWithEmail,
ILogger $logger,
\OC\Authentication\TwoFactorAuth\Manager $twoFactorAuthManager) {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->signinWithEmail = $signinWithEmail;
$this->logger = $logger;
$this->twoFactorAuthManager = $twoFactorAuthManager;
}
Expand Down Expand Up @@ -127,6 +133,7 @@ public function addUser() {
$userId = isset($_POST['userid']) ? $_POST['userid'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
$groups = isset($_POST['groups']) ? $_POST['groups'] : null;
$emailAddress = isset($_POST['email']) ? $_POST['email'] : '';
$user = $this->userSession->getUser();
$isAdmin = $this->groupManager->isAdmin($user->getUID());
$subAdminManager = $this->groupManager->getSubAdmin();
Expand Down Expand Up @@ -156,13 +163,25 @@ public function addUser() {
}

try {
$newUser = $this->userManager->createUser($userId, $password);
$this->logger->info('Successful addUser call with userid: '.$userId, ['app' => 'ocs_api']);
$password = ($password === null) ? '' : $password;
$newUser = null;
$userAddedToGroups = ($groups === null) ? [null] : $groups;
$response = $this->signinWithEmail->create($userId, $password, $userAddedToGroups, $emailAddress);
if ($response->getStatus() === Http::STATUS_CREATED) {
$newUser = $this->userManager->get($userId);
$this->logger->info('Successful addUser call with userid: '.$userId, ['app' => 'ocs_api']);
} elseif ($response->getStatus() === Http::STATUS_FORBIDDEN) {
if (isset($response->getData()['message'])) {
throw new \Exception($response->getData()['message']);
}
}

if (\is_array($groups)) {
foreach ($groups as $group) {
$this->groupManager->get($group)->addUser($newUser);
$this->logger->info('Added userid '.$userId.' to group '.$group, ['app' => 'ocs_api']);
if ($newUser !== null) {
$this->groupManager->get($group)->addUser($newUser);
$this->logger->info('Added userid ' . $userId . ' to group ' . $group, ['app' => 'ocs_api']);
}
}
}
return new Result(null, 100);
Expand Down
124 changes: 101 additions & 23 deletions apps/provisioning_api/tests/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@
namespace OCA\Provisioning_API\Tests;

use OC\OCS\Result;
use OC\User\Service\SigninWithEmail;
use OC\User\User;
use OCA\Provisioning_API\Users;
use OCP\API;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\ISubAdminManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use PHPUnit_Framework_MockObject_MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase as OriginalTest;
use OCP\IUser;
use OC\SubAdmin;
Expand All @@ -50,12 +62,24 @@ class UsersTest extends OriginalTest {
protected $groupManager;
/** @var IUserSession | PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
/** @var SigninWithEmail | \PHPUnit_Framework_MockObject_MockObject */
private $signinWithEmail;
/** @var ILogger | PHPUnit_Framework_MockObject_MockObject */
protected $logger;
/** @var Users | PHPUnit_Framework_MockObject_MockObject */
protected $api;
/** @var \OC\Authentication\TwoFactorAuth\Manager | PHPUnit_Framework_MockObject_MockObject */
private $twoFactorAuthManager;
private $urlGenerator;
private $secureRandom;
private $defaults;
private $timeFactory;
private $mailer;
private $l10n;
private $config;
private $appManager;
private $avatarManager;
private $eventDispatcher;

protected function tearDown() {
$_GET = null;
Expand All @@ -79,11 +103,26 @@ protected function setUp() {
$this->twoFactorAuthManager->expects($this->any())
->method('isTwoFactorAuthenticated')
->willReturn(false);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->defaults = $this->createMock(\OC_Defaults::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->mailer = $this->createMock(IMailer::class);
$this->l10n = $this->createMock(IL10N::class);
$this->config = $this->createMock(IConfig::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->avatarManager = $this->createMock(IAvatarManager::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->signinWithEmail = new SigninWithEmail($this->userSession, $this->groupManager,
$this->urlGenerator, $this->userManager, $this->secureRandom, $this->defaults,
$this->timeFactory, $this->mailer, $this->l10n, $this->logger, $this->config,
$this->appManager, $this->avatarManager, $this->eventDispatcher);
$this->api = $this->getMockBuilder(Users::class)
->setConstructorArgs([
$this->userManager,
$this->groupManager,
$this->userSession,
$this->signinWithEmail,
$this->logger,
$this->twoFactorAuthManager
])
Expand Down Expand Up @@ -324,15 +363,20 @@ public function testAddUserExistingGroupNonExistingGroup() {
public function testAddUserSuccessful() {
$_POST['userid'] = 'NewUser';
$_POST['password'] = 'PasswordOfTheNewUser';
$newUser = $this->createMock(User::class);
$newUser->expects($this->any())
->method('getUID')
->willReturn('NewUser');
$this->userManager
->expects($this->once())
->expects($this->any())
->method('userExists')
->with('NewUser')
->will($this->returnValue(false));
$this->userManager
->expects($this->once())
->method('createUser')
->with('NewUser', 'PasswordOfTheNewUser');
->with('NewUser', 'PasswordOfTheNewUser')
->willReturn($newUser);
$this->logger
->expects($this->once())
->method('info')
Expand All @@ -343,14 +387,21 @@ public function testAddUserSuccessful() {
->method('getUID')
->will($this->returnValue('adminUser'));
$this->userSession
->expects($this->once())
->expects($this->any())
->method('getUser')
->will($this->returnValue($loggedInUser));
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('adminUser')
->willReturn(true);
$subadminManager = $this->createMock(ISubAdminManager::class);
$subadminManager->expects($this->any())
->method('getSubAdminsGroups')
->willReturn([]);
$this->groupManager->expects($this->any())
->method('getSubAdmin')
->willReturn($subadminManager);

$expected = new Result(null, 100);
$this->assertEquals($expected, $this->api->addUser());
Expand All @@ -361,7 +412,7 @@ public function testAddUserExistingGroup() {
$_POST['password'] = 'PasswordOfTheNewUser';
$_POST['groups'] = ['ExistingGroup'];
$this->userManager
->expects($this->once())
->expects($this->any())
->method('userExists')
->with('NewUser')
->willReturn(false);
Expand All @@ -371,7 +422,7 @@ public function testAddUserExistingGroup() {
->method('getUID')
->will($this->returnValue('adminUser'));
$this->userSession
->expects($this->once())
->expects($this->any())
->method('getUser')
->will($this->returnValue($loggedInUser));
$this->groupManager
Expand All @@ -384,28 +435,43 @@ public function testAddUserExistingGroup() {
->method('groupExists')
->with('ExistingGroup')
->willReturn(true);
$user = $this->createMock(IUser::class);
$newUser = $this->createMock(User::class);
$newUser->expects($this->any())
->method('getUID')
->willReturn('NewUser');
$this->userManager
->expects($this->once())
->method('createUser')
->with('NewUser', 'PasswordOfTheNewUser')
->willReturn($user);
->willReturn($newUser);
$group = $this->createMock(IGroup::class);
$group
->expects($this->once())
->method('addUser')
->with($user);
->with($newUser);
$group->expects($this->any())
->method('getGID')
->willReturn('ExistingGroup');
$this->groupManager
->expects($this->once())
->method('get')
->with('ExistingGroup')
->willReturn($group);

$subadminManager = $this->createMock(ISubAdminManager::class);
$subadminManager->expects($this->any())
->method('getSubAdminsGroups')
->willReturn([]);
$this->groupManager->expects($this->any())
->method('getSubAdmin')
->willReturn($subadminManager);

$this->logger
->expects($this->exactly(2))
->expects($this->any())
->method('info')
->withConsecutive(
['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']]
['Added userid NewUser to group ExistingGroup'],
['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']]
);

$expected = new Result(null, 100);
Expand All @@ -416,7 +482,7 @@ public function testAddUserUnsuccessful() {
$_POST['userid'] = 'NewUser';
$_POST['password'] = 'PasswordOfTheNewUser';
$this->userManager
->expects($this->once())
->expects($this->any())
->method('userExists')
->with('NewUser')
->will($this->returnValue(false));
Expand All @@ -435,7 +501,7 @@ public function testAddUserUnsuccessful() {
->method('getUID')
->will($this->returnValue('adminUser'));
$this->userSession
->expects($this->once())
->expects($this->any())
->method('getUser')
->will($this->returnValue($loggedInUser));
$this->groupManager
Expand Down Expand Up @@ -571,8 +637,12 @@ public function testAddUserAsSubAdminExistingGroups() {
$_POST['userid'] = 'NewUser';
$_POST['password'] = 'PasswordOfTheNewUser';
$_POST['groups'] = ['ExistingGroup1', 'ExistingGroup2'];
$newUser = $this->createMock(User::class);
$newUser->expects($this->any())
->method('getUID')
->willReturn('NewUser');
$this->userManager
->expects($this->once())
->expects($this->any())
->method('userExists')
->with('NewUser')
->willReturn(false);
Expand All @@ -582,7 +652,7 @@ public function testAddUserAsSubAdminExistingGroups() {
->method('getUID')
->will($this->returnValue('subAdminUser'));
$this->userSession
->expects($this->once())
->expects($this->any())
->method('getUser')
->will($this->returnValue($loggedInUser));
$this->groupManager
Expand All @@ -598,22 +668,27 @@ public function testAddUserAsSubAdminExistingGroups() {
['ExistingGroup2']
)
->willReturn(true);
$user = $this->createMock(IUser::class);
$this->userManager
->expects($this->once())
->method('createUser')
->with('NewUser', 'PasswordOfTheNewUser')
->willReturn($user);
->willReturn($newUser);
$existingGroup1 = $this->createMock(IGroup::class);
$existingGroup2 = $this->createMock(IGroup::class);
$existingGroup1
->expects($this->once())
->method('addUser')
->with($user);
->with($newUser);
$existingGroup2
->expects($this->once())
->method('addUser')
->with($user);
->with($newUser);
$existingGroup1->expects($this->any())
->method('getGID')
->willReturn('ExistingGroup1');
$existingGroup2->expects($this->any())
->method('getGID')
->willReturn('ExistingGroup2');
$this->groupManager
->expects($this->exactly(4))
->method('get')
Expand All @@ -631,16 +706,19 @@ public function testAddUserAsSubAdminExistingGroups() {
->expects($this->exactly(3))
->method('info')
->withConsecutive(
['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']],
['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']]
['Added userid NewUser to group ExistingGroup1'],
['Added userid NewUser to group ExistingGroup2'],
['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']]
);
$subAdminManager = $this->getMockBuilder(SubAdmin::class)
->disableOriginalConstructor()->getMock();
$this->groupManager
->expects($this->once())
->expects($this->any())
->method('getSubAdmin')
->willReturn($subAdminManager);
$subAdminManager->expects($this->once())
->method('getSubAdminsGroups')
->willReturn([]);
$subAdminManager
->expects($this->once())
->method('isSubAdmin')
Expand Down
Loading

0 comments on commit 718bc05

Please sign in to comment.