Hooks.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Settings;
  29. use OCA\Settings\Activity\GroupProvider;
  30. use OCA\Settings\Activity\Provider;
  31. use OCP\Activity\IManager as IActivityManager;
  32. use OCP\IConfig;
  33. use OCP\IGroup;
  34. use OCP\IGroupManager;
  35. use OCP\IURLGenerator;
  36. use OCP\IUser;
  37. use OCP\IUserManager;
  38. use OCP\IUserSession;
  39. use OCP\L10N\IFactory;
  40. use OCP\Mail\IMailer;
  41. class Hooks {
  42. /** @var IActivityManager */
  43. protected $activityManager;
  44. /** @var IGroupManager|\OC\Group\Manager */
  45. protected $groupManager;
  46. /** @var IUserManager */
  47. protected $userManager;
  48. /** @var IUserSession */
  49. protected $userSession;
  50. /** @var IURLGenerator */
  51. protected $urlGenerator;
  52. /** @var IMailer */
  53. protected $mailer;
  54. /** @var IConfig */
  55. protected $config;
  56. /** @var IFactory */
  57. protected $languageFactory;
  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. $this->activityManager = $activityManager;
  67. $this->groupManager = $groupManager;
  68. $this->userManager = $userManager;
  69. $this->userSession = $userSession;
  70. $this->urlGenerator = $urlGenerator;
  71. $this->mailer = $mailer;
  72. $this->config = $config;
  73. $this->languageFactory = $languageFactory;
  74. }
  75. /**
  76. * @param string $uid
  77. * @throws \InvalidArgumentException
  78. * @throws \BadMethodCallException
  79. * @throws \Exception
  80. */
  81. public function onChangePassword($uid) {
  82. $user = $this->userManager->get($uid);
  83. if (!$user instanceof IUser || $user->getLastLogin() === 0) {
  84. // User didn't login, so don't create activities and emails.
  85. return;
  86. }
  87. $event = $this->activityManager->generateEvent();
  88. $event->setApp('settings')
  89. ->setType('personal_settings')
  90. ->setAffectedUser($user->getUID());
  91. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  92. $language = $this->languageFactory->getUserLanguage($user);
  93. $l = $this->languageFactory->get('settings', $language);
  94. $actor = $this->userSession->getUser();
  95. if ($actor instanceof IUser) {
  96. if ($actor->getUID() !== $user->getUID()) {
  97. // Admin changed the password through the user panel
  98. $text = $l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  99. $event->setAuthor($actor->getUID())
  100. ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
  101. } else {
  102. // User changed their password themselves through settings
  103. $text = $l->t('Your password on %s was changed.', [$instanceUrl]);
  104. $event->setAuthor($actor->getUID())
  105. ->setSubject(Provider::PASSWORD_CHANGED_SELF);
  106. }
  107. } else {
  108. if (\OC::$CLI) {
  109. // Admin used occ to reset the password
  110. $text = $l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
  111. $event->setSubject(Provider::PASSWORD_RESET);
  112. } else {
  113. // User reset their password from Lost page
  114. $text = $l->t('Your password on %s was reset.', [$instanceUrl]);
  115. $event->setSubject(Provider::PASSWORD_RESET_SELF);
  116. }
  117. }
  118. $this->activityManager->publish($event);
  119. if ($user->getEMailAddress() !== null) {
  120. $template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
  121. 'displayname' => $user->getDisplayName(),
  122. 'emailAddress' => $user->getEMailAddress(),
  123. 'instanceUrl' => $instanceUrl,
  124. ]);
  125. $template->setSubject($l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  126. $template->addHeader();
  127. $template->addHeading($l->t('Password changed for %s', [$user->getDisplayName()]), false);
  128. $template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator.'));
  129. $template->addFooter();
  130. $message = $this->mailer->createMessage();
  131. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  132. $message->useTemplate($template);
  133. $this->mailer->send($message);
  134. }
  135. }
  136. /**
  137. * @param IUser $user
  138. * @param string|null $oldMailAddress
  139. * @throws \InvalidArgumentException
  140. * @throws \BadMethodCallException
  141. */
  142. public function onChangeEmail(IUser $user, $oldMailAddress) {
  143. if ($oldMailAddress === $user->getEMailAddress() ||
  144. $user->getLastLogin() === 0) {
  145. // Email didn't really change or user didn't login,
  146. // so don't create activities and emails.
  147. return;
  148. }
  149. $event = $this->activityManager->generateEvent();
  150. $event->setApp('settings')
  151. ->setType('personal_settings')
  152. ->setAffectedUser($user->getUID());
  153. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  154. $language = $this->languageFactory->getUserLanguage($user);
  155. $l = $this->languageFactory->get('settings', $language);
  156. $actor = $this->userSession->getUser();
  157. if ($actor instanceof IUser) {
  158. $subject = Provider::EMAIL_CHANGED_SELF;
  159. if ($actor->getUID() !== $user->getUID()) {
  160. $subject = Provider::EMAIL_CHANGED;
  161. }
  162. $text = $l->t('Your email address on %s was changed.', [$instanceUrl]);
  163. $event->setAuthor($actor->getUID())
  164. ->setSubject($subject);
  165. } else {
  166. $text = $l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  167. $event->setSubject(Provider::EMAIL_CHANGED);
  168. }
  169. $this->activityManager->publish($event);
  170. if ($oldMailAddress !== null) {
  171. $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
  172. 'displayname' => $user->getDisplayName(),
  173. 'newEMailAddress' => $user->getEMailAddress(),
  174. 'oldEMailAddress' => $oldMailAddress,
  175. 'instanceUrl' => $instanceUrl,
  176. ]);
  177. $template->setSubject($l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  178. $template->addHeader();
  179. $template->addHeading($l->t('Email address changed for %s', [$user->getDisplayName()]), false);
  180. $template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator.'));
  181. if ($user->getEMailAddress()) {
  182. $template->addBodyText($l->t('The new email address is %s', [$user->getEMailAddress()]));
  183. }
  184. $template->addFooter();
  185. $message = $this->mailer->createMessage();
  186. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  187. $message->useTemplate($template);
  188. $this->mailer->send($message);
  189. }
  190. }
  191. /**
  192. * @param IGroup $group
  193. * @param IUser $user
  194. * @throws \InvalidArgumentException
  195. * @throws \BadMethodCallException
  196. */
  197. public function addUserToGroup(IGroup $group, IUser $user): void {
  198. $subAdminManager = $this->groupManager->getSubAdmin();
  199. $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
  200. $usersToNotify[] = $user;
  201. $event = $this->activityManager->generateEvent();
  202. $event->setApp('settings')
  203. ->setType('group_settings');
  204. $actor = $this->userSession->getUser();
  205. if ($actor instanceof IUser) {
  206. $event->setAuthor($actor->getUID())
  207. ->setSubject(GroupProvider::ADDED_TO_GROUP, [
  208. 'user' => $user->getUID(),
  209. 'group' => $group->getGID(),
  210. 'actor' => $actor->getUID(),
  211. ]);
  212. } else {
  213. $event->setSubject(GroupProvider::ADDED_TO_GROUP, [
  214. 'user' => $user->getUID(),
  215. 'group' => $group->getGID(),
  216. ]);
  217. }
  218. foreach ($usersToNotify as $userToNotify) {
  219. $event->setAffectedUser($userToNotify->getUID());
  220. $this->activityManager->publish($event);
  221. }
  222. }
  223. /**
  224. * @param IGroup $group
  225. * @param IUser $user
  226. * @throws \InvalidArgumentException
  227. * @throws \BadMethodCallException
  228. */
  229. public function removeUserFromGroup(IGroup $group, IUser $user): void {
  230. $subAdminManager = $this->groupManager->getSubAdmin();
  231. $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
  232. $usersToNotify[] = $user;
  233. $event = $this->activityManager->generateEvent();
  234. $event->setApp('settings')
  235. ->setType('group_settings');
  236. $actor = $this->userSession->getUser();
  237. if ($actor instanceof IUser) {
  238. $event->setAuthor($actor->getUID())
  239. ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
  240. 'user' => $user->getUID(),
  241. 'group' => $group->getGID(),
  242. 'actor' => $actor->getUID(),
  243. ]);
  244. } else {
  245. $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
  246. 'user' => $user->getUID(),
  247. 'group' => $group->getGID(),
  248. ]);
  249. }
  250. foreach ($usersToNotify as $userToNotify) {
  251. $event->setAffectedUser($userToNotify->getUID());
  252. $this->activityManager->publish($event);
  253. }
  254. }
  255. }