[Security] Remove UserLogin

This commit is contained in:
Michał Pysiak 2024-10-09 08:10:57 +02:00 committed by Grzegorz Sadowski
parent 4f55481f93
commit f66e840449
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
9 changed files with 13 additions and 213 deletions

View file

@ -1855,11 +1855,6 @@ parameters:
count: 1
path: src/Sylius/Bundle/UserBundle/Provider/UserProviderInterface.php
-
message: "#^Method Sylius\\\\Bundle\\\\UserBundle\\\\Security\\\\UserLoginInterface\\:\\:login\\(\\) has no return type specified\\.$#"
count: 1
path: src/Sylius/Bundle/UserBundle/Security/UserLoginInterface.php
-
message: "#^Method Sylius\\\\Component\\\\Addressing\\\\Comparator\\\\AddressComparator\\:\\:normalizeAddress\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1

View file

@ -14,25 +14,25 @@ declare(strict_types=1);
namespace Sylius\Bundle\ShopBundle\EventListener;
use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\UserBundle\Security\UserLoginInterface;
use Sylius\Bundle\UserBundle\UserEvents;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
final class UserRegistrationListener
final readonly class UserRegistrationListener
{
public function __construct(
private ObjectManager $userManager,
private GeneratorInterface $tokenGenerator,
private EventDispatcherInterface $eventDispatcher,
private ChannelContextInterface $channelContext,
private UserLoginInterface $userLogin,
private Security $security,
private string $firewallContextName,
) {
}
@ -74,6 +74,6 @@ final class UserRegistrationListener
$this->userManager->persist($user);
$this->userManager->flush();
$this->userLogin->login($user, $this->firewallContextName);
$this->security->login($user, $this->firewallContextName);
}
}

View file

@ -58,7 +58,7 @@
<argument type="service" id="sylius.shop_user.token_generator.email_verification" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.security.user_login" />
<argument type="service" id="security.helper" />
<argument>%sylius_shop.firewall_context_name%</argument>
<tag name="kernel.event_listener" event="sylius.customer.post_register" method="handleUserVerification" />
</service>

View file

@ -16,13 +16,13 @@ namespace spec\Sylius\Bundle\ShopBundle\EventListener;
use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\UserBundle\Security\UserLoginInterface;
use Sylius\Bundle\UserBundle\UserEvents;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
@ -33,14 +33,14 @@ final class UserRegistrationListenerSpec extends ObjectBehavior
GeneratorInterface $tokenGenerator,
EventDispatcherInterface $eventDispatcher,
ChannelContextInterface $channelContext,
UserLoginInterface $userLogin,
Security $security,
): void {
$this->beConstructedWith(
$userManager,
$tokenGenerator,
$eventDispatcher,
$channelContext,
$userLogin,
$security,
'shop',
);
}
@ -80,7 +80,7 @@ final class UserRegistrationListenerSpec extends ObjectBehavior
GeneratorInterface $tokenGenerator,
EventDispatcherInterface $eventDispatcher,
ChannelContextInterface $channelContext,
UserLoginInterface $userLogin,
Security $security,
GenericEvent $event,
CustomerInterface $customer,
ShopUserInterface $user,
@ -97,7 +97,7 @@ final class UserRegistrationListenerSpec extends ObjectBehavior
$userManager->persist($user)->shouldBeCalled();
$userManager->flush()->shouldBeCalled();
$userLogin->login($user, 'shop')->shouldBeCalled();
$security->login($user, 'shop')->shouldBeCalled();
$tokenGenerator->generate()->shouldNotBeCalled();
$user->setEmailVerificationToken(Argument::any())->shouldNotBeCalled();
@ -115,7 +115,7 @@ final class UserRegistrationListenerSpec extends ObjectBehavior
GeneratorInterface $tokenGenerator,
EventDispatcherInterface $eventDispatcher,
ChannelContextInterface $channelContext,
UserLoginInterface $userLogin,
Security $security,
GenericEvent $event,
CustomerInterface $customer,
ShopUserInterface $user,
@ -132,7 +132,7 @@ final class UserRegistrationListenerSpec extends ObjectBehavior
$userManager->persist($user)->shouldBeCalled();
$userManager->flush()->shouldBeCalled();
$userLogin->login($user, 'shop')->shouldBeCalled();
$security->login($user, 'shop')->shouldBeCalled();
$tokenGenerator->generate()->shouldNotBeCalled();
$user->setEmailVerificationToken(Argument::any())->shouldNotBeCalled();

View file

@ -236,7 +236,7 @@ final class SyliusUserExtension extends AbstractResourceExtension
$container->setDefinition($providerEmailOrNameBasedServiceId, $emailOrNameBasedProviderDefinition);
}
private function createResettingTokenParameters(string $userType, array $config, ContainerBuilder $container)
private function createResettingTokenParameters(string $userType, array $config, ContainerBuilder $container): void
{
$container->setParameter(sprintf('sylius.%s_user.token.password_reset.ttl', $userType), $config['resetting']['token']['ttl']);
}

View file

@ -64,13 +64,6 @@
<service id="Sylius\Component\User\Security\PasswordUpdaterInterface" alias="sylius.security.password_updater" />
<service id="sylius.security.user_login" class="Sylius\Bundle\UserBundle\Security\UserLogin">
<argument type="service" id="security.token_storage" />
<argument type="service" id="security.user_checker" />
<argument type="service" id="event_dispatcher" />
</service>
<service id="Sylius\Bundle\UserBundle\Security\UserLoginInterface" alias="sylius.security.user_login" />
<!-- Listeners -->
<service id="sylius.listener.password_updater" class="Sylius\Bundle\UserBundle\EventListener\PasswordUpdaterListener">
<argument type="service" id="sylius.security.password_updater" />

