-
Notifications
You must be signed in to change notification settings - Fork 13
/
LDAPService.php
1246 lines (1112 loc) · 44.6 KB
/
LDAPService.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace SilverStripe\LDAP\Services;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\LDAP\Model\LDAPGateway;
use SilverStripe\LDAP\Model\LDAPGroupMapping;
use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
use SilverStripe\Security\RandomGenerator;
use Laminas\Ldap\Ldap;
/**
* Class LDAPService
*
* Provides LDAP operations expressed in terms of the SilverStripe domain.
* All other modules should access LDAP through this class.
*
* This class builds on top of LDAPGateway's detailed code by adding:
* - caching
* - data aggregation and restructuring from multiple lower-level calls
* - error handling
*
* LDAPService relies on Laminas LDAP module's data structures for some parameters and some return values.
*/
class LDAPService implements Flushable
{
use Injectable;
use Extensible;
use Configurable;
/**
* @var array
*/
private static $dependencies = [
'gateway' => '%$' . LDAPGateway::class
];
/**
* If configured, only user objects within these locations will be exposed to this service.
*
* @var array
* @config
*/
private static $users_search_locations = [];
/**
* If configured, only group objects within these locations will be exposed to this service.
* @var array
*
* @config
*/
private static $groups_search_locations = [];
/**
* Location to create new users in (distinguished name).
* @var string
*
* @config
*/
private static $new_users_dn;
/**
* Location to create new groups in (distinguished name).
* @var string
*
* @config
*/
private static $new_groups_dn;
/**
* @var array
*/
private static $_cache_nested_groups = [];
/**
* If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always
* be added to this group's membership when imported, regardless of any sort of group mappings.
*
* @var string
* @config
*/
private static $default_group;
/**
* For samba4 directory, there is no way to enforce password history on password resets.
* This only happens with changePassword (which requires the old password).
* This works around it by making the old password up and setting it administratively.
*
* A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag,
* but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020
*
* @var bool
*/
private static $password_history_workaround = false;
/**
* Get the cache object used for LDAP results. Note that the default lifetime set here
* is 8 hours, but you can change that by adding configuration:
*
* <code>
* SilverStripe\Core\Injector\Injector:
* Psr\SimpleCache\CacheInterface.ldap:
* constructor:
* defaultLifetime: 3600 # time in seconds
* </code>
*
* @return Psr\SimpleCache\CacheInterface
*/
public static function get_cache()
{
return Injector::inst()->get(CacheInterface::class . '.ldap');
}
/**
* Flushes out the LDAP results cache when flush=1 is called.
*/
public static function flush()
{
$cache = self::get_cache();
$cache->clear();
}
/**
* @var LDAPGateway
*/
public $gateway;
/**
* Setter for gateway. Useful for overriding the gateway with a fake for testing.
* @var LDAPGateway
*/
public function setGateway($gateway)
{
$this->gateway = $gateway;
}
/**
* Return the LDAP gateway currently in use. Can be strung together to access the underlying Laminas\Ldap instance, or
* the PHP ldap resource itself. For example:
* - Get the Laminas\Ldap object: $service->getGateway()->getLdap() // Laminas\Ldap\Ldap object
* - Get the underlying PHP ldap resource: $service->getGateway()->getLdap()->getResource() // php resource
*
* @return LDAPGateway
*/
public function getGateway()
{
return $this->gateway;
}
/**
* Checkes whether or not the service is enabled.
*
* @return bool
*/
public function enabled()
{
$options = Config::inst()->get(LDAPGateway::class, 'options');
return !empty($options);
}
/**
* Authenticate the given username and password with LDAP.
*
* @param string $username
* @param string $password
*
* @return array
*/
public function authenticate($username, $password)
{
$result = $this->getGateway()->authenticate($username, $password);
$messages = $result->getMessages();
// all messages beyond the first one are for debugging and
// not suitable to display to the user.
foreach ($messages as $i => $message) {
if ($i > 0) {
$this->getLogger()->debug(str_replace("\n", "\n ", $message ?? ''));
}
}
$message = $messages[0]; // first message is user readable, suitable for showing on login form
// show better errors than the defaults for various status codes returned by LDAP
if (!empty($messages[1]) && strpos($messages[1] ?? '', 'NT_STATUS_ACCOUNT_LOCKED_OUT') !== false) {
$message = _t(
__CLASS__ . '.ACCOUNTLOCKEDOUT',
'Your account has been temporarily locked because of too many failed login attempts. ' .
'Please try again later.'
);
}
if (!empty($messages[1]) && strpos($messages[1] ?? '', 'NT_STATUS_LOGON_FAILURE') !== false) {
$message = _t(
__CLASS__ . '.INVALIDCREDENTIALS',
'The provided details don\'t seem to be correct. Please try again.'
);
}
return [
'success' => $result->getCode() === 1,
'identity' => $result->getIdentity(),
'message' => $message,
'code' => $result->getCode()
];
}
/**
* Return all nodes (organizational units, containers, and domains) within the current base DN.
*
* @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default.
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getNodes($cached = true, $attributes = [])
{
$cache = self::get_cache();
$cacheKey = 'nodes' . md5(implode('', $attributes));
$results = $cache->has($cacheKey);
if (!$results || !$cached) {
$results = [];
$records = $this->getGateway()->getNodes(null, Ldap::SEARCH_SCOPE_SUB, $attributes);
foreach ($records as $record) {
$results[$record['dn']] = $record;
}
$cache->set($cacheKey, $results);
}
return $results;
}
/**
* Return all AD groups in configured search locations, including all nested groups.
* Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway
* to use the default baseDn defined in the connection.
*
* @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default.
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @param string $indexBy Attribute to use as an index.
* @return array
*/
public function getGroups($cached = true, $attributes = [], $indexBy = 'dn')
{
$searchLocations = $this->config()->groups_search_locations ?: [null];
$cache = self::get_cache();
$cacheKey = 'groups' . md5(implode('', array_merge($searchLocations, $attributes)));
$results = $cache->has($cacheKey);
if (!$results || !$cached) {
$results = [];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getGroups($searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
if (!$records) {
continue;
}
foreach ($records as $record) {
$results[$record[$indexBy]] = $record;
}
}
$cache->set($cacheKey, $results);
}
if ($cached && $results === true) {
$results = $cache->get($cacheKey);
}
return $results;
}
/**
* Return all member groups (and members of those, recursively) underneath a specific group DN.
* Note that these get cached in-memory per-request for performance to avoid re-querying for the same results.
*
* @param string $dn
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getNestedGroups($dn, $attributes = [])
{
if (isset(self::$_cache_nested_groups[$dn])) {
return self::$_cache_nested_groups[$dn];
}
$searchLocations = $this->config()->groups_search_locations ?: [null];
$results = [];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getNestedGroups($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
foreach ($records as $record) {
$results[$record['dn']] = $record;
}
}
self::$_cache_nested_groups[$dn] = $results;
return $results;
}
/**
* Get a particular AD group's data given a GUID.
*
* @param string $guid
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getGroupByGUID($guid, $attributes = [])
{
$searchLocations = $this->config()->groups_search_locations ?: [null];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getGroupByGUID($guid, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
if ($records) {
return $records[0];
}
}
}
/**
* Get a particular AD group's data given a DN.
*
* @param string $dn
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getGroupByDN($dn, $attributes = [])
{
$searchLocations = $this->config()->groups_search_locations ?: [null];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getGroupByDN($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
if ($records) {
return $records[0];
}
}
}
/**
* Return all AD users in configured search locations, including all users in nested groups.
* Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway
* to use the default baseDn defined in the connection.
*
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getUsers($attributes = [])
{
$searchLocations = $this->config()->users_search_locations ?: [null];
$results = [];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getUsersWithIterator($searchLocation, $attributes);
if (!$records) {
continue;
}
foreach ($records as $record) {
$results[$record['objectguid']] = $record;
}
}
return $results;
}
/**
* Get a specific AD user's data given a GUID.
*
* @param string $guid
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getUserByGUID($guid, $attributes = [])
{
$searchLocations = $this->config()->users_search_locations ?: [null];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getUserByGUID($guid, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
if ($records) {
return $records[0];
}
}
}
/**
* Get a specific AD user's data given a DN.
*
* @param string $dn
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
*
* @return array
*/
public function getUserByDN($dn, $attributes = [])
{
$searchLocations = $this->config()->users_search_locations ?: [null];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getUserByDN($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
if ($records) {
return $records[0];
}
}
}
/**
* Get a specific user's data given an email.
*
* @param string $email
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getUserByEmail($email, $attributes = [])
{
$searchLocations = $this->config()->users_search_locations ?: [null];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getUserByEmail($email, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
if ($records) {
return $records[0];
}
}
}
/**
* Get a specific user's data given a username.
*
* @param string $username
* @param array $attributes List of specific AD attributes to return. Empty array means return everything.
* @return array
*/
public function getUserByUsername($username, $attributes = [])
{
$searchLocations = $this->config()->users_search_locations ?: [null];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getUserByUsername(
$username,
$searchLocation,
Ldap::SEARCH_SCOPE_SUB,
$attributes
);
if ($records) {
return $records[0];
}
}
}
/**
* Get a username for an email.
*
* @param string $email
* @return string|null
*/
public function getUsernameByEmail($email)
{
$data = $this->getUserByEmail($email);
if (empty($data)) {
return null;
}
return $this->getGateway()->getCanonicalUsername($data);
}
/**
* Given a group DN, get the group membership data in LDAP.
*
* @param string $dn
* @return array
*/
public function getLDAPGroupMembers($dn)
{
$groupObj = Group::get()->filter('DN', $dn)->first();
$groupData = $this->getGroupByGUID($groupObj->GUID);
$members = !empty($groupData['member']) ? $groupData['member'] : [];
// If a user belongs to a single group, this comes through as a string.
// Normalise to a array so it's consistent.
if ($members && is_string($members)) {
$members = [$members];
}
return $members;
}
/**
* Update the current Member record with data from LDAP.
*
* It's allowed to pass an unwritten Member record here, because it's not always possible to satisfy
* field constraints without importing data from LDAP (for example if the application requires Username
* through a Validator). Even though unwritten, it still must have the GUID set.
*
* Constraints:
* - GUID of the member must have already been set, for integrity reasons we don't allow it to change here.
*
* @param Member $member
* @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with.
* If not given, the data will be looked up by the user's GUID.
* @param bool $updateGroups controls whether to run the resource-intensive group update function as well. This is
* skipped during login to reduce load.
* @return bool
* @internal param $Member
*/
public function updateMemberFromLDAP(Member $member, $data = null, $updateGroups = true)
{
if (!$this->enabled()) {
return false;
}
if (!$member->GUID) {
$this->getLogger()->debug(sprintf('Cannot update Member ID %s, GUID not set', $member->ID));
return false;
}
if (!$data) {
$data = $this->getUserByGUID($member->GUID);
if (!$data) {
$this->getLogger()->debug(sprintf('Could not retrieve data for user. GUID: %s', $member->GUID));
return false;
}
}
$member->IsExpired = ($data['useraccountcontrol'] & 2) == 2;
$member->LastSynced = (string)DBDatetime::now();
foreach ($member->config()->ldap_field_mappings as $attribute => $field) {
// Special handling required for attributes that don't exist in the response.
if (!isset($data[$attribute])) {
// a attribute we're expecting is missing from the LDAP response
if ($this->config()->get("reset_missing_attributes")) {
// (Destructive) Reset the corresponding attribute on our side if instructed to do so.
if (method_exists($member->$field, "delete")
&& method_exists($member->$field, "deleteFile")
&& $member->$field->exists()
) {
$member->$field->deleteFile();
$member->$field->delete();
} else {
$member->$field = null;
}
// or log the information.
} else {
$this->getLogger()->debug(
sprintf(
'Attribute %s configured in Member.ldap_field_mappings, ' .
'but no available attribute in AD data (GUID: %s, Member ID: %s)',
$attribute,
$data['objectguid'],
$member->ID
)
);
}
// No further processing required.
continue;
}
if (in_array($attribute, $this->config()->photo_attributes ?? [])) {
$this->processThumbnailPhoto($member, $field, $data, $attribute);
} else {
$member->$field = $data[$attribute];
}
}
// this is to keep track of which group the user gets mapped to if there
// is a default group defined so when they are removed from unmapped
// groups in ->updateMemberGroups() they are not then removed from the
// default group
$mappedGroupIDs = [];
// if a default group was configured, ensure the user is in that group
if ($this->config()->default_group) {
$group = Group::get()->filter('Code', $this->config()->default_group)->limit(1)->first();
if (!($group && $group->exists())) {
$this->getLogger()->debug(
sprintf(
'LDAPService.default_group misconfiguration! There is no such group with Code = \'%s\'',
$this->config()->default_group
)
);
} else {
// The member must exist before we can use it as a relation:
if (!$member->exists()) {
$member->write();
}
$group->Members()->add($member, [
'IsImportedFromLDAP' => '1'
]);
$mappedGroupIDs[] = $group->ID;
}
}
// Member must have an ID before manipulating Groups, otherwise they will not be added correctly.
// However we cannot do a full ->write before the groups are associated, because this will upsync
// the Member, in effect deleting all their LDAP group associations!
$member->writeWithoutSync();
if ($updateGroups) {
$this->updateMemberGroups($data, $member, $mappedGroupIDs);
}
// This will throw an exception if there are two distinct GUIDs with the same email address.
// We are happy with a raw 500 here at this stage.
$member->write();
}
/**
* Attempts to process the thumbnail photo for a given user and store it against a {@link Member} object. This will
* overwrite any old file with the same name. The filename of the photo will be set to 'thumbnailphoto-<GUID>.jpg'.
*
* At the moment only JPEG files are supported, as this is mainly to support Active Directory, which always provides
* the photo in a JPEG format, and always at a specific resolution.
*
* @param Member $member The SilverStripe Member object we are storing this file on
* @param string $fieldName The SilverStripe field name we are storing this new file against
* @param array $data Array of all data returned about this user from LDAP
* @param string $attributeName Name of the attribute in the $data array to get the binary blog from
* @return boolean true on success, false on failure
*
* @throws ValidationException
*/
protected function processThumbnailPhoto(Member $member, $fieldName, $data, $attributeName)
{
$imageClass = $member->getRelationClass($fieldName);
if ($imageClass !== Image::class && !is_subclass_of($imageClass, Image::class)) {
$this->getLogger()->warning(
sprintf(
'Member field %s configured for ' . $attributeName . ' AD attribute, but it isn\'t a valid relation to an '
. 'Image class',
$fieldName
)
);
return false;
}
// Use and update existing Image object (if one exists), or create a new image if not - prevents ID thrashing
/** @var File $existingObj */
$existingObj = $member->getComponent($fieldName);
if ($existingObj && $existingObj->exists()) {
$file = $existingObj;
// If the file hashes match, and the file already exists, we don't need to update anything.
$hash = $existingObj->File->getHash();
if (hash_equals($hash ?? '', sha1($data[$attributeName] ?? ''))) {
return true;
}
} else {
$file = Image::create();
}
// Setup variables
$thumbnailFolder = Folder::find_or_make($member->config()->ldap_thumbnail_path);
$filename = sprintf($attributeName . '-%s.jpg', $data['objectguid']);
$filePath = File::join_paths($thumbnailFolder->getFilename(), $filename);
$fileCfg = [
// if there's a filename conflict we've got new content so overwrite it.
'conflict' => AssetStore::CONFLICT_OVERWRITE,
'visibility' => AssetStore::VISIBILITY_PUBLIC
];
if (isset($data['displayname'])) {
$userName = $data['displayname'];
} else {
$userName = $member->Name;
}
$file->Title = sprintf('Thumbnail photo for %s', $userName);
$file->ShowInSearch = false;
$file->ParentID = $thumbnailFolder->ID;
$file->setFromString($data[$attributeName], $filePath, null, null, $fileCfg);
$file->write();
$file->publishRecursive();
if ($file->exists()) {
$relationField = sprintf('%sID', $fieldName);
$member->{$relationField} = $file->ID;
}
return true;
}
/**
* Ensure the user is mapped to any applicable groups.
* @param array $data
* @param Member $member
* @param array $mappedGroupsToKeep
*/
public function updateMemberGroups($data, Member $member, $mappedGroupsToKeep = [])
{
// this is to keep track of which groups the user gets mapped to and
// we'll use that later to remove them from any groups that they're no
// longer mapped to
$mappedGroupIDs = $mappedGroupsToKeep;
if (isset($data['memberof'])) {
$ldapGroups = is_array($data['memberof']) ? $data['memberof'] : [$data['memberof']];
foreach ($ldapGroups as $groupDN) {
foreach (LDAPGroupMapping::get() as $mapping) {
if (!$mapping->DN) {
$this->getLogger()->debug(
sprintf(
'LDAPGroupMapping ID %s is missing DN field. Skipping',
$mapping->ID
)
);
continue;
}
// the user is a direct member of group with a mapping, add them to the SS group.
if ($mapping->DN == $groupDN) {
$group = $mapping->Group();
if ($group && $group->exists()) {
$group->Members()->add($member, [
'IsImportedFromLDAP' => '1'
]);
$mappedGroupIDs[] = $mapping->GroupID;
}
}
// the user *might* be a member of a nested group provided the scope of the mapping
// is to include the entire subtree. Check all those mappings and find the LDAP child groups
// to see if they are a member of one of those. If they are, add them to the SS group
if ($mapping->Scope == 'Subtree') {
$childGroups = $this->getNestedGroups($mapping->DN, ['dn']);
if (!$childGroups) {
continue;
}
foreach ($childGroups as $childGroupDN => $childGroupRecord) {
if ($childGroupDN == $groupDN) {
$group = $mapping->Group();
if ($group && $group->exists()) {
$group->Members()->add($member, [
'IsImportedFromLDAP' => '1'
]);
$mappedGroupIDs[] = $mapping->GroupID;
}
}
}
}
}
}
}
// Lookup the previous mappings and see if there is any mappings no longer present.
$unmappedGroups = $member->Groups()->alterDataQuery(function (DataQuery $query) {
// join with the Group_Members table because we only want those group members assigned by this module.
$query->leftJoin("Group_Members", '"Group_Members"."GroupID" = "Group"."ID"');
$query->where('"Group_Members"."IsImportedFromLDAP" = 1');
});
// Don't remove associations which have just been added and we know are already correct!
if (!empty($mappedGroupIDs)) {
$unmappedGroups = $unmappedGroups->filter("GroupID:NOT", $mappedGroupIDs);
}
// Remove the member from any previously mapped groups, where the mapping
// has since been removed in the LDAP data source
if ($unmappedGroups->count()) {
foreach ($unmappedGroups as $group) {
$group->Members()->remove($member);
}
}
}
/**
* Sync a specific Group by updating it with LDAP data.
*
* @param Group $group An existing Group or a new Group object
* @param array $data LDAP group object data
*
* @return bool
*/
public function updateGroupFromLDAP(Group $group, $data)
{
if (!$this->enabled()) {
return false;
}
// Synchronise specific guaranteed fields.
$group->Code = $data['samaccountname'];
$group->Title = $data['samaccountname'];
if (!empty($data['description'])) {
$group->Description = $data['description'];
}
$group->DN = $data['dn'];
$group->LastSynced = (string)DBDatetime::now();
$group->write();
// Mappings on this group are automatically maintained to contain just the group's DN.
// First, scan through existing mappings and remove ones that are not matching (in case the group moved).
$hasCorrectMapping = false;
foreach ($group->LDAPGroupMappings() as $mapping) {
if ($mapping->DN === $data['dn']) {
// This is the correct mapping we want to retain.
$hasCorrectMapping = true;
} else {
$mapping->delete();
}
}
// Second, if the main mapping was not found, add it in.
if (!$hasCorrectMapping) {
$mapping = new LDAPGroupMapping();
$mapping->DN = $data['dn'];
$mapping->write();
$group->LDAPGroupMappings()->add($mapping);
}
}
/**
* Creates a new LDAP user from the passed Member record.
* Note that the Member record must have a non-empty Username field for this to work.
*
* @param Member $member
* @throws ValidationException
* @throws Exception
*/
public function createLDAPUser(Member $member)
{
if (!$this->enabled()) {
return;
}
if (empty($member->Username)) {
throw new ValidationException('Member missing Username. Cannot create LDAP user');
}
if (!$this->config()->new_users_dn) {
throw new Exception('LDAPService::new_users_dn must be configured to create LDAP users');
}
// Normalise username to lowercase to ensure we don't have duplicates of different cases
$member->Username = strtolower($member->Username ?? '');
// Create user in LDAP using available information.
$dn = sprintf('CN=%s,%s', $member->Username, $this->config()->new_users_dn);
try {
$this->add($dn, [
'objectclass' => 'user',
'cn' => $member->Username,
'accountexpires' => '9223372036854775807',
'useraccountcontrol' => '66048',
'userprincipalname' => sprintf(
'%s@%s',
$member->Username,
$this->getGateway()->config()->options['accountDomainName']
),
]);
} catch (Exception $e) {
throw new ValidationException('LDAP synchronisation failure: ' . $e->getMessage());
}
$user = $this->getUserByUsername($member->Username);
if (empty($user['objectguid'])) {
throw new ValidationException('LDAP synchronisation failure: user missing GUID');
}
// Creation was successful, mark the user as LDAP managed by setting the GUID.
$member->GUID = $user['objectguid'];
}
/**
* Creates a new LDAP group from the passed Group record.
*
* @param Group $group
* @throws ValidationException
*/
public function createLDAPGroup(Group $group)
{
if (!$this->enabled()) {
return;
}
if (empty($group->Title)) {
throw new ValidationException('Group missing Title. Cannot create LDAP group');
}
if (!$this->config()->new_groups_dn) {
throw new Exception('LDAPService::new_groups_dn must be configured to create LDAP groups');
}
// LDAP isn't really meant to distinguish between a Title and Code. Squash them.
$group->Code = $group->Title;
$dn = sprintf('CN=%s,%s', $group->Title, $this->config()->new_groups_dn);
try {
$this->add($dn, [
'objectclass' => 'group',
'cn' => $group->Title,
'name' => $group->Title,
'samaccountname' => $group->Title,
'description' => $group->Description,
'distinguishedname' => $dn
]);
} catch (Exception $e) {
throw new ValidationException('LDAP group creation failure: ' . $e->getMessage());
}
$data = $this->getGroupByDN($dn);
if (empty($data['objectguid'])) {
throw new ValidationException(
new ValidationResult(
false,
'LDAP group creation failure: group might have been created in LDAP. GUID is missing.'
)
);
}
// Creation was successful, mark the group as LDAP managed by setting the GUID.
$group->GUID = $data['objectguid'];
$group->DN = $data['dn'];
}
/**
* Update the Member data back to the corresponding LDAP user object.
*
* @param Member $member
* @throws ValidationException
*/
public function updateLDAPFromMember(Member $member)
{
if (!$this->enabled()) {
return;
}
if (!$member->GUID) {
throw new ValidationException('Member missing GUID. Cannot update LDAP user');
}
$data = $this->getUserByGUID($member->GUID);
if (empty($data['objectguid'])) {
throw new ValidationException('LDAP synchronisation failure: user missing GUID');
}
if (empty($member->Username)) {
throw new ValidationException('Member missing Username. Cannot update LDAP user');
}
$dn = $data['distinguishedname'];
// Normalise username to lowercase to ensure we don't have duplicates of different cases
$member->Username = strtolower($member->Username ?? '');
try {
// If the common name (cn) has changed, we need to ensure they've been moved
// to the new DN, to avoid any clashes between user objects.
if ($data['cn'] != $member->Username) {
$newDn = sprintf('CN=%s,%s', $member->Username, preg_replace('/^CN=(.+?),/', '', $dn ?? ''));
$this->move($dn, $newDn);
$dn = $newDn;
}
} catch (Exception $e) {
throw new ValidationException('LDAP move failure: ' . $e->getMessage());
}
try {
$attributes = [
'displayname' => sprintf('%s %s', $member->FirstName, $member->Surname),
'name' => sprintf('%s %s', $member->FirstName, $member->Surname),
'userprincipalname' => sprintf(
'%s@%s',
$member->Username,
$this->getGateway()->config()->options['accountDomainName']
),
];
foreach ($member->config()->ldap_field_mappings as $attribute => $field) {
$relationClass = $member->getRelationClass($field);
if (!$relationClass) {
$attributes[$attribute] = $member->$field;
}
}
$this->update($dn, $attributes);
} catch (Exception $e) {
throw new ValidationException('LDAP synchronisation failure: ' . $e->getMessage());
}
}
/**
* Ensure the user belongs to the correct groups in LDAP from their membership
* to local LDAP mapped SilverStripe groups.
*
* This also removes them from LDAP groups if they've been taken out of one.
* It will not affect group membership of non-mapped groups, so it will
* not touch such internal AD groups like "Domain Users".
*
* @param Member $member
* @throws ValidationException
*/
public function updateLDAPGroupsForMember(Member $member)
{
if (!$this->enabled()) {