|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kuleuven\AuthenticationBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
6
|
|
|
use Symfony\Component\Ldap\Entry; |
|
7
|
|
|
|
|
8
|
|
|
class LdapAttributesProvider implements AttributesByUsernameProviderInterface, AttributesInjectionProviderInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var LdapService |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $ldapService; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $filter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $enabled; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var SessionInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $session; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param LdapService $ldapService |
|
32
|
|
|
* @param array $filter |
|
33
|
|
|
* @param bool $enabled |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(LdapService $ldapService, $filter = [], $enabled = false) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->ldapService = $ldapService; |
|
38
|
|
|
$this->filter = (!empty($filter) ? $filter : []); |
|
39
|
|
|
$this->enabled = $enabled; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param SessionInterface $session |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setSession(SessionInterface $session) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->session = $session; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function isEnabled() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->enabled; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param array $filter |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getAttributesByFilter(array $filter) |
|
63
|
|
|
{ |
|
64
|
|
|
// Return early if filter is empty |
|
65
|
|
|
if (empty($filter)) { |
|
66
|
|
|
return []; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Get attributes |
|
70
|
|
|
$hash = 'kuleuven-authentication-ldap-results-' . md5(serialize($filter)); |
|
71
|
|
|
if (!empty($this->session) && $this->session->has($hash)) { |
|
72
|
|
|
// Retrieve from the session (cache) |
|
73
|
|
|
$attributes = $this->session->get($hash); |
|
74
|
|
|
} else { |
|
75
|
|
|
// Search LDAP |
|
76
|
|
|
$ldapResults = $this->ldapService->search($filter); |
|
77
|
|
|
// Return empty array if filter returns more than one user |
|
78
|
|
|
if (1 !== $ldapResults->count()) { |
|
79
|
|
|
return []; |
|
80
|
|
|
} |
|
81
|
|
|
// Get the first result |
|
82
|
|
|
/** @var Entry $ldapSingleResult */ |
|
83
|
|
|
$ldapSingleResult = $ldapResults->toArray()['0']; |
|
84
|
|
|
// Extract attributes as strings |
|
85
|
|
|
$attributes = $ldapSingleResult->getAttributes(); |
|
86
|
|
|
array_walk($attributes, function (&$value) { |
|
87
|
|
|
$value = (is_array($value) ? implode(';', $value) : $value); |
|
88
|
|
|
}); |
|
89
|
|
|
// Save to the session (cache) |
|
90
|
|
|
if (!empty($this->session)) { |
|
91
|
|
|
$this->session->set($hash, $attributes); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Return attributes |
|
96
|
|
|
return $attributes; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getAttributesByUsername($username) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getAttributesByFilter(['uid' => $username]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getAttributes() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->getAttributesByFilter($this->filter); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|