@@ -34,169 +34,169 @@ |
||
| 34 | 34 | |
| 35 | 35 | class Hooks { |
| 36 | 36 | |
| 37 | - /** @var IActivityManager */ |
|
| 38 | - protected $activityManager; |
|
| 39 | - /** @var IUserManager */ |
|
| 40 | - protected $userManager; |
|
| 41 | - /** @var IUserSession */ |
|
| 42 | - protected $userSession; |
|
| 43 | - /** @var IURLGenerator */ |
|
| 44 | - protected $urlGenerator; |
|
| 45 | - /** @var IMailer */ |
|
| 46 | - protected $mailer; |
|
| 47 | - /** @var IConfig */ |
|
| 48 | - protected $config; |
|
| 49 | - /** @var IFactory */ |
|
| 50 | - protected $languageFactory; |
|
| 51 | - /** @var IL10N */ |
|
| 52 | - protected $l; |
|
| 53 | - |
|
| 54 | - public function __construct(IActivityManager $activityManager, |
|
| 55 | - IUserManager $userManager, |
|
| 56 | - IUserSession $userSession, |
|
| 57 | - IURLGenerator $urlGenerator, |
|
| 58 | - IMailer $mailer, |
|
| 59 | - IConfig $config, |
|
| 60 | - IFactory $languageFactory, |
|
| 61 | - IL10N $l) { |
|
| 62 | - $this->activityManager = $activityManager; |
|
| 63 | - $this->userManager = $userManager; |
|
| 64 | - $this->userSession = $userSession; |
|
| 65 | - $this->urlGenerator = $urlGenerator; |
|
| 66 | - $this->mailer = $mailer; |
|
| 67 | - $this->config = $config; |
|
| 68 | - $this->languageFactory = $languageFactory; |
|
| 69 | - $this->l = $l; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param string $uid |
|
| 74 | - * @throws \InvalidArgumentException |
|
| 75 | - * @throws \BadMethodCallException |
|
| 76 | - * @throws \Exception |
|
| 77 | - */ |
|
| 78 | - public function onChangePassword($uid) { |
|
| 79 | - $user = $this->userManager->get($uid); |
|
| 80 | - |
|
| 81 | - if (!$user instanceof IUser || $user->getEMailAddress() === null) { |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $event = $this->activityManager->generateEvent(); |
|
| 86 | - $event->setApp('settings') |
|
| 87 | - ->setType('personal_settings') |
|
| 88 | - ->setAffectedUser($user->getUID()); |
|
| 89 | - |
|
| 90 | - $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 91 | - |
|
| 92 | - $actor = $this->userSession->getUser(); |
|
| 93 | - if ($actor instanceof IUser) { |
|
| 94 | - if ($actor->getUID() !== $user->getUID()) { |
|
| 95 | - $this->l = $this->languageFactory->get( |
|
| 96 | - 'settings', |
|
| 97 | - $this->config->getUserValue( |
|
| 98 | - $user->getUID(), 'core', 'lang', |
|
| 99 | - $this->config->getSystemValue('default_language', 'en') |
|
| 100 | - ) |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 104 | - $event->setAuthor($actor->getUID()) |
|
| 105 | - ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
|
| 106 | - } else { |
|
| 107 | - $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]); |
|
| 108 | - $event->setAuthor($actor->getUID()) |
|
| 109 | - ->setSubject(Provider::PASSWORD_CHANGED_SELF); |
|
| 110 | - } |
|
| 111 | - } else { |
|
| 112 | - $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
|
| 113 | - $event->setSubject(Provider::PASSWORD_RESET); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $this->activityManager->publish($event); |
|
| 117 | - |
|
| 118 | - if ($user->getEMailAddress() !== null) { |
|
| 119 | - $template = $this->mailer->createEMailTemplate(); |
|
| 120 | - $template->addHeader(); |
|
| 121 | - $template->addHeading($this->l->t('Password changed for %s', $user->getDisplayName()), false); |
|
| 122 | - $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 123 | - $template->addFooter(); |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - $message = $this->mailer->createMessage(); |
|
| 127 | - $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
|
| 128 | - $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 129 | - $message->setBody($template->renderText(), 'text/plain'); |
|
| 130 | - $message->setHtmlBody($template->renderHtml()); |
|
| 131 | - |
|
| 132 | - $this->mailer->send($message); |
|
| 133 | - } |
|
| 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 | - |
|
| 144 | - if ($oldMailAddress === $user->getEMailAddress()) { |
|
| 145 | - // Email didn't really change, so don't create activities and emails |
|
| 146 | - return; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $event = $this->activityManager->generateEvent(); |
|
| 150 | - $event->setApp('settings') |
|
| 151 | - ->setType('personal_settings') |
|
| 152 | - ->setAffectedUser($user->getUID()); |
|
| 153 | - |
|
| 154 | - $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 155 | - |
|
| 156 | - $actor = $this->userSession->getUser(); |
|
| 157 | - if ($actor instanceof IUser) { |
|
| 158 | - if ($actor->getUID() !== $user->getUID()) { |
|
| 159 | - $this->l = $this->languageFactory->get( |
|
| 160 | - 'settings', |
|
| 161 | - $this->config->getUserValue( |
|
| 162 | - $user->getUID(), 'core', 'lang', |
|
| 163 | - $this->config->getSystemValue('default_language', 'en') |
|
| 164 | - ) |
|
| 165 | - ); |
|
| 166 | - |
|
| 167 | - $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 168 | - $event->setAuthor($actor->getUID()) |
|
| 169 | - ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]); |
|
| 170 | - } else { |
|
| 171 | - $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); |
|
| 172 | - $event->setAuthor($actor->getUID()) |
|
| 173 | - ->setSubject(Provider::EMAIL_CHANGED_SELF); |
|
| 174 | - } |
|
| 175 | - } else { |
|
| 176 | - $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
|
| 177 | - $event->setSubject(Provider::EMAIL_CHANGED); |
|
| 178 | - } |
|
| 179 | - $this->activityManager->publish($event); |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - if ($oldMailAddress !== null) { |
|
| 183 | - $template = $this->mailer->createEMailTemplate(); |
|
| 184 | - $template->addHeader(); |
|
| 185 | - $template->addHeading($this->l->t('Email address changed for %s', $user->getDisplayName()), false); |
|
| 186 | - $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 187 | - if ($user->getEMailAddress()) { |
|
| 188 | - $template->addBodyText($this->l->t('The new email address is %s', $user->getEMailAddress())); |
|
| 189 | - } |
|
| 190 | - $template->addFooter(); |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - $message = $this->mailer->createMessage(); |
|
| 194 | - $message->setTo([$oldMailAddress => $user->getDisplayName()]); |
|
| 195 | - $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 196 | - $message->setBody($template->renderText(), 'text/plain'); |
|
| 197 | - $message->setHtmlBody($template->renderHtml()); |
|
| 198 | - |
|
| 199 | - $this->mailer->send($message); |
|
| 200 | - } |
|
| 201 | - } |
|
| 37 | + /** @var IActivityManager */ |
|
| 38 | + protected $activityManager; |
|
| 39 | + /** @var IUserManager */ |
|
| 40 | + protected $userManager; |
|
| 41 | + /** @var IUserSession */ |
|
| 42 | + protected $userSession; |
|
| 43 | + /** @var IURLGenerator */ |
|
| 44 | + protected $urlGenerator; |
|
| 45 | + /** @var IMailer */ |
|
| 46 | + protected $mailer; |
|
| 47 | + /** @var IConfig */ |
|
| 48 | + protected $config; |
|
| 49 | + /** @var IFactory */ |
|
| 50 | + protected $languageFactory; |
|
| 51 | + /** @var IL10N */ |
|
| 52 | + protected $l; |
|
| 53 | + |
|
| 54 | + public function __construct(IActivityManager $activityManager, |
|
| 55 | + IUserManager $userManager, |
|
| 56 | + IUserSession $userSession, |
|
| 57 | + IURLGenerator $urlGenerator, |
|
| 58 | + IMailer $mailer, |
|
| 59 | + IConfig $config, |
|
| 60 | + IFactory $languageFactory, |
|
| 61 | + IL10N $l) { |
|
| 62 | + $this->activityManager = $activityManager; |
|
| 63 | + $this->userManager = $userManager; |
|
| 64 | + $this->userSession = $userSession; |
|
| 65 | + $this->urlGenerator = $urlGenerator; |
|
| 66 | + $this->mailer = $mailer; |
|
| 67 | + $this->config = $config; |
|
| 68 | + $this->languageFactory = $languageFactory; |
|
| 69 | + $this->l = $l; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param string $uid |
|
| 74 | + * @throws \InvalidArgumentException |
|
| 75 | + * @throws \BadMethodCallException |
|
| 76 | + * @throws \Exception |
|
| 77 | + */ |
|
| 78 | + public function onChangePassword($uid) { |
|
| 79 | + $user = $this->userManager->get($uid); |
|
| 80 | + |
|
| 81 | + if (!$user instanceof IUser || $user->getEMailAddress() === null) { |
|
| 82 | + return; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $event = $this->activityManager->generateEvent(); |
|
| 86 | + $event->setApp('settings') |
|
| 87 | + ->setType('personal_settings') |
|
| 88 | + ->setAffectedUser($user->getUID()); |
|
| 89 | + |
|
| 90 | + $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 91 | + |
|
| 92 | + $actor = $this->userSession->getUser(); |
|
| 93 | + if ($actor instanceof IUser) { |
|
| 94 | + if ($actor->getUID() !== $user->getUID()) { |
|
| 95 | + $this->l = $this->languageFactory->get( |
|
| 96 | + 'settings', |
|
| 97 | + $this->config->getUserValue( |
|
| 98 | + $user->getUID(), 'core', 'lang', |
|
| 99 | + $this->config->getSystemValue('default_language', 'en') |
|
| 100 | + ) |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 104 | + $event->setAuthor($actor->getUID()) |
|
| 105 | + ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
|
| 106 | + } else { |
|
| 107 | + $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]); |
|
| 108 | + $event->setAuthor($actor->getUID()) |
|
| 109 | + ->setSubject(Provider::PASSWORD_CHANGED_SELF); |
|
| 110 | + } |
|
| 111 | + } else { |
|
| 112 | + $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
|
| 113 | + $event->setSubject(Provider::PASSWORD_RESET); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $this->activityManager->publish($event); |
|
| 117 | + |
|
| 118 | + if ($user->getEMailAddress() !== null) { |
|
| 119 | + $template = $this->mailer->createEMailTemplate(); |
|
| 120 | + $template->addHeader(); |
|
| 121 | + $template->addHeading($this->l->t('Password changed for %s', $user->getDisplayName()), false); |
|
| 122 | + $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 123 | + $template->addFooter(); |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + $message = $this->mailer->createMessage(); |
|
| 127 | + $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
|
| 128 | + $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 129 | + $message->setBody($template->renderText(), 'text/plain'); |
|
| 130 | + $message->setHtmlBody($template->renderHtml()); |
|
| 131 | + |
|
| 132 | + $this->mailer->send($message); |
|
| 133 | + } |
|
| 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 | + |
|
| 144 | + if ($oldMailAddress === $user->getEMailAddress()) { |
|
| 145 | + // Email didn't really change, so don't create activities and emails |
|
| 146 | + return; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $event = $this->activityManager->generateEvent(); |
|
| 150 | + $event->setApp('settings') |
|
| 151 | + ->setType('personal_settings') |
|
| 152 | + ->setAffectedUser($user->getUID()); |
|
| 153 | + |
|
| 154 | + $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 155 | + |
|
| 156 | + $actor = $this->userSession->getUser(); |
|
| 157 | + if ($actor instanceof IUser) { |
|
| 158 | + if ($actor->getUID() !== $user->getUID()) { |
|
| 159 | + $this->l = $this->languageFactory->get( |
|
| 160 | + 'settings', |
|
| 161 | + $this->config->getUserValue( |
|
| 162 | + $user->getUID(), 'core', 'lang', |
|
| 163 | + $this->config->getSystemValue('default_language', 'en') |
|
| 164 | + ) |
|
| 165 | + ); |
|
| 166 | + |
|
| 167 | + $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 168 | + $event->setAuthor($actor->getUID()) |
|
| 169 | + ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]); |
|
| 170 | + } else { |
|
| 171 | + $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); |
|
| 172 | + $event->setAuthor($actor->getUID()) |
|
| 173 | + ->setSubject(Provider::EMAIL_CHANGED_SELF); |
|
| 174 | + } |
|
| 175 | + } else { |
|
| 176 | + $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
|
| 177 | + $event->setSubject(Provider::EMAIL_CHANGED); |
|
| 178 | + } |
|
| 179 | + $this->activityManager->publish($event); |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + if ($oldMailAddress !== null) { |
|
| 183 | + $template = $this->mailer->createEMailTemplate(); |
|
| 184 | + $template->addHeader(); |
|
| 185 | + $template->addHeading($this->l->t('Email address changed for %s', $user->getDisplayName()), false); |
|
| 186 | + $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 187 | + if ($user->getEMailAddress()) { |
|
| 188 | + $template->addBodyText($this->l->t('The new email address is %s', $user->getEMailAddress())); |
|
| 189 | + } |
|
| 190 | + $template->addFooter(); |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + $message = $this->mailer->createMessage(); |
|
| 194 | + $message->setTo([$oldMailAddress => $user->getDisplayName()]); |
|
| 195 | + $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 196 | + $message->setBody($template->renderText(), 'text/plain'); |
|
| 197 | + $message->setHtmlBody($template->renderHtml()); |
|
| 198 | + |
|
| 199 | + $this->mailer->send($message); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | 202 | } |