1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Ldap; |
13
|
|
|
|
14
|
|
|
use Jitamin\Group\LdapGroupProvider; |
15
|
|
|
use LogicException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* LDAP Group Finder. |
19
|
|
|
*/ |
20
|
|
|
class Group |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Query. |
24
|
|
|
* |
25
|
|
|
* @var Query |
26
|
|
|
*/ |
27
|
|
|
protected $query; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param Query $query |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Query $query) |
35
|
|
|
{ |
36
|
|
|
$this->query = $query; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get groups. |
41
|
|
|
* |
42
|
|
|
* @static |
43
|
|
|
* |
44
|
|
|
* @param Client $client |
45
|
|
|
* @param string $query |
46
|
|
|
* |
47
|
|
|
* @return LdapGroupProvider[] |
48
|
|
|
*/ |
49
|
|
|
public static function getGroups(Client $client, $query) |
50
|
|
|
{ |
51
|
|
|
$self = new static(new Query($client)); |
52
|
|
|
|
53
|
|
|
return $self->find($query); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Find groups. |
58
|
|
|
* |
59
|
|
|
* @param string $query |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function find($query) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->query->execute($this->getBasDn(), $query, $this->getAttributes()); |
66
|
|
|
$groups = []; |
67
|
|
|
|
68
|
|
|
if ($this->query->hasResult()) { |
69
|
|
|
$groups = $this->build(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $groups; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Build groups list. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function build() |
81
|
|
|
{ |
82
|
|
|
$groups = []; |
83
|
|
|
|
84
|
|
|
foreach ($this->query->getEntries()->getAll() as $entry) { |
85
|
|
|
$groups[] = new LdapGroupProvider($entry->getDn(), $entry->getFirstValue($this->getAttributeName())); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $groups; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Ge the list of attributes to fetch when reading the LDAP group entry. |
93
|
|
|
* |
94
|
|
|
* Must returns array with index that start at 0 otherwise ldap_search returns a warning "Array initialization wrong" |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getAttributes() |
99
|
|
|
{ |
100
|
|
|
return array_values(array_filter([ |
101
|
|
|
$this->getAttributeName(), |
102
|
|
|
])); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get LDAP group name attribute. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getAttributeName() |
111
|
|
|
{ |
112
|
|
|
if (!LDAP_GROUP_ATTRIBUTE_NAME) { |
113
|
|
|
throw new LogicException('LDAP full name attribute empty, check the parameter LDAP_GROUP_ATTRIBUTE_NAME'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return strtolower(LDAP_GROUP_ATTRIBUTE_NAME); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get LDAP group base DN. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getBasDn() |
125
|
|
|
{ |
126
|
|
|
if (!LDAP_GROUP_BASE_DN) { |
127
|
|
|
throw new LogicException('LDAP group base DN empty, check the parameter LDAP_GROUP_BASE_DN'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return LDAP_GROUP_BASE_DN; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.