From 3f59b399ffc6a743a29f67e3151dfe47d40c7b41 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 16 Apr 2026 00:46:25 +0200 Subject: [PATCH] feat: Trigged UserLoggedInEvent when user log in Co-Authored-By: Malik Mann Signed-off-by: Carl Schwan --- lib/Controller/SAMLController.php | 4 ++++ tests/unit/Controller/SAMLControllerTest.php | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/Controller/SAMLController.php b/lib/Controller/SAMLController.php index 61b7ac17f..01d211d9c 100644 --- a/lib/Controller/SAMLController.php +++ b/lib/Controller/SAMLController.php @@ -29,6 +29,7 @@ use OCP\AppFramework\Http\Attribute\PublicPage; use OCP\AppFramework\Http\Attribute\UseSession; use OCP\AppFramework\Services\IAppConfig; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -38,6 +39,7 @@ use OCP\Security\ICrypto; use OCP\Security\ITrustedDomainHelper; use OCP\Server; +use OCP\User\Events\UserLoggedInEvent; use OneLogin\Saml2\Auth; use OneLogin\Saml2\Error; use OneLogin\Saml2\Settings; @@ -66,6 +68,7 @@ public function __construct( private ICrypto $crypto, private ITrustedDomainHelper $trustedDomainHelper, private SessionService $sessionService, + private IEventDispatcher $eventDispatcher, ) { parent::__construct($appName, $request); } @@ -402,6 +405,7 @@ public function assertionConsumerService(): Http\RedirectResponse { if ($firstLogin) { $this->userBackend->initializeHomeDir($user->getUID()); } + $this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $user->getUID(), null, false)); } catch (NoUserFoundException) { throw new \InvalidArgumentException('User "' . $this->userBackend->getCurrentUserId() . '" is not valid'); } catch (Exception $e) { diff --git a/tests/unit/Controller/SAMLControllerTest.php b/tests/unit/Controller/SAMLControllerTest.php index 0d490652d..74824f531 100644 --- a/tests/unit/Controller/SAMLControllerTest.php +++ b/tests/unit/Controller/SAMLControllerTest.php @@ -19,6 +19,7 @@ use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IAppConfig; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -28,6 +29,7 @@ use OCP\IUserSession; use OCP\Security\ICrypto; use OCP\Security\ITrustedDomainHelper; +use OCP\User\Events\UserLoggedInEvent; use Override; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; @@ -49,8 +51,9 @@ class SAMLControllerTest extends TestCase { private IL10N&MockObject $l; private ICrypto&MockObject $crypto; private SAMLController $samlController; - private ITrustedDomainHelper|MockObject $trustedDomainController; - private SessionService|MockObject $sessionService; + private ITrustedDomainHelper&MockObject $trustedDomainController; + private SessionService&MockObject $sessionService; + private IEventDispatcher&MockObject $eventDispatcher; #[Override] protected function setUp(): void { @@ -71,6 +74,7 @@ protected function setUp(): void { $this->crypto = $this->createMock(ICrypto::class); $this->trustedDomainController = $this->createMock(ITrustedDomainHelper::class); $this->sessionService = $this->createMock(SessionService::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->l->expects($this->any())->method('t')->willReturnCallback( static fn (string $param): string => $param @@ -95,7 +99,8 @@ protected function setUp(): void { $this->userData, $this->crypto, $this->trustedDomainController, - $this->sessionService + $this->sessionService, + $this->eventDispatcher, ); } @@ -263,6 +268,7 @@ public function testLoginWithEnvVariable(array $samlUserData, string $redirect, if (isset($samlUserData['uid']) && !($userState === 0 && $autoProvision === 0)) { $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('MyUid'); $im = $this->userResolver ->expects($this->once()) ->method('findExistingUser')