-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #873 from nextcloud/feat/email-info-button
feat(suspicious_login): Add "more info" button to email notifications
- Loading branch information
Showing
1 changed file
with
29 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
* | ||
|
@@ -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); | ||
|