Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented PostLoginEvent in SAMLController #870

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Controller/SAMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,6 +65,7 @@ class SAMLController extends Controller {
*/
private $crypto;
private ITrustedDomainHelper $trustedDomainHelper;
private IEventDispatcher $eventDispatcher;

public function __construct(
string $appName,
Expand All @@ -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;
Expand All @@ -93,6 +97,7 @@ public function __construct(
$this->userData = $userData;
$this->crypto = $crypto;
$this->trustedDomainHelper = $trustedDomainHelper;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Controller/SAMLControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down
Loading