Passed
Push — master ( df18c4...5e52c1 )
by Christoph
13:50 queued 18s
created

UserDeletedTokenCleanupListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 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\Listeners;
27
28
use OC\Authentication\Token\Manager;
29
use OCP\EventDispatcher\Event;
30
use OCP\EventDispatcher\IEventListener;
31
use OCP\ILogger;
32
use OCP\User\Events\UserDeletedEvent;
33
use Throwable;
34
35
class UserDeletedTokenCleanupListener implements IEventListener {
36
37
	/** @var Manager */
38
	private $manager;
39
40
	/** @var ILogger */
41
	private $logger;
42
43
	public function __construct(Manager $manager,
44
								ILogger $logger) {
45
		$this->manager = $manager;
46
		$this->logger = $logger;
47
	}
48
49
	public function handle(Event $event): void {
50
		if (!($event instanceof UserDeletedEvent)) {
51
			// Unrelated
52
			return;
53
		}
54
55
		/**
56
		 * Catch any exception during this process as any failure here shouldn't block the
57
		 * user deletion.
58
		 */
59
		try {
60
			$uid = $event->getUser()->getUID();
61
			$tokens = $this->manager->getTokenByUser($uid);
62
			foreach ($tokens as $token) {
63
				$this->manager->invalidateTokenById($uid, $token->getId());
64
			}
65
		} catch (Throwable $e) {
66
			$this->logger->logException($e, [
67
				'message' => 'Could not clean up auth tokens after user deletion: ' . $e->getMessage(),
68
				'error' => ILogger::ERROR,
69
			]);
70
		}
71
	}
72
}
73