aboutsummaryrefslogtreecommitdiffstats
path: root/apps/provisioning_api/lib
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2018-03-16 15:30:48 +0100
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2018-03-22 15:06:04 +0100
commit41b690ed53be36498607c13c2b8b3d84e240c84b (patch)
tree0c8b2f651af986e233cc6de46e6d1f5bc45c46dd /apps/provisioning_api/lib
parentf843b7edfe7b3bc6e45d4610778d2df98b3985e3 (diff)
downloadnextcloud-server-41b690ed53be36498607c13c2b8b3d84e240c84b.tar.gz
nextcloud-server-41b690ed53be36498607c13c2b8b3d84e240c84b.zip
Allow admin to create users withoutpassword by sending mail automatically
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/provisioning_api/lib')
-rw-r--r--apps/provisioning_api/lib/Controller/UsersController.php40
1 files changed, 38 insertions, 2 deletions
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php
index cd277adb162..e3b7840cd3b 100644
--- a/apps/provisioning_api/lib/Controller/UsersController.php
+++ b/apps/provisioning_api/lib/Controller/UsersController.php
@@ -50,6 +50,7 @@ use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
+use OCP\Security\ISecureRandom;
class UsersController extends OCSController {
@@ -73,6 +74,8 @@ class UsersController extends OCSController {
private $newUserMailHelper;
/** @var FederatedFileSharingFactory */
private $federatedFileSharingFactory;
+ /** @var ISecureRandom */
+ private $secureRandom;
/**
* @param string $appName
@@ -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,
@@ -99,7 +103,8 @@ class UsersController extends OCSController {
ILogger $logger,
IFactory $l10nFactory,
NewUserMailHelper $newUserMailHelper,
- FederatedFileSharingFactory $federatedFileSharingFactory) {
+ FederatedFileSharingFactory $federatedFileSharingFactory,
+ ISecureRandom $secureRandom) {
parent::__construct($appName, $request);
$this->userManager = $userManager;
@@ -112,6 +117,7 @@ class UsersController extends OCSController {
$this->l10nFactory = $l10nFactory;
$this->newUserMailHelper = $newUserMailHelper;
$this->federatedFileSharingFactory = $federatedFileSharingFactory;
+ $this->secureRandom = $secureRandom;
}
/**
@@ -199,11 +205,12 @@ class UsersController extends OCSController {
*
* @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();
@@ -228,6 +235,18 @@ class UsersController extends OCSController {
}
}
+ $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']);
@@ -237,7 +256,24 @@ class UsersController extends OCSController {
$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.',