You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hooks.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Settings;
  22. use OC\Settings\Activity\Provider;
  23. use OCP\Activity\IManager as IActivityManager;
  24. use OCP\IL10N;
  25. use OCP\IURLGenerator;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. use OCP\IUserSession;
  29. use OCP\Mail\IMailer;
  30. class Hooks {
  31. /** @var IActivityManager */
  32. protected $activityManager;
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /** @var IUserSession */
  36. protected $userSession;
  37. /** @var IURLGenerator */
  38. protected $urlGenerator;
  39. /** @var IMailer */
  40. protected $mailer;
  41. /** @var IL10N */
  42. protected $l;
  43. public function __construct(IActivityManager $activityManager, IUserManager $userManager, IUserSession $userSession, IURLGenerator $urlGenerator, IMailer $mailer, IL10N $l) {
  44. $this->activityManager = $activityManager;
  45. $this->userManager = $userManager;
  46. $this->userSession = $userSession;
  47. $this->urlGenerator = $urlGenerator;
  48. $this->mailer = $mailer;
  49. $this->l = $l;
  50. }
  51. /**
  52. * @param string $uid
  53. * @throws \InvalidArgumentException
  54. * @throws \BadMethodCallException
  55. * @throws \Exception
  56. */
  57. public function onChangePassword($uid) {
  58. $user = $this->userManager->get($uid);
  59. if (!$user instanceof IUser || $user->getEMailAddress() === null) {
  60. return;
  61. }
  62. $event = $this->activityManager->generateEvent();
  63. $event->setApp('settings')
  64. ->setType('personal_settings')
  65. ->setAffectedUser($user->getUID());
  66. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  67. $actor = $this->userSession->getUser();
  68. if ($actor instanceof IUser) {
  69. if ($actor->getUID() !== $user->getUID()) {
  70. $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  71. $event->setAuthor($actor->getUID())
  72. ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
  73. } else {
  74. $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
  75. $event->setAuthor($actor->getUID())
  76. ->setSubject(Provider::PASSWORD_CHANGED_SELF);
  77. }
  78. } else {
  79. $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
  80. $event->setSubject(Provider::PASSWORD_RESET);
  81. }
  82. $this->activityManager->publish($event);
  83. if ($user->getEMailAddress() !== null) {
  84. $template = $this->mailer->createEMailTemplate();
  85. $template->addHeader();
  86. $template->addHeading($this->l->t('Password changed for %s', $user->getDisplayName()), false);
  87. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  88. $template->addFooter();
  89. $message = $this->mailer->createMessage();
  90. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  91. $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  92. $message->setBody($template->renderText(), 'text/plain');
  93. $message->setHtmlBody($template->renderHtml());
  94. $this->mailer->send($message);
  95. }
  96. }
  97. /**
  98. * @param IUser $user
  99. * @param string|null $oldMailAddress
  100. * @throws \InvalidArgumentException
  101. * @throws \BadMethodCallException
  102. */
  103. public function onChangeEmail(IUser $user, $oldMailAddress) {
  104. if ($oldMailAddress === $user->getEMailAddress()) {
  105. // Email didn't really change, so don't create activities and emails
  106. return;
  107. }
  108. $event = $this->activityManager->generateEvent();
  109. $event->setApp('settings')
  110. ->setType('personal_settings')
  111. ->setAffectedUser($user->getUID());
  112. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  113. $actor = $this->userSession->getUser();
  114. if ($actor instanceof IUser) {
  115. if ($actor->getUID() !== $user->getUID()) {
  116. $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  117. $event->setAuthor($actor->getUID())
  118. ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]);
  119. } else {
  120. $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
  121. $event->setAuthor($actor->getUID())
  122. ->setSubject(Provider::EMAIL_CHANGED_SELF);
  123. }
  124. } else {
  125. $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  126. $event->setSubject(Provider::EMAIL_CHANGED);
  127. }
  128. $this->activityManager->publish($event);
  129. if ($oldMailAddress !== null) {
  130. $template = $this->mailer->createEMailTemplate();
  131. $template->addHeader();
  132. $template->addHeading($this->l->t('Email address changed for %s', $user->getDisplayName()), false);
  133. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  134. if ($user->getEMailAddress()) {
  135. $template->addBodyText($this->l->t('The new email address is %s', $user->getEMailAddress()));
  136. }
  137. $template->addFooter();
  138. $message = $this->mailer->createMessage();
  139. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  140. $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  141. $message->setBody($template->renderText(), 'text/plain');
  142. $message->setHtmlBody($template->renderHtml());
  143. $this->mailer->send($message);
  144. }
  145. }
  146. }