From d8721cd6e8c967588bb5954ba4a194ff384cd8b5 Mon Sep 17 00:00:00 2001 From: Jake Esser Date: Wed, 31 Jul 2024 18:16:02 +0200 Subject: [PATCH 1/2] Implemented PostLoginEvent --- lib/Controller/SAMLController.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Controller/SAMLController.php b/lib/Controller/SAMLController.php index 984cc660..fca9b671 100644 --- a/lib/Controller/SAMLController.php +++ b/lib/Controller/SAMLController.php @@ -20,6 +20,7 @@ use OCA\User_SAML\UserResolver; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -29,6 +30,7 @@ use OCP\Security\ICrypto; use OCP\Security\ITrustedDomainHelper; use OCP\Server; +use OCP\User\Events\PostLoginEvent; use OneLogin\Saml2\Auth; use OneLogin\Saml2\Error; use OneLogin\Saml2\Settings; @@ -63,6 +65,7 @@ class SAMLController extends Controller { */ private $crypto; private ITrustedDomainHelper $trustedDomainHelper; + private IEventDispatcher $eventDispatcher; public function __construct( string $appName, @@ -78,7 +81,8 @@ public function __construct( UserResolver $userResolver, UserData $userData, ICrypto $crypto, - ITrustedDomainHelper $trustedDomainHelper + ITrustedDomainHelper $trustedDomainHelper, + IEventDispatcher $eventDispatcher ) { parent::__construct($appName, $request); $this->session = $session; @@ -93,6 +97,7 @@ public function __construct( $this->userData = $userData; $this->crypto = $crypto; $this->trustedDomainHelper = $trustedDomainHelper; + $this->eventDispatcher = $eventDispatcher; } /** @@ -392,6 +397,7 @@ public function assertionConsumerService(): Http\RedirectResponse { if ($firstLogin) { $this->userBackend->initializeHomeDir($user->getUID()); } + $this->eventDispatcher->dispatchTyped(new PostLoginEvent($user, $user->getUID(), '', false)); } catch (NoUserFoundException) { throw new \InvalidArgumentException('User "' . $this->userBackend->getCurrentUserId() . '" is not valid'); } catch (\Exception $e) { From d4a437777baf73383c0bd89c1397e77a869d94a2 Mon Sep 17 00:00:00 2001 From: Jake Esser Date: Fri, 2 Aug 2024 13:28:37 +0200 Subject: [PATCH 2/2] Added SAML Controller dispatchTyped test --- tests/unit/Controller/SAMLControllerTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/unit/Controller/SAMLControllerTest.php b/tests/unit/Controller/SAMLControllerTest.php index 3987ac12..deb432b6 100644 --- a/tests/unit/Controller/SAMLControllerTest.php +++ b/tests/unit/Controller/SAMLControllerTest.php @@ -16,6 +16,7 @@ use OCA\User_SAML\UserResolver; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -57,6 +58,7 @@ class SAMLControllerTest extends TestCase { /** @var SAMLController */ private $samlController; private ITrustedDomainHelper|MockObject $trustedDomainController; + private IEventDispatcher $eventDispatcher; protected function setUp(): void { parent::setUp(); @@ -74,6 +76,7 @@ protected function setUp(): void { $this->userData = $this->createMock(UserData::class); $this->crypto = $this->createMock(ICrypto::class); $this->trustedDomainController = $this->createMock(ITrustedDomainHelper::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->l->expects($this->any())->method('t')->willReturnCallback( function ($param) { @@ -294,6 +297,10 @@ public function testLoginWithEnvVariable(array $samlUserData, string $redirect, ->expects($this->exactly((int)($autoProvision < 2))) ->method('updateLastLoginTimestamp'); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped'); + if ($userState === 0) { $this->userResolver ->expects($this->any())