Hooks.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Settings;
  26. use OC\Settings\Activity\GroupProvider;
  27. use OC\Settings\Activity\Provider;
  28. use OCP\Activity\IManager as IActivityManager;
  29. use OCP\IConfig;
  30. use OCP\IGroup;
  31. use OCP\IGroupManager;
  32. use OCP\IL10N;
  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 IL10N */
  57. protected $l;
  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. IL10N $l) {
  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->l = $l;
  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. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  94. $actor = $this->userSession->getUser();
  95. if ($actor instanceof IUser) {
  96. if ($actor->getUID() !== $user->getUID()) {
  97. $this->l = $this->languageFactory->get(
  98. 'settings',
  99. $this->config->getUserValue(
  100. $user->getUID(), 'core', 'lang',
  101. $this->config->getSystemValue('default_language', 'en')
  102. )
  103. );
  104. $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
  105. $event->setAuthor($actor->getUID())
  106. ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
  107. } else {
  108. $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
  109. $event->setAuthor($actor->getUID())
  110. ->setSubject(Provider::PASSWORD_CHANGED_SELF);
  111. }
  112. } else {
  113. $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
  114. $event->setSubject(Provider::PASSWORD_RESET);
  115. }
  116. $this->activityManager->publish($event);
  117. if ($user->getEMailAddress() !== null) {
  118. $template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
  119. 'displayname' => $user->getDisplayName(),
  120. 'emailAddress' => $user->getEMailAddress(),
  121. 'instanceUrl' => $instanceUrl,
  122. ]);
  123. $template->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  124. $template->addHeader();
  125. $template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
  126. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  127. $template->addFooter();
  128. $message = $this->mailer->createMessage();
  129. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  130. $message->useTemplate($template);
  131. $this->mailer->send($message);
  132. }
  133. }
  134. /**
  135. * @param IUser $user
  136. * @param string|null $oldMailAddress
  137. * @throws \InvalidArgumentException
  138. * @throws \BadMethodCallException
  139. */
  140. public function onChangeEmail(IUser $user, $oldMailAddress) {
  141. if ($oldMailAddress === $user->getEMailAddress() ||
  142. $user->getLastLogin() === 0) {
  143. // Email didn't really change or user didn't login,
  144. // so don't create activities and emails.
  145. return;
  146. }
  147. $event = $this->activityManager->generateEvent();
  148. $event->setApp('settings')
  149. ->setType('personal_settings')
  150. ->setAffectedUser($user->getUID());
  151. $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
  152. $actor = $this->userSession->getUser();
  153. if ($actor instanceof IUser) {
  154. $subject = Provider::EMAIL_CHANGED_SELF;
  155. if ($actor->getUID() !== $user->getUID()) {
  156. $this->l = $this->languageFactory->get(
  157. 'settings',
  158. $this->config->getUserValue(
  159. $user->getUID(), 'core', 'lang',
  160. $this->config->getSystemValue('default_language', 'en')
  161. )
  162. );
  163. $subject = Provider::EMAIL_CHANGED;
  164. }
  165. $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
  166. $event->setAuthor($actor->getUID())
  167. ->setSubject($subject);
  168. } else {
  169. $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
  170. $event->setSubject(Provider::EMAIL_CHANGED);
  171. }
  172. $this->activityManager->publish($event);
  173. if ($oldMailAddress !== null) {
  174. $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
  175. 'displayname' => $user->getDisplayName(),
  176. 'newEMailAddress' => $user->getEMailAddress(),
  177. 'oldEMailAddress' => $oldMailAddress,
  178. 'instanceUrl' => $instanceUrl,
  179. ]);
  180. $template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
  181. $template->addHeader();
  182. $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
  183. $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
  184. if ($user->getEMailAddress()) {
  185. $template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
  186. }
  187. $template->addFooter();
  188. $message = $this->mailer->createMessage();
  189. $message->setTo([$oldMailAddress => $user->getDisplayName()]);
  190. $message->useTemplate($template);
  191. $this->mailer->send($message);
  192. }
  193. }
  194. /**
  195. * @param IGroup $group
  196. * @param IUser $user
  197. * @throws \InvalidArgumentException
  198. * @throws \BadMethodCallException
  199. */
  200. public function addUserToGroup(IGroup $group, IUser $user) {
  201. $subAdminManager = $this->groupManager->getSubAdmin();
  202. $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
  203. $usersToNotify[] = $user;
  204. $event = $this->activityManager->generateEvent();
  205. $event->setApp('settings')
  206. ->setType('group_settings');
  207. $actor = $this->userSession->getUser();
  208. if ($actor instanceof IUser) {
  209. $event->setAuthor($actor->getUID())
  210. ->setSubject(GroupProvider::ADDED_TO_GROUP, [
  211. 'user' => $user->getUID(),
  212. 'group' => $group->getGID(),
  213. 'actor' => $actor->getUID(),
  214. ]);
  215. } else {
  216. $event->setSubject(GroupProvider::ADDED_TO_GROUP, [
  217. 'user' => $user->getUID(),
  218. 'group' => $group->getGID(),
  219. ]);
  220. }
  221. foreach ($usersToNotify as $userToNotify) {
  222. $event->setAffectedUser($userToNotify->getUID());
  223. $this->activityManager->publish($event);
  224. }
  225. }
  226. /**
  227. * @param IGroup $group
  228. * @param IUser $user
  229. * @throws \InvalidArgumentException
  230. * @throws \BadMethodCallException
  231. */
  232. public function removeUserFromGroup(IGroup $group, IUser $user) {
  233. $subAdminManager = $this->groupManager->getSubAdmin();
  234. $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
  235. $usersToNotify[] = $user;
  236. $event = $this->activityManager->generateEvent();
  237. $event->setApp('settings')
  238. ->setType('group_settings');
  239. $actor = $this->userSession->getUser();
  240. if ($actor instanceof IUser) {
  241. $event->setAuthor($actor->getUID())
  242. ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
  243. 'user' => $user->getUID(),
  244. 'group' => $group->getGID(),
  245. 'actor' => $actor->getUID(),
  246. ]);
  247. } else {
  248. $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
  249. 'user' => $user->getUID(),
  250. 'group' => $group->getGID(),
  251. ]);
  252. }
  253. foreach ($usersToNotify as $userToNotify) {
  254. $event->setAffectedUser($userToNotify->getUID());
  255. $this->activityManager->publish($event);
  256. }
  257. }
  258. }