mirror of https://github.com/nextcloud/server
feat(admin_audit): Migrate to non-deprecated IEventDispatcher
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
2c71a77f4b
commit
eb09053897
|
@ -26,6 +26,7 @@ declare(strict_types=1);
|
|||
*/
|
||||
namespace OCA\AdminAudit\Actions;
|
||||
|
||||
use OCP\Authentication\TwoFactorAuth\IProvider;
|
||||
use OCP\IUser;
|
||||
|
||||
/**
|
||||
|
@ -35,14 +36,14 @@ use OCP\IUser;
|
|||
*/
|
||||
class Security extends Action {
|
||||
/**
|
||||
* Log twofactor auth enabled
|
||||
*
|
||||
* @param IUser $user
|
||||
* @param array $params
|
||||
* Logs failed twofactor challenge
|
||||
*/
|
||||
public function twofactorFailed(IUser $user, array $params): void {
|
||||
$params['uid'] = $user->getUID();
|
||||
$params['displayName'] = $user->getDisplayName();
|
||||
public function twofactorFailed(IUser $user, IProvider $provider): void {
|
||||
$params = [
|
||||
'displayName' => $user->getDisplayName(),
|
||||
'uid' => $user->getUID(),
|
||||
'provider' => $provider->getDisplayName(),
|
||||
];
|
||||
|
||||
$this->log(
|
||||
'Failed two factor attempt by user %s (%s) with provider %s',
|
||||
|
@ -56,14 +57,14 @@ class Security extends Action {
|
|||
}
|
||||
|
||||
/**
|
||||
* Logs unsharing of data
|
||||
*
|
||||
* @param IUser $user
|
||||
* @param array $params
|
||||
* Logs successful twofactor challenge
|
||||
*/
|
||||
public function twofactorSuccess(IUser $user, array $params): void {
|
||||
$params['uid'] = $user->getUID();
|
||||
$params['displayName'] = $user->getDisplayName();
|
||||
public function twofactorSuccess(IUser $user, IProvider $provider): void {
|
||||
$params = [
|
||||
'displayName' => $user->getDisplayName(),
|
||||
'uid' => $user->getUID(),
|
||||
'provider' => $provider->getDisplayName(),
|
||||
];
|
||||
|
||||
$this->log(
|
||||
'Successful two factor attempt by user %s (%s) with provider %s',
|
||||
|
|
|
@ -57,7 +57,10 @@ use OCP\AppFramework\Bootstrap\IBootContext;
|
|||
use OCP\AppFramework\Bootstrap\IBootstrap;
|
||||
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
||||
use OCP\Authentication\TwoFactorAuth\IProvider;
|
||||
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
|
||||
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
|
||||
use OCP\Console\ConsoleEvent;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use OCP\IConfig;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\IPreview;
|
||||
|
@ -65,6 +68,7 @@ use OCP\IServerContainer;
|
|||
use OCP\IUserSession;
|
||||
use OCP\Log\Audit\CriticalActionPerformedEvent;
|
||||
use OCP\Log\ILogFactory;
|
||||
use OCP\Preview\BeforePreviewFetchedEvent;
|
||||
use OCP\Share;
|
||||
use OCP\Util;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
@ -109,8 +113,9 @@ class Application extends App implements IBootstrap {
|
|||
$this->groupHooks($logger, $serverContainer->get(IGroupManager::class));
|
||||
$this->authHooks($logger);
|
||||
|
||||
/** @var EventDispatcherInterface $eventDispatcher */
|
||||
$eventDispatcher = $serverContainer->get(EventDispatcherInterface::class);
|
||||
|
||||
/** @var IEventDispatcher $eventDispatcher */
|
||||
$eventDispatcher = $serverContainer->get(IEventDispatcher::class);
|
||||
$this->consoleHooks($logger, $eventDispatcher);
|
||||
$this->appHooks($logger, $eventDispatcher);
|
||||
|
||||
|
@ -169,7 +174,7 @@ class Application extends App implements IBootstrap {
|
|||
}
|
||||
|
||||
private function appHooks(IAuditLogger $logger,
|
||||
EventDispatcherInterface $eventDispatcher): void {
|
||||
IEventDispatcher $eventDispatcher): void {
|
||||
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
|
||||
$appActions = new AppManagement($logger);
|
||||
$appActions->enableApp($event->getAppID());
|
||||
|
@ -185,27 +190,26 @@ class Application extends App implements IBootstrap {
|
|||
}
|
||||
|
||||
private function consoleHooks(IAuditLogger $logger,
|
||||
EventDispatcherInterface $eventDispatcher): void {
|
||||
$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) {
|
||||
IEventDispatcher $eventDispatcher): void {
|
||||
$eventDispatcher->addListener(ConsoleEvent::class, function (ConsoleEvent $event) use ($logger) {
|
||||
$appActions = new Console($logger);
|
||||
$appActions->runCommand($event->getArguments());
|
||||
});
|
||||
}
|
||||
|
||||
private function fileHooks(IAuditLogger $logger,
|
||||
EventDispatcherInterface $eventDispatcher): void {
|
||||
IEventDispatcher $eventDispatcher): void {
|
||||
$fileActions = new Files($logger);
|
||||
$eventDispatcher->addListener(
|
||||
IPreview::EVENT,
|
||||
function (GenericEvent $event) use ($fileActions) {
|
||||
/** @var File $file */
|
||||
$file = $event->getSubject();
|
||||
BeforePreviewFetchedEvent::class,
|
||||
function (BeforePreviewFetchedEvent $event) use ($fileActions) {
|
||||
$file = $event->getNode();
|
||||
$fileActions->preview([
|
||||
'path' => mb_substr($file->getInternalPath(), 5),
|
||||
'width' => $event->getArguments()['width'],
|
||||
'height' => $event->getArguments()['height'],
|
||||
'crop' => $event->getArguments()['crop'],
|
||||
'mode' => $event->getArguments()['mode']
|
||||
'width' => $event->getWidth(),
|
||||
'height' => $event->getHeight(),
|
||||
'crop' => $event->isCrop(),
|
||||
'mode' => $event->getMode()
|
||||
]);
|
||||
}
|
||||
);
|
||||
|
@ -267,14 +271,14 @@ class Application extends App implements IBootstrap {
|
|||
}
|
||||
|
||||
private function securityHooks(IAuditLogger $logger,
|
||||
EventDispatcherInterface $eventDispatcher): void {
|
||||
$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) {
|
||||
IEventDispatcher $eventDispatcher): void {
|
||||
$eventDispatcher->addListener(TwoFactorProviderChallengePassed::class, function (TwoFactorProviderChallengePassed $event) use ($logger) {
|
||||
$security = new Security($logger);
|
||||
$security->twofactorSuccess($event->getSubject(), $event->getArguments());
|
||||
$security->twofactorSuccess($event->getUser(), $event->getProvider());
|
||||
});
|
||||
$eventDispatcher->addListener(IProvider::EVENT_FAILED, function (GenericEvent $event) use ($logger) {
|
||||
$eventDispatcher->addListener(TwoFactorProviderChallengeFailed::class, function (TwoFactorProviderChallengeFailed $event) use ($logger) {
|
||||
$security = new Security($logger);
|
||||
$security->twofactorFailed($event->getSubject(), $event->getArguments());
|
||||
$security->twofactorFailed($event->getUser(), $event->getProvider());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,9 @@ declare(strict_types=1);
|
|||
namespace OCA\AdminAudit\Tests\Actions;
|
||||
|
||||
use OCA\AdminAudit\Actions\Security;
|
||||
use OCP\IUser;
|
||||
use OCA\AdminAudit\AuditLogger;
|
||||
use OCP\Authentication\TwoFactorAuth\IProvider;
|
||||
use OCP\IUser;
|
||||
use Test\TestCase;
|
||||
|
||||
class SecurityTest extends TestCase {
|
||||
|
@ -60,7 +61,11 @@ class SecurityTest extends TestCase {
|
|||
['app' => 'admin_audit']
|
||||
);
|
||||
|
||||
$this->security->twofactorFailed($this->user, ['provider' => 'myprovider']);
|
||||
$provider = $this->createMock(IProvider::class);
|
||||
$provider->method('getDisplayName')
|
||||
->willReturn('myprovider');
|
||||
|
||||
$this->security->twofactorFailed($this->user, $provider);
|
||||
}
|
||||
|
||||
public function testTwofactorSuccess() {
|
||||
|
@ -71,6 +76,10 @@ class SecurityTest extends TestCase {
|
|||
['app' => 'admin_audit']
|
||||
);
|
||||
|
||||
$this->security->twofactorSuccess($this->user, ['provider' => 'myprovider']);
|
||||
$provider = $this->createMock(IProvider::class);
|
||||
$provider->method('getDisplayName')
|
||||
->willReturn('myprovider');
|
||||
|
||||
$this->security->twofactorSuccess($this->user, $provider);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,21 +67,16 @@ class HookManager {
|
|||
/** @var Defaults */
|
||||
private $themingDefaults;
|
||||
|
||||
/** @var EventDispatcherInterface */
|
||||
private $eventDispatcher;
|
||||
|
||||
public function __construct(IUserManager $userManager,
|
||||
SyncService $syncService,
|
||||
CalDavBackend $calDav,
|
||||
CardDavBackend $cardDav,
|
||||
Defaults $themingDefaults,
|
||||
EventDispatcherInterface $eventDispatcher) {
|
||||
Defaults $themingDefaults) {
|
||||
$this->userManager = $userManager;
|
||||
$this->syncService = $syncService;
|
||||
$this->calDav = $calDav;
|
||||
$this->cardDav = $cardDav;
|
||||
$this->themingDefaults = $themingDefaults;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
public function setup() {
|
||||
|
|
|
@ -39,19 +39,14 @@ use OCP\IL10N;
|
|||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Test\TestCase;
|
||||
|
||||
class HookManagerTest extends TestCase {
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
|
||||
/** @var EventDispatcherInterface | MockObject */
|
||||
private $eventDispatcher;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
$this->l10n
|
||||
->expects($this->any())
|
||||
|
@ -106,7 +101,7 @@ class HookManagerTest extends TestCase {
|
|||
'principals/users/newUser',
|
||||
'contacts', ['{DAV:}displayname' => 'Contacts']);
|
||||
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
|
||||
$hm->firstLogin($user);
|
||||
}
|
||||
|
||||
|
@ -145,7 +140,7 @@ class HookManagerTest extends TestCase {
|
|||
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
|
||||
$card->expects($this->never())->method('createAddressBook');
|
||||
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
|
||||
$hm->firstLogin($user);
|
||||
}
|
||||
|
||||
|
@ -193,7 +188,7 @@ class HookManagerTest extends TestCase {
|
|||
'principals/users/newUser',
|
||||
'contacts', ['{DAV:}displayname' => 'Contacts']);
|
||||
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
|
||||
$hm->firstLogin($user);
|
||||
}
|
||||
|
||||
|
@ -243,7 +238,7 @@ class HookManagerTest extends TestCase {
|
|||
]);
|
||||
$card->expects($this->once())->method('deleteAddressBook');
|
||||
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
|
||||
$hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
|
||||
$hm->preDeleteUser(['uid' => 'newUser']);
|
||||
$hm->postDeleteUser(['uid' => 'newUser']);
|
||||
}
|
||||
|
|
|
@ -91,7 +91,7 @@ try {
|
|||
|
||||
$application = new Application(
|
||||
\OC::$server->getConfig(),
|
||||
\OC::$server->getEventDispatcher(),
|
||||
\OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class),
|
||||
\OC::$server->getRequest(),
|
||||
\OC::$server->get(\Psr\Log\LoggerInterface::class),
|
||||
\OC::$server->query(\OC\MemoryInfo::class)
|
||||
|
|
|
@ -36,6 +36,7 @@ use OC_App;
|
|||
use OCP\AppFramework\QueryException;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\Console\ConsoleEvent;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -44,13 +45,12 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class Application {
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
private SymfonyApplication $application;
|
||||
/** @var EventDispatcherInterface */
|
||||
/** @var IEventDispatcher */
|
||||
private $dispatcher;
|
||||
/** @var IRequest */
|
||||
private $request;
|
||||
|
@ -60,7 +60,7 @@ class Application {
|
|||
private $memoryInfo;
|
||||
|
||||
public function __construct(IConfig $config,
|
||||
EventDispatcherInterface $dispatcher,
|
||||
IEventDispatcher $dispatcher,
|
||||
IRequest $request,
|
||||
LoggerInterface $logger,
|
||||
MemoryInfo $memoryInfo) {
|
||||
|
@ -204,10 +204,12 @@ class Application {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function run(InputInterface $input = null, OutputInterface $output = null) {
|
||||
$this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
|
||||
$event = new ConsoleEvent(
|
||||
ConsoleEvent::EVENT_RUN,
|
||||
$this->request->server['argv']
|
||||
));
|
||||
);
|
||||
$this->dispatcher->dispatchTyped($event);
|
||||
$this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, $event);
|
||||
return $this->application->run($input, $output);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue