RemoteWipeEmailListener.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  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\Authentication\Listeners;
  26. use Exception;
  27. use OC\Authentication\Events\RemoteWipeFinished;
  28. use OC\Authentication\Events\RemoteWipeStarted;
  29. use OCP\EventDispatcher\Event;
  30. use OCP\EventDispatcher\IEventListener;
  31. use OCP\IL10N;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use OCP\L10N\IFactory as IL10nFactory;
  35. use OCP\Mail\IMailer;
  36. use OCP\Mail\IMessage;
  37. use Psr\Log\LoggerInterface;
  38. use function substr;
  39. /**
  40. * @template-implements IEventListener<\OC\Authentication\Events\ARemoteWipeEvent>
  41. */
  42. class RemoteWipeEmailListener implements IEventListener {
  43. /** @var IMailer */
  44. private $mailer;
  45. /** @var IUserManager */
  46. private $userManager;
  47. /** @var IL10N */
  48. private $l10n;
  49. /** @var LoggerInterface */
  50. private $logger;
  51. public function __construct(IMailer $mailer,
  52. IUserManager $userManager,
  53. IL10nFactory $l10nFactory,
  54. LoggerInterface $logger) {
  55. $this->mailer = $mailer;
  56. $this->userManager = $userManager;
  57. $this->l10n = $l10nFactory->get('core');
  58. $this->logger = $logger;
  59. }
  60. /**
  61. * @param Event $event
  62. */
  63. public function handle(Event $event): void {
  64. if ($event instanceof RemoteWipeStarted) {
  65. $uid = $event->getToken()->getUID();
  66. $user = $this->userManager->get($uid);
  67. if ($user === null) {
  68. $this->logger->warning("not sending a wipe started email because user <$uid> does not exist (anymore)");
  69. return;
  70. }
  71. if ($user->getEMailAddress() === null) {
  72. $this->logger->info("not sending a wipe started email because user <$uid> has no email set");
  73. return;
  74. }
  75. try {
  76. $this->mailer->send(
  77. $this->getWipingStartedMessage($event, $user)
  78. );
  79. } catch (Exception $e) {
  80. $this->logger->error("Could not send remote wipe started email to <$uid>", [
  81. 'exception' => $e,
  82. ]);
  83. }
  84. } elseif ($event instanceof RemoteWipeFinished) {
  85. $uid = $event->getToken()->getUID();
  86. $user = $this->userManager->get($uid);
  87. if ($user === null) {
  88. $this->logger->warning("not sending a wipe finished email because user <$uid> does not exist (anymore)");
  89. return;
  90. }
  91. if ($user->getEMailAddress() === null) {
  92. $this->logger->info("not sending a wipe finished email because user <$uid> has no email set");
  93. return;
  94. }
  95. try {
  96. $this->mailer->send(
  97. $this->getWipingFinishedMessage($event, $user)
  98. );
  99. } catch (Exception $e) {
  100. $this->logger->error("Could not send remote wipe finished email to <$uid>", [
  101. 'exception' => $e,
  102. ]);
  103. }
  104. }
  105. }
  106. private function getWipingStartedMessage(RemoteWipeStarted $event, IUser $user): IMessage {
  107. $message = $this->mailer->createMessage();
  108. $emailTemplate = $this->mailer->createEMailTemplate('auth.RemoteWipeStarted');
  109. $plainHeading = $this->l10n->t('Wiping of device %s has started', [$event->getToken()->getName()]);
  110. $htmlHeading = $this->l10n->t('Wiping of device »%s« has started', [$event->getToken()->getName()]);
  111. $emailTemplate->setSubject(
  112. $this->l10n->t(
  113. '»%s« started remote wipe',
  114. [
  115. substr($event->getToken()->getName(), 0, 15)
  116. ]
  117. )
  118. );
  119. $emailTemplate->addHeader();
  120. $emailTemplate->addHeading(
  121. $htmlHeading,
  122. $plainHeading
  123. );
  124. $emailTemplate->addBodyText(
  125. $this->l10n->t('Device or application »%s« has started the remote wipe process. You will receive another email once the process has finished', [$event->getToken()->getName()])
  126. );
  127. $emailTemplate->addFooter();
  128. $message->setTo([$user->getEMailAddress()]);
  129. $message->useTemplate($emailTemplate);
  130. return $message;
  131. }
  132. private function getWipingFinishedMessage(RemoteWipeFinished $event, IUser $user): IMessage {
  133. $message = $this->mailer->createMessage();
  134. $emailTemplate = $this->mailer->createEMailTemplate('auth.RemoteWipeFinished');
  135. $plainHeading = $this->l10n->t('Wiping of device %s has finished', [$event->getToken()->getName()]);
  136. $htmlHeading = $this->l10n->t('Wiping of device »%s« has finished', [$event->getToken()->getName()]);
  137. $emailTemplate->setSubject(
  138. $this->l10n->t(
  139. '»%s« finished remote wipe',
  140. [
  141. substr($event->getToken()->getName(), 0, 15)
  142. ]
  143. )
  144. );
  145. $emailTemplate->addHeader();
  146. $emailTemplate->addHeading(
  147. $htmlHeading,
  148. $plainHeading
  149. );
  150. $emailTemplate->addBodyText(
  151. $this->l10n->t('Device or application »%s« has finished the remote wipe process.', [$event->getToken()->getName()])
  152. );
  153. $emailTemplate->addFooter();
  154. $message->setTo([$user->getEMailAddress()]);
  155. $message->useTemplate($emailTemplate);
  156. return $message;
  157. }
  158. }