Skip to content

Commit

Permalink
Merge pull request #3085 from stof/clean_user_provider
Browse files Browse the repository at this point in the history
Remove the implementation of the old UserProviderInterface method
  • Loading branch information
stof authored Jun 25, 2024
2 parents dc09a0c + 165dbcf commit c079a95
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 58 deletions.
25 changes: 1 addition & 24 deletions src/Security/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
Expand Down Expand Up @@ -48,24 +47,6 @@ public function loadUserByIdentifier(string $identifier): SecurityUserInterface
return $user;
}

/**
* @param string $username
*/
public function loadUserByUsername($username): SecurityUserInterface
{
$user = $this->findUser($username);

if (!$user) {
if (class_exists(UserNotFoundException::class)) {
throw new UserNotFoundException(sprintf('Username "%s" does not exist.', $username));
}

throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}

return $user;
}

public function refreshUser(SecurityUserInterface $user): SecurityUserInterface
{
if (!$user instanceof UserInterface) {
Expand All @@ -77,11 +58,7 @@ public function refreshUser(SecurityUserInterface $user): SecurityUserInterface
}

if (null === $reloadedUser = $this->userManager->findUserBy(['id' => $user->getId()])) {
if (class_exists(UserNotFoundException::class)) {
throw new UserNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
}

throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
throw new UserNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
}

return $reloadedUser;
Expand Down
17 changes: 4 additions & 13 deletions tests/Security/EmailProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;

class EmailProviderTest extends TestCase
Expand Down Expand Up @@ -45,23 +44,19 @@ public function testLoadUserByUsername()
->with('foobar')
->will($this->returnValue($user));

$this->assertSame($user, $this->userProvider->loadUserByUsername('foobar'));
$this->assertSame($user, $this->userProvider->loadUserByIdentifier('foobar'));
}

public function testLoadUserByInvalidUsername()
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}
$this->expectException(UserNotFoundException::class);

$this->userManager->expects($this->once())
->method('findUserByEmail')
->with('foobar')
->will($this->returnValue(null));

$this->userProvider->loadUserByUsername('foobar');
$this->userProvider->loadUserByIdentifier('foobar');
}

public function testRefreshUserBy()
Expand Down Expand Up @@ -89,11 +84,7 @@ public function testRefreshUserBy()

public function testRefreshDeleted()
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}
$this->expectException(UserNotFoundException::class);

$user = $this->getMockForAbstractClass('FOS\UserBundle\Model\User');
$this->userManager->expects($this->once())
Expand Down
11 changes: 3 additions & 8 deletions tests/Security/EmailUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;

class EmailUserProviderTest extends TestCase
Expand Down Expand Up @@ -45,23 +44,19 @@ public function testLoadUserByUsername()
->with('foobar')
->will($this->returnValue($user));

$this->assertSame($user, $this->userProvider->loadUserByUsername('foobar'));
$this->assertSame($user, $this->userProvider->loadUserByIdentifier('foobar'));
}

public function testLoadUserByInvalidUsername()
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}
$this->expectException(UserNotFoundException::class);

$this->userManager->expects($this->once())
->method('findUserByUsernameOrEmail')
->with('foobar')
->will($this->returnValue(null));

$this->userProvider->loadUserByUsername('foobar');
$this->userProvider->loadUserByIdentifier('foobar');
}

public function testRefreshUserBy()
Expand Down
17 changes: 4 additions & 13 deletions tests/Security/UserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;

class UserProviderTest extends TestCase
Expand Down Expand Up @@ -45,23 +44,19 @@ public function testLoadUserByUsername()
->with('foobar')
->will($this->returnValue($user));

$this->assertSame($user, $this->userProvider->loadUserByUsername('foobar'));
$this->assertSame($user, $this->userProvider->loadUserByIdentifier('foobar'));
}

public function testLoadUserByInvalidUsername()
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}
$this->expectException(UserNotFoundException::class);

$this->userManager->expects($this->once())
->method('findUserByUsername')
->with('foobar')
->will($this->returnValue(null));

$this->userProvider->loadUserByUsername('foobar');
$this->userProvider->loadUserByIdentifier('foobar');
}

public function testRefreshUserBy()
Expand Down Expand Up @@ -89,11 +84,7 @@ public function testRefreshUserBy()

public function testRefreshDeleted()
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}
$this->expectException(UserNotFoundException::class);

$user = $this->getMockForAbstractClass('FOS\UserBundle\Model\User');
$this->userManager->expects($this->once())
Expand Down

0 comments on commit c079a95

Please sign in to comment.