Hooks.php 6.0 KB

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