From 786258ff30eeb6cffb24b5b1a81f0c502d763620 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 13 Apr 2017 12:34:34 +0200 Subject: Send an email to the old address on email change Signed-off-by: Joas Schilling --- settings/Application.php | 2 +- settings/Hooks.php | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'settings') diff --git a/settings/Application.php b/settings/Application.php index c54c8928b51..52661c5bae2 100644 --- a/settings/Application.php +++ b/settings/Application.php @@ -168,6 +168,6 @@ class Application extends App { /** @var Hooks $hooks */ $hooks = $this->getContainer()->query(Hooks::class); - $hooks->onChangeEmail($parameters['user']); + $hooks->onChangeEmail($parameters['user'], $parameters['old_value']); } } diff --git a/settings/Hooks.php b/settings/Hooks.php index cc445d3fafb..721aeb23886 100644 --- a/settings/Hooks.php +++ b/settings/Hooks.php @@ -112,27 +112,54 @@ class Hooks { /** * @param IUser $user + * @param string|null $oldMailAddress * @throws \InvalidArgumentException * @throws \BadMethodCallException */ - public function onChangeEmail(IUser $user) { + public function onChangeEmail(IUser $user, $oldMailAddress) { $event = $this->activityManager->generateEvent(); $event->setApp('settings') ->setType('personal_settings') ->setAffectedUser($user->getUID()); + $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); + $actor = $this->userSession->getUser(); if ($actor instanceof IUser) { if ($actor->getUID() !== $user->getUID()) { + $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]); $event->setAuthor($actor->getUID()) ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]); } else { + $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); $event->setAuthor($actor->getUID()) ->setSubject(Provider::EMAIL_CHANGED_SELF); } } else { + $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); $event->setSubject(Provider::EMAIL_CHANGED); } $this->activityManager->publish($event); + + + if ($oldMailAddress !== null) { + $template = $this->mailer->createEMailTemplate(); + $template->addHeader(); + $template->addHeading($this->l->t('Email address changed for %s', $user->getDisplayName()), false); + $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); + if ($user->getEMailAddress()) { + $template->addBodyText($this->l->t('The new email address is %s', $user->getEMailAddress())); + } + $template->addFooter(); + + + $message = $this->mailer->createMessage(); + $message->setTo([$oldMailAddress => $user->getDisplayName()]); + $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); + $message->setBody($template->renderText(), 'text/plain'); + $message->setHtmlBody($template->renderHTML()); + + $this->mailer->send($message); + } } } -- cgit v1.2.3