Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-8138: Refactored deprecated loadUserByUsername method in custom providers #400

Merged
merged 9 commits into from
Jul 23, 2024
120 changes: 0 additions & 120 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12365,16 +12365,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 @@ -47195,116 +47185,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
Loading