Skip to content

Commit da23f87

Browse files
alongoszSteveb-p
andauthored
IBX-8138: Refactored deprecated loadUserByUsername method (#400)
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]>
1 parent 8cd53fc commit da23f87

File tree

7 files changed

+243
-478
lines changed

7 files changed

+243
-478
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -12330,16 +12330,6 @@ parameters:
1233012330
count: 1
1233112331
path: src/lib/MVC/Symfony/Security/InteractiveLoginToken.php
1233212332

12333-
-
12334-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProvider\\:\\:loadUserByUsername\\(\\) has parameter \\$user with no type specified\\.$#"
12335-
count: 1
12336-
path: src/lib/MVC/Symfony/Security/User/EmailProvider.php
12337-
12338-
-
12339-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProvider\\:\\:loadUserByUsername\\(\\) has parameter \\$user with no type specified\\.$#"
12340-
count: 1
12341-
path: src/lib/MVC/Symfony/Security/User/UsernameProvider.php
12342-
1234312333
-
1234412334
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\UserInterface\\:\\:setAPIUser\\(\\) has no return type specified\\.$#"
1234512335
count: 1
@@ -47100,116 +47090,6 @@ parameters:
4710047090
count: 1
4710147091
path: tests/lib/MVC/Symfony/Security/InteractiveLoginTokenTest.php
4710247092

47103-
-
47104-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:supportsClassProvider\\(\\) has no return type specified\\.$#"
47105-
count: 1
47106-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47107-
47108-
-
47109-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByAPIUser\\(\\) has no return type specified\\.$#"
47110-
count: 1
47111-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47112-
47113-
-
47114-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsername\\(\\) has no return type specified\\.$#"
47115-
count: 1
47116-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47117-
47118-
-
47119-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsernameAlreadyUserObject\\(\\) has no return type specified\\.$#"
47120-
count: 1
47121-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47122-
47123-
-
47124-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsernameUserNotFound\\(\\) has no return type specified\\.$#"
47125-
count: 1
47126-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47127-
47128-
-
47129-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUser\\(\\) has no return type specified\\.$#"
47130-
count: 1
47131-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47132-
47133-
-
47134-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUserNotFound\\(\\) has no return type specified\\.$#"
47135-
count: 1
47136-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47137-
47138-
-
47139-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUserNotSupported\\(\\) has no return type specified\\.$#"
47140-
count: 1
47141-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47142-
47143-
-
47144-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has no return type specified\\.$#"
47145-
count: 1
47146-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47147-
47148-
-
47149-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$class with no type specified\\.$#"
47150-
count: 1
47151-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47152-
47153-
-
47154-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$supports with no type specified\\.$#"
47155-
count: 1
47156-
path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
47157-
47158-
-
47159-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:supportsClassProvider\\(\\) has no return type specified\\.$#"
47160-
count: 1
47161-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47162-
47163-
-
47164-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByAPIUser\\(\\) has no return type specified\\.$#"
47165-
count: 1
47166-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47167-
47168-
-
47169-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsername\\(\\) has no return type specified\\.$#"
47170-
count: 1
47171-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47172-
47173-
-
47174-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsernameAlreadyUserObject\\(\\) has no return type specified\\.$#"
47175-
count: 1
47176-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47177-
47178-
-
47179-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsernameUserNotFound\\(\\) has no return type specified\\.$#"
47180-
count: 1
47181-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47182-
47183-
-
47184-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUser\\(\\) has no return type specified\\.$#"
47185-
count: 1
47186-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47187-
47188-
-
47189-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUserNotFound\\(\\) has no return type specified\\.$#"
47190-
count: 1
47191-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47192-
47193-
-
47194-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUserNotSupported\\(\\) has no return type specified\\.$#"
47195-
count: 1
47196-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47197-
47198-
-
47199-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has no return type specified\\.$#"
47200-
count: 1
47201-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47202-
47203-
-
47204-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$class with no type specified\\.$#"
47205-
count: 1
47206-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47207-
47208-
-
47209-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$supports with no type specified\\.$#"
47210-
count: 1
47211-
path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
47212-
4721347093
-
4721447094
message: "#^Parameter \\#1 \\$expirationDate of class Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\PasswordInfo constructor expects DateTimeImmutable\\|null, DateTimeImmutable\\|false given\\.$#"
4721547095
count: 1

src/contracts/Repository/UserService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public function loadUser(int $userId, array $prioritizedLanguages = []): User;
165165
* @return \Ibexa\Contracts\Core\Repository\Values\User\User
166166
*
167167
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException if a user with the given credentials was not found
168+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
168169
*/
169170
public function loadUserByLogin(string $login, array $prioritizedLanguages = []): User;
170171

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

src/lib/MVC/Symfony/Security/User/EmailProvider.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88

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

11+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1112
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
12-
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
13-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
13+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
14+
use Symfony\Component\Security\Core\User\UserInterface;
1415

1516
final class EmailProvider extends BaseProvider
1617
{
17-
public function loadUserByUsername($user)
18+
public function loadUserByIdentifier(string $identifier): UserInterface
1819
{
1920
try {
20-
// SecurityContext always tries to authenticate anonymous users when checking granted access.
21-
// In that case $user is an instance of \Ibexa\Core\MVC\Symfony\Security\User.
22-
// We don't need to reload the user here.
23-
if ($user instanceof UserInterface) {
24-
return $user;
25-
}
26-
2721
return $this->createSecurityUser(
28-
$this->userService->loadUserByEmail($user)
22+
$this->userService->loadUserByEmail($identifier)
2923
);
30-
} catch (NotFoundException $e) {
31-
throw new UsernameNotFoundException($e->getMessage(), 0, $e);
24+
} catch (NotFoundException|InvalidArgumentException $e) {
25+
throw new UserNotFoundException($e->getMessage(), 0, $e);
3226
}
3327
}
28+
29+
public function loadUserByUsername(string $username): UserInterface
30+
{
31+
return $this->loadUserByIdentifier($username);
32+
}
3433
}

src/lib/MVC/Symfony/Security/User/UsernameProvider.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88

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

11+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1112
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
12-
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
13-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
13+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
14+
use Symfony\Component\Security\Core\User\UserInterface;
1415

1516
final class UsernameProvider extends BaseProvider
1617
{
17-
public function loadUserByUsername($user)
18+
public function loadUserByIdentifier(string $identifier): UserInterface
1819
{
1920
try {
20-
// SecurityContext always tries to authenticate anonymous users when checking granted access.
21-
// In that case $user is an instance of \Ibexa\Core\MVC\Symfony\Security\User.
22-
// We don't need to reload the user here.
23-
if ($user instanceof UserInterface) {
24-
return $user;
25-
}
26-
2721
return $this->createSecurityUser(
28-
$this->userService->loadUserByLogin($user)
22+
$this->userService->loadUserByLogin($identifier)
2923
);
30-
} catch (NotFoundException $e) {
31-
throw new UsernameNotFoundException($e->getMessage(), 0, $e);
24+
} catch (NotFoundException|InvalidArgumentException $e) {
25+
throw new UserNotFoundException($e->getMessage(), 0, $e);
3226
}
3327
}
28+
29+
public function loadUserByUsername(string $username): UserInterface
30+
{
31+
return $this->loadUserByIdentifier($username);
32+
}
3433
}

0 commit comments

Comments
 (0)