-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathNotifier.php
More file actions
103 lines (83 loc) · 3.17 KB
/
Notifier.php
File metadata and controls
103 lines (83 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\SuspiciousLogin\Notifications;
use OCA\SuspiciousLogin\AppInfo\Application;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
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 IURLGenerator */
protected $url;
public function __construct(IFactory $factory, IRequest $request, IConfig $config, IURLGenerator $urlGenerator) {
$this->config = $config;
$this->factory = $factory;
$this->request = $request;
$this->url = $urlGenerator;
}
#[\Override]
public function getID(): string {
return Application::APP_ID;
}
#[\Override]
public function getName(): string {
return $this->factory->get(Application::APP_ID)->t('Suspicious Login');
}
#[\Override]
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== Application::APP_ID) {
// Not my app => throw
throw new UnknownNotificationException();
}
// Read the language from the notification
$l = $this->factory->get(Application::APP_ID, $languageCode);
/** @var string $suspiciousIp */
$params = $notification->getSubjectParameters();
$suspiciousIp = $params['ip'] ?? '';
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 UnknownNotificationException();
}
$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('Open %s ↗', ['iplookup.flagfox.net']);
$link = 'https://iplookup.flagfox.net/?ip=' . $suspiciousIp;
$action->setLabel($label)
->setParsedLabel($label)
->setLink($link, IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
// TODO: deduplicate with \OCA\SuspiciousLogin\Listener\LoginMailListener::getMail
$additionalText = ' ' . $l->t('More info about the suspicious IP address available on %s', '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-dark.svg')));
return $notification;
default:
// Unknown subject => Unknown notification => throw
throw new UnknownNotificationException();
}
}
}