|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kuleuven\AuthenticationBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Ldap\Ldap; |
|
6
|
|
|
|
|
7
|
|
|
class LdapService |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Ldap |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $ldap; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $rdn; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $password; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $base; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Ldap $ldap |
|
31
|
|
|
* @param string $rdn |
|
32
|
|
|
* @param string $password |
|
33
|
|
|
* @param string $base |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
Ldap $ldap, |
|
37
|
|
|
$rdn = '', |
|
38
|
|
|
$password = '', |
|
39
|
|
|
$base = 'ou=people,dc=kuleuven,dc=be' |
|
40
|
|
|
) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->ldap = $ldap; |
|
43
|
|
|
$this->rdn = $rdn; |
|
44
|
|
|
$this->password = $password; |
|
45
|
|
|
$this->base = $base; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function bind() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->ldap->bind($this->rdn, $this->password); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $data |
|
58
|
|
|
* @param string $type |
|
59
|
|
|
* @param string $format |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function createFilter(array $data = [], $type = '&', $format = '%s') |
|
63
|
|
|
{ |
|
64
|
|
|
$string = ''; |
|
65
|
|
|
foreach ($data as $key => $value) { |
|
66
|
|
|
if (is_array($value)) { |
|
67
|
|
|
if (0 === count(array_filter(array_keys($value), 'is_numeric'))) { |
|
68
|
|
|
$string .= $this->createFilter($value, $key, $format); |
|
69
|
|
|
} else { |
|
70
|
|
|
$string .= '(|(' . $key . '=' . implode(')(' . $key . '=', $value) . '))'; |
|
71
|
|
|
} |
|
72
|
|
|
} else { |
|
73
|
|
|
$value = sprintf($format, $value); |
|
74
|
|
|
$string .= "(${key}=${value})"; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
return "(${type}${string})"; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $filter |
|
82
|
|
|
* @param array $attributes |
|
83
|
|
|
* @param bool $attrsOnly |
|
84
|
|
|
* @param int $sizeLimit |
|
85
|
|
|
* @param int $timeLimit |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function search($filter, $attributes = [], $attrsOnly = false, $sizeLimit = 0, $timeLimit = 0) |
|
89
|
|
|
{ |
|
90
|
|
|
// Create filter |
|
91
|
|
|
if (is_array($filter)) { |
|
92
|
|
|
$filter = $this->createFilter($filter, '&', '%s'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Catch empty attributes |
|
96
|
|
|
if (empty($attributes)) { |
|
97
|
|
|
$attributes = []; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Bind |
|
101
|
|
|
$this->bind(); |
|
102
|
|
|
|
|
103
|
|
|
// Search - maxItems not added |
|
104
|
|
|
$results = $this->ldap->query($this->base, $filter, [ |
|
105
|
|
|
'attrsOnly' => $attrsOnly, |
|
106
|
|
|
'filter' => $attributes, |
|
107
|
|
|
'sizeLimit' => $sizeLimit, |
|
108
|
|
|
'timeout' => $timeLimit, |
|
109
|
|
|
])->execute(); |
|
110
|
|
|
|
|
111
|
|
|
// Return |
|
112
|
|
|
return $results; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param $filter |
|
117
|
|
|
* @param array $attributes |
|
118
|
|
|
* @param bool $attrsOnly |
|
119
|
|
|
* @param int $sizeLimit |
|
120
|
|
|
* @param int $timeLimit |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
public function fuzzy($filter, $attributes = [], $attrsOnly = false, $sizeLimit = 0, $timeLimit = 0) |
|
124
|
|
|
{ |
|
125
|
|
|
if (is_array($filter)) { |
|
126
|
|
|
$filter = $this->createFilter($filter, '&', '*%s*'); |
|
127
|
|
|
} |
|
128
|
|
|
return $this->search($filter, $attributes, $attrsOnly, $sizeLimit, $timeLimit); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|