2018-05-22 08:52:16 +02:00
|
|
|
<?php
|
|
|
|
|
2019-12-03 19:57:53 +01:00
|
|
|
declare(strict_types=1);
|
2018-05-22 08:52:16 +02:00
|
|
|
|
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-05-22 08:52:16 +02:00
|
|
|
*/
|
|
|
|
namespace OC\Authentication\TwoFactorAuth;
|
|
|
|
|
|
|
|
use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
|
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
|
|
|
use OCP\Authentication\TwoFactorAuth\IRegistry;
|
2018-09-29 19:03:07 +02:00
|
|
|
use OCP\Authentication\TwoFactorAuth\RegistryEvent;
|
2020-06-09 14:33:06 +02:00
|
|
|
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderDisabled;
|
2023-06-30 14:14:36 +02:00
|
|
|
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
|
|
|
|
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
|
|
|
|
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderUserDeleted;
|
2019-09-10 11:37:53 +02:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2018-05-22 08:52:16 +02:00
|
|
|
use OCP\IUser;
|
|
|
|
|
|
|
|
class Registry implements IRegistry {
|
|
|
|
/** @var ProviderUserAssignmentDao */
|
|
|
|
private $assignmentDao;
|
|
|
|
|
2019-09-10 11:37:53 +02:00
|
|
|
/** @var IEventDispatcher */
|
2018-09-29 19:03:07 +02:00
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
public function __construct(ProviderUserAssignmentDao $assignmentDao,
|
2019-09-10 11:37:53 +02:00
|
|
|
IEventDispatcher $dispatcher) {
|
2018-05-22 08:52:16 +02:00
|
|
|
$this->assignmentDao = $assignmentDao;
|
2018-09-29 19:03:07 +02:00
|
|
|
$this->dispatcher = $dispatcher;
|
2018-05-22 08:52:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderStates(IUser $user): array {
|
|
|
|
return $this->assignmentDao->getState($user->getUID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enableProviderFor(IProvider $provider, IUser $user) {
|
|
|
|
$this->assignmentDao->persist($provider->getId(), $user->getUID(), 1);
|
2018-09-29 19:03:07 +02:00
|
|
|
|
|
|
|
$event = new RegistryEvent($provider, $user);
|
|
|
|
$this->dispatcher->dispatch(self::EVENT_PROVIDER_ENABLED, $event);
|
2023-06-30 14:14:36 +02:00
|
|
|
$this->dispatcher->dispatchTyped(new TwoFactorProviderForUserRegistered($user, $provider));
|
2018-05-22 08:52:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function disableProviderFor(IProvider $provider, IUser $user) {
|
|
|
|
$this->assignmentDao->persist($provider->getId(), $user->getUID(), 0);
|
2018-09-29 19:03:07 +02:00
|
|
|
|
|
|
|
$event = new RegistryEvent($provider, $user);
|
|
|
|
$this->dispatcher->dispatch(self::EVENT_PROVIDER_DISABLED, $event);
|
2023-06-30 14:14:36 +02:00
|
|
|
$this->dispatcher->dispatchTyped(new TwoFactorProviderForUserUnregistered($user, $provider));
|
2018-05-22 08:52:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 10:51:44 +01:00
|
|
|
public function deleteUserData(IUser $user): void {
|
2020-06-09 14:33:06 +02:00
|
|
|
foreach ($this->assignmentDao->deleteByUser($user->getUID()) as $provider) {
|
|
|
|
$event = new TwoFactorProviderDisabled($provider['provider_id']);
|
|
|
|
$this->dispatcher->dispatchTyped($event);
|
2023-06-30 14:14:36 +02:00
|
|
|
$this->dispatcher->dispatchTyped(new TwoFactorProviderUserDeleted($user, $provider['provider_id']));
|
2020-06-09 14:33:06 +02:00
|
|
|
}
|
2020-01-08 10:51:44 +01:00
|
|
|
}
|
|
|
|
|
2018-09-10 17:02:37 +02:00
|
|
|
public function cleanUp(string $providerId) {
|
|
|
|
$this->assignmentDao->deleteAll($providerId);
|
|
|
|
}
|
2018-05-22 08:52:16 +02:00
|
|
|
}
|