Skip to content

Commit 5d65384

Browse files
feat: Trigged UserLoggedInEvent when user log in
Co-Authored-By: Malik Mann <github@dermm.io> Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent d1a4917 commit 5d65384

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/Controller/SAMLController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCP\AppFramework\Http\Attribute\PublicPage;
2929
use OCP\AppFramework\Http\Attribute\UseSession;
3030
use OCP\AppFramework\Services\IAppConfig;
31+
use OCP\EventDispatcher\IEventDispatcher;
3132
use OCP\IConfig;
3233
use OCP\IL10N;
3334
use OCP\IRequest;
@@ -37,6 +38,7 @@
3738
use OCP\Security\ICrypto;
3839
use OCP\Security\ITrustedDomainHelper;
3940
use OCP\Server;
41+
use OCP\User\Events\UserLoggedInEvent;
4042
use OneLogin\Saml2\Auth;
4143
use OneLogin\Saml2\Error;
4244
use OneLogin\Saml2\Settings;
@@ -65,6 +67,7 @@ public function __construct(
6567
private ICrypto $crypto,
6668
private ITrustedDomainHelper $trustedDomainHelper,
6769
private SessionService $sessionService,
70+
private IEventDispatcher $eventDispatcher
6871
) {
6972
parent::__construct($appName, $request);
7073
}
@@ -250,6 +253,7 @@ public function login(int $idp = 1): Http\RedirectResponse|Http\TemplateResponse
250253
if ($firstLogin) {
251254
$this->userBackend->initializeHomeDir($user->getUID());
252255
}
256+
$this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $user->getUID(), null, false));
253257
} catch (NoUserFoundException $e) {
254258
if ($e->getMessage()) {
255259
$this->logger->warning('Error while trying to login using sso environment variable: ' . $e->getMessage(), ['app' => 'user_saml']);
@@ -400,6 +404,7 @@ public function assertionConsumerService(): Http\RedirectResponse {
400404
if ($firstLogin) {
401405
$this->userBackend->initializeHomeDir($user->getUID());
402406
}
407+
$this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $user->getUID(), null, false));
403408
} catch (NoUserFoundException) {
404409
throw new \InvalidArgumentException('User "' . $this->userBackend->getCurrentUserId() . '" is not valid');
405410
} catch (Exception $e) {

tests/unit/Controller/SAMLControllerTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCP\AppFramework\Http\RedirectResponse;
2020
use OCP\AppFramework\Http\TemplateResponse;
2121
use OCP\AppFramework\Services\IAppConfig;
22+
use OCP\EventDispatcher\IEventDispatcher;
2223
use OCP\IConfig;
2324
use OCP\IL10N;
2425
use OCP\IRequest;
@@ -28,6 +29,7 @@
2829
use OCP\IUserSession;
2930
use OCP\Security\ICrypto;
3031
use OCP\Security\ITrustedDomainHelper;
32+
use OCP\User\Events\UserLoggedInEvent;
3133
use Override;
3234
use PHPUnit\Framework\Attributes\DataProvider;
3335
use PHPUnit\Framework\MockObject\MockObject;
@@ -51,6 +53,7 @@ class SAMLControllerTest extends TestCase {
5153
private SAMLController $samlController;
5254
private ITrustedDomainHelper|MockObject $trustedDomainController;
5355
private SessionService|MockObject $sessionService;
56+
private IEventDispatcher|MockObject $eventDispatcher;
5457

5558
#[Override]
5659
protected function setUp(): void {
@@ -71,6 +74,7 @@ protected function setUp(): void {
7174
$this->crypto = $this->createMock(ICrypto::class);
7275
$this->trustedDomainController = $this->createMock(ITrustedDomainHelper::class);
7376
$this->sessionService = $this->createMock(SessionService::class);
77+
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
7478

7579
$this->l->expects($this->any())->method('t')->willReturnCallback(
7680
static fn (string $param): string => $param
@@ -95,7 +99,8 @@ protected function setUp(): void {
9599
$this->userData,
96100
$this->crypto,
97101
$this->trustedDomainController,
98-
$this->sessionService
102+
$this->sessionService,
103+
$this->eventDispatcher,
99104
);
100105
}
101106

@@ -304,6 +309,11 @@ public function testLoginWithEnvVariable(array $samlUserData, string $redirect,
304309
->method('createUserIfNotExists')
305310
->with('MyUid');
306311

312+
$this->eventDispatcher
313+
->expects($this->once())
314+
->method('dispatchTyped')
315+
->with(new UserLoggedInEvent($user, 'MyUid', null, false));
316+
307317
$expected = new RedirectResponse($redirect);
308318
$result = $this->samlController->login(1);
309319
$this->assertEquals($expected, $result);

0 commit comments

Comments
 (0)