8000 [Security] Allow in memory user to have extra fields by noniagriconomie · Pull Request #40097 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Allow in memory user to have extra fields #40097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
use `security.password_hasher_factory` and `Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface` instead
* Deprecate the `security.user_password_encoder.generic` service, the `security.password_encoder` and the `Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface` aliases,
use `security.user_password_hasher`, `security.password_hasher` and `Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface` instead
* Add `extra_fields` for `memory` provider to allow configuring extra fields for users

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private function addProvidersSection(ArrayNodeDefinition $rootNode)
'memory' => [
'users' => [
'foo' => ['password' => 'foo', 'roles' => 'ROLE_USER'],
'bar' => ['password' => 'bar', 'roles' => '[ROLE_USER, ROLE_ADMIN]'],
'bar' => ['password' => 'bar', 'roles' => ['ROLE_USER', 'ROLE_ADMIN'], 'extra_fields' => ['name' => 'John', 'age' => 77]],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function create(ContainerBuilder $container, string $id, array $config)
$users = [];

foreach ($config['users'] as $username => $user) {
$users[$username] = ['password' => null !== $user['password'] ? (string) $user['password'] : $defaultPassword, 'roles' => $user['roles']];
$users[$username] = ['password' => null !== $user['password'] ? (string) $user['password'] : $defaultPassword, 'roles' => $user['roles'], 'extra_fields' => $user['extra_fields']];
}

$definition->addArgument($users);
Expand All @@ -57,6 +57,9 @@ public function addConfiguration(NodeDefinition $node)
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
->prototype('scalar')->end()
->end()
->arrayNode('extra_fields')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserInterface;

class MemoryUserExtraFieldsTest extends AbstractWebTestCase
{
public function testMemoryUserHasExtraFields()
{
$client = $this->createClient(['test_case' => 'MemoryUserExtraFields', 'root_config' => 'config.yml']);

$client->request('POST', '/login', [
'_username' => 'foo',
'_password' => 'bar',
]);
$client->request('GET', '/memory-user-extra-fields');

$response = $client->getResponse();

$this->assertSame(200, $response->getStatusCode());
$this->assertStringContainsString('username:foo, age:77', $response->getContent());
}
}

class MemoryUserExtraFieldsController
{
public function __invoke(UserInterface $user): Response
{
$username = $user->getUsername();

/** @var User $user */
$age = $user->getExtraFields()['age'] ?? '';

return new Response(
sprintf('username:%s, age:%s', $username, $age)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;

return [
new FrameworkBundle(),
new SecurityBundle(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
imports:
- { resource: ./../config/framework.yml }

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext

providers:
in_memory:
memory:
users:
foo: { password: bar, roles: [ROLE_USER], extra_fields: {'age': 77} }

firewalls:
default:
form_login:
check_path: login

access_control:
- { path: ^/memory-user-extra-fields, roles: ROLE_USER }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
login:
path: /login

memory-user-extra-fields:
path: /memory-user-extra-fields
defaults:
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\MemoryUserExtraFieldsController
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testConstructor()
$user = $provider->loadUserByUsername('fabien');
$this->assertEquals('foo', $user->getPassword());
$this->assertEquals(['ROLE_USER'], $user->getRoles());
$this->assertEquals(['name' => 'John', 'age' => 77, 'salary' => 1234.56], $user->getExtraFields());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertSame ?

Same for the others in this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if it is related to this PR to change the method name

and for this new added line I just duplicate the above line to keep things similar

if asked/decided I can handle it inside this PR of course :)

$this->assertFalse($user->isEnabled());
}

Expand All @@ -37,6 +38,7 @@ public function testRefresh()
$refreshedUser = $provider->refreshUser($user);
$this->assertEquals('foo', $refreshedUser->getPassword());
$this->assertEquals(['ROLE_USER'], $refreshedUser->getRoles());
$this->assertEquals(['name' => 'John', 'age' => 77, 'salary' => 1234.56], $refreshedUser->getExtraFields());
$this->assertFalse($refreshedUser->isEnabled());
$this->assertFalse($refreshedUser->isCredentialsNonExpired());
}
Expand All @@ -48,6 +50,7 @@ protected function createProvider(): InMemoryUserProvider
'password' => 'foo',
'enabled' => false,
'roles' => ['ROLE_USER'],
'extra_fields' => ['name' => 'John', 'age' => 77, 'salary' => 1234.56],
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class InMemoryUserProvider implements UserProviderInterface

/**
* The user array is a hash where the keys are usernames and the values are
* an array of attributes: 'password', 'enabled', and 'roles'.
* an array of attributes: 'password', 'enabled', 'roles' and 'extra_fields'.
*
* @param array $users An array of users
*/
Expand All @@ -38,7 +38,8 @@ public function __construct(array $users = [])
$password = $attributes['password'] ?? null;
$enabled = $attributes['enabled'] ?? true;
$roles = $attributes['roles'] ?? [];
$user = new User($username, $password, $roles, $enabled, true, true, true);
$extraFields = $attributes['extra_fields'] ?? [];
$user = new User($username, $password, $roles, $enabled, true, true, true, $extraFields);

$this->createUser($user);
}
Expand All @@ -65,7 +66,7 @@ public function loadUserByUsername(string $username)
{
$user = $this->getUser($username);

return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(), $user->isCredentialsNonExpired(), $user->isAccountNonLocked());
return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(), $user->isCredentialsNonExpired(), $user->isAccountNonLocked(), $user->getExtraFields());
}

/**
Expand All @@ -79,7 +80,7 @@ public function refreshUser(UserInterface $user)

$storedUser = $this->getUser($user->getUsername());

return new User($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked());
return new User($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked(), $storedUser->getExtraFields());
}

/**
Expand Down
0