-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathLdapUserProvider.php
More file actions
217 lines (196 loc) · 6.83 KB
/
LdapUserProvider.php
File metadata and controls
217 lines (196 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* This file is part of the LdapToolsBundle package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace LdapTools\Bundle\LdapToolsBundle\Security\User;
use LdapTools\BatchModify\BatchCollection;
use LdapTools\Bundle\LdapToolsBundle\Event\LoadUserEvent;
use LdapTools\Exception\EmptyResultException;
use LdapTools\Exception\MultiResultException;
use LdapTools\LdapManager;
use LdapTools\Object\LdapObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
/**
* Loads a user from LDAP.
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
class LdapUserProvider implements UserProviderInterface
{
/**
* @var LdapManager
*/
protected $ldap;
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var LdapRoleMapper
*/
protected $roleMapper;
/**
* @var array Default attributes selected for the Advanced User Interface.
*/
protected $defaultAttributes = [
'username',
'guid',
'accountExpirationDate',
'enabled',
'groups',
'locked',
'passwordMustChange',
];
/**
* @var array
*/
protected $options = [
'refresh_user_roles' => false,
'refresh_user_attributes' => false,
'search_base' => null,
'ldap_object_type' => 'user',
'user' => LdapUser::class,
'additional_attributes' => [],
];
/**
* @param LdapManager $ldap
* @param EventDispatcherInterface $dispatcher
* @param LdapRoleMapper $roleMapper
* @param array $options
*/
public function __construct(LdapManager $ldap, EventDispatcherInterface $dispatcher, LdapRoleMapper $roleMapper, array $options)
{
$this->ldap = $ldap;
$this->dispatcher = $dispatcher;
$this->roleMapper = $roleMapper;
$this->options = array_merge($this->options, $options);
}
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
{
$this->dispatcher->dispatch(new LoadUserEvent($username, $this->ldap->getDomainContext()), LoadUserEvent::BEFORE);
$ldapUser = $this->getLdapUser('username', $username);
$user = $this->constructUserClass($ldapUser);
$this->roleMapper->setRoles($user);
$this->dispatcher->dispatch(new LoadUserEvent($username, $this->ldap->getDomainContext(), $user, $ldapUser), LoadUserEvent::AFTER);
return $user;
}
/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof LdapUserInterface) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
$roles = $user->getRoles();
if ($this->options['refresh_user_attributes']) {
$user = $this->constructUserClass($this->getLdapUser('guid', $user->getLdapGuid()));
}
if ($this->options['refresh_user_roles']) {
$this->roleMapper->setRoles($user);
} else {
$user->setRoles($roles);
}
return $user;
}
/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
return is_subclass_of($class, LdapUserInterface::class);
}
/**
* Search for, and return, the LDAP user by a specific attribute.
*
* @param string $attribute
* @param string $value
* @return LdapObject
*/
public function getLdapUser($attribute, $value)
{
try {
$query = $this->ldap->buildLdapQuery()
->select($this->getAttributesToSelect())
->from($this->options['ldap_object_type'])
->where([$attribute => $value]);
if (!is_null($this->options['search_base'])) {
$query->setBaseDn($this->options['search_base']);
}
return $query->getLdapQuery()->getSingleResult();
} catch (EmptyResultException $e) {
throw new UsernameNotFoundException(sprintf('Username "%s" was not found.', $value));
} catch (MultiResultException $e) {
throw new UsernameNotFoundException(sprintf('Multiple results for "%s" were found.', $value));
}
}
/**
* Get all the attributes that should be selected for when querying LDAP.
*
* @return array
*/
protected function getAttributesToSelect()
{
return array_values(array_unique(array_filter(array_merge(
$this->defaultAttributes,
$this->options['additional_attributes']
))));
}
/**
* @param LdapObject $ldapObject
* @return LdapUserInterface
*/
protected function constructUserClass(LdapObject $ldapObject)
{
if (!$this->supportsClass($this->options['user'])) {
throw new UnsupportedUserException(sprintf(
'The LDAP user provider class "%s" must implement "%s".',
$this->options['user'],
LdapUserInterface::class
));
}
$errorMessage = 'Unable to instantiate user class "%s". Error was: %s';
try {
/** @var LdapUserInterface $user */
$user = new $this->options['user']();
$user->setUsername($ldapObject->get('username'));
$user->setLdapGuid($ldapObject->get('guid'));
} catch (\Throwable $e) {
throw new UnsupportedUserException(sprintf($errorMessage, $this->options['user'], $e->getMessage()));
// Unlikely to help much in PHP 5.6, but oh well...
} catch (\Exception $e) {
throw new UnsupportedUserException(sprintf($errorMessage, $this->options['user'], $e->getMessage()));
}
// If the class also happens to extend the LdapTools LdapObject class, then set the attributes and type...
if ($user instanceof LdapObject) {
$this->hydrateLdapObjectUser($ldapObject, $user);
}
return $user;
}
/**
* @param LdapObject $ldapObject
* @param $user
*/
protected function hydrateLdapObjectUser(LdapObject $ldapObject, LdapObject $user)
{
$user->setBatchCollection(new BatchCollection($ldapObject->get('dn')));
$user->refresh($ldapObject->toArray());
// This is to avoid the constructor
$refObject = new \ReflectionObject($user);
$refProperty = $refObject->getProperty('type');
$refProperty->setAccessible(true);
$refProperty->setValue($user, $this->options['ldap_object_type']);
}
}