Commit 04566d37 authored by Fabien Potencier's avatar Fabien Potencier

feature #1611 Allow defining users provider as a service ID (henrikbjorn, hkdobrev)

This PR was merged into the 2.3.x-dev branch.

Discussion
----------

Allow defining users provider as a service ID

Replaces #1217.
Closes #1216.

Commits
-------

81961b82 Fix typo in security docs
4c0cd2ba Update password in tests
cb44f3f0 Update code style - use short array syntax
bac2c455 add test and documentation for using a service id string instead of a closure
dc7572ef allow using a service name for the users config in firewall
parents f51da0dd 81961b82
......@@ -500,8 +500,8 @@ Defining a custom User Provider
Using an array of users is simple and useful when securing an admin section of
a personal website, but you can override this default mechanism with you own.
The ``users`` setting can be defined as a service that returns an instance of
`UserProviderInterface
The ``users`` setting can be defined as a service or a service id that returns
an instance of `UserProviderInterface
<http://api.symfony.com/master/Symfony/Component/Security/Core/User/UserProviderInterface.html>`_::
'users' => function () use ($app) {
......
......@@ -245,6 +245,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
$protected = false === $security ? false : count($firewall);
$listeners = ['security.channel_listener'];
if (is_string($users)) {
$users = $app->raw($users);
}
if ($protected) {
if (!isset($app['security.context_listener.'.$context])) {
if (!isset($app['security.user_provider.'.$name])) {
......
......@@ -278,6 +278,35 @@ class SecurityServiceProviderTest extends WebTestCase
$this->assertEquals('fabien', $app['user']->getUsername());
}
public function testUserAsServiceString()
{
$users = [
'fabien' => ['ROLE_ADMIN', '$2y$15$lzUNsTegNXvZW3qtfucV0erYBcEqWVeyOmjolB7R1uodsAVJ95vvu'],
];
$app = new Application();
$app->register(new SecurityServiceProvider(), [
'security.firewalls' => [
'default' => [
'http' => true,
'users' => 'my_user_provider',
],
],
]);
$app['my_user_provider'] = $app['security.user_provider.inmemory._proto']($users);
$app->get('/', function () { return 'foo'; });
$request = Request::create('/');
$app->handle($request);
$this->assertNull($app['user']);
$request->headers->set('PHP_AUTH_USER', 'fabien');
$request->headers->set('PHP_AUTH_PW', 'foo');
$app->handle($request);
$this->assertInstanceOf('Symfony\Component\Security\Core\User\UserInterface', $app['user']);
$this->assertEquals('fabien', $app['user']->getUsername());
}
public function testUserWithNoToken()
{
$app = new Application();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment