Skip to content

Commit

Permalink
Merge pull request PrestaShop#32992 from FabienPapet/symfony6-fix-use…
Browse files Browse the repository at this point in the history
…r-deprecations

Symfony 6 - Fix user deprecations
  • Loading branch information
kpodemski authored Aug 9, 2023
2 parents cd2f0c4 + 5072967 commit 9420938
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Adapter/Security/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function onKernelRequest(RequestEvent $event)

//if employee loggdin in legacy context, authenticate him into sf2 security context
if (isset($this->legacyContext->employee) && $this->legacyContext->employee->isLoggedBack()) {
$user = $this->userProvider->loadUserByUsername($this->legacyContext->employee->email);
$user = $this->userProvider->loadUserByIdentifier($this->legacyContext->employee->email);
$token = new UsernamePasswordToken($user, 'admin', $user->getRoles());
$this->securityTokenStorage->setToken($token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
PrestaShopBundle\Security\OAuth2\Repository\ClientRepository:
arguments:
- '@security.user.provider.concrete.oauth2'
- '@security.user_password_encoder.generic'
- '@security.user_password_hasher'

PrestaShopBundle\Security\OAuth2\Repository\AccessTokenRepository:

Expand Down
10 changes: 8 additions & 2 deletions src/PrestaShopBundle/Security/Admin/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
namespace PrestaShopBundle\Security\Admin;

use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Class Employee is used for Symfony security components to authenticate the user.
*/
class Employee implements UserInterface, EquatableInterface
class Employee implements UserInterface, EquatableInterface, PasswordAuthenticatedUserInterface
{
/**
* @var int
Expand Down Expand Up @@ -95,7 +96,7 @@ public function getRoles()
*
* @return string
*/
public function getPassword()
public function getPassword(): ?string
{
return $this->password;
}
Expand All @@ -120,6 +121,11 @@ public function getUsername()
return $this->username;
}

public function getUserIdentifier(): string
{
return $this->getUsername();
}

/**
* Get the id of the current employee.
*
Expand Down
14 changes: 12 additions & 2 deletions src/PrestaShopBundle/Security/Admin/EmployeeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(
*
* @throws UserNotFoundException
*/
public function loadUserByUsername($username)
public function loadUserByIdentifier(string $username): Employee
{
$cacheKey = sha1($username);
$cachedEmployee = $this->cache->getItem("app.employees_{$cacheKey}");
Expand Down Expand Up @@ -112,7 +112,7 @@ public function refreshUser(UserInterface $employee)
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $employee::class));
}

return $this->loadUserByUsername($employee->getUsername());
return $this->loadUserByIdentifier($employee->getUserIdentifier());
}

/**
Expand All @@ -126,4 +126,14 @@ public function supportsClass($class)
{
return $class === 'PrestaShopBundle\Security\Admin\Employee';
}

/**
* Needed by the interface but not used.
*
* @deprecated since 9.0, to be removed when Symfony > 6.à
*/
public function loadUserByUsername(string $username)
{
return $this->loadUserByIdentifier($username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use PrestaShopBundle\Security\OAuth2\Entity\Client;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

Expand All @@ -48,11 +49,11 @@ class ClientRepository implements ClientRepositoryInterface
private $userProvider;

/**
* @var UserPasswordEncoderInterface
* @var UserPasswordHasherInterface
*/
private $passwordEncoder;

public function __construct(UserProviderInterface $userProvider, UserPasswordEncoderInterface $passwordEncoder)
public function __construct(UserProviderInterface $userProvider, UserPasswordHasherInterface $passwordEncoder)
{
$this->userProvider = $userProvider;
$this->passwordEncoder = $passwordEncoder;
Expand All @@ -67,7 +68,7 @@ public function getClientEntity($clientIdentifier): ?Client
}

$client = new Client();
$client->setIdentifier($user->getUsername());
$client->setIdentifier($user->getUserIdentifier());

return $client;
}
Expand All @@ -77,15 +78,24 @@ public function validateClient($clientIdentifier, $clientSecret, $grantType): bo
if ($grantType !== 'client_credentials' || $clientSecret === null) {
return false;
}

$client = $this->getUser($clientIdentifier);

return $client !== null && $this->passwordEncoder->isPasswordValid($client, $clientSecret);
if ($client === null) {
return false;
}

if (!$client instanceof PasswordAuthenticatedUserInterface) {
throw new \LogicException(sprintf('The class %s should implement %s.', $client::class, PasswordAuthenticatedUserInterface::class));
}

return $this->passwordEncoder->isPasswordValid($client, $clientSecret);
}

