Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from DerManoMann/host_list
Browse files Browse the repository at this point in the history
Fixes #8:  Allow to congfigure a list of hosts
  • Loading branch information
DerManoMann committed Feb 5, 2016
2 parents 62c94d3 + 7fc20af commit 6d3df6c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
14 changes: 14 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ Alternatively, you can download the [`ldap-auth-service-provider.zip`][1] file a
The Ldap related code depends on [`zend-ldap`](https://github.com/zendframework/zend-ldap), so all configuration options are just passed through.
For more details check the [`zend-ldap docs`](http://framework.zend.com/manual/current/en/index.html#zend-ldap).

In addition the provider allows to configure a list of hosts to try. If none in the list can't be connected, the regularly configured host is used as
last resort.

Example:
````
ldap:
ldap:
hosts:
- ldap1
- ldap2
host: localhost
````
In this case the code will try to connect in the order: ldap1, ldap2, localhost.


### Custom user class
The LdapUserProvider class allows to configure a custom User class to be used.
Expand Down
36 changes: 34 additions & 2 deletions src/LdapAuthenticationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,40 @@ public function register(Container $app)
// the actual Ldap resource
if (!isset($app['security.ldap.'.$serviceName.'.ldap'])) {
$app['security.ldap.'.$serviceName.'.ldap'] = function () use ($app, $serviceName) {
// we need just the ldap options here
return new Ldap($app['security.ldap.config']($serviceName)['ldap']);
// ldap options
$options = $app['security.ldap.config']($serviceName)['ldap'];

// check for host list
if (array_key_exists('hosts', $options) && is_array($options['hosts'])) {
// keep local
$hosts = $options['hosts'];

// remove from options...
unset($options['hosts']);

foreach ($hosts as $host) {
try {
// do not override default host
$ldap = new Ldap(array_merge($options, ['host' => $host]));

// force connect...
$ldap->getResource();

return $ldap;
} catch (LdapException $le) {
if ($app->offsetExists('logger')) {
$app['logger']->warning(sprintf('LDAP: Failed connecting to host: %s', $host));
}
}
}
}

if ($app->offsetExists('logger')) {
$app['logger']->info(sprintf('LDAP: Using default host: %s', $options['host']));
}

// just pass through all options using configured (single) host
return new Ldap($options);
};
}

Expand Down
36 changes: 34 additions & 2 deletions src/Silex1LdapAuthenticationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,40 @@ public function register(Application $app)
// the actual Ldap resource
if (!isset($app['security.ldap.'.$serviceName.'.ldap'])) {
$app['security.ldap.'.$serviceName.'.ldap'] = function () use ($app, $serviceName) {
// we need just the ldap options here
return new Ldap($app['security.ldap.config']($serviceName)['ldap']);
// ldap options
$options = $app['security.ldap.config']($serviceName)['ldap'];

// check for host list
if (array_key_exists('hosts', $options) && is_array($options['hosts'])) {
// keep local
$hosts = $options['hosts'];

// remove from options...
unset($options['hosts']);

foreach ($hosts as $host) {
try {
// do not override default host
$ldap = new Ldap(array_merge($options, ['host' => $host]));

// force connect...
$ldap->getResource();

return $ldap;
} catch (LdapException $le) {
if ($app->offsetExists('logger')) {
$app['logger']->warning(sprintf('LDAP: Failed connecting to host: %s', $host));
}
}
}
}

if ($app->offsetExists('logger')) {
$app['logger']->info(sprintf('LDAP: Using default host: %s', $options['host']));
}

// just pass through all options using configured (single) host
return new Ldap($options);
};
}

Expand Down

0 comments on commit 6d3df6c

Please sign in to comment.