-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from thewilkybarkid/user-provider
Add simple user provider
- Loading branch information
Showing
6 changed files
with
226 additions
and
0 deletions.
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
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,50 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Security\User; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class OAuthUser implements UserInterface | ||
{ | ||
private $username; | ||
private $roles; | ||
|
||
public function __construct($username, array $roles) | ||
{ | ||
$this->username = $username; | ||
$this->roles = $roles; | ||
} | ||
|
||
public function getRoles() | ||
{ | ||
return $this->roles; | ||
} | ||
|
||
public function getPassword() | ||
{ | ||
return ''; | ||
} | ||
|
||
public function getSalt() | ||
{ | ||
return null; | ||
} | ||
|
||
public function getUsername() | ||
{ | ||
return $this->username; | ||
} | ||
|
||
public function eraseCredentials() | ||
{ | ||
// Do nothing. | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Security\User; | ||
|
||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
|
||
class OAuthUserProvider implements UserProviderInterface | ||
{ | ||
private $roles; | ||
|
||
public function __construct(array $roles = ['ROLE_USER', 'ROLE_OAUTH_USER']) | ||
{ | ||
$this->roles = $roles; | ||
} | ||
|
||
public function loadUserByUsername($username) | ||
{ | ||
return new OAuthUser($username, $this->roles); | ||
} | ||
|
||
public function refreshUser(UserInterface $user) | ||
{ | ||
if (!$user instanceof OAuthUser) { | ||
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); | ||
} | ||
|
||
return $this->loadUserByUsername($user->getUsername()); | ||
} | ||
|
||
public function supportsClass($class) | ||
{ | ||
return OAuthUser::class === $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Security\User; | ||
|
||
use KnpU\OAuth2ClientBundle\Security\User\OAuthUser; | ||
use KnpU\OAuth2ClientBundle\Security\User\OAuthUserProvider; | ||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | ||
use Symfony\Component\Security\Core\User\User; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class OAuthUserProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testLoadUserByUsername() | ||
{ | ||
$userProvider = new OAuthUserProvider(['role 1', 'role 2']); | ||
|
||
$expected = new OAuthUser('username', ['role 1', 'role 2']); | ||
|
||
$this->assertEquals($expected, $userProvider->loadUserByUsername('username')); | ||
} | ||
|
||
public function testRefreshUser() | ||
{ | ||
$userProvider = new OAuthUserProvider(['role 1', 'role 2']); | ||
|
||
$user = new OAuthUser('username', ['role 3']); | ||
$expected = new OAuthUser('username', ['role 1', 'role 2']); | ||
|
||
$this->assertEquals($expected, $userProvider->refreshUser($user)); | ||
} | ||
|
||
public function testRefreshOtherUser() | ||
{ | ||
$userProvider = new OAuthUserProvider(); | ||
|
||
$this->setExpectedException(UnsupportedUserException::class); | ||
|
||
$userProvider->refreshUser($this->getMock(UserInterface::class)); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsClassProvider | ||
*/ | ||
public function testSupportsClass($class, $supports) | ||
{ | ||
$userProvider = new OAuthUserProvider(); | ||
|
||
$this->assertSame($supports, $userProvider->supportsClass($class)); | ||
} | ||
|
||
public function supportsClassProvider() | ||
{ | ||
yield 'OAuthUser' => [OAuthUser::class, true]; | ||
yield 'other user' => [User::class, false]; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Security\User; | ||
|
||
use KnpU\OAuth2ClientBundle\Security\User\OAuthUser; | ||
|
||
class OAuthUserTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testRoles() | ||
{ | ||
$user = new OAuthUser('username', ['role 1', 'role 2']); | ||
|
||
$this->assertSame(['role 1', 'role 2'], $user->getRoles()); | ||
} | ||
|
||
public function testPassword() | ||
{ | ||
$user = new OAuthUser('username', ['role 1', 'role 2']); | ||
|
||
$this->assertSame('', $user->getPassword()); | ||
} | ||
|
||
public function testSalt() | ||
{ | ||
$user = new OAuthUser('username', ['role 1', 'role 2']); | ||
|
||
$this->assertNull($user->getSalt()); | ||
} | ||
|
||
public function testUsername() | ||
{ | ||
$user = new OAuthUser('username', ['role 1', 'role 2']); | ||
|
||
$this->assertSame('username', $user->getUsername()); | ||
} | ||
} |