private function getUser($clientIdentifier): ?UserInterface
{
try {
return $this->userProvider->loadUserByUsername($clientIdentifier);
return $this->userProvider->loadUserByIdentifier($clientIdentifier);
} catch (UserNotFoundException $exception) {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/PrestaShopBundle/Security/OAuth2/ResourceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function isTokenValid(ServerRequestInterface $request): bool
{
try {
$this->leagueResourceServer->validateAuthenticatedRequest($request);
} catch (OAuthServerException $e) {
} catch (OAuthServerException) {
return false;
}

Expand All @@ -79,7 +79,7 @@ public function getUser(ServerRequestInterface $request): ?UserInterface
}

try {
return $this->userProvider->loadUserByUsername($audience);
return $this->userProvider->loadUserByIdentifier($audience);
} catch (UserNotFoundException $exception) {
return null;
}
Expand All @@ -89,7 +89,7 @@ private function getAudience(ServerRequestInterface $request): ?string
{
try {
return $this->leagueResourceServer->validateAuthenticatedRequest($request)->getAttribute('oauth_client_id');
} catch (OAuthServerException $exception) {
} catch (OAuthServerException) {
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/PrestaShopBundle/Service/DataProvider/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getUser(): ?UserInterface
}

if ($this->legacyContext->getContext()->employee && !empty($this->legacyContext->getContext()->employee->email)) {
return $this->userProvider->loadUserByUsername($this->legacyContext->getContext()->employee->email);
return $this->userProvider->loadUserByIdentifier($this->legacyContext->getContext()->employee->email);
}

return null;
Expand All @@ -69,7 +69,7 @@ public function getUsername(): string
{
$user = $this->getUser();
if ($user instanceof UserInterface) {
return $user->getUsername();
return $user->getUserIdentifier();
}

return self::ANONYMOUS_USER;
Expand Down
2 changes: 1 addition & 1 deletion tests/UI/data/demo/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export default {
keycloak: new ModuleData({
tag: 'keycloak_connector_demo',
name: 'Keycloak OAuth2 connector demo',
releaseZip: 'https://github.com/PrestaShop/keycloak_connector_demo/releases/download/v1.0.3/keycloak_connector_demo.zip',
releaseZip: 'https://github.com/PrestaShop/keycloak_connector_demo/releases/download/v1.0.4/keycloak_connector_demo.zip',
}),
};
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ class Titles extends BOBasePage {
* @return {Promise<void>}
*/
async filterTitles(page: Page, filterType: string, filterBy: string, value: string): Promise<void> {
const currentUrl: string = page.url();

switch (filterType) {
case 'input':
await this.setValue(page, this.filterColumn(filterBy), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use PrestaShop\PrestaShop\Adapter\Session\Repository\CustomerSessionRepository;
use PrestaShop\PrestaShop\Core\Domain\Security\Command\BulkDeleteCustomerSessionsCommand;

class BulkDeleteCustomerSessionHandlerTest extends TestCase
class BulkDeleteCustomersSessionHandlerTest extends TestCase
{
public function testHandleDeleteShouldBeCalledOnlyOnce(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use PrestaShop\PrestaShop\Core\Domain\Security\Exception\SessionException;
use PrestaShop\PrestaShop\Core\Domain\Security\ValueObject\CustomerSessionId;

class CustomerSessionTest extends TestCase
class CustomerSessionIdTest extends TestCase
{
/**
* @dataProvider createsSessionIdWithValidValuesData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use PrestaShop\PrestaShop\Core\Domain\Security\Exception\SessionException;
use PrestaShop\PrestaShop\Core\Domain\Security\ValueObject\EmployeeSessionId;

class EmployeeSessionTest extends TestCase
class EmployeeSessionIdTest extends TestCase
{
/**
* @dataProvider createsSessionIdWithValidValuesData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
use League\OAuth2\Server\Entities\ClientEntityInterface;
use PHPUnit\Framework\TestCase;
use PrestaShopBundle\Security\OAuth2\Repository\ClientRepository;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\User\User;

class ClientRepositoryTest extends TestCase
{
Expand All @@ -43,8 +43,8 @@ public function setUp(): void
$userProvider = new InMemoryUserProvider(['myclientid' => ['password' => 'myclientsecret']]);
$this->clientRepository = new ClientRepository(
$userProvider,
new UserPasswordEncoder(new EncoderFactory([
User::class => ['algorithm' => 'plaintext', 'ignore_case' => false],
new UserPasswordHasher(new PasswordHasherFactory([
InMemoryUser::class => ['algorithm' => 'plaintext', 'ignore_case' => false],
]))
);
parent::setUp();
Expand Down

0 comments on commit 9420938

Please sign in to comment.