diff --git a/lib/Listener/LoginMailListener.php b/lib/Listener/LoginMailListener.php index 1a1adc64..ad651a1f 100644 --- a/lib/Listener/LoginMailListener.php +++ b/lib/Listener/LoginMailListener.php @@ -3,6 +3,7 @@ declare(strict_types=1); /** * @copyright Copyright (c) 2019, Roeland Jago Douma + * @copyright Copyright (c) 2024, Joshua Richards () * * @author Roeland Jago Douma * @@ -26,6 +27,7 @@ namespace OCA\SuspiciousLogin\Listener; use Exception; +use OCP\IConfig; use OCA\SuspiciousLogin\Event\SuspiciousLoginEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; @@ -40,21 +42,25 @@ class LoginMailListener implements IEventListener { /** @var ILogger */ private $logger; + /** @var IMailer */ private $mailer; + /** @var IUserManager */ private $userManager; + /** @var IL10N */ private $l; + + /** @var IConfig */ + protected $config; - public function __construct(ILogger $logger, - IMailer $mailer, - IUserManager $userManager, - IL10N $l) { + public function __construct(ILogger $logger, IMailer $mailer, IUserManager $userManager, IL10N $l, IConfig $config) { $this->logger = $logger; $this->mailer = $mailer; $this->userManager = $userManager; $this->l = $l; + $this->config = $config; } public function handle(Event $event): void { @@ -86,6 +92,9 @@ public function handle(Event $event): void { } private function getMail(SuspiciousLoginEvent $event, IUser $user): IMessage { + $suspiciousIp = $event->getIp() ?? ''; + $addButton = $this->config->getAppValue('suspicious_login', 'show_more_info_button', '1') === "1"; + $message = $this->mailer->createMessage(); $emailTemplate = $this->mailer->createEMailTemplate('suspiciousLogin.suspiciousLoginDetected'); @@ -94,9 +103,24 @@ private function getMail(SuspiciousLoginEvent $event, IUser $user): IMessage { $emailTemplate->addHeading( $this->l->t('New login location detected') ); + + $additionalText = ''; + // Add explanation for more information about the IP-address (if enabled) + if ($addButton) { + $additionalText = ' ' . $this->l->t('You can get more info by pressing the button which will open %s and show info about the suspicious IP-address.', 'https://iplookup.flagfox.net'); + } $emailTemplate->addBodyText( - $this->l->t('A new login into your account was detected. The IP address %s was classified as suspicious. If this was you, you can ignore this message. Otherwise you should change your password.', [$event->getIp()]) + $this->l->t('A new login into your account was detected. The IP address %s was classified as suspicious. If this was you, you can ignore this message. Otherwise you should change your password.', $suspiciousIp) . $additionalText ); + // Add button for more information about the IP-address (if enabled) + if ($addButton) { + $link = 'https://iplookup.flagfox.net/?ip=' . $suspiciousIp; + $emailTemplate->addBodyButton( + htmlspecialchars($this->l->t('More information ↗')), + $link, + false + ); + } $emailTemplate->addFooter(); $message->setTo([$user->getEMailAddress()]); $message->useTemplate($emailTemplate);