aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/lib/Listener/AppPasswordCreatedActivityListener.php
blob: a51eee1a799743d8719d91bd644adc309fc6b398 (plain)
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
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\Settings\Listener;

use BadMethodCallException;
use OC\Authentication\Events\AppPasswordCreatedEvent;
use OCA\Settings\Activity\Provider;
use OCP\Activity\IManager as IActivityManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/**
 * @template-implements IEventListener<\OC\Authentication\Events\AppPasswordCreatedEvent>
 */
class AppPasswordCreatedActivityListener implements IEventListener {
	public function __construct(
		private IActivityManager $activityManager,
		private IUserSession $userSession,
		private LoggerInterface $logger,
	) {
	}

	public function handle(Event $event): void {
		if (!($event instanceof AppPasswordCreatedEvent)) {
			return;
		}

		$activity = $this->activityManager->generateEvent();
		$activity->setApp('settings')
			->setType('security')
			->setAffectedUser($event->getToken()->getUID())
			->setAuthor($this->userSession->getUser() ? $this->userSession->getUser()->getUID() : '')
			->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $event->getToken()->getName()])
			->setObject('app_token', $event->getToken()->getId());

		try {
			$this->activityManager->publish($activity);
		} catch (BadMethodCallException $e) {
			$this->logger->warning('Could not publish activity: ' . $e->getMessage(), [
				'exception' => $e
			]);
		}
	}
}