diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2017-02-16 07:16:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-16 07:16:45 +0100 |
commit | f25c89461c986b31171270e2186d000030b8df2e (patch) | |
tree | bddf12ca2774031dbbf4bc787e6a024c9cd1631e /settings | |
parent | 47c6a8019f1a957a6da28b365b9f7b9a194a62b8 (diff) | |
parent | 33829502972388760be41ddcc731868a1a970b16 (diff) | |
download | nextcloud-server-f25c89461c986b31171270e2186d000030b8df2e.tar.gz nextcloud-server-f25c89461c986b31171270e2186d000030b8df2e.zip |
Merge pull request #2287 from nextcloud/registrationEmail
Send password/activation link to new user
Diffstat (limited to 'settings')
-rw-r--r-- | settings/Controller/UsersController.php | 57 | ||||
-rw-r--r-- | settings/js/users/users.js | 2 | ||||
-rw-r--r-- | settings/templates/users/main.php | 3 |
3 files changed, 57 insertions, 5 deletions
diff --git a/settings/Controller/UsersController.php b/settings/Controller/UsersController.php index 719b6eb68f6..19c5b068167 100644 --- a/settings/Controller/UsersController.php +++ b/settings/Controller/UsersController.php @@ -49,7 +49,9 @@ use OCP\IUserManager; use OCP\IUserSession; use OCP\Mail\IMailer; use OCP\IAvatarManager; -use Punic\Exception; +use OCP\Security\ICrypto; +use OCP\Security\ISecureRandom; +use OCP\AppFramework\Utility\ITimeFactory; /** * @package OC\Settings\Controller @@ -85,6 +87,13 @@ class UsersController extends Controller { private $avatarManager; /** @var AccountManager */ private $accountManager; + /** @var ISecureRandom */ + private $secureRandom; + /** @var ITimeFactory */ + private $timeFactory; + /** @var ICrypto */ + private $crypto; + /** * @param string $appName @@ -103,6 +112,9 @@ class UsersController extends Controller { * @param IAppManager $appManager * @param IAvatarManager $avatarManager * @param AccountManager $accountManager + * @param ISecureRandom $secureRandom + * @param ITimeFactory $timeFactory + * @param ICrypto $crypto */ public function __construct($appName, IRequest $request, @@ -119,8 +131,10 @@ class UsersController extends Controller { IURLGenerator $urlGenerator, IAppManager $appManager, IAvatarManager $avatarManager, - AccountManager $accountManager -) { + AccountManager $accountManager, + ISecureRandom $secureRandom, + ITimeFactory $timeFactory, + ICrypto $crypto) { parent::__construct($appName, $request); $this->userManager = $userManager; $this->groupManager = $groupManager; @@ -135,6 +149,9 @@ class UsersController extends Controller { $this->urlGenerator = $urlGenerator; $this->avatarManager = $avatarManager; $this->accountManager = $accountManager; + $this->secureRandom = $secureRandom; + $this->timeFactory = $timeFactory; + $this->crypto = $crypto; // check for encryption state - TODO see formatUserForIndex $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption'); @@ -362,6 +379,21 @@ class UsersController extends Controller { ); } + $generatedPassword = false; + if ($password === '') { + if ($email === '') { + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.') + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + $password = $this->secureRandom->generate(32); + $generatedPassword = true; + } + try { $user = $this->userManager->createUser($username, $password); } catch (\Exception $exception) { @@ -394,10 +426,27 @@ class UsersController extends Controller { if($email !== '') { $user->setEMailAddress($email); + if ($generatedPassword) { + $token = $this->secureRandom->generate( + 21, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + ); + $tokenValue = $this->timeFactory->getTime() . ':' . $token; + $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; + $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); + $this->config->setUserValue($username, 'core', 'lostpassword', $encryptedValue); + + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $username, 'token' => $token]); + } else { + $link = $this->urlGenerator->getAbsoluteURL('/'); + } + // data for the mail template $mailData = array( 'username' => $username, - 'url' => $this->urlGenerator->getAbsoluteURL('/') + 'url' => $link ); $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank'); diff --git a/settings/js/users/users.js b/settings/js/users/users.js index 3cf7b5e810a..a6dcafcdac3 100644 --- a/settings/js/users/users.js +++ b/settings/js/users/users.js @@ -918,7 +918,7 @@ $(document).ready(function () { })); return false; } - if ($.trim(password) === '') { + if ($.trim(password) === '' && !$('#CheckboxMailOnUserCreate').is(':checked')) { OC.Notification.showTemporary(t('settings', 'Error creating user: {message}', { message: t('settings', 'A valid password must be provided') })); diff --git a/settings/templates/users/main.php b/settings/templates/users/main.php index b363a4c4da8..59ddf6a1dd7 100644 --- a/settings/templates/users/main.php +++ b/settings/templates/users/main.php @@ -72,6 +72,9 @@ translation('settings'); <?php p($l->t('Send email to new user')) ?> </label> </p> + <p class="info-text"> + <?php p($l->t('When the password of the new user is left empty an activation email with a link to set the password is send to the user')) ?> + </p> <p> <input type="checkbox" name="EmailAddress" value="EmailAddress" id="CheckboxEmailAddress" class="checkbox" <?php if ($_['show_email'] === 'true') print_unescaped('checked="checked"'); ?> /> |