Skip to content

Commit

Permalink
IBX-8138: Refactored deprecated loadUserByUsername method (#400)
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-8138 and #400

Key changes:

* Implemented `loadUserByIdentifier` in custom User providers

* Aligned Username and Email providers with Symfony interface

* [Tests] Dropped usage of deprecated `loadUserByUsername` method

* [Tests] Dropped redundancy in EmailProvider and UsernameProvider tests

* [PHPDoc] Added missing throw annotations to `UserService::loadUserBy*` methods

* [PHPStan] Aligned baseline with the changes

---------

Co-Authored-By: Paweł Niedzielski <[email protected]>
  • Loading branch information
2 people authored and barw4 committed Oct 17, 2024
1 parent 8817e7b commit 7a51f9d
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 478 deletions.
120 changes: 0 additions & 120 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12225,16 +12225,6 @@ parameters:
count: 1
path: src/lib/MVC/Symfony/Security/InteractiveLoginToken.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProvider\\:\\:loadUserByUsername\\(\\) has parameter \\$user with no type specified\\.$#"
count: 1
path: src/lib/MVC/Symfony/Security/User/EmailProvider.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProvider\\:\\:loadUserByUsername\\(\\) has parameter \\$user with no type specified\\.$#"
count: 1
path: src/lib/MVC/Symfony/Security/User/UsernameProvider.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\UserInterface\\:\\:setAPIUser\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -46905,116 +46895,6 @@ parameters:
count: 1
path: tests/lib/MVC/Symfony/Security/InteractiveLoginTokenTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:supportsClassProvider\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByAPIUser\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsername\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsernameAlreadyUserObject\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsernameUserNotFound\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUser\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUserNotFound\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUserNotSupported\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$class with no type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$supports with no type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:supportsClassProvider\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByAPIUser\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsername\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsernameAlreadyUserObject\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsernameUserNotFound\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUser\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUserNotFound\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUserNotSupported\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$class with no type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$supports with no type specified\\.$#"
count: 1
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

-
message: "#^Parameter \\#1 \\$expirationDate of class Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\PasswordInfo constructor expects DateTimeImmutable\\|null, DateTimeImmutable\\|false given\\.$#"
count: 1
Expand Down
2 changes: 2 additions & 0 deletions src/contracts/Repository/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public function loadUser(int $userId, array $prioritizedLanguages = []): User;
* @return \Ibexa\Contracts\Core\Repository\Values\User\User
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException if a user with the given credentials was not found
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function loadUserByLogin(string $login, array $prioritizedLanguages = []): User;

Expand All @@ -187,6 +188,7 @@ public function checkUserCredentials(User $user, string $credentials): bool;
* @return \Ibexa\Contracts\Core\Repository\Values\User\User
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function loadUserByEmail(string $email, array $prioritizedLanguages = []): User;

Expand Down
25 changes: 12 additions & 13 deletions src/lib/MVC/Symfony/Security/User/EmailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@

namespace Ibexa\Core\MVC\Symfony\Security\User;

use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;

final class EmailProvider extends BaseProvider
{
public function loadUserByUsername($user)
public function loadUserByIdentifier(string $identifier): UserInterface
{
try {
// SecurityContext always tries to authenticate anonymous users when checking granted access.
// In that case $user is an instance of \Ibexa\Core\MVC\Symfony\Security\User.
// We don't need to reload the user here.
if ($user instanceof UserInterface) {
return $user;
}

return $this->createSecurityUser(
$this->userService->loadUserByEmail($user)
$this->userService->loadUserByEmail($identifier)
);
} catch (NotFoundException $e) {
throw new UsernameNotFoundException($e->getMessage(), 0, $e);
} catch (NotFoundException|InvalidArgumentException $e) {
throw new UserNotFoundException($e->getMessage(), 0, $e);
}
}

public function loadUserByUsername(string $username): UserInterface
{
return $this->loadUserByIdentifier($username);
}
}
25 changes: 12 additions & 13 deletions src/lib/MVC/Symfony/Security/User/UsernameProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@

namespace Ibexa\Core\MVC\Symfony\Security\User;

use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;

final class UsernameProvider extends BaseProvider
{
public function loadUserByUsername($user)
public function loadUserByIdentifier(string $identifier): UserInterface
{
try {
// SecurityContext always tries to authenticate anonymous users when checking granted access.
// In that case $user is an instance of \Ibexa\Core\MVC\Symfony\Security\User.
// We don't need to reload the user here.
if ($user instanceof UserInterface) {
return $user;
}

return $this->createSecurityUser(
$this->userService->loadUserByLogin($user)
$this->userService->loadUserByLogin($identifier)
);
} catch (NotFoundException $e) {
throw new UsernameNotFoundException($e->getMessage(), 0, $e);
} catch (NotFoundException|InvalidArgumentException $e) {
throw new UserNotFoundException($e->getMessage(), 0, $e);
}
}

public function loadUserByUsername(string $username): UserInterface
{
return $this->loadUserByIdentifier($username);
}
}
Loading

0 comments on commit 7a51f9d

Please sign in to comment.