|
1
|
|
|
<?php |
|
2
|
|
|
namespace SOG\Dashboard; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\Ldap\Attribute; |
|
5
|
|
|
use Zend\Ldap\Collection; |
|
6
|
|
|
use Zend\Ldap\Dn; |
|
7
|
|
|
use Zend\Ldap\Exception\LdapException; |
|
8
|
|
|
use Zend\Ldap\Ldap; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This class provides an adapter for nicer querying just abobe the concrete Zend\Ldap implementation. |
|
12
|
|
|
* |
|
13
|
|
|
* Class LdapAdapter |
|
14
|
|
|
* @package SOG\Dashboard |
|
15
|
|
|
*/ |
|
16
|
|
|
class LdapAdapter extends Ldap |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string The algorithm used for password generation. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $password_algorithm = Attribute::PASSWORD_HASH_SSHA; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Returns all groups (OUs) with their common names |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $fields A list of fields we want to return from the search |
|
27
|
|
|
* @return bool|\Zend\Ldap\Collection |
|
28
|
|
|
* @throws LdapException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getGroups($fields = ['cn']) |
|
31
|
|
|
{ |
|
32
|
|
|
$results = $this->search( |
|
33
|
|
|
'(objectClass=groupOfNames)', |
|
34
|
|
|
'ou=groups,o=sog-de,dc=sog', |
|
35
|
|
|
self::SEARCH_SCOPE_ONE, |
|
36
|
|
|
$fields, |
|
37
|
|
|
'cn' |
|
38
|
|
|
); |
|
39
|
|
|
return $results; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Retrieves an alphabetically sorted list of all users, by recusively search the tree. |
|
44
|
|
|
* This will find users in ou=active and ou=inactive |
|
45
|
|
|
* Note that this search is subject (as all other searches) to the maximum results returned by the LDAP server, |
|
46
|
|
|
* it might not contain *all* users. |
|
47
|
|
|
* |
|
48
|
|
|
* @return Collection |
|
49
|
|
|
* @throws LdapException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getAllUsers() |
|
52
|
|
|
{ |
|
53
|
|
|
$results = $this->search( |
|
54
|
|
|
'objectClass=inetOrgPerson', |
|
55
|
|
|
'ou=people,o=sog-de,dc=sog', |
|
56
|
|
|
self::SEARCH_SCOPE_SUB, |
|
57
|
|
|
['cn', 'uid', 'mail', 'dn'], |
|
58
|
|
|
'cn' |
|
59
|
|
|
); |
|
60
|
|
|
return $results; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Adds the DN to the given group |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $dnOfUser dn of the user to add |
|
67
|
|
|
* @param string $dnOfGroup dn of the group |
|
68
|
|
|
* @param string $role A groupOfNames attribute, such as owner, pending, member (default) |
|
69
|
|
|
* @throws LdapException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function addToGroup($dnOfUser, $dnOfGroup, $role = 'member') |
|
72
|
|
|
{ |
|
73
|
|
|
$entry = $this->getEntry($dnOfGroup); |
|
74
|
|
|
Attribute::setAttribute($entry, $role, $dnOfUser, true); |
|
|
|
|
|
|
75
|
|
|
$this->update($dnOfGroup, $entry); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Removes the DN from the given group |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $dnOfUser dn of the user to remove |
|
82
|
|
|
* @param string $dnOfGroup dn of the group |
|
83
|
|
|
* @param string $role A groupOfNames attribute, such as owner, pending, member (default) |
|
84
|
|
|
* @throws LdapException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function removeFromGroup($dnOfUser, $dnOfGroup, $role = 'member') |
|
87
|
|
|
{ |
|
88
|
|
|
$entry = $this->getEntry($dnOfGroup); |
|
89
|
|
|
Attribute::removeFromAttribute($entry, $role, $dnOfUser); |
|
|
|
|
|
|
90
|
|
|
$this->update($dnOfGroup, $entry); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Checks whether the given user is member of the given group. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $dnOfUser |
|
97
|
|
|
* @param string $dnOfGroup |
|
98
|
|
|
* @return bool True if user is member of group, false otherwise |
|
99
|
|
|
* @throws LdapException |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isMemberOfGroup($dnOfUser, $dnOfGroup) |
|
102
|
|
|
{ |
|
103
|
|
|
return ( |
|
104
|
|
|
$this->search( |
|
105
|
|
|
sprintf('member=%s', $dnOfUser), |
|
106
|
|
|
$dnOfGroup, |
|
107
|
|
|
self::SEARCH_SCOPE_BASE |
|
108
|
|
|
)->count() > 0); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Retrieve the members of the given group with their requested details |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $group_ou OU of the group |
|
115
|
|
|
* @param array $details Attributes to fetch |
|
116
|
|
|
* @return array The details of all owners indexed by their `uid` |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getOwnerDetails($group_ou, $details = ['mail']) |
|
119
|
|
|
{ |
|
120
|
|
|
$owners = $this->getOwners($group_ou)->toArray(); |
|
121
|
|
|
// make sure to include uid, as we want to index the returned array by it |
|
122
|
|
|
$details = array_merge($details, ['uid']); |
|
123
|
|
|
$result = []; |
|
124
|
|
|
if (empty($owners) === false) { |
|
125
|
|
|
foreach ($owners[0]['owner'] as $owner) { |
|
126
|
|
|
try { |
|
127
|
|
|
// retrieve details and add to result array |
|
128
|
|
|
$entry = $this->getEntry($owner, $details, true); |
|
129
|
|
|
$result[Attribute::getAttribute($entry, 'uid', 0)] = $entry; |
|
|
|
|
|
|
130
|
|
|
} catch (LdapException $ex) { |
|
131
|
|
|
// it's ok. |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Retrieve the members of the given group |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $group_ou OU of the group |
|
142
|
|
|
* @return bool|Collection |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getOwners($group_ou) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->getMembers($group_ou, ['owner']); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Retrieves all members for the given group CN |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $group_ou The common name of the group for which we want to retrieve the members |
|
153
|
|
|
* @param array $fields A list of fields we want to return from the search |
|
154
|
|
|
* @return bool|\Zend\Ldap\Collection |
|
155
|
|
|
* @throws LdapException |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getMembers($group_ou, $fields = ['member']) |
|
158
|
|
|
{ |
|
159
|
|
|
$results = $this->search( |
|
160
|
|
|
sprintf('(&(objectClass=groupOfNames)(ou=%s))', $group_ou), |
|
161
|
|
|
'ou=groups,o=sog-de,dc=sog', |
|
162
|
|
|
self::SEARCH_SCOPE_ONE, |
|
163
|
|
|
$fields, |
|
164
|
|
|
$fields[0] |
|
165
|
|
|
); |
|
166
|
|
|
return $results; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Attempts to retrieve the member or guest (DN) with the given mail address. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $mail The given mail address |
|
173
|
|
|
* @param string $attribute The name of the mail attribute to check |
|
174
|
|
|
* @return bool|string False if the search returned no results, the users' attributes otherwise. |
|
175
|
|
|
* @throws LdapException |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getMemberByMail($mail, $attribute = 'mail-alternative') |
|
178
|
|
|
{ |
|
179
|
|
|
$results = $this->search( |
|
180
|
|
|
sprintf('(&(objectClass=inetOrgPerson)(%s=%s))', $attribute, $mail), |
|
181
|
|
|
'ou=people,o=sog-de,dc=sog', |
|
182
|
|
|
self::SEARCH_SCOPE_SUB, |
|
183
|
|
|
['dn', 'uid', 'cn', 'displayName', $attribute], |
|
184
|
|
|
'dn' |
|
185
|
|
|
); |
|
186
|
|
|
if ($results->count() > 0) |
|
187
|
|
|
return $results->getFirst(); |
|
188
|
|
|
else |
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* This method can be used to assign the ROLE_GROUP_ADMIN role to a user. It checks if the given DN is a owner |
|
194
|
|
|
* of any group. Further checks should be done somewhere else. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $user_dn The user DN for which we want to check |
|
197
|
|
|
* @return bool True if the given user is a owner of any group, false otherwise. |
|
198
|
|
|
* @throws LdapException |
|
199
|
|
|
*/ |
|
200
|
|
|
public function isOwner($user_dn) |
|
201
|
|
|
{ |
|
202
|
|
|
$result = $this->getOwnedGroups($user_dn); |
|
203
|
|
|
// we don't care about specifics, we only want to know if the user is owner of any group |
|
204
|
|
|
return ($result != null && $result->count() > 0); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Returns the groups owned by the user with the given dn |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $user_dn The user DN for which we want to check |
|
211
|
|
|
* @return bool|Collection The groups owned by the user |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getOwnedGroups($user_dn) |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->getMemberships($user_dn, ['cn', 'ou'], 'owner'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Retrieves all memberships for the given DN |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $user_dn The DN for which to get the memberships |
|
222
|
|
|
* @param array $fields A list of fields we want to return from the search |
|
223
|
|
|
* @param string $attribute The attribute which we use for filtering |
|
224
|
|
|
* @return bool|\Zend\Ldap\Collection |
|
225
|
|
|
* @throws LdapException |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getMemberships($user_dn, $fields = ['cn'], $attribute = 'member') |
|
228
|
|
|
{ |
|
229
|
|
|
$results = $this->search( |
|
230
|
|
|
sprintf('(&(objectClass=groupOfNames)(%s=%s))', $attribute, $user_dn), |
|
231
|
|
|
'ou=groups,o=sog-de,dc=sog', |
|
232
|
|
|
self::SEARCH_SCOPE_ONE, |
|
233
|
|
|
$fields, |
|
234
|
|
|
'cn' |
|
235
|
|
|
); |
|
236
|
|
|
return $results; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Updates the email for the given DN. |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $dn User's dn |
|
243
|
|
|
* @param string $newEmail The new email address |
|
244
|
|
|
* @throws LdapException |
|
245
|
|
|
*/ |
|
246
|
|
|
public function updateEmail($dn, $newEmail) |
|
247
|
|
|
{ |
|
248
|
|
|
$entry = $this->getEntry($dn); |
|
249
|
|
|
Attribute::setAttribute($entry, 'mail-alternative', $newEmail); |
|
|
|
|
|
|
250
|
|
|
$this->update($dn, $entry); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Updates the password for the given DN. Only the DN him/herself can change the password. |
|
256
|
|
|
* Thus we need to bind as the DN first, update the password and then rebind to the privileged user. |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $dn The DN for which to update the password |
|
259
|
|
|
* @param string $old_password The old password, we need to bind to the DN first |
|
260
|
|
|
* @param string $new_password The new password |
|
261
|
|
|
* @throws LdapException |
|
262
|
|
|
*/ |
|
263
|
|
|
public function updatePassword($dn, $old_password, $new_password) |
|
264
|
|
|
{ |
|
265
|
|
|
try { |
|
266
|
|
|
$this->bind($dn, $old_password); |
|
267
|
|
|
$attributes = []; |
|
268
|
|
|
Attribute::setPassword($attributes, $new_password, $this->password_algorithm); |
|
269
|
|
|
$this->update($dn, $attributes); |
|
270
|
|
|
} catch (LdapException $ex) { |
|
271
|
|
|
throw $ex; |
|
272
|
|
|
} finally { |
|
273
|
|
|
// rebind to privileged user |
|
274
|
|
|
$this->bind(); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Force a password update using the privileged user, this is used by the password reset process. |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $dn |
|
282
|
|
|
* @param string $new_password |
|
283
|
|
|
* @throws LdapException |
|
284
|
|
|
*/ |
|
285
|
|
|
public function forceUpdatePassword($dn, $new_password) |
|
286
|
|
|
{ |
|
287
|
|
|
$attributes = []; |
|
288
|
|
|
Attribute::setPassword($attributes, $new_password, $this->password_algorithm); |
|
289
|
|
|
$this->update($dn, $attributes); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Adds a new object with the given parameters to the ou=inactive subtree. |
|
295
|
|
|
* |
|
296
|
|
|
* @param string $username The username of the new member |
|
297
|
|
|
* @param string $password The password in plaintext |
|
298
|
|
|
* @param string $firstName The first name of the new member |
|
299
|
|
|
* @param string $lastName The last name of the new member |
|
300
|
|
|
* @param string $mail The users' personal email address |
|
301
|
|
|
* @throws LdapException |
|
302
|
|
|
* @return array All stored attributes of the new member |
|
303
|
|
|
*/ |
|
304
|
|
|
public function createMember($username, $password, $firstName, $lastName, $mail) |
|
305
|
|
|
{ |
|
306
|
|
|
$dn = sprintf('uid=%s,ou=inactive,ou=people,o=sog-de,dc=sog', $username); |
|
307
|
|
|
$sog_mail = sprintf('%[email protected]', $username); |
|
308
|
|
|
$sog_mail_alias = sprintf('%[email protected]', $username); |
|
309
|
|
|
$info = []; |
|
310
|
|
|
|
|
311
|
|
|
// core data |
|
312
|
|
|
Attribute::setAttribute($info, 'dn', $dn); |
|
313
|
|
|
Attribute::setAttribute($info, 'uid', $username); |
|
314
|
|
|
Attribute::setAttribute($info, 'cn', $firstName . " " . $lastName); |
|
315
|
|
|
Attribute::setAttribute($info, 'displayName', $firstName); |
|
316
|
|
|
Attribute::setAttribute($info, 'givenName', $firstName); |
|
317
|
|
|
Attribute::setAttribute($info, 'sn', $lastName); |
|
318
|
|
|
|
|
319
|
|
|
// password |
|
320
|
|
|
Attribute::setPassword($info, $password, $this->password_algorithm); |
|
321
|
|
|
|
|
322
|
|
|
// meta data |
|
323
|
|
|
Attribute::setAttribute($info, 'mail', $sog_mail); |
|
324
|
|
|
Attribute::setAttribute($info, 'mail-alternative', $mail); |
|
325
|
|
|
Attribute::setAttribute($info, 'mailAlias', $sog_mail_alias); |
|
326
|
|
|
Attribute::setAttribute($info, 'mailHomeDirectory', sprintf('/srv/vmail/%s', $sog_mail)); |
|
327
|
|
|
Attribute::setAttribute($info, 'mailStorageDirectory', sprintf('maildir:/srv/vmail/%s/Maildir', $sog_mail)); |
|
328
|
|
|
Attribute::setAttribute($info, 'mailEnabled', 'TRUE'); |
|
329
|
|
|
Attribute::setAttribute($info, 'mailGidNumber', 5000); |
|
330
|
|
|
Attribute::setAttribute($info, 'mailUidNumber', 5000); |
|
331
|
|
|
Attribute::setAttribute($info, 'objectClass', [ |
|
332
|
|
|
'person', |
|
333
|
|
|
'sogperson', |
|
334
|
|
|
'organizationalPerson', |
|
335
|
|
|
'inetOrgPerson', |
|
336
|
|
|
'top', |
|
337
|
|
|
'PostfixBookMailAccount', |
|
338
|
|
|
'PostfixBookMailForward' |
|
339
|
|
|
]); |
|
340
|
|
|
|
|
341
|
|
|
$this->add($dn, $info); |
|
342
|
|
|
return $info; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Create a guest user, reserved for non-members who should receive messages to a mailing list. |
|
347
|
|
|
* |
|
348
|
|
|
* @param string $name The name of the guest |
|
349
|
|
|
* @param string $mail The guests' email address |
|
350
|
|
|
* @return array The LDAP attributes for the guest |
|
351
|
|
|
* @throws LdapException If adding the DN wasn't successful |
|
352
|
|
|
*/ |
|
353
|
|
|
public function createGuest($name, $mail) |
|
354
|
|
|
{ |
|
355
|
|
|
$username = 'guest'.$this->generateUsername($name); |
|
356
|
|
|
$dn = sprintf('uid=%s,ou=guests,ou=people,o=sog-de,dc=sog', $username); |
|
357
|
|
|
$info = []; |
|
358
|
|
|
|
|
359
|
|
|
// core data |
|
360
|
|
|
Attribute::setAttribute($info, 'dn', $dn); |
|
361
|
|
|
Attribute::setAttribute($info, 'uid', $username); |
|
362
|
|
|
Attribute::setAttribute($info, 'cn', "Guest ".$name); |
|
363
|
|
|
Attribute::setAttribute($info, 'displayName', "Guest ".$name); |
|
364
|
|
|
Attribute::setAttribute($info, 'sn', "Guest ".$name); |
|
365
|
|
|
Attribute::setAttribute($info, 'cn', "Guest ".$name); |
|
366
|
|
|
|
|
367
|
|
|
// meta data |
|
368
|
|
|
Attribute::setAttribute($info, 'mail', $mail); |
|
369
|
|
|
Attribute::setAttribute($info, 'objectClass', [ |
|
370
|
|
|
'inetOrgPerson', |
|
371
|
|
|
'top' |
|
372
|
|
|
]); |
|
373
|
|
|
|
|
374
|
|
|
$this->add($dn, $info); |
|
375
|
|
|
return $info; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Generates a unique username by replacing some special characters, converting to lowercase and then |
|
380
|
|
|
* appending an optional number if the username already exists. |
|
381
|
|
|
* |
|
382
|
|
|
* @param string $name The given name for the member |
|
383
|
|
|
* @return string The generated unique UID |
|
384
|
|
|
*/ |
|
385
|
|
|
public function generateUsername($name) |
|
386
|
|
|
{ |
|
387
|
|
|
$username = strtolower($name); |
|
388
|
|
|
// normalize special chars in username |
|
389
|
|
|
$username = str_replace( |
|
390
|
|
|
['ä', 'ö', 'ü', 'ß', ' '], |
|
391
|
|
|
['ae', 'oe', 'ue', 'ss', '.'], |
|
392
|
|
|
$username |
|
393
|
|
|
); |
|
394
|
|
|
|
|
395
|
|
|
$suffix = 2; |
|
396
|
|
|
$check = $username; |
|
397
|
|
|
while ($this->usernameExists($check) === true) { |
|
398
|
|
|
$check = $username . $suffix; |
|
399
|
|
|
$suffix++; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
return $check; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Checks if the given username is already taken, looks in the active and inactive subtree |
|
407
|
|
|
* |
|
408
|
|
|
* @param string $uid Does this username already exist? |
|
409
|
|
|
* @return bool True if member exists, false otherwise. |
|
410
|
|
|
*/ |
|
411
|
|
|
public function usernameExists($uid) |
|
412
|
|
|
{ |
|
413
|
|
|
return ($this->exists(sprintf('uid=%s,ou=active,ou=people,o=sog-de,dc=sog', $uid)) || |
|
414
|
|
|
$this->exists(sprintf('uid=%s,ou=inactive,ou=people,o=sog-de,dc=sog', $uid)) || |
|
415
|
|
|
$this->exists(sprintf('uid=%s,ou=guests,ou=people,o=sog-de,dc=sog', $uid))); |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Allowing the given user to access the given group. This will move the entry from the `pending` to the `member` |
|
420
|
|
|
* field. |
|
421
|
|
|
* |
|
422
|
|
|
* @param string $uid The username for which to approve the membership in $group |
|
423
|
|
|
* @param string $group The group for which the membership of $user is approved, we expect the `ou` value |
|
424
|
|
|
* @throws LdapException |
|
425
|
|
|
*/ |
|
426
|
|
View Code Duplication |
public function approveGroupMembership($uid, $group) |
|
|
|
|
|
|
427
|
|
|
{ |
|
428
|
|
|
|
|
429
|
|
|
$dnOfGroup = sprintf('ou=%s,ou=groups,o=sog-de,dc=sog', $group); |
|
430
|
|
|
$entry = $this->getEntry($dnOfGroup); |
|
431
|
|
|
if (is_null($entry)) { |
|
432
|
|
|
throw new LdapException($this, sprintf('Can\'t find group %s', $group)); |
|
433
|
|
|
} |
|
434
|
|
|
$dnOfUser = $this->findUserDN($uid); |
|
435
|
|
|
Attribute::removeFromAttribute($entry, 'pending', $dnOfUser); |
|
436
|
|
|
Attribute::setAttribute($entry, 'member', $dnOfUser, true); |
|
437
|
|
|
$this->update($dnOfGroup, $entry); |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Returns the dn of the first user with the given uid |
|
442
|
|
|
* |
|
443
|
|
|
* @param string $uid The uid of the user |
|
444
|
|
|
* @return string dn of the first user with the given uid |
|
445
|
|
|
* @throws LdapException |
|
446
|
|
|
*/ |
|
447
|
|
|
public function findUserDN($uid) |
|
448
|
|
|
{ |
|
449
|
|
|
// return early if we deal with a DN anyways |
|
450
|
|
|
if (Dn::checkDn($uid)) { |
|
451
|
|
|
return $uid; |
|
452
|
|
|
} |
|
453
|
|
|
$results = $this->search( |
|
454
|
|
|
sprintf('(&(objectClass=inetOrgPerson)(uid=%s))', $uid), |
|
455
|
|
|
'ou=people,o=sog-de,dc=sog', |
|
456
|
|
|
self::SEARCH_SCOPE_SUB, |
|
457
|
|
|
['dn'], |
|
458
|
|
|
'dn' |
|
459
|
|
|
); |
|
460
|
|
|
return $results->getFirst()['dn']; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Move a (new) inactive member to the active subtree. Being in ou=active is required for certain actions such as |
|
465
|
|
|
* accessing the Dashboard or Open Atrium. |
|
466
|
|
|
* |
|
467
|
|
|
* @param string $uid The username for the member to be activated |
|
468
|
|
|
* @throws LdapException |
|
469
|
|
|
*/ |
|
470
|
|
|
public function activateMember($uid) |
|
471
|
|
|
{ |
|
472
|
|
|
$active = sprintf('uid=%s,ou=active,ou=people,o=sog-de,dc=sog', $uid); |
|
473
|
|
|
$inactive = sprintf('uid=%s,ou=inactive,ou=people,o=sog-de,dc=sog', $uid); |
|
474
|
|
|
$this->move($inactive, $active); |
|
475
|
|
|
|
|
476
|
|
|
$this->refreshPendingRequests($inactive, $active); |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
/** |
|
480
|
|
|
* For all groups, update the pending field from the inactive DN to the active DN. |
|
481
|
|
|
* |
|
482
|
|
|
* @param string $from Inactive DN |
|
483
|
|
|
* @param string $to Active DN |
|
484
|
|
|
* @throws LdapException |
|
485
|
|
|
*/ |
|
486
|
|
|
private function refreshPendingRequests($from, $to) |
|
487
|
|
|
{ |
|
488
|
|
|
$results = $this->search( |
|
489
|
|
|
sprintf('(&(objectClass=groupOfNames)(pending=%s))', $from), |
|
490
|
|
|
'ou=groups,o=sog-de,dc=sog', |
|
491
|
|
|
self::SEARCH_SCOPE_ONE, |
|
492
|
|
|
['ou'] |
|
493
|
|
|
); |
|
494
|
|
|
|
|
495
|
|
|
foreach ($results as $group) { |
|
496
|
|
|
$this->dropMembershipRequest($from, Attribute::getAttribute($group, 'ou', 0)); |
|
|
|
|
|
|
497
|
|
|
$this->requestGroupMembership($to, Attribute::getAttribute($group, 'ou', 0)); |
|
|
|
|
|
|
498
|
|
|
} |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* Remove a membership request for $group. This will remove the user's dn from the `pending` attribute of the $group |
|
503
|
|
|
* |
|
504
|
|
|
* @param string $uid The username of the user who has done the request |
|
505
|
|
|
* @param string $group The group for which the request shall be removed, we expect the `ou` value |
|
506
|
|
|
* @throws LdapException If group can't be found or update wasn't successful |
|
507
|
|
|
* @return boolean True, if pending contained $user and the entry has been deleted; false otherwise |
|
508
|
|
|
*/ |
|
509
|
|
View Code Duplication |
public function dropMembershipRequest($uid, $group) |
|
|
|
|
|
|
510
|
|
|
{ |
|
511
|
|
|
$dnOfGroup = sprintf('ou=%s,ou=groups,o=sog-de,dc=sog', $group); |
|
512
|
|
|
$entry = $this->getEntry($dnOfGroup); |
|
513
|
|
|
if (is_null($entry)) { |
|
514
|
|
|
throw new LdapException($this, sprintf('Can\'t find group %s', $group)); |
|
515
|
|
|
} |
|
516
|
|
|
$dnOfUser = $this->findUserDN($uid); |
|
517
|
|
|
if (!Attribute::attributeHasValue($entry, 'pending', $dnOfUser)) { |
|
518
|
|
|
return false; |
|
519
|
|
|
} |
|
520
|
|
|
Attribute::removeFromAttribute($entry, 'pending', $dnOfUser); |
|
521
|
|
|
$this->update($dnOfGroup, $entry); |
|
522
|
|
|
return true; |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* Requesting access for the given user to $group. This will add an entry to the `pending` attribute of the $group |
|
527
|
|
|
* |
|
528
|
|
|
* @param string $uid The username for which to request the membership in $group |
|
529
|
|
|
* @param string $group The group for which the membership of $user is requested, we expect the `ou` value |
|
530
|
|
|
* @throws LdapException If group can't be found or update wasn't successful |
|
531
|
|
|
* @return boolean True, if pending didn't already contain $user; false otherwise (also when already a member) |
|
532
|
|
|
*/ |
|
533
|
|
|
public function requestGroupMembership($uid, $group) |
|
534
|
|
|
{ |
|
535
|
|
|
$dnOfGroup = sprintf('ou=%s,ou=groups,o=sog-de,dc=sog', $group); |
|
536
|
|
|
$entry = $this->getEntry($dnOfGroup); |
|
537
|
|
|
if (is_null($entry)) { |
|
538
|
|
|
throw new LdapException($this, sprintf('Can\'t find group %s', $group)); |
|
539
|
|
|
} |
|
540
|
|
|
$dnOfUser = $this->findUserDN($uid); |
|
541
|
|
|
if (Attribute::attributeHasValue($entry, 'member', $dnOfUser) || Attribute::attributeHasValue($entry, 'pending', $dnOfUser)) { |
|
542
|
|
|
return false; |
|
543
|
|
|
} |
|
544
|
|
|
Attribute::setAttribute($entry, 'pending', $dnOfUser, true); |
|
545
|
|
|
$this->update($dnOfGroup, $entry); |
|
546
|
|
|
return true; |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
/** |
|
550
|
|
|
* Move an active member to the inactive subtree, thus preventing him/her from logging on to certain services. |
|
551
|
|
|
* |
|
552
|
|
|
* @param string $uid The username of the member to be deactivated |
|
553
|
|
|
* @throws LdapException |
|
554
|
|
|
*/ |
|
555
|
|
|
public function deactivateMember($uid) |
|
556
|
|
|
{ |
|
557
|
|
|
$active = sprintf('uid=%s,ou=active,ou=people,o=sog-de,dc=sog', $uid); |
|
558
|
|
|
$inactive = sprintf('uid=%s,ou=inactive,ou=people,o=sog-de,dc=sog', $uid); |
|
559
|
|
|
$this->move($active, $inactive); |
|
560
|
|
|
} |
|
561
|
|
|
} |
|
562
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.