Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract provider to load users from the database
- Loading branch information
1 parent
247c01a
commit b42d97d
Showing
6 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
//---------------------------------------------------------------------- | ||
// | ||
// Copyright (C) 2017 Artem Rodygin | ||
// | ||
// You should have received a copy of the MIT License along with | ||
// this file. If not, see <http://opensource.org/licenses/MIT>. | ||
// | ||
//---------------------------------------------------------------------- | ||
|
||
namespace Pignus\Model; | ||
|
||
use Doctrine\Common\Persistence\ObjectRepository; | ||
|
||
/** | ||
* User repository interface. | ||
*/ | ||
interface UserRepositoryInterface extends ObjectRepository | ||
{ | ||
/** | ||
* Finds user by username. | ||
* | ||
* @param string $username | ||
* | ||
* @return \Symfony\Component\Security\Core\User\UserInterface|null | ||
*/ | ||
public function findOneByUsername(string $username); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
//---------------------------------------------------------------------- | ||
// | ||
// Copyright (C) 2017 Artem Rodygin | ||
// | ||
// You should have received a copy of the MIT License along with | ||
// this file. If not, see <http://opensource.org/licenses/MIT>. | ||
// | ||
//---------------------------------------------------------------------- | ||
|
||
namespace Pignus\Provider; | ||
|
||
use Pignus\Model\UserRepositoryInterface; | ||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | ||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
|
||
/** | ||
* Abstract provider to load users from the database. | ||
*/ | ||
abstract class AbstractUserProvider implements UserProviderInterface | ||
{ | ||
protected $repository; | ||
|
||
/** | ||
* Dependency Injection constructor. | ||
* | ||
* @param UserRepositoryInterface $repository | ||
*/ | ||
public function __construct(UserRepositoryInterface $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadUserByUsername($username) | ||
{ | ||
$user = $this->repository->findOneByUsername($username); | ||
|
||
if ($user === null) { | ||
throw new UsernameNotFoundException(); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function refreshUser(UserInterface $user) | ||
{ | ||
if (!$this->supportsClass(get_class($user))) { | ||
throw new UnsupportedUserException(); | ||
} | ||
|
||
$user = $this->repository->findOneByUsername($user->getUsername()); | ||
|
||
if ($user === null) { | ||
throw new UsernameNotFoundException(); | ||
} | ||
|
||
return $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
//---------------------------------------------------------------------- | ||
// | ||
// Copyright (C) 2017 Artem Rodygin | ||
// | ||
// You should have received a copy of the MIT License along with | ||
// this file. If not, see <http://opensource.org/licenses/MIT>. | ||
// | ||
//---------------------------------------------------------------------- | ||
|
||
namespace Pignus\Tests\Provider; | ||
|
||
use Pignus\Model\UserRepositoryInterface; | ||
use Pignus\Tests\Model\DummyUser; | ||
use Symfony\Component\Security\Core\User\User; | ||
|
||
class AbstractUserProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var DummyUser */ | ||
protected $user; | ||
|
||
/** @var DummyUserProvider */ | ||
protected $provider; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = new DummyUser(); | ||
|
||
$reflection = new \ReflectionProperty(DummyUser::class, 'username'); | ||
$reflection->setAccessible(true); | ||
$reflection->setValue($this->user, 'admin'); | ||
|
||
$repository = $this->createMock(UserRepositoryInterface::class); | ||
$repository | ||
->method('findOneByUsername') | ||
->willReturnMap([ | ||
['admin', $this->user], | ||
]); | ||
|
||
/** @var UserRepositoryInterface $repository */ | ||
$this->provider = new DummyUserProvider($repository); | ||
} | ||
|
||
public function testLoadUserByUsername() | ||
{ | ||
$result = $this->provider->loadUserByUsername('admin'); | ||
|
||
self::assertInstanceOf(DummyUser::class, $result); | ||
self::assertEquals('admin', $result->getUsername()); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException | ||
*/ | ||
public function testLoadUnknownUserByUsername() | ||
{ | ||
$this->provider->loadUserByUsername('unknown'); | ||
} | ||
|
||
public function testRefreshUser() | ||
{ | ||
$result = $this->provider->refreshUser($this->user); | ||
|
||
self::assertInstanceOf(DummyUser::class, $result); | ||
self::assertEquals('admin', $result->getUsername()); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException | ||
*/ | ||
public function testRefreshUnknownUser() | ||
{ | ||
$reflection = new \ReflectionProperty(DummyUser::class, 'username'); | ||
$reflection->setAccessible(true); | ||
$reflection->setValue($this->user, 'unknown'); | ||
|
||
$this->provider->refreshUser($this->user); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException | ||
*/ | ||
public function testUnsupportedUserException() | ||
{ | ||
$user = new User('admin', 'secret'); | ||
|
||
$this->provider->refreshUser($user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
//---------------------------------------------------------------------- | ||
// | ||
// Copyright (C) 2017 Artem Rodygin | ||
// | ||
// You should have received a copy of the MIT License along with | ||
// this file. If not, see <http://opensource.org/licenses/MIT>. | ||
// | ||
//---------------------------------------------------------------------- | ||
|
||
namespace Pignus\Tests\Provider; | ||
|
||
use Pignus\Provider\AbstractUserProvider; | ||
use Pignus\Tests\Model\DummyUser; | ||
|
||
class DummyUserProvider extends AbstractUserProvider | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsClass($class) | ||
{ | ||
return $class === DummyUser::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters