diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-02-24 00:01:13 -0600 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2017-02-28 16:30:33 -0600 |
commit | 50f3efad6f9026be32457e14d925a231b9240eaf (patch) | |
tree | 3dff851ec26781185733712be1adba6a16d634a9 /apps/provisioning_api/lib | |
parent | 5fc924f6c95f9f718f283deb79f71320b16ceebb (diff) | |
download | nextcloud-server-50f3efad6f9026be32457e14d925a231b9240eaf.tar.gz nextcloud-server-50f3efad6f9026be32457e14d925a231b9240eaf.zip |
OCS API endpoint to resend welcome message
* send a POST request to ocs/v1.php/cloud/users/USERNAME/resendWelcomeMessage to trigger
the welcome message to be send
* fixes #3367
example curl statement:
curl -i https://example.org/ocs/v1.php/cloud/users/USERNAME/welcome -H "OCS-APIRequest: true" -u admin:password -X POST
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'apps/provisioning_api/lib')
-rw-r--r-- | apps/provisioning_api/lib/Controller/UsersController.php | 101 |
1 files changed, 100 insertions, 1 deletions
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 766775dfdbe..9155d34276b 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -32,6 +32,7 @@ namespace OCA\Provisioning_API\Controller; use OC\Accounts\AccountManager; use \OC_Helper; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCSController; @@ -41,8 +42,11 @@ use OCP\IGroup; use OCP\IGroupManager; use OCP\ILogger; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUserManager; use OCP\IUserSession; +use OCP\L10N\IFactory; +use OCP\Mail\IMailer; class UsersController extends OCSController { @@ -58,6 +62,16 @@ class UsersController extends OCSController { private $accountManager; /** @var ILogger */ private $logger; + /** @var string */ + private $fromMailAddress; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var IMailer */ + private $mailer; + /** @var \OC_Defaults */ + private $defaults; + /** @var IFactory */ + private $l10nFactory; /** * @param string $appName @@ -68,6 +82,11 @@ class UsersController extends OCSController { * @param IUserSession $userSession * @param AccountManager $accountManager * @param ILogger $logger + * @param string $fromMailAddress + * @param IURLGenerator $urlGenerator + * @param IMailer $mailer + * @param \OC_Defaults $defaults + * @param IFactory $l10nFactory */ public function __construct($appName, IRequest $request, @@ -76,7 +95,12 @@ class UsersController extends OCSController { IGroupManager $groupManager, IUserSession $userSession, AccountManager $accountManager, - ILogger $logger) { + ILogger $logger, + $fromMailAddress, + IURLGenerator $urlGenerator, + IMailer $mailer, + \OC_Defaults $defaults, + IFactory $l10nFactory) { parent::__construct($appName, $request); $this->userManager = $userManager; @@ -85,6 +109,11 @@ class UsersController extends OCSController { $this->userSession = $userSession; $this->accountManager = $accountManager; $this->logger = $logger; + $this->fromMailAddress = $fromMailAddress; + $this->urlGenerator = $urlGenerator; + $this->mailer = $mailer; + $this->defaults = $defaults; + $this->l10nFactory = $l10nFactory; } /** @@ -718,4 +747,74 @@ class UsersController extends OCSController { } return $data; } + + /** + * @NoAdminRequired + * @PasswordConfirmationRequired + * + * resend welcome message + * + * @param string $userId + * @return DataResponse + * @throws OCSException + */ + public function resendWelcomeMessage($userId) { + $currentLoggedInUser = $this->userSession->getUser(); + + $targetUser = $this->userManager->get($userId); + if($targetUser === null) { + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + + // Check if admin / subadmin + $subAdminManager = $this->groupManager->getSubAdmin(); + if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) + && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { + // No rights + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + + $email = $targetUser->getEMailAddress(); + if ($email === '' || $email === null) { + throw new OCSException('Email address not available', 101); + } + $username = $targetUser->getUID(); + $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); + if (!$this->l10nFactory->languageExists('settings', $lang)) { + $lang = 'en'; + } + + $l10n = $this->l10nFactory->get('settings', $lang); + + // data for the mail template + $mailData = [ + 'username' => $username, + 'url' => $this->urlGenerator->getAbsoluteURL('/') + ]; + + // FIXME: set users language in email + $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank'); + $mailContent = $mail->render(); + + // FIXME: set users language in email + $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank'); + $plainTextMailContent = $mail->render(); + + $subject = $l10n->t('Your %s account was created', [$this->defaults->getName()]); + + try { + $message = $this->mailer->createMessage(); + $message->setTo([$email => $username]); + $message->setSubject($subject); + $message->setHtmlBody($mailContent); + $message->setPlainBody($plainTextMailContent); + $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]); + $this->mailer->send($message); + } catch(\Exception $e) { + $this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings')); + throw new OCSException('Sending email failed', 102); + } + + return new DataResponse(); + } } |