1
|
|
|
<?php |
2
|
|
|
namespace SOG\Dashboard\Authentication; |
3
|
|
|
|
4
|
|
|
use Silex\Application; |
5
|
|
|
use Silex\ServiceProviderInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class sets up the required hooks for providing LDAP-based authentication. |
9
|
|
|
* The implementation follows https://github.com/DerManoMann/ldap-auth-service-provider - thank you! |
10
|
|
|
* |
11
|
|
|
* Class LdapAuthenticationServiceProvider |
12
|
|
|
* @package SOG\Dashboard\Authentication |
13
|
|
|
*/ |
14
|
|
|
class LdapAuthenticationServiceProvider implements ServiceProviderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string The entry point for the authentication, this can be e.g. `form` or `http`, we're interested in `form` |
18
|
|
|
*/ |
19
|
|
|
private $entry_point = 'form'; |
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* {@inheritdoc}
|
23
|
|
|
*/ |
24
|
|
|
public function register(Application $app) |
25
|
|
|
{ |
26
|
|
|
// let's first setup the user provider, we'll hand over the LDAP instance |
27
|
|
|
if (isset($app['security.ldap.user_provider']) === false) { |
28
|
|
|
$app['security.ldap.user_provider'] = $app->protect(function () use ($app) { |
29
|
|
|
return new LdapUserProvider($app['ldap']); |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$entry_point = $this->entry_point; |
34
|
|
|
// now setup the authentication listener |
35
|
|
|
$app['security.authentication_listener.factory.ldap'] = $app->protect(function ($name, $options = []) use ($app, $entry_point) { |
36
|
|
|
|
37
|
|
|
if ($entry_point && isset($app['security.entry_point.' . $name . '.' . $entry_point]) === false) { |
38
|
|
|
$app['security.entry_point.' . $name . '.' . $entry_point] = $app['security.entry_point.' . $entry_point . '._proto']($name, $options); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// the authentication provider handles the actual authentication and is called automatically |
42
|
|
|
$app['security.authentication_provider.' . $name . '.ldap'] = function () use ($app, $name) { |
43
|
|
|
return new LdapAuthenticationProvider( |
44
|
|
|
$app['ldap'], |
45
|
|
|
$app['security.user_provider.' . $name] |
46
|
|
|
); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
if ($entry_point) { |
50
|
|
|
$app['security.authentication_listener.' . $name . '.ldap'] = $app['security.authentication_listener.' . $entry_point . '._proto']($name, $options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return array( |
54
|
|
|
'security.authentication_provider.' . $name . '.ldap', |
55
|
|
|
'security.authentication_listener.' . $name . '.ldap', |
56
|
|
|
$entry_point ? 'security.entry_point.' . $name . '.' . $entry_point : null, |
57
|
|
|
'pre_auth', |
58
|
|
|
); |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* {@inheritdoc}
|
64
|
|
|
*/ |
65
|
|
|
public function boot(Application $app) |
66
|
|
|
{ |
67
|
|
|
// nothing to do here. |
68
|
|
|
} |
69
|
|
|
} |