Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 353adf8

Browse files
committed
[Ldap] Add users metadata in ldap component
1 parent 519ba3c commit 353adf8

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/LdapFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function create(ContainerBuilder $container, $id, $config)
3636
->replaceArgument(5, $config['uid_key'])
3737
->replaceArgument(6, $config['filter'])
3838
->replaceArgument(7, $config['password_attribute'])
39+
->replaceArgument(8, $config['user_metadata'])
3940
;
4041
}
4142

@@ -52,6 +53,9 @@ public function addConfiguration(NodeDefinition $node)
5253
->scalarNode('base_dn')->isRequired()->cannotBeEmpty()->end()
5354
->scalarNode('search_dn')->end()
5455
->scalarNode('search_password')->end()
56+
->arrayNode('user_metadata')
57+
->prototype('scalar')->end()
58+
->end()
5559
->arrayNode('default_roles')
5660
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
5761
->requiresAtLeastOneElement()

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<argument /> <!-- uid key -->
185185
<argument /> <!-- filter -->
186186
<argument /> <!-- password_attribute -->
187+
<argument /> <!-- users metadata (mail etc) -->
187188
</service>
188189

189190
<service id="security.user.provider.chain" class="Symfony\Component\Security\Core\User\ChainUserProvider" abstract="true" />

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/JsonLoginLdap/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ security:
2121
search_password: ''
2222
default_roles: ROLE_USER
2323
uid_key: uid
24+
user_metadata: ['mail']
2425

2526
firewalls:
2627
main:

src/Symfony/Component/Ldap/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* Added a new option called `user_metadata` as an array it will allow you to ask for more information that will be saved in the metadata field of the user, so you can get field value as an array and anything that is in the ldap.
8+
49
4.3.0
510
-----
611

src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
334334
->will($this->returnValue(new Entry('foo', [
335335
'sAMAccountName' => ['foo'],
336336
'userpassword' => ['bar'],
337+
'mail' => ['[email protected]'],
337338
]
338339
)))
339340
;
@@ -353,7 +354,7 @@ public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute()
353354
->will($this->returnValue($query))
354355
;
355356

356-
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword');
357+
$provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={username})', 'userpassword', ['mail']);
357358
$this->assertInstanceOf(
358359
'Symfony\Component\Security\Core\User\User',
359360
$provider->loadUserByUsername('foo')

src/Symfony/Component/Security/Core/User/LdapUserProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class LdapUserProvider implements UserProviderInterface
3434
private $uidKey;
3535
private $defaultSearch;
3636
private $passwordAttribute;
37+
private $userMetadata;
3738

38-
public function __construct(LdapInterface $ldap, string $baseDn, string $searchDn = null, string $searchPassword = null, array $defaultRoles = [], string $uidKey = null, string $filter = null, string $passwordAttribute = null)
39+
public function __construct(LdapInterface $ldap, string $baseDn, string $searchDn = null, string $searchPassword = null, array $defaultRoles = [], string $uidKey = null, string $filter = null, string $passwordAttribute = null, array $userMetadata = [])
3940
{
4041
if (null === $uidKey) {
4142
$uidKey = 'sAMAccountName';
@@ -53,6 +54,7 @@ public function __construct(LdapInterface $ldap, string $baseDn, string $searchD
5354
$this->uidKey = $uidKey;
5455
$this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter);
5556
$this->passwordAttribute = $passwordAttribute;
57+
$this->userMetadata = $userMetadata;
5658
}
5759

5860
/**
@@ -123,12 +125,17 @@ public function supportsClass($class)
123125
protected function loadUser($username, Entry $entry)
124126
{
125127
$password = null;
128+
$metadata = [];
126129

127130
if (null !== $this->passwordAttribute) {
128131
$password = $this->getAttributeValue($entry, $this->passwordAttribute);
129132
}
130133

131-
return new User($username, $password, $this->defaultRoles);
134+
foreach ($this->userMetadata as $field) {
135+
$metadata[$field] = $this->getAttributeValue($entry, $field);
136+
}
137+
138+
return new User($username, $password, $this->defaultRoles, true, true, true, true, $metadata);
132139
}
133140

134141
/**

src/Symfony/Component/Security/Core/User/User.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ final class User implements UserInterface, EquatableInterface, AdvancedUserInter
2727
private $credentialsNonExpired;
2828
private $accountNonLocked;
2929
private $roles;
30+
private $metadata;
3031

31-
public function __construct(?string $username, ?string $password, array $roles = [], bool $enabled = true, bool $userNonExpired = true, bool $credentialsNonExpired = true, bool $userNonLocked = true)
32+
public function __construct(?string $username, ?string $password, array $roles = [], bool $enabled = true, bool $userNonExpired = true, bool $credentialsNonExpired = true, bool $userNonLocked = true, array $metadata = [])
3233
{
3334
if ('' === $username || null === $username) {
3435
throw new \InvalidArgumentException('The username cannot be empty.');
@@ -41,6 +42,7 @@ public function __construct(?string $username, ?string $password, array $roles =
4142
$this->credentialsNonExpired = $credentialsNonExpired;
4243
$this->accountNonLocked = $userNonLocked;
4344
$this->roles = $roles;
45+
$this->metadata = $metadata;
4446
}
4547

4648
public function __toString()
@@ -118,6 +120,11 @@ public function eraseCredentials()
118120
{
119121
}
120122

123+
public function getMetadata()
124+
{
125+
return $this->metadata;
126+
}
127+
121128
/**
122129
* {@inheritdoc}
123130
*/

0 commit comments

Comments
 (0)