-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-8356: Removed `Ibexa\Core\MVC\Symfony\Security\Authentication\Aut…
…henticatorInterface` to be replaced with Symfony-based authentication #375
- Loading branch information
1 parent
232ee8a
commit 62e04b2
Showing
6 changed files
with
128 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
src/lib/MVC/Symfony/Security/Authentication/AuthenticatorInterface.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...y/Authentication/EventSubscriber/OnAuthenticationTokenCreatedRepositoryUserSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Core\MVC\Symfony\Security\Authentication\EventSubscriber; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Core\MVC\Symfony\Security\UserInterface as IbexaUser; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent; | ||
|
||
final readonly class OnAuthenticationTokenCreatedRepositoryUserSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private PermissionResolver $permissionResolver, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
AuthenticationTokenCreatedEvent::class => ['onAuthenticationTokenCreated', 10], | ||
]; | ||
} | ||
|
||
public function onAuthenticationTokenCreated(AuthenticationTokenCreatedEvent $event): void | ||
{ | ||
$user = $event->getAuthenticatedToken()->getUser(); | ||
if (!$user instanceof IbexaUser) { | ||
return; | ||
} | ||
|
||
$this->permissionResolver->setCurrentUserReference($user->getAPIUser()); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...thentication/EventSubscriber/OnAuthenticationTokenCreatedRepositoryUserSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Core\MVC\Symfony\Security\Authentication\EventSubscriber; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Core\MVC\Symfony\Security\Authentication\EventSubscriber\OnAuthenticationTokenCreatedRepositoryUserSubscriber; | ||
use Ibexa\Core\MVC\Symfony\Security\User; | ||
use Ibexa\Core\Repository\Values\User\User as ApiUser; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
use Symfony\Component\Security\Core\User\InMemoryUser; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | ||
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent; | ||
|
||
final class OnAuthenticationTokenCreatedRepositoryUserSubscriberTest extends TestCase | ||
{ | ||
public function testGetSubscribedEvents(): void | ||
{ | ||
$subscriber = new OnAuthenticationTokenCreatedRepositoryUserSubscriber( | ||
$this->createMock(PermissionResolver::class) | ||
); | ||
|
||
self::assertEquals( | ||
[ | ||
AuthenticationTokenCreatedEvent::class => ['onAuthenticationTokenCreated', 10], | ||
], | ||
$subscriber->getSubscribedEvents() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForTestSettingCurrentUserReference | ||
*/ | ||
public function testSettingCurrentUserReference( | ||
UserInterface $user, | ||
bool $isPermissionResolverInvoked | ||
): void { | ||
$permissionResolver = $this->createMock(PermissionResolver::class); | ||
$permissionResolver | ||
->expects($isPermissionResolverInvoked === true ? self::once() : self::never()) | ||
->method('setCurrentUserReference'); | ||
|
||
$subscriber = new OnAuthenticationTokenCreatedRepositoryUserSubscriber($permissionResolver); | ||
|
||
$subscriber->onAuthenticationTokenCreated( | ||
$this->getAuthenticationTokenCreatedEvent($user) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{\Symfony\Component\Security\Core\User\UserInterface, bool}> | ||
*/ | ||
public function dataProviderForTestSettingCurrentUserReference(): iterable | ||
{ | ||
yield 'authorizing Ibexa user' => [ | ||
new User($this->createMock(ApiUser::class)), | ||
true, | ||
]; | ||
|
||
yield 'authorizing non-Ibexa user' => [ | ||
new InMemoryUser('foo', 'bar'), | ||
false, | ||
]; | ||
} | ||
|
||
private function getAuthenticationTokenCreatedEvent(UserInterface $user): AuthenticationTokenCreatedEvent | ||
{ | ||
return new AuthenticationTokenCreatedEvent( | ||
new UsernamePasswordToken($user, 'test_firewall'), | ||
new Passport( | ||
new UserBadge('foo'), | ||
new PasswordCredentials('bar') | ||
) | ||
); | ||
} | ||
} |