/
LdapUserProvider.php
95 lines (81 loc) · 2.43 KB
/
LdapUserProvider.php
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Ldap\Exception\ConnectionException;
use Symfony\Component\Security\Ldap\LdapInterface;
/**
* LdapUserProvider is a simple user provider on top of ldap.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class LdapUserProvider implements LdapUserProviderInterface
{
private $ldap;
private $defaultRoles;
/**
* @param LdapInterface $ldap
* @param array $defaultRoles
*/
public function __construct(LdapInterface $ldap, array $defaultRoles = array())
{
$this->ldap = $ldap;
$this->defaultRoles = $defaultRoles;
}
/**
* {@inheritDoc}
*
* Due to Ldap limitation, this method should never be called.
* Call loadUserByUsernameAndPassword instead.
*/
public function loadUserByUsername($username)
{
throw new \BadMethodCallException(sprintf('You should not call the method "%s"', __METHOD__));
}
/**
* {@inheritDoc}
*/
public function loadUserByUsernameAndPassword($username, $password)
{
$this->ldap->setUsername($username);
$this->ldap->setPassword($password);
try {
$this->ldap->bind();
} catch (ConnectionException $e) {
throw new UsernameNotFoundException(sprintf('The presented password is invalid. "%s"', $e->getMessage()));
}
return new User($username, null, $this->loadRoles());
}
/**
* {@inheritDoc}
*/
public function loadRoles()
{
return $this->defaultRoles;
}
/**
* {@inheritDoc}
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return new User($user->getUsername(), null, $user->getRoles());
}
/**
* {@inheritDoc}
*/
public function supportsClass($class)
{
return $class === 'Symfony\Component\Security\Core\User\User';
}
}