Skip to content

Commit

Permalink
Allow admin to create users withoutpassword by sending mail automatic…
Browse files Browse the repository at this point in the history
…ally

Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
  • Loading branch information
skjnldsv committed Mar 22, 2018
1 parent f843b7e commit 41b690e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
40 changes: 38 additions & 2 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Security\ISecureRandom;

class UsersController extends OCSController {

Expand All @@ -73,6 +74,8 @@ class UsersController extends OCSController {
private $newUserMailHelper;
/** @var FederatedFileSharingFactory */
private $federatedFileSharingFactory;
/** @var ISecureRandom */
private $secureRandom;

/**
* @param string $appName
Expand All @@ -87,6 +90,7 @@ class UsersController extends OCSController {
* @param IFactory $l10nFactory
* @param NewUserMailHelper $newUserMailHelper
* @param FederatedFileSharingFactory $federatedFileSharingFactory
* @param ISecureRandom $secureRandom
*/
public function __construct(string $appName,
IRequest $request,
Expand All @@ -99,7 +103,8 @@ public function __construct(string $appName,
ILogger $logger,
IFactory $l10nFactory,
NewUserMailHelper $newUserMailHelper,
FederatedFileSharingFactory $federatedFileSharingFactory) {
FederatedFileSharingFactory $federatedFileSharingFactory,
ISecureRandom $secureRandom) {
parent::__construct($appName, $request);

$this->userManager = $userManager;
Expand All @@ -112,6 +117,7 @@ public function __construct(string $appName,
$this->l10nFactory = $l10nFactory;
$this->newUserMailHelper = $newUserMailHelper;
$this->federatedFileSharingFactory = $federatedFileSharingFactory;
$this->secureRandom = $secureRandom;
}

/**
Expand Down Expand Up @@ -199,11 +205,12 @@ public function getUsersDetails(string $search = '', $limit = null, $offset = 0)
*
* @param string $userid
* @param string $password
* @param string $email
* @param array $groups
* @return DataResponse
* @throws OCSException
*/
public function addUser(string $userid, string $password, array $groups = []): DataResponse {
public function addUser(string $userid, string $password = '', string $email='', array $groups = []): DataResponse {
$user = $this->userSession->getUser();
$isAdmin = $this->groupManager->isAdmin($user->getUID());
$subAdminManager = $this->groupManager->getSubAdmin();
Expand All @@ -228,6 +235,18 @@ public function addUser(string $userid, string $password, array $groups = []): D
}
}

$generatePasswordResetToken = false;
if ($password === '') {
if ($email === '') {
throw new OCSException('To send a password link to the user an email address is required.', 108);
}

$password = $this->secureRandom->generate(10);
// Make sure we pass the password_policy
$password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
$generatePasswordResetToken = true;
}

try {
$newUser = $this->userManager->createUser($userid, $password);
$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
Expand All @@ -237,7 +256,24 @@ public function addUser(string $userid, string $password, array $groups = []): D
$this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']);
}

// Send new user mail only if a mail is set
if ($email !== '') {
$newUser->setEMailAddress($email);
try {
$emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
$this->newUserMailHelper->sendMail($newUser, $emailTemplate);
} catch (\Exception $e) {
$this->logger->logException($e, [
'message' => "Can't send new user mail to $email",
'level' => \OCP\Util::ERROR,
'app' => 'ocs_api',
]);
throw new OCSException('Unable to send the invitation mail', 109);
}
}

return new DataResponse();

} catch (HintException $e ) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with hint exception.',
Expand Down
27 changes: 17 additions & 10 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use PHPUnit_Framework_MockObject_MockObject;
use Test\TestCase;

Expand Down Expand Up @@ -85,6 +86,8 @@ class UsersControllerTest extends TestCase {
private $newUserMailHelper;
/** @var FederatedFileSharingFactory|\PHPUnit_Framework_MockObject_MockObject */
private $federatedFileSharingFactory;
/** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
private $secureRandom;

protected function setUp() {
parent::setUp();
Expand All @@ -100,6 +103,7 @@ protected function setUp() {
$this->l10nFactory = $this->createMock(IFactory::class);
$this->newUserMailHelper = $this->createMock(NewUserMailHelper::class);
$this->federatedFileSharingFactory = $this->createMock(FederatedFileSharingFactory::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);

$this->api = $this->getMockBuilder(UsersController::class)
->setConstructorArgs([
Expand All @@ -114,7 +118,8 @@ protected function setUp() {
$this->logger,
$this->l10nFactory,
$this->newUserMailHelper,
$this->federatedFileSharingFactory
$this->federatedFileSharingFactory,
$this->secureRandom
])
->setMethods(['fillStorageInfo'])
->getMock();
Expand Down Expand Up @@ -242,7 +247,7 @@ public function testAddUserAlreadyExisting() {
->with('adminUser')
->willReturn(true);

$this->api->addUser('AlreadyExistingUser', 'password', []);
$this->api->addUser('AlreadyExistingUser', 'password', '', []);
}

/**
Expand Down Expand Up @@ -278,7 +283,7 @@ public function testAddUserNonExistingGroup() {
->with('NonExistingGroup')
->willReturn(false);

$this->api->addUser('NewUser', 'pass', ['NonExistingGroup']);
$this->api->addUser('NewUser', 'pass', '', ['NonExistingGroup']);
}

/**
Expand Down Expand Up @@ -320,7 +325,7 @@ public function testAddUserExistingGroupNonExistingGroup() {
['NonExistingGroup', false]
]));

$this->api->addUser('NewUser', 'pass', ['ExistingGroup', 'NonExistingGroup']);
$this->api->addUser('NewUser', 'pass', '', ['ExistingGroup', 'NonExistingGroup']);
}

public function testAddUserSuccessful() {
Expand Down Expand Up @@ -412,7 +417,7 @@ public function testAddUserExistingGroup() {
['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']]
);

$this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup'])->getData());
$this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', ['ExistingGroup'])->getData());
}

/**
Expand Down Expand Up @@ -490,7 +495,7 @@ public function testAddUserAsSubAdminNoGroup() {
->with()
->willReturn($subAdminManager);

$this->api->addUser('NewUser', 'PasswordOfTheNewUser', []);
$this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', []);
}

/**
Expand Down Expand Up @@ -539,7 +544,7 @@ public function testAddUserAsSubAdminValidGroupNotSubAdmin() {
->with('ExistingGroup')
->willReturn(true);

$this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup'])->getData();
$this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', ['ExistingGroup'])->getData();
}

public function testAddUserAsSubAdminExistingGroups() {
Expand Down Expand Up @@ -630,7 +635,7 @@ public function testAddUserAsSubAdminExistingGroups() {
)
->willReturn(true);

$this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup1', 'ExistingGroup2'])->getData());
$this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', ['ExistingGroup1', 'ExistingGroup2'])->getData());
}

/**
Expand Down Expand Up @@ -2928,7 +2933,8 @@ public function testGetCurrentUserLoggedIn() {
$this->logger,
$this->l10nFactory,
$this->newUserMailHelper,
$this->federatedFileSharingFactory
$this->federatedFileSharingFactory,
$this->secureRandom
])
->setMethods(['getUserData'])
->getMock();
Expand Down Expand Up @@ -2990,7 +2996,8 @@ public function testGetUser() {
$this->logger,
$this->l10nFactory,
$this->newUserMailHelper,
$this->federatedFileSharingFactory
$this->federatedFileSharingFactory,
$this->secureRandom
])
->setMethods(['getUserData'])
->getMock();
Expand Down

0 comments on commit 41b690e

Please sign in to comment.