1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\LDAP\Tasks; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Dev\BuildTask; |
8
|
|
|
use SilverStripe\LDAP\Services\LDAPService; |
9
|
|
|
use SilverStripe\ORM\DB; |
10
|
|
|
use SilverStripe\Security\Group; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LDAPGroupSyncTask |
14
|
|
|
* |
15
|
|
|
* A task to sync all groups to the site using LDAP. |
16
|
|
|
* |
17
|
|
|
* @package activedirectory |
18
|
|
|
*/ |
19
|
|
|
class LDAPGroupSyncTask extends BuildTask |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private static $segment = 'LDAPGroupSyncTask'; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private static $dependencies = [ |
|
|
|
|
31
|
|
|
'ldapService' => '%$' . LDAPService::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Setting this to true causes the sync to delete any local Group |
36
|
|
|
* records that were previously imported, but no longer existing in LDAP. |
37
|
|
|
* |
38
|
|
|
* @config |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private static $destructive = false; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getTitle() |
47
|
|
|
{ |
48
|
|
|
return _t(__CLASS__ . '.SYNCTITLE', 'Sync all groups from Active Directory'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
* @var HTTPRequest $request |
54
|
|
|
*/ |
55
|
|
|
public function run($request) |
56
|
|
|
{ |
57
|
|
|
// get all groups from LDAP, but only get the attributes we need. |
58
|
|
|
// this is useful to avoid holding onto too much data in memory |
59
|
|
|
// especially in the case where getGroups() would return a lot of groups |
60
|
|
|
$ldapGroups = $this->ldapService->getGroups( |
|
|
|
|
61
|
|
|
false, |
62
|
|
|
['objectguid', 'samaccountname', 'dn', 'name', 'description'], |
63
|
|
|
// Change the indexing attribute so we can look up by GUID during the deletion process below. |
64
|
|
|
'objectguid' |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$start = time(); |
68
|
|
|
|
69
|
|
|
$created = 0; |
70
|
|
|
$updated = 0; |
71
|
|
|
$deleted = 0; |
72
|
|
|
|
73
|
|
View Code Duplication |
foreach ($ldapGroups as $data) { |
|
|
|
|
74
|
|
|
$group = Group::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
75
|
|
|
|
76
|
|
|
if (!($group && $group->exists())) { |
77
|
|
|
// create the initial Group with some internal fields |
78
|
|
|
$group = new Group(); |
79
|
|
|
$group->GUID = $data['objectguid']; |
80
|
|
|
|
81
|
|
|
$this->log(sprintf( |
82
|
|
|
'Creating new Group (GUID: %s, sAMAccountName: %s)', |
83
|
|
|
$data['objectguid'], |
84
|
|
|
$data['samaccountname'] |
85
|
|
|
)); |
86
|
|
|
$created++; |
87
|
|
|
} else { |
88
|
|
|
$this->log(sprintf( |
89
|
|
|
'Updating existing Group "%s" (ID: %s, GUID: %s, sAMAccountName: %s)', |
90
|
|
|
$group->getTitle(), |
91
|
|
|
$group->ID, |
92
|
|
|
$data['objectguid'], |
93
|
|
|
$data['samaccountname'] |
94
|
|
|
)); |
95
|
|
|
$updated++; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$this->ldapService->updateGroupFromLDAP($group, $data); |
100
|
|
|
} catch (Exception $e) { |
101
|
|
|
$this->log($e->getMessage()); |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// remove Group records that were previously imported, but no longer exist in the directory |
107
|
|
|
// NOTE: DB::query() here is used for performance and so we don't run out of memory |
108
|
|
|
if ($this->config()->destructive) { |
109
|
|
|
foreach (DB::query('SELECT "ID", "GUID" FROM "Group" WHERE "GUID" IS NOT NULL') as $record) { |
110
|
|
|
if (!isset($ldapGroups[$record['GUID']])) { |
111
|
|
|
$group = Group::get()->byId($record['ID']); |
112
|
|
|
|
113
|
|
|
$this->log(sprintf( |
114
|
|
|
'Removing Group "%s" (GUID: %s) that no longer exists in LDAP.', |
115
|
|
|
$group->Title, |
116
|
|
|
$group->GUID |
117
|
|
|
)); |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
// Cascade into mappings, just to clean up behind ourselves. |
121
|
|
|
foreach ($group->LDAPGroupMappings() as $mapping) { |
122
|
|
|
$mapping->delete(); |
123
|
|
|
} |
124
|
|
|
$group->delete(); |
125
|
|
|
} catch (Exception $e) { |
126
|
|
|
$this->log($e->getMessage()); |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$deleted++; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$end = time() - $start; |
136
|
|
|
|
137
|
|
|
$this->log(sprintf( |
138
|
|
|
'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds', |
139
|
|
|
$created, |
140
|
|
|
$updated, |
141
|
|
|
$deleted, |
142
|
|
|
round($end, 0) |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sends a message, formatted either for the CLI or browser |
148
|
|
|
* |
149
|
|
|
* @param string $message |
150
|
|
|
*/ |
151
|
|
|
protected function log($message) |
152
|
|
|
{ |
153
|
|
|
$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message; |
154
|
|
|
echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.