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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\IConfig;
  25. use OCP\IL10N;
  26. use OCP\IURLGenerator;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use OCP\IUserSession;
  30. use OCP\L10N\IFactory;
  31. use OCP\Mail\IMailer;
  32. class Hooks {
  33. /** @var IActivityManager */
  34. protected $activityManager;
  35. /** @var IUserManager */
  36. protected $userManager;
  37. /** @var IUserSession */
  38. protected $userSession;
  39. /** @var IURLGenerator */
  40. protected $urlGenerator;
  41. /** @var IMailer */
  42. protected $mailer;
  43. /** @var IConfig */
  44. protected $config;
  45. /** @var IFactory */
  46. protected $languageFactory;
  47. /** @var IL10N */
  48. protected $l;
  49. public function __construct(IActivityManager $activityManager,
  50. IUserManager $userManager,
  51. IUserSession $userSession,
  52. IURLGenerator $urlGenerator,
  53. IMailer $mailer,
  54. IConfig $config,
  55. IFactory $languageFactory,
  56. IL10N $l) {
  57. $this->activityManager = $activityManager;
  58. $this->userManager = $userManager;
  59. $this->userSession = $userSession;
  60. $this->urlGenerator = $urlGenerator;
  61. $this->mailer = $mailer;
  62. $this->config = $config;
  63. $this->languageFactory = $languageFactory;
  64. $this->l = $l;
  65. }
  66. /**
  67. * @param string $uid
  68. * @throws \InvalidArgumentException
  69. * @throws \BadMethodCallException
  70. * @throws \Exception
  71. */
  72. public function onChangePassword($uid) {
  73. $user = $this->userManager->get($uid);
  74. if (!$user instanceof IUser || $user->getLastLogin() === 0) {
  75. // User didn't login, so don't create activities and emails.
  76. return;
  77. }
  78. $event = $this->activityManager->generateEvent();
  79. $event->setApp('settings')
  80. ->setType('personal_settings')
  81. ->setAffectedUser($user->getUID());
  82. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  83. $actor = $this->userSession->getUser();
  84. if ($actor instanceof IUser) {
  85. if ($actor->getUID() !== $user->getUID()) {
  86. $this->l = $this->languageFactory->get(
  87. 'settings',
  88. $this->config->getUserValue(
  89. $user->getUID(), 'core', 'lang',
  90. $this->config->getSystemValue('default_language', 'en')
  91. )
  92. );
  93. $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  94. $event->setAuthor($actor->getUID())
  95. ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
  96. } else {
  97. $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
  98. $event->setAuthor($actor->getUID())
  99. ->setSubject(Provider::PASSWORD_CHANGED_SELF);
  100. }
  101. } else {
  102. $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
  103. $event->setSubject(Provider::PASSWORD_RESET);
  104. }
  105. $this->activityManager->publish($event);
  106. if ($user->getEMailAddress() !== null) {
  107. $template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
  108. 'displayname' => $user->getDisplayName(),
  109. 'emailAddress' => $user->getEMailAddress(),
  110. 'instanceUrl' => $instanceUrl,
  111. ]);
  112. $template->addHeader();
  113. $template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
  114. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  115. $template->addFooter();
  116. $message = $this->mailer->createMessage();
  117. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  118. $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  119. $message->setBody($template->renderText(), 'text/plain');
  120. $message->setHtmlBody($template->renderHtml());
  121. $this->mailer->send($message);
  122. }
  123. }
  124. /**
  125. * @param IUser $user
  126. * @param string|null $oldMailAddress
  127. * @throws \InvalidArgumentException
  128. * @throws \BadMethodCallException
  129. */
  130. public function onChangeEmail(IUser $user, $oldMailAddress) {
  131. if ($oldMailAddress === $user->getEMailAddress() ||
  132. $user->getLastLogin() === 0) {
  133. // Email didn't really change or user didn't login,
  134. // so don't create activities and emails.
  135. return;
  136. }
  137. $event = $this->activityManager->generateEvent();
  138. $event->setApp('settings')
  139. ->setType('personal_settings')
  140. ->setAffectedUser($user->getUID());
  141. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  142. $actor = $this->userSession->getUser();
  143. if ($actor instanceof IUser) {
  144. if ($actor->getUID() !== $user->getUID()) {
  145. $this->l = $this->languageFactory->get(
  146. 'settings',
  147. $this->config->getUserValue(
  148. $user->getUID(), 'core', 'lang',
  149. $this->config->getSystemValue('default_language', 'en')
  150. )
  151. );
  152. $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  153. $event->setAuthor($actor->getUID())
  154. ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]);
  155. } else {
  156. $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
  157. $event->setAuthor($actor->getUID())
  158. ->setSubject(Provider::EMAIL_CHANGED_SELF);
  159. }
  160. } else {
  161. $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  162. $event->setSubject(Provider::EMAIL_CHANGED);
  163. }
  164. $this->activityManager->publish($event);
  165. if ($oldMailAddress !== null) {
  166. $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
  167. 'displayname' => $user->getDisplayName(),
  168. 'newEMailAddress' => $user->getEMailAddress(),
  169. 'oldEMailAddress' => $oldMailAddress,
  170. 'instanceUrl' => $instanceUrl,
  171. ]);
  172. $template->addHeader();
  173. $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
  174. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  175. if ($user->getEMailAddress()) {
  176. $template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
  177. }
  178. $template->addFooter();
  179. $message = $this->mailer->createMessage();
  180. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  181. $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  182. $message->setBody($template->renderText(), 'text/plain');
  183. $message->setHtmlBody($template->renderHtml());
  184. $this->mailer->send($message);
  185. }
  186. }
  187. }