Hooks.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if ($actor->getUID() !== $user->getUID()) {
  148. $this->l = $this->languageFactory->get(
  149. 'settings',
  150. $this->config->getUserValue(
  151. $user->getUID(), 'core', 'lang',
  152. $this->config->getSystemValue('default_language', 'en')
  153. )
  154. );
  155. $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  156. $event->setAuthor($actor->getUID())
  157. ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]);
  158. } else {
  159. $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
  160. $event->setAuthor($actor->getUID())
  161. ->setSubject(Provider::EMAIL_CHANGED_SELF);
  162. }
  163. } else {
  164. $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  165. $event->setSubject(Provider::EMAIL_CHANGED);
  166. }
  167. $this->activityManager->publish($event);
  168. if ($oldMailAddress !== null) {
  169. $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
  170. 'displayname' => $user->getDisplayName(),
  171. 'newEMailAddress' => $user->getEMailAddress(),
  172. 'oldEMailAddress' => $oldMailAddress,
  173. 'instanceUrl' => $instanceUrl,
  174. ]);
  175. $template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  176. $template->addHeader();
  177. $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
  178. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  179. if ($user->getEMailAddress()) {
  180. $template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
  181. }
  182. $template->addFooter();
  183. $message = $this->mailer->createMessage();
  184. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  185. $message->useTemplate($template);
  186. $this->mailer->send($message);
  187. }
  188. }
  189. }