Hooks.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\IL10N;
  36. use OCP\IURLGenerator;
  37. use OCP\IUser;
  38. use OCP\IUserManager;
  39. use OCP\IUserSession;
  40. use OCP\L10N\IFactory;
  41. use OCP\Mail\IMailer;
  42. class Hooks {
  43. /** @var IActivityManager */
  44. protected $activityManager;
  45. /** @var IGroupManager|\OC\Group\Manager */
  46. protected $groupManager;
  47. /** @var IUserManager */
  48. protected $userManager;
  49. /** @var IUserSession */
  50. protected $userSession;
  51. /** @var IURLGenerator */
  52. protected $urlGenerator;
  53. /** @var IMailer */
  54. protected $mailer;
  55. /** @var IConfig */
  56. protected $config;
  57. /** @var IFactory */
  58. protected $languageFactory;
  59. /** @var IL10N */
  60. protected $l;
  61. public function __construct(IActivityManager $activityManager,
  62. IGroupManager $groupManager,
  63. IUserManager $userManager,
  64. IUserSession $userSession,
  65. IURLGenerator $urlGenerator,
  66. IMailer $mailer,
  67. IConfig $config,
  68. IFactory $languageFactory,
  69. IL10N $l) {
  70. $this->activityManager = $activityManager;
  71. $this->groupManager = $groupManager;
  72. $this->userManager = $userManager;
  73. $this->userSession = $userSession;
  74. $this->urlGenerator = $urlGenerator;
  75. $this->mailer = $mailer;
  76. $this->config = $config;
  77. $this->languageFactory = $languageFactory;
  78. $this->l = $l;
  79. }
  80. /**
  81. * @param string $uid
  82. * @throws \InvalidArgumentException
  83. * @throws \BadMethodCallException
  84. * @throws \Exception
  85. */
  86. public function onChangePassword($uid) {
  87. $user = $this->userManager->get($uid);
  88. if (!$user instanceof IUser || $user->getLastLogin() === 0) {
  89. // User didn't login, so don't create activities and emails.
  90. return;
  91. }
  92. $event = $this->activityManager->generateEvent();
  93. $event->setApp('settings')
  94. ->setType('personal_settings')
  95. ->setAffectedUser($user->getUID());
  96. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  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. $this->l = $this->languageFactory->get(
  102. 'settings',
  103. $this->config->getUserValue(
  104. $user->getUID(), 'core', 'lang',
  105. $this->config->getSystemValue('default_language', 'en')
  106. )
  107. );
  108. $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  109. $event->setAuthor($actor->getUID())
  110. ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
  111. } else {
  112. // User changed their password themselves through settings
  113. $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
  114. $event->setAuthor($actor->getUID())
  115. ->setSubject(Provider::PASSWORD_CHANGED_SELF);
  116. }
  117. } else {
  118. if (\OC::$CLI) {
  119. // Admin used occ to reset the password
  120. $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
  121. $event->setSubject(Provider::PASSWORD_RESET);
  122. } else {
  123. // User reset their password from Lost page
  124. $text = $this->l->t('Your password on %s was reset.', [$instanceUrl]);
  125. $event->setSubject(Provider::PASSWORD_RESET_SELF);
  126. }
  127. }
  128. $this->activityManager->publish($event);
  129. if ($user->getEMailAddress() !== null) {
  130. $template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
  131. 'displayname' => $user->getDisplayName(),
  132. 'emailAddress' => $user->getEMailAddress(),
  133. 'instanceUrl' => $instanceUrl,
  134. ]);
  135. $template->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  136. $template->addHeader();
  137. $template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
  138. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  139. $template->addFooter();
  140. $message = $this->mailer->createMessage();
  141. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  142. $message->useTemplate($template);
  143. $this->mailer->send($message);
  144. }
  145. }
  146. /**
  147. * @param IUser $user
  148. * @param string|null $oldMailAddress
  149. * @throws \InvalidArgumentException
  150. * @throws \BadMethodCallException
  151. */
  152. public function onChangeEmail(IUser $user, $oldMailAddress) {
  153. if ($oldMailAddress === $user->getEMailAddress() ||
  154. $user->getLastLogin() === 0) {
  155. // Email didn't really change or user didn't login,
  156. // so don't create activities and emails.
  157. return;
  158. }
  159. $event = $this->activityManager->generateEvent();
  160. $event->setApp('settings')
  161. ->setType('personal_settings')
  162. ->setAffectedUser($user->getUID());
  163. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  164. $actor = $this->userSession->getUser();
  165. if ($actor instanceof IUser) {
  166. $subject = Provider::EMAIL_CHANGED_SELF;
  167. if ($actor->getUID() !== $user->getUID()) {
  168. $this->l = $this->languageFactory->get(
  169. 'settings',
  170. $this->config->getUserValue(
  171. $user->getUID(), 'core', 'lang',
  172. $this->config->getSystemValue('default_language', 'en')
  173. )
  174. );
  175. $subject = Provider::EMAIL_CHANGED;
  176. }
  177. $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
  178. $event->setAuthor($actor->getUID())
  179. ->setSubject($subject);
  180. } else {
  181. $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  182. $event->setSubject(Provider::EMAIL_CHANGED);
  183. }
  184. $this->activityManager->publish($event);
  185. if ($oldMailAddress !== null) {
  186. $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
  187. 'displayname' => $user->getDisplayName(),
  188. 'newEMailAddress' => $user->getEMailAddress(),
  189. 'oldEMailAddress' => $oldMailAddress,
  190. 'instanceUrl' => $instanceUrl,
  191. ]);
  192. $template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  193. $template->addHeader();
  194. $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
  195. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  196. if ($user->getEMailAddress()) {
  197. $template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
  198. }
  199. $template->addFooter();
  200. $message = $this->mailer->createMessage();
  201. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  202. $message->useTemplate($template);
  203. $this->mailer->send($message);
  204. }
  205. }
  206. /**
  207. * @param IGroup $group
  208. * @param IUser $user
  209. * @throws \InvalidArgumentException
  210. * @throws \BadMethodCallException
  211. */
  212. public function addUserToGroup(IGroup $group, IUser $user): void {
  213. $subAdminManager = $this->groupManager->getSubAdmin();
  214. $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
  215. $usersToNotify[] = $user;
  216. $event = $this->activityManager->generateEvent();
  217. $event->setApp('settings')
  218. ->setType('group_settings');
  219. $actor = $this->userSession->getUser();
  220. if ($actor instanceof IUser) {
  221. $event->setAuthor($actor->getUID())
  222. ->setSubject(GroupProvider::ADDED_TO_GROUP, [
  223. 'user' => $user->getUID(),
  224. 'group' => $group->getGID(),
  225. 'actor' => $actor->getUID(),
  226. ]);
  227. } else {
  228. $event->setSubject(GroupProvider::ADDED_TO_GROUP, [
  229. 'user' => $user->getUID(),
  230. 'group' => $group->getGID(),
  231. ]);
  232. }
  233. foreach ($usersToNotify as $userToNotify) {
  234. $event->setAffectedUser($userToNotify->getUID());
  235. $this->activityManager->publish($event);
  236. }
  237. }
  238. /**
  239. * @param IGroup $group
  240. * @param IUser $user
  241. * @throws \InvalidArgumentException
  242. * @throws \BadMethodCallException
  243. */
  244. public function removeUserFromGroup(IGroup $group, IUser $user): void {
  245. $subAdminManager = $this->groupManager->getSubAdmin();
  246. $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
  247. $usersToNotify[] = $user;
  248. $event = $this->activityManager->generateEvent();
  249. $event->setApp('settings')
  250. ->setType('group_settings');
  251. $actor = $this->userSession->getUser();
  252. if ($actor instanceof IUser) {
  253. $event->setAuthor($actor->getUID())
  254. ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
  255. 'user' => $user->getUID(),
  256. 'group' => $group->getGID(),
  257. 'actor' => $actor->getUID(),
  258. ]);
  259. } else {
  260. $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
  261. 'user' => $user->getUID(),
  262. 'group' => $group->getGID(),
  263. ]);
  264. }
  265. foreach ($usersToNotify as $userToNotify) {
  266. $event->setAffectedUser($userToNotify->getUID());
  267. $this->activityManager->publish($event);
  268. }
  269. }
  270. }