Skip to content

Commit

Permalink
make workorounds over deprecated method in symofny 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Dec 2, 2021
1 parent 6339528 commit 2964e0e
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 9 deletions.
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
security:
always_authenticate_before_granting: true
providers:
sylius_admin_user_provider:
id: sylius.admin_user_provider.email_or_name_based
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<referencedMethod name="Symfony\Component\Security\Core\User\UserInterface::getUsername" /> <!-- deprecated in Symfony 5.3 -->
<referencedMethod name="Symfony\Component\Security\Core\User\UserProviderInterface::loadUserByUsername" /> <!-- deprecated in Symfony 5.3 -->
<referencedMethod name="Faker\Generator::__get"/>
<referencedMethod name="Symfony\Component\Security\Core\Authentication\Token\AbstractToken::isAuthenticated" /> <!-- deprecated in Symfony 5.4 -->
</errorLevel>
</DeprecatedMethod>

Expand Down Expand Up @@ -202,6 +203,7 @@
<referencedFunction name="Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch" />
<referencedFunction name="Symfony\Component\HttpKernel\Config\FileLocator::__construct" />
<referencedFunction name="Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch" />
<referencedFunction name="Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct" /> <!-- removed parameter in Symfony 5.4 -->
</errorLevel>
<errorLevel type="suppress">
<referencedFunction name="Doctrine\ORM\Query\Expr::andX" />
Expand All @@ -225,6 +227,7 @@
<errorLevel type="info">
<referencedFunction name="Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch" />
<referencedFunction name="SyliusLabs\AssociationHydrator\AssociationHydrator::__construct" />
<referencedFunction name="Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct" />
</errorLevel>
</InvalidArgument>

Expand Down
8 changes: 7 additions & 1 deletion src/Sylius/Behat/Service/SecurityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public function __construct(SessionInterface $session, CookieSetterInterface $co

public function logIn(UserInterface $user): void
{
$token = new UsernamePasswordToken($user, $user->getPassword(), $this->firewallContextName, $user->getRoles());
/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
$token = new UsernamePasswordToken($user, $this->firewallContextName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, $user->getPassword(), $this->firewallContextName, $user->getRoles());
}

$this->setToken($token);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,7 @@
</service>

<service id="Sylius\Bundle\CoreBundle\Calculator\DelayStampCalculatorInterface" class="Sylius\Bundle\CoreBundle\Calculator\DelayStampCalculator"/>

<service id="security.authentication_manager" alias="security.authentication.manager" />
</services>
</container>
21 changes: 16 additions & 5 deletions src/Sylius/Bundle/CoreBundle/Security/UserImpersonator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ public function __construct(SessionInterface $session, string $firewallContextNa

public function impersonate(UserInterface $user): void
{
$token = new UsernamePasswordToken(
$user,
$this->firewallContextName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
$token = new UsernamePasswordToken(
$user,
$this->firewallContextName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
} else {
$token = new UsernamePasswordToken(
$user,
$user->getPassword(),
$this->firewallContextName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
}

$this->session->set($this->sessionTokenParameter, serialize($token));
$this->session->save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,38 @@

namespace Sylius\Bundle\ProductBundle\Controller;

use Sylius\Component\Product\Generator\SlugGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ProductSlugController extends AbstractController
{
private ?SlugGeneratorInterface $slugGenerator;

public function __construct(?SlugGeneratorInterface $slugGenerator = null)
{
$this->slugGenerator = $slugGenerator;

if ($this->slugGenerator === null) {
@trigger_error(sprintf('Not passing a $slugGenerator to %s constructor is deprecated since Sylius 1.11 and will be prohibited in Sylius 2.0.', self::class), \E_USER_DEPRECATED);
}
}

/**
* @psalm-suppress DeprecatedMethod
*/
public function generateAction(Request $request): Response
{
$name = $request->query->get('name');

if ($this->slugGenerator !== null) {
return new JsonResponse([
'slug' => $this->slugGenerator->generate((string) $name),
]);
}

return new JsonResponse([
'slug' => $this->get('sylius.generator.slug')->generate($name),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<defaults public="true" />

<service id="sylius.controller.product_slug" class="Sylius\Bundle\ProductBundle\Controller\ProductSlugController">
<argument type="service" id="sylius.generator.slug" />
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
Expand Down
36 changes: 34 additions & 2 deletions src/Sylius/Bundle/UserBundle/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,45 @@

use Sylius\Bundle\UserBundle\Form\Type\UserLoginType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Webmozart\Assert\Assert;

class SecurityController extends AbstractController
{
private ?AuthenticationUtils $authenticationUtils;

private ?FormFactoryInterface $formFactory;

public function __construct(?AuthenticationUtils $authenticationUtils = null, ?FormFactoryInterface $formFactory = null)
{
$this->authenticationUtils = $authenticationUtils;
$this->formFactory = $formFactory;

if ($this->authenticationUtils === null) {
@trigger_error(sprintf('Not passing a $authenticationUtils to %s constructor is deprecated since Sylius 1.11 and will be prohibited in Sylius 2.0.', self::class), \E_USER_DEPRECATED);
}

if ($this->formFactory === null) {
@trigger_error(sprintf('Not passing a $formFactory to %s constructor is deprecated since Sylius 1.11 and will be prohibited in Sylius 2.0.', self::class), \E_USER_DEPRECATED);
}
}

/**
* Login form action.
*
* @psalm-suppress DeprecatedMethod
*/
public function loginAction(Request $request): Response
{
$authenticationUtils = $this->get('security.authentication_utils');
if ($this->authenticationUtils !== null) {
$authenticationUtils = $this->authenticationUtils;
} else {
$authenticationUtils = $this->get('security.authentication_utils');
}

$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();

Expand All @@ -36,7 +63,12 @@ public function loginAction(Request $request): Response
Assert::notNull($template, 'Template is not configured.');

$formType = $options['form'] ?? UserLoginType::class;
$form = $this->get('form.factory')->createNamed('', $formType);

if ($this->formFactory !== null) {
$form = $this->formFactory->createNamed('', $formType);
} else {
$form = $this->get('form.factory')->createNamed('', $formType);
}

return $this->render($template, [
'form' => $form->createView(),
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/UserBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

<!-- Controllers -->
<service id="sylius.controller.user_security" class="Sylius\Bundle\UserBundle\Controller\SecurityController">
<argument type="service" id="security.authentication_utils" />
<argument type="service" id="form.factory" />
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
Expand Down
10 changes: 10 additions & 0 deletions src/Sylius/Bundle/UserBundle/Security/UserLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ public function login(UserInterface $user, ?string $firewallName = null): void

protected function createToken(UserInterface $user, string $firewallName): UsernamePasswordToken
{
/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
return new UsernamePasswordToken(
$user,
$firewallName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
}

return new UsernamePasswordToken(
$user,
null,
$firewallName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
Expand Down
9 changes: 8 additions & 1 deletion tests/Controller/AdminProductAjaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ private function authenticateAdminUser(): void
$session = self::$container->get('session');
$firewallName = 'admin';
$firewallContext = 'admin';
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());

/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
$token = new UsernamePasswordToken($user, $firewallName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
}

$session->set(sprintf('_security_%s', $firewallContext), serialize($token));
$session->save();

Expand Down

0 comments on commit 2964e0e

Please sign in to comment.