Passed
Push — master ( 0d0850...93133b )
by Morris
08:35
created

RemoteWipeEmailListener   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 76
c 1
b 0
f 0
dl 0
loc 129
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getWipingFinishedMessage() 0 26 1
B handle() 0 43 9
A getWipingStartedMessage() 0 26 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright 2019 Christoph Wurst <[email protected]>
5
 *
6
 * @author 2019 Christoph Wurst <[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
namespace OC\Authentication\Listeners;
25
26
use Exception;
27
use OC\Authentication\Events\RemoteWipeFinished;
28
use OC\Authentication\Events\RemoteWipeStarted;
29
use OCP\EventDispatcher\Event;
30
use OCP\EventDispatcher\IEventListener;
31
use OCP\IL10N;
32
use OCP\ILogger;
33
use OCP\IUser;
34
use OCP\IUserManager;
35
use OCP\L10N\IFactory as IL10nFactory;
36
use OCP\Mail\IMailer;
37
use OCP\Mail\IMessage;
38
use function substr;
39
40
class RemoteWipeEmailListener implements IEventListener {
41
42
	/** @var IMailer */
43
	private $mailer;
44
45
	/** @var IUserManager */
46
	private $userManager;
47
48
	/** @var IL10N */
49
	private $l10n;
50
51
	/** @var ILogger */
52
	private $logger;
53
54
	public function __construct(IMailer $mailer,
55
								IUserManager $userManager,
56
								IL10nFactory $l10nFactory,
57
								ILogger $logger) {
58
		$this->mailer = $mailer;
59
		$this->userManager = $userManager;
60
		$this->l10n = $l10nFactory->get('core');
61
		$this->logger = $logger;
62
	}
63
64
	/**
65
	 * @param Event $event
66
	 */
67
	public function handle(Event $event): void {
68
		if ($event instanceof RemoteWipeStarted) {
69
			$uid = $event->getToken()->getUID();
70
			$user = $this->userManager->get($uid);
71
			if ($user === null) {
72
				$this->logger->warning("not sending a wipe started email because user <$uid> does not exist (anymore)");
73
				return;
74
			}
75
			if ($user->getEMailAddress() === null) {
76
				$this->logger->info("not sending a wipe started email because user <$uid> has no email set");
77
				return;
78
			}
79
80
			try {
81
				$this->mailer->send(
82
					$this->getWipingStartedMessage($event, $user)
83
				);
84
			} catch (Exception $e) {
85
				$this->logger->logException($e, [
86
					'message' => "Could not send remote wipe started email to <$uid>",
87
					'level' => ILogger::ERROR,
88
				]);
89
			}
90
		} else if ($event instanceof RemoteWipeFinished) {
91
			$uid = $event->getToken()->getUID();
92
			$user = $this->userManager->get($uid);
93
			if ($user === null) {
94
				$this->logger->warning("not sending a wipe finished email because user <$uid> does not exist (anymore)");
95
				return;
96
			}
97
			if ($user->getEMailAddress() === null) {
98
				$this->logger->info("not sending a wipe finished email because user <$uid> has no email set");
99
				return;
100
			}
101
102
			try {
103
				$this->mailer->send(
104
					$this->getWipingFinishedMessage($event, $user)
105
				);
106
			} catch (Exception $e) {
107
				$this->logger->logException($e, [
108
					'message' => "Could not send remote wipe finished email to <$uid>",
109
					'level' => ILogger::ERROR,
110
				]);
111
			}
112
		}
113
	}
114
115
	private function getWipingStartedMessage(RemoteWipeStarted $event, IUser $user): IMessage {
116
		$message = $this->mailer->createMessage();
117
		$emailTemplate = $this->mailer->createEMailTemplate('auth.RemoteWipeStarted');
118
		$plainHeading = $this->l10n->t('Wiping of device %s has started', [$event->getToken()->getName()]);
119
		$htmlHeading = $this->l10n->t('Wiping of device »%s« has started', [$event->getToken()->getName()]);
120
		$emailTemplate->setSubject(
121
			$this->l10n->t(
122
				'»%s« started remote wipe',
123
				[
124
					substr($event->getToken()->getName(), 0, 15)
125
				]
126
			)
127
		);
128
		$emailTemplate->addHeader();
129
		$emailTemplate->addHeading(
130
			$htmlHeading,
131
			$plainHeading
132
		);
133
		$emailTemplate->addBodyText(
134
			$this->l10n->t('Device or application »%s« has started the remote wipe process. You will receive another email once the process has finished', [$event->getToken()->getName()])
135
		);
136
		$emailTemplate->addFooter();
137
		$message->setTo([$user->getEMailAddress()]);
138
		$message->useTemplate($emailTemplate);
139
140
		return $message;
141
	}
142
143
	private function getWipingFinishedMessage(RemoteWipeFinished $event, IUser $user): IMessage {
144
		$message = $this->mailer->createMessage();
145
		$emailTemplate = $this->mailer->createEMailTemplate('auth.RemoteWipeFinished');
146
		$plainHeading = $this->l10n->t('Wiping of device %s has finished', [$event->getToken()->getName()]);
147
		$htmlHeading = $this->l10n->t('Wiping of device »%s« has finished', [$event->getToken()->getName()]);
148
		$emailTemplate->setSubject(
149
			$this->l10n->t(
150
				'»%s« finished remote wipe',
151
				[
152
					substr($event->getToken()->getName(), 0, 15)
153
				]
154
			)
155
		);
156
		$emailTemplate->addHeader();
157
		$emailTemplate->addHeading(
158
			$htmlHeading,
159
			$plainHeading
160
		);
161
		$emailTemplate->addBodyText(
162
			$this->l10n->t('Device or application »%s« has finished the remote wipe process.', [$event->getToken()->getName()])
163
		);
164
		$emailTemplate->addFooter();
165
		$message->setTo([$user->getEMailAddress()]);
166
		$message->useTemplate($emailTemplate);
167
168
		return $message;
169
	}
170
171
}
172