Skip to content

Commit 3f59b39

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 3fc6502 commit 3f59b39

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

lib/Controller/SAMLController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\AppFramework\Http\Attribute\PublicPage;
3030
use OCP\AppFramework\Http\Attribute\UseSession;
3131
use OCP\AppFramework\Services\IAppConfig;
32+
use OCP\EventDispatcher\IEventDispatcher;
3233
use OCP\IConfig;
3334
use OCP\IL10N;
3435
use OCP\IRequest;
@@ -38,6 +39,7 @@
3839
use OCP\Security\ICrypto;
3940
use OCP\Security\ITrustedDomainHelper;
4041
use OCP\Server;
42+
use OCP\User\Events\UserLoggedInEvent;
4143
use OneLogin\Saml2\Auth;
4244
use OneLogin\Saml2\Error;
4345
use OneLogin\Saml2\Settings;
@@ -66,6 +68,7 @@ public function __construct(
6668
private ICrypto $crypto,
6769
private ITrustedDomainHelper $trustedDomainHelper,
6870
private SessionService $sessionService,
71+
private IEventDispatcher $eventDispatcher,
6972
) {
7073
parent::__construct($appName, $request);
7174
}
@@ -402,6 +405,7 @@ public function assertionConsumerService(): Http\RedirectResponse {
402405
if ($firstLogin) {
403406
$this->userBackend->initializeHomeDir($user->getUID());
404407
}
408+
$this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $user->getUID(), null, false));
405409
} catch (NoUserFoundException) {
406410
throw new \InvalidArgumentException('User "' . $this->userBackend->getCurrentUserId() . '" is not valid');
407411
} catch (Exception $e) {

tests/unit/Controller/SAMLControllerTest.php

Lines changed: 9 additions & 3 deletions
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;
@@ -49,8 +51,9 @@ class SAMLControllerTest extends TestCase {
4951
private IL10N&MockObject $l;
5052
private ICrypto&MockObject $crypto;
5153
private SAMLController $samlController;
52-
private ITrustedDomainHelper|MockObject $trustedDomainController;
53-
private SessionService|MockObject $sessionService;
54+
private ITrustedDomainHelper&MockObject $trustedDomainController;
55+
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

@@ -263,6 +268,7 @@ public function testLoginWithEnvVariable(array $samlUserData, string $redirect,
263268

264269
if (isset($samlUserData['uid']) && !($userState === 0 && $autoProvision === 0)) {
265270
$user = $this->createMock(IUser::class);
271+
$user->method('getUID')->willReturn('MyUid');
266272
$im = $this->userResolver
267273
->expects($this->once())
268274
->method('findExistingUser')

0 commit comments

Comments
 (0)