Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 53 additions & 38 deletions lib/Notifications/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@
use OCP\Notification\IAction;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;

class Notifier implements INotifier {

/** @var IConfig */
protected $config;


/** @var IFactory */
private $factory;

/** @var IRequest */
private $request;

/** @var IConfig */
protected $config;

/** @var IURLGenerator */
protected $url;

public function __construct(IFactory $factory, IRequest $request, IConfig $config, IURLGenerator $urlGenerator) {
public function __construct(
IFactory $factory,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator
) {
$this->config = $config;
$this->factory = $factory;
$this->request = $request;
Expand All @@ -51,49 +57,58 @@ public function getName(): string {
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== Application::APP_ID) {
// Not my app => throw
throw new InvalidArgumentException();
throw new UnknownNotificationException('Unknown app id');
}

// Read the language from the notification
$l = $this->factory->get(Application::APP_ID, $languageCode);

/** @var string $suspiciousIp */
$params = $notification->getSubjectParameters();
$suspiciousIp = $params['ip'] ?? '';
$subjectParameters = $notification->getSubjectParameters();

switch ($notification->getSubject()) {
case 'suspicious_login_detected':
if ($suspiciousIp === $this->request->getRemoteAddress()) {
// It is the potential attacking user so don't render the notification for them
throw new InvalidArgumentException();
}

$additionalText = '';
// Add button for more information about the IP-address
if ($this->config->getAppValue('suspicious_login', 'show_more_info_button', '1') === "1") {
$action = $notification->createAction();
$label = $l->t('More information ↗');
$link = 'https://iplookup.flagfox.net/?ip=' . $suspiciousIp;
$action->setLabel($label)
->setParsedLabel($label)
->setLink($link, IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
$additionalText = ' ' . $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');
}

$notification->setParsedSubject(
$l->t('New login detected')
)->setParsedMessage(
$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
);

$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('suspicious_login', 'app.svg')));

return $notification;
$notification = $this->parseSuspiciousLoginDetected($notification, $subjectParameters, $l);
default:
// Unknown subject => Unknown notification => throw
throw new InvalidArgumentException();
throw new UnknownNotificationException('Unknown subject');
}
return $notification;
}

protected function parseSuspiciousLoginDetected(INotification $notification, array $subjectParameters, IL10N $l): INotification {
$additionalText = '';
$suspiciousIp = $subjectParameters['ip'] ?? '';

if ($suspiciousIp === $this->request->getRemoteAddress()) {
// It is the potential attacking user so don't render the notification for them
throw new InvalidArgumentException();
}

if ($this->config->getAppValue('suspicious_login', 'show_more_info_button', '1') === "1") {
// If enabled (the default), add button to retrieve more information about the IP address
$label = $l->t('More information ↗');
$link = 'https://iplookup.flagfox.net/?ip=' . $suspiciousIp;
$additionalText =
' '
. $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');

$action = $notification->createAction();
$action->setLabel($label)
->setParsedLabel($label)
->setLink($link, IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
}

$notification->setParsedSubject($l->t('New login detected'))
->setParsedMessage(
$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);

$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('suspicious_login', 'app.svg')));

return $notification;
}
}