Passed
Push — master ( 9beda0...9f7fca )
by Georg
13:29 queued 11s
created

RemoteWipe::sendNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2019 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2019 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\Authentication\Token;
27
28
use BadMethodCallException;
29
use OC\Authentication\Events\RemoteWipeFinished;
30
use OC\Authentication\Events\RemoteWipeStarted;
31
use OC\Authentication\Exceptions\InvalidTokenException;
32
use OC\Authentication\Exceptions\WipeTokenException;
33
use OCP\Activity\IEvent;
34
use OCP\Activity\IManager as IActivityManager;
35
use OCP\AppFramework\Utility\ITimeFactory;
36
use OCP\EventDispatcher\IEventDispatcher;
37
use OCP\ILogger;
38
use OCP\Notification\IManager as INotificationManager;
39
use Symfony\Component\EventDispatcher\EventDispatcher;
40
41
class RemoteWipe {
42
43
	/** @var IProvider */
44
	private $tokenProvider;
45
46
	/** @var IEventDispatcher */
47
	private $eventDispatcher;
48
49
	/** @var ILogger */
50
	private $logger;
51
52
	public function __construct(IProvider $tokenProvider,
53
								IEventDispatcher $eventDispatcher,
54
								ILogger $logger) {
55
		$this->tokenProvider = $tokenProvider;
56
		$this->eventDispatcher = $eventDispatcher;
57
		$this->logger = $logger;
58
	}
59
60
	/**
61
	 * @param string $token
62
	 *
63
	 * @return bool whether wiping was started
64
	 * @throws InvalidTokenException
65
	 *
66
	 */
67
	public function start(string $token): bool {
68
		try {
69
			$this->tokenProvider->getToken($token);
70
71
			// We expect a WipedTokenException here. If we reach this point this
72
			// is an ordinary token
73
			return false;
74
		} catch (WipeTokenException $e) {
75
			// Expected -> continue below
76
		}
77
78
		$dbToken = $e->getToken();
79
80
		$this->logger->info("user " . $dbToken->getUID() . " started a remote wipe");
81
82
		$this->eventDispatcher->dispatch(RemoteWipeStarted::class, new RemoteWipeStarted($dbToken));
83
84
		return true;
85
	}
86
87
	/**
88
	 * @param string $token
89
	 *
90
	 * @return bool whether wiping could be finished
91
	 * @throws InvalidTokenException
92
	 */
93
	public function finish(string $token): bool {
94
		try {
95
			$this->tokenProvider->getToken($token);
96
97
			// We expect a WipedTokenException here. If we reach this point this
98
			// is an ordinary token
99
			return false;
100
		} catch (WipeTokenException $e) {
101
			// Expected -> continue below
102
		}
103
104
		$dbToken = $e->getToken();
105
106
		$this->tokenProvider->invalidateToken($token);
107
108
		$this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe");
109
		$this->eventDispatcher->dispatch(RemoteWipeFinished::class, new RemoteWipeFinished($dbToken));
110
111
		return true;
112
	}
113
114
}
115