View file

@ -1,63 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\UserBundle\Security;
use Sylius\Bundle\UserBundle\Event\UserEvent;
use Sylius\Bundle\UserBundle\UserEvents;
use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
use Webmozart\Assert\Assert;
readonly class UserLogin implements UserLoginInterface
{
public function __construct(
private TokenStorageInterface $tokenStorage,
private UserCheckerInterface $userChecker,
private EventDispatcherInterface $eventDispatcher,
) {
}
public function login(UserInterface $user, ?string $firewallName = null): void
{
$firewallName = $firewallName ?? 'main';
Assert::isInstanceOf($user, SymfonyUserInterface::class);
$this->userChecker->checkPreAuth($user);
$this->userChecker->checkPostAuth($user);
$token = $this->createToken($user, $firewallName);
if (null === $token->getUser() || [] === $token->getUser()->getRoles()) {
throw new AuthenticationException('Unauthenticated token');
}
$this->tokenStorage->setToken($token);
$this->eventDispatcher->dispatch(new UserEvent($user), UserEvents::SECURITY_IMPLICIT_LOGIN);
}
protected function createToken(UserInterface $user, string $firewallName): UsernamePasswordToken
{
Assert::isInstanceOf($user, SymfonyUserInterface::class);
return new UsernamePasswordToken(
$user,
$firewallName,
array_map(static fn (object|string $role): string => $role, $user->getRoles()),
);
}
}

View file

@ -1,21 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\UserBundle\Security;
use Sylius\Component\User\Model\UserInterface;
interface UserLoginInterface
{
public function login(UserInterface $user, ?string $firewallName = null);
}

View file

@ -1,104 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace spec\Sylius\Bundle\UserBundle\Security;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\UserBundle\Event\UserEvent;
use Sylius\Bundle\UserBundle\Security\UserLoginInterface;
use Sylius\Bundle\UserBundle\UserEvents;
use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
final class UserLoginSpec extends ObjectBehavior
{
function let(TokenStorageInterface $tokenStorage, UserCheckerInterface $userChecker, EventDispatcherInterface $eventDispatcher): void
{
$this->beConstructedWith($tokenStorage, $userChecker, $eventDispatcher);
}
function it_implements_user_login_interface(): void
{
$this->shouldImplement(UserLoginInterface::class);
}
function it_throws_exception_and_does_not_log_user_in_when_user_is_disabled(
TokenStorageInterface $tokenStorage,
UserCheckerInterface $userChecker,
EventDispatcherInterface $eventDispatcher,
UserInterface $user,
): void {
$user->getRoles()->willReturn(['ROLE_TEST']);
$userChecker->checkPreAuth($user)->willThrow(DisabledException::class);
$tokenStorage->setToken(Argument::type(UsernamePasswordToken::class))->shouldNotBeCalled();
$eventDispatcher->dispatch(Argument::type(UserEvent::class), UserEvents::SECURITY_IMPLICIT_LOGIN)->shouldNotBeCalled();
$this->shouldThrow(DisabledException::class)->during('login', [$user]);
}
function it_throws_exception_and_does_not_log_user_in_when_user_account_status_is_invalid(
TokenStorageInterface $tokenStorage,
UserCheckerInterface $userChecker,
EventDispatcherInterface $eventDispatcher,
UserInterface $user,
): void {
$user->getRoles()->willReturn(['ROLE_TEST']);
$userChecker->checkPreAuth($user)->shouldBeCalled();
$userChecker->checkPostAuth($user)->willThrow(CredentialsExpiredException::class);
$tokenStorage->setToken(Argument::type(UsernamePasswordToken::class))->shouldNotBeCalled();
$eventDispatcher->dispatch(Argument::type(UserEvent::class), UserEvents::SECURITY_IMPLICIT_LOGIN)->shouldNotBeCalled();
$this->shouldThrow(CredentialsExpiredException::class)->during('login', [$user]);
}
function it_throws_exception_and_does_not_log_user_in_when_user_has_no_roles(
TokenStorageInterface $tokenStorage,
UserCheckerInterface $userChecker,
EventDispatcherInterface $eventDispatcher,
UserInterface $user,
): void {
$user->getRoles()->willReturn([]);
$userChecker->checkPreAuth($user)->shouldBeCalled();
$userChecker->checkPostAuth($user)->shouldBeCalled();
$tokenStorage->setToken(Argument::type(UsernamePasswordToken::class))->shouldNotBeCalled();
$eventDispatcher->dispatch(Argument::type(UserEvent::class), UserEvents::SECURITY_IMPLICIT_LOGIN)->shouldNotBeCalled();
$this->shouldThrow(AuthenticationException::class)->during('login', [$user]);
}
function it_logs_user_in(
TokenStorageInterface $tokenStorage,
UserCheckerInterface $userChecker,
EventDispatcherInterface $eventDispatcher,
UserInterface $user,
): void {
$user->getRoles()->willReturn(['ROLE_TEST']);
$userChecker->checkPreAuth($user)->shouldBeCalled();
$userChecker->checkPostAuth($user)->shouldBeCalled();
$tokenStorage->setToken(Argument::type(UsernamePasswordToken::class))->shouldBeCalled();
$eventDispatcher->dispatch(Argument::type(UserEvent::class), UserEvents::SECURITY_IMPLICIT_LOGIN)->shouldBeCalled();
$this->login($user);
}
}