Hooks.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings;
  7. use OCA\Settings\Activity\Provider;
  8. use OCP\Activity\IManager as IActivityManager;
  9. use OCP\Defaults;
  10. use OCP\IConfig;
  11. use OCP\IGroupManager;
  12. use OCP\IURLGenerator;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. use OCP\IUserSession;
  16. use OCP\L10N\IFactory;
  17. use OCP\Mail\IMailer;
  18. class Hooks {
  19. /** @var IActivityManager */
  20. protected $activityManager;
  21. /** @var IGroupManager|\OC\Group\Manager */
  22. protected $groupManager;
  23. /** @var IUserManager */
  24. protected $userManager;
  25. /** @var IUserSession */
  26. protected $userSession;
  27. /** @var IURLGenerator */
  28. protected $urlGenerator;
  29. /** @var IMailer */
  30. protected $mailer;
  31. /** @var IConfig */
  32. protected $config;
  33. /** @var IFactory */
  34. protected $languageFactory;
  35. /** @var Defaults */
  36. protected $defaults;
  37. public function __construct(IActivityManager $activityManager,
  38. IGroupManager $groupManager,
  39. IUserManager $userManager,
  40. IUserSession $userSession,
  41. IURLGenerator $urlGenerator,
  42. IMailer $mailer,
  43. IConfig $config,
  44. IFactory $languageFactory,
  45. Defaults $defaults) {
  46. $this->activityManager = $activityManager;
  47. $this->groupManager = $groupManager;
  48. $this->userManager = $userManager;
  49. $this->userSession = $userSession;
  50. $this->urlGenerator = $urlGenerator;
  51. $this->mailer = $mailer;
  52. $this->config = $config;
  53. $this->languageFactory = $languageFactory;
  54. $this->defaults = $defaults;
  55. }
  56. /**
  57. * @param string $uid
  58. * @throws \InvalidArgumentException
  59. * @throws \BadMethodCallException
  60. * @throws \Exception
  61. */
  62. public function onChangePassword($uid) {
  63. $user = $this->userManager->get($uid);
  64. if (!$user instanceof IUser || $user->getLastLogin() === 0) {
  65. // User didn't login, so don't create activities and emails.
  66. return;
  67. }
  68. $event = $this->activityManager->generateEvent();
  69. $event->setApp('settings')
  70. ->setType('personal_settings')
  71. ->setAffectedUser($user->getUID());
  72. $instanceName = $this->defaults->getName();
  73. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  74. $language = $this->languageFactory->getUserLanguage($user);
  75. $l = $this->languageFactory->get('settings', $language);
  76. $actor = $this->userSession->getUser();
  77. if ($actor instanceof IUser) {
  78. if ($actor->getUID() !== $user->getUID()) {
  79. // Admin changed the password through the user panel
  80. $text = $l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  81. $event->setAuthor($actor->getUID())
  82. ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
  83. } else {
  84. // User changed their password themselves through settings
  85. $text = $l->t('Your password on %s was changed.', [$instanceUrl]);
  86. $event->setAuthor($actor->getUID())
  87. ->setSubject(Provider::PASSWORD_CHANGED_SELF);
  88. }
  89. } else {
  90. if (\OC::$CLI) {
  91. // Admin used occ to reset the password
  92. $text = $l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
  93. $event->setSubject(Provider::PASSWORD_RESET);
  94. } else {
  95. // User reset their password from Lost page
  96. $text = $l->t('Your password on %s was reset.', [$instanceUrl]);
  97. $event->setSubject(Provider::PASSWORD_RESET_SELF);
  98. }
  99. }
  100. $this->activityManager->publish($event);
  101. if ($user->getEMailAddress() !== null) {
  102. $template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
  103. 'displayname' => $user->getDisplayName(),
  104. 'emailAddress' => $user->getEMailAddress(),
  105. 'instanceUrl' => $instanceUrl,
  106. ]);
  107. $template->setSubject($l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceName]));
  108. $template->addHeader();
  109. $template->addHeading($l->t('Password changed for %s', [$user->getDisplayName()]), false);
  110. $template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator.'));
  111. $template->addFooter();
  112. $message = $this->mailer->createMessage();
  113. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  114. $message->useTemplate($template);
  115. $this->mailer->send($message);
  116. }
  117. }
  118. /**
  119. * @param IUser $user
  120. * @param string|null $oldMailAddress
  121. * @throws \InvalidArgumentException
  122. * @throws \BadMethodCallException
  123. */
  124. public function onChangeEmail(IUser $user, $oldMailAddress) {
  125. if ($oldMailAddress === $user->getEMailAddress() ||
  126. $user->getLastLogin() === 0) {
  127. // Email didn't really change or user didn't login,
  128. // so don't create activities and emails.
  129. return;
  130. }
  131. $event = $this->activityManager->generateEvent();
  132. $event->setApp('settings')
  133. ->setType('personal_settings')
  134. ->setAffectedUser($user->getUID());
  135. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  136. $language = $this->languageFactory->getUserLanguage($user);
  137. $l = $this->languageFactory->get('settings', $language);
  138. $actor = $this->userSession->getUser();
  139. if ($actor instanceof IUser) {
  140. $subject = Provider::EMAIL_CHANGED_SELF;
  141. if ($actor->getUID() !== $user->getUID()) {
  142. // set via the OCS API
  143. if ($this->config->getAppValue('settings', 'disable_activity.email_address_changed_by_admin', 'no') === 'yes') {
  144. return;
  145. }
  146. $subject = Provider::EMAIL_CHANGED;
  147. }
  148. $text = $l->t('Your email address on %s was changed.', [$instanceUrl]);
  149. $event->setAuthor($actor->getUID())
  150. ->setSubject($subject);
  151. } else {
  152. // set with occ
  153. if ($this->config->getAppValue('settings', 'disable_activity.email_address_changed_by_admin', 'no') === 'yes') {
  154. return;
  155. }
  156. $text = $l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  157. $event->setSubject(Provider::EMAIL_CHANGED);
  158. }
  159. $this->activityManager->publish($event);
  160. if ($oldMailAddress !== null) {
  161. $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
  162. 'displayname' => $user->getDisplayName(),
  163. 'newEMailAddress' => $user->getEMailAddress(),
  164. 'oldEMailAddress' => $oldMailAddress,
  165. 'instanceUrl' => $instanceUrl,
  166. ]);
  167. $template->setSubject($l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  168. $template->addHeader();
  169. $template->addHeading($l->t('Email address changed for %s', [$user->getDisplayName()]), false);
  170. $template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator.'));
  171. if ($user->getEMailAddress()) {
  172. $template->addBodyText($l->t('The new email address is %s', [$user->getEMailAddress()]));
  173. }
  174. $template->addFooter();
  175. $message = $this->mailer->createMessage();
  176. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  177. $message->useTemplate($template);
  178. $this->mailer->send($message);
  179. }
  180. }
  181. }