1
0

Hooks.php 7.5 KB

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