Skip to content

Commit

Permalink
Merge pull request #873 from nextcloud/feat/email-info-button
Browse files Browse the repository at this point in the history
feat(suspicious_login): Add "more info" button to email notifications
  • Loading branch information
st3iny authored May 12, 2024
2 parents 30b748f + 740874e commit 93aed8c
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions lib/Listener/LoginMailListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
* @copyright Copyright (c) 2024, Joshua Richards (<[email protected]>)
*
* @author Roeland Jago Douma <[email protected]>
*
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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');

Expand All @@ -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);
Expand Down

0 comments on commit 93aed8c

Please sign in to comment.