diff --git a/lib/Controller/SAMLController.php b/lib/Controller/SAMLController.php index 5cf236071..c8d33f276 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); } @@ -392,6 +395,7 @@ public function assertionConsumerService(): Http\RedirectResponse { if ($firstLogin) { $this->userBackend->initializeHomeDir($user->getUID()); } + $this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $user->getUID(), null, true)); } catch (NoUserFoundException $e) { throw new \InvalidArgumentException('User "' . $this->userBackend->getCurrentUserId() . '" is not valid.', previous: $e); } catch (Exception $e) { diff --git a/tests/unit/Controller/SAMLControllerTest.php b/tests/unit/Controller/SAMLControllerTest.php index 0d490652d..926df7d59 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; @@ -49,8 +50,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 +73,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 +98,8 @@ protected function setUp(): void { $this->userData, $this->crypto, $this->trustedDomainController, - $this->sessionService + $this->sessionService, + $this->eventDispatcher, ); } @@ -263,6 +267,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')