From 718bc05de3a2420c175affdce407173d55d733b6 Mon Sep 17 00:00:00 2001 From: Sujith H Date: Wed, 26 Sep 2018 19:09:56 +0530 Subject: [PATCH] Update provisioning api for new user registration with email Update the provisioning api for new user registration with email. Signed-off-by: Sujith H --- apps/provisioning_api/appinfo/routes.php | 10 ++ apps/provisioning_api/lib/Users.php | 27 +++- apps/provisioning_api/tests/UsersTest.php | 124 +++++++++++++++---- lib/private/User/Service/SigninWithEmail.php | 13 +- 4 files changed, 142 insertions(+), 32 deletions(-) diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index 78cbddc0533c..f0323d8a34d7 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -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; @@ -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() ); diff --git a/apps/provisioning_api/lib/Users.php b/apps/provisioning_api/lib/Users.php index af27c8938f34..2e551c57581f 100644 --- a/apps/provisioning_api/lib/Users.php +++ b/apps/provisioning_api/lib/Users.php @@ -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; @@ -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 */ @@ -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; } @@ -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(); @@ -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); diff --git a/apps/provisioning_api/tests/UsersTest.php b/apps/provisioning_api/tests/UsersTest.php index bf0e93347a71..146ed889caf9 100644 --- a/apps/provisioning_api/tests/UsersTest.php +++ b/apps/provisioning_api/tests/UsersTest.php @@ -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; @@ -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; @@ -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 ]) @@ -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') @@ -343,7 +387,7 @@ 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 @@ -351,6 +395,13 @@ public function testAddUserSuccessful() { ->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()); @@ -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); @@ -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 @@ -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); @@ -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)); @@ -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 @@ -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); @@ -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 @@ -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') @@ -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') diff --git a/lib/private/User/Service/SigninWithEmail.php b/lib/private/User/Service/SigninWithEmail.php index dbf115650e7f..039192a568c5 100644 --- a/lib/private/User/Service/SigninWithEmail.php +++ b/lib/private/User/Service/SigninWithEmail.php @@ -228,12 +228,15 @@ public function create($username, $password, array $groups= [], $email='') { if ($user instanceof User) { if ($groups !== null) { foreach ($groups as $groupName) { - $group = $this->groupManager->get($groupName); - - if (empty($group)) { - $group = $this->groupManager->createGroup($groupName); + if ($groupName !== null) { + $group = $this->groupManager->get($groupName); + + if (empty($group)) { + $group = $this->groupManager->createGroup($groupName); + } + $group->addUser($user); + $this->log->info('Added userid ' . $user->getUID() . ' to group ' . $group->getGID()); } - $group->addUser($user); } } /**