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 6.8KB

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