diff options
author | Joas Schilling <coding@schilljs.com> | 2017-04-13 11:49:02 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-04-13 11:49:02 +0200 |
commit | 49fd41b21f43831198ec64aeeeaa46ecdf87849e (patch) | |
tree | e4f58d832e6b349dd0671d914a62e87f908a9737 /settings/Application.php | |
parent | f23a36b0a6f4721bc923a952d2644dddfe8b13fa (diff) | |
download | nextcloud-server-49fd41b21f43831198ec64aeeeaa46ecdf87849e.tar.gz nextcloud-server-49fd41b21f43831198ec64aeeeaa46ecdf87849e.zip |
Send an email on password change
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'settings/Application.php')
-rw-r--r-- | settings/Application.php | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/settings/Application.php b/settings/Application.php index e6c79eb40db..c52fe6393e2 100644 --- a/settings/Application.php +++ b/settings/Application.php @@ -143,28 +143,66 @@ class Application extends App { Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo'); } - public function onPasswordChange($parameters) { - $uid = $parameters['uid']; + /** + * @param array $parameters + * @throws \InvalidArgumentException + * @throws \BadMethodCallException + * @throws \Exception + */ + public function onPasswordChange(array $parameters) { + $userManager = $this->getContainer()->getServer()->getUserManager(); + $user = $userManager->get($parameters['uid']); + + if (!$user instanceof IUser || $user->getEMailAddress() === null) { + return; + } $activityManager = $this->getContainer()->getServer()->getActivityManager(); $event = $activityManager->generateEvent(); $event->setApp('settings') ->setType('personal_settings') - ->setAffectedUser($uid); + ->setAffectedUser($user->getUID()); + + /** @var IL10N $l */ + $l = $this->getContainer()->query(IL10N::class); + $urlGenerator = $this->getContainer()->getServer()->getURLGenerator(); + $instanceUrl = $urlGenerator->getAbsoluteURL('/'); $actor = $this->getContainer()->getServer()->getUserSession()->getUser(); if ($actor instanceof IUser) { - if ($actor->getUID() !== $uid) { + if ($actor->getUID() !== $user->getUID()) { + $text = $l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); $event->setAuthor($actor->getUID()) ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); } else { + $text = $l->t('Your password on %s was changed.', [$instanceUrl]); $event->setAuthor($actor->getUID()) ->setSubject(Provider::PASSWORD_CHANGED_SELF); } } else { + $text = $l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); $event->setSubject(Provider::PASSWORD_RESET); } + $activityManager->publish($event); + + if ($user->getEMailAddress() !== null) { + $mailer = $this->getContainer()->getServer()->getMailer(); + $template = $mailer->createEMailTemplate(); + $template->addHeader(); + $template->addHeading($l->t('Password changed for %s', $user->getDisplayName()), false); + $template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator as soon as possible.')); + $template->addFooter(); + + + $message = $mailer->createMessage(); + $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); + $message->setSubject($l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); + $message->setBody($template->renderText(), 'text/plain'); + $message->setHtmlBody($template->renderHTML()); + + $mailer->send($message); + } } public function onChangeInfo($parameters) { |