1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\TwoFactorBackupCodes\Notifications; |
26
|
|
|
|
27
|
|
|
use OCP\IURLGenerator; |
28
|
|
|
use OCP\L10N\IFactory; |
29
|
|
|
use OCP\Notification\INotification; |
30
|
|
|
use OCP\Notification\INotifier; |
31
|
|
|
|
32
|
|
|
class Notifier implements INotifier { |
33
|
|
|
|
34
|
|
|
/** @var IFactory */ |
35
|
|
|
private $factory; |
36
|
|
|
|
37
|
|
|
/** @var IURLGenerator */ |
38
|
|
|
private $url; |
39
|
|
|
|
40
|
|
|
public function __construct(IFactory $factory, IURLGenerator $url) { |
41
|
|
|
$this->factory = $factory; |
42
|
|
|
$this->url = $url; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Identifier of the notifier, only use [a-z0-9_] |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
* @since 17.0.0 |
50
|
|
|
*/ |
51
|
|
|
public function getID(): string { |
52
|
|
|
return 'twofactor_backupcodes'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Human readable name describing the notifier |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
* @since 17.0.0 |
60
|
|
|
*/ |
61
|
|
|
public function getName(): string { |
62
|
|
|
return $this->factory->get('twofactor_backupcodes')->t('Second-factor backup codes'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function prepare(INotification $notification, string $languageCode): INotification { |
66
|
|
|
if ($notification->getApp() !== 'twofactor_backupcodes') { |
67
|
|
|
// Not my app => throw |
68
|
|
|
throw new \InvalidArgumentException(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Read the language from the notification |
72
|
|
|
$l = $this->factory->get('twofactor_backupcodes', $languageCode); |
73
|
|
|
|
74
|
|
|
switch ($notification->getSubject()) { |
75
|
|
|
case 'create_backupcodes': |
76
|
|
|
$notification->setParsedSubject( |
77
|
|
|
$l->t('Generate backup codes') |
78
|
|
|
)->setParsedMessage( |
79
|
|
|
$l->t('You enabled two-factor authentication but did not generate backup codes yet. They are needed to restore access to your account in case you lose your second factor.') |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$notification->setLink($this->url->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security'])); |
83
|
|
|
|
84
|
|
|
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/password.svg'))); |
85
|
|
|
|
86
|
|
|
return $notification; |
87
|
|
|
|
88
|
|
|
default: |
89
|
|
|
// Unknown subject => Unknown notification => throw |
90
|
|
|
throw new \InvalidArgumentException(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|