Skip to content
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

Searched username transformation #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ private function addUserNode()
->scalarNode('filter')->end()
->scalarNode('name_attribute')->defaultValue('uid')->end()
->variableNode('attributes')->defaultValue(array())->end()
->scalarNode('name_regex_pattern')->end()
->scalarNode('name_regex_replacement')->end()
->end()
;

Expand Down
10 changes: 8 additions & 2 deletions Manager/LdapManagerUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,19 @@ private function addLdapUser()
? $this->params['user']['filter']
: '';

$username = $this->username;

if(isset($this->params['user']['name_regex_pattern']) && isset($this->params['user']['name_regex_replacement'])){
$username = preg_replace($this->params['user']['name_regex_pattern'], $this->params['user']['name_regex_replacement'], $username);
}

$entries = $this->ldapConnection
->search(array(
'base_dn' => $this->params['user']['base_dn'],
'filter' => sprintf('(&%s(%s=%s))',
$filter,
$this->params['user']['name_attribute'],
$this->ldapConnection->escape($this->username)
$this->ldapConnection->escape($username)
)
));

Expand All @@ -144,7 +150,7 @@ private function addLdapRoles()
if (null === $this->ldapUser) {
throw new \RuntimeException('Cannot assign LDAP roles before authenticating user against LDAP');
}

$this->ldapUser['roles'] = array();

if (!isset($this->params['role'])) {
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ security:
providers:
ldap:
id: imag_ldap.security.user.provider

encoders:
IMAG\LdapBundle\User\LdapUser: plaintext

Expand All @@ -92,6 +92,8 @@ imag_ldap:
user:
base_dn: ou=people,dc=host,dc=foo
# filter: (&(foo=bar)(ObjectClass=Person)) #Optional
# name_regex_pattern: '#^[^\\]*\\(.*)$#' #Optional
# name_regex_replacement: '$1' #Optional
name_attribute: uid
role:
base_dn: ou=group, dc=host, dc=foo
Expand Down Expand Up @@ -156,7 +158,7 @@ security:
providers:
multiples:
chain:
providers: [ldap, db]
providers: [ldap, db]
ldap:
id: imag_ldap.security.user.provider
db:
Expand All @@ -169,7 +171,7 @@ security:
``` yml
# app/config/security.yml

providers: [db, ldap]
providers: [db, ldap]
```

### Subscribe to PRE_BIND event
Expand Down
4 changes: 2 additions & 2 deletions User/LdapUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class LdapUser implements LdapUserInterface
{
protected $username,
$email,
$roles,
$roles = array(),
$dn,
$attributes
$attributes = array()
;

public function getRoles()
Expand Down