Skip to content

Commit 62e04b2

Browse files
authored
IBX-8356: Removed Ibexa\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface to be replaced with Symfony-based authentication
#375
1 parent 232ee8a commit 62e04b2

File tree

6 files changed

+128
-70
lines changed

6 files changed

+128
-70
lines changed

src/bundle/Core/DependencyInjection/Compiler/SecurityPass.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,5 @@ public function process(ContainerBuilder $container): void
7878
'setEventDispatcher',
7979
[new Reference('event_dispatcher')]
8080
);
81-
$successHandlerDef->addMethodCall(
82-
'setPermissionResolver',
83-
[$permissionResolverRef]
84-
);
8581
}
8682
}

src/bundle/Core/Resources/config/security.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ services:
4747
Ibexa\Core\MVC\Symfony\Security\Authentication\EventSubscriber\AccessDeniedSubscriber:
4848
autowire: true
4949
autoconfigure: true
50+
51+
Ibexa\Core\MVC\Symfony\Security\Authentication\EventSubscriber\OnAuthenticationTokenCreatedRepositoryUserSubscriber:
52+
autowire: true
53+
autoconfigure: true

src/lib/MVC/Symfony/Security/Authentication/AuthenticatorInterface.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/lib/MVC/Symfony/Security/Authentication/DefaultAuthenticationSuccessHandler.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88

99
namespace Ibexa\Core\MVC\Symfony\Security\Authentication;
1010

11-
use Ibexa\Contracts\Core\Repository\PermissionResolver;
1211
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
13-
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
1412
use Psr\EventDispatcher\EventDispatcherInterface;
1513
use Symfony\Component\HttpFoundation\Request;
16-
use Symfony\Component\HttpFoundation\Response;
17-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1814
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as BaseSuccessHandler;
1915

2016
final class DefaultAuthenticationSuccessHandler extends BaseSuccessHandler
@@ -23,8 +19,6 @@ final class DefaultAuthenticationSuccessHandler extends BaseSuccessHandler
2319

2420
private ConfigResolverInterface $configResolver;
2521

26-
private PermissionResolver $permissionResolver;
27-
2822
public function setConfigResolver(ConfigResolverInterface $configResolver): void
2923
{
3024
$this->configResolver = $configResolver;
@@ -35,21 +29,6 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): v
3529
$this->eventDispatcher = $eventDispatcher;
3630
}
3731

38-
public function setPermissionResolver(PermissionResolver $permissionResolver): void
39-
{
40-
$this->permissionResolver = $permissionResolver;
41-
}
42-
43-
public function onAuthenticationSuccess(Request $request, TokenInterface $token): ?Response
44-
{
45-
$user = $token->getUser();
46-
if ($user instanceof UserInterface && isset($this->permissionResolver)) {
47-
$this->permissionResolver->setCurrentUserReference($user->getAPIUser());
48-
}
49-
50-
return parent::onAuthenticationSuccess($request, $token);
51-
}
52-
5332
protected function determineTargetUrl(Request $request): string
5433
{
5534
if (isset($this->configResolver)) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Core\MVC\Symfony\Security\Authentication\EventSubscriber;
10+
11+
use Ibexa\Contracts\Core\Repository\PermissionResolver;
12+
use Ibexa\Core\MVC\Symfony\Security\UserInterface as IbexaUser;
13+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent;
15+
16+
final readonly class OnAuthenticationTokenCreatedRepositoryUserSubscriber implements EventSubscriberInterface
17+
{
18+
public function __construct(
19+
private PermissionResolver $permissionResolver,
20+
) {
21+
}
22+
23+
public static function getSubscribedEvents(): array
24+
{
25+
return [
26+
AuthenticationTokenCreatedEvent::class => ['onAuthenticationTokenCreated', 10],
27+
];
28+
}
29+
30+
public function onAuthenticationTokenCreated(AuthenticationTokenCreatedEvent $event): void
31+
{
32+
$user = $event->getAuthenticatedToken()->getUser();
33+
if (!$user instanceof IbexaUser) {
34+
return;
35+
}
36+
37+
$this->permissionResolver->setCurrentUserReference($user->getAPIUser());
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Core\MVC\Symfony\Security\Authentication\EventSubscriber;
10+
11+
use Ibexa\Contracts\Core\Repository\PermissionResolver;
12+
use Ibexa\Core\MVC\Symfony\Security\Authentication\EventSubscriber\OnAuthenticationTokenCreatedRepositoryUserSubscriber;
13+
use Ibexa\Core\MVC\Symfony\Security\User;
14+
use Ibexa\Core\Repository\Values\User\User as ApiUser;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
17+
use Symfony\Component\Security\Core\User\InMemoryUser;
18+
use Symfony\Component\Security\Core\User\UserInterface;
19+
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
20+
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
21+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
22+
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent;
23+
24+
final class OnAuthenticationTokenCreatedRepositoryUserSubscriberTest extends TestCase
25+
{
26+
public function testGetSubscribedEvents(): void
27+
{
28+
$subscriber = new OnAuthenticationTokenCreatedRepositoryUserSubscriber(
29+
$this->createMock(PermissionResolver::class)
30+
);
31+
32+
self::assertEquals(
33+
[
34+
AuthenticationTokenCreatedEvent::class => ['onAuthenticationTokenCreated', 10],
35+
],
36+
$subscriber->getSubscribedEvents()
37+
);
38+
}
39+
40+
/**
41+
* @dataProvider dataProviderForTestSettingCurrentUserReference
42+
*/
43+
public function testSettingCurrentUserReference(
44+
UserInterface $user,
45+
bool $isPermissionResolverInvoked
46+
): void {
47+
$permissionResolver = $this->createMock(PermissionResolver::class);
48+
$permissionResolver
49+
->expects($isPermissionResolverInvoked === true ? self::once() : self::never())
50+
->method('setCurrentUserReference');
51+
52+
$subscriber = new OnAuthenticationTokenCreatedRepositoryUserSubscriber($permissionResolver);
53+
54+
$subscriber->onAuthenticationTokenCreated(
55+
$this->getAuthenticationTokenCreatedEvent($user)
56+
);
57+
}
58+
59+
/**
60+
* @return iterable<string, array{\Symfony\Component\Security\Core\User\UserInterface, bool}>
61+
*/
62+
public function dataProviderForTestSettingCurrentUserReference(): iterable
63+
{
64+
yield 'authorizing Ibexa user' => [
65+
new User($this->createMock(ApiUser::class)),
66+
true,
67+
];
68+
69+
yield 'authorizing non-Ibexa user' => [
70+
new InMemoryUser('foo', 'bar'),
71+
false,
72+
];
73+
}
74+
75+
private function getAuthenticationTokenCreatedEvent(UserInterface $user): AuthenticationTokenCreatedEvent
76+
{
77+
return new AuthenticationTokenCreatedEvent(
78+
new UsernamePasswordToken($user, 'test_firewall'),
79+
new Passport(
80+
new UserBadge('foo'),
81+
new PasswordCredentials('bar')
82+
)
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)