1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class LDAPMemberExtension. |
4
|
|
|
* |
5
|
|
|
* Adds mappings from AD attributes to SilverStripe {@link Member} fields. |
6
|
|
|
*/ |
7
|
|
|
class LDAPMemberExtension extends DataExtension |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private static $db = array( |
|
|
|
|
13
|
|
|
// Unique user identifier, same field is used by SAMLMemberExtension |
14
|
|
|
'GUID' => 'Varchar(50)', |
15
|
|
|
'Username' => 'Varchar(64)', |
16
|
|
|
'IsImportedFromLDAP' => 'Boolean', |
17
|
|
|
'IsExpired' => 'Boolean', |
18
|
|
|
'LastSynced' => 'SS_Datetime', |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* These fields are used by {@link LDAPMemberSync} to map specific AD attributes |
23
|
|
|
* to {@link Member} fields. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
* @config |
27
|
|
|
*/ |
28
|
|
|
private static $ldap_field_mappings = array( |
|
|
|
|
29
|
|
|
'givenname' => 'FirstName', |
30
|
|
|
'samaccountname' => 'Username', |
31
|
|
|
'sn' => 'Surname', |
32
|
|
|
'mail' => 'Email', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The location (relative to /assets) where to save thumbnailphoto data. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
* @config |
40
|
|
|
*/ |
41
|
|
|
private static $ldap_thumbnail_path = 'Uploads'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* When enabled, LDAP managed Member records (IsImportedFromLDAP flag) |
45
|
|
|
* have their data written back to LDAP on write. |
46
|
|
|
* |
47
|
|
|
* This requires setting write permissions on the user configured in the LDAP |
48
|
|
|
* credentials, which is why this is disabled by default. |
49
|
|
|
* |
50
|
|
|
* Note that some constants must be configured in your environment file: |
51
|
|
|
* LDAP_DOMAIN - the base DN of the directory. e.g. "DN=mydomain,DC=com" |
52
|
|
|
* |
53
|
|
|
* @var bool |
54
|
|
|
* @config |
55
|
|
|
*/ |
56
|
|
|
private static $update_ldap_from_local = false; |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* If enabled, Member records with a Username field have the user created in LDAP |
60
|
|
|
* on write. |
61
|
|
|
* |
62
|
|
|
* This requires setting write permissions on the user configured in the LDAP |
63
|
|
|
* credentials, which is why this is disabled by default. |
64
|
|
|
* |
65
|
|
|
* Note that some constants must be configured in your environment file: |
66
|
|
|
* LDAP_DOMAIN - the base DN of the directory. e.g. "DN=mydomain,DC=com" |
67
|
|
|
* LDAP_NEW_USERS_OBJECT_CATEGORY - the type of object. e.g. "CN=Person,CN=Schema,DC=mydomain,DC=com" |
68
|
|
|
* LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
69
|
|
|
* |
70
|
|
|
* @var bool |
71
|
|
|
* @config |
72
|
|
|
*/ |
73
|
|
|
private static $create_users_in_ldap = false; |
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
77
|
|
|
* |
78
|
|
|
* This requires setting write permissions on the user configured in the LDAP |
79
|
|
|
* credentials, which is why this is disabled by default. |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
* @config |
83
|
|
|
*/ |
84
|
|
|
private static $delete_users_in_ldap = false; |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param FieldList $fields |
88
|
|
|
*/ |
89
|
|
|
public function updateCMSFields(FieldList $fields) |
90
|
|
|
{ |
91
|
|
|
// Redo LDAP metadata fields as read-only and move to LDAP tab. |
92
|
|
|
$ldapMetadata = array(); |
93
|
|
|
$fields->replaceField('IsImportedFromLDAP', $ldapMetadata[] = new ReadonlyField( |
94
|
|
|
'IsImportedFromLDAP', |
95
|
|
|
_t('LDAPMemberExtension.ISIMPORTEDFROMLDAP', 'Is user imported from LDAP/AD?') |
96
|
|
|
)); |
97
|
|
|
$fields->replaceField('GUID', $ldapMetadata[] = new ReadonlyField('GUID')); |
98
|
|
|
$fields->replaceField('IsExpired', $ldapMetadata[] = new ReadonlyField( |
99
|
|
|
'IsExpired', |
100
|
|
|
_t('LDAPMemberExtension.ISEXPIRED', 'Has user\'s LDAP/AD login expired?')) |
101
|
|
|
); |
102
|
|
|
$fields->replaceField('LastSynced', $ldapMetadata[] = new ReadonlyField( |
103
|
|
|
'LastSynced', |
104
|
|
|
_t('LDAPMemberExtension.LASTSYNCED', 'Last synced')) |
105
|
|
|
); |
106
|
|
|
$fields->addFieldsToTab('Root.LDAP', $ldapMetadata); |
107
|
|
|
|
108
|
|
|
$message = null; |
109
|
|
|
if ($this->owner->IsImportedFromLDAP && $this->owner->config()->update_ldap_from_local) { |
110
|
|
|
$message = _t('LDAPMemberExtension.INFOIMPORTEDCANCHANGE', |
111
|
|
|
'This user is automatically imported from LDAP. '. |
112
|
|
|
'Changing fields here will update them in LDAP.' |
113
|
|
|
); |
114
|
|
|
} elseif ($this->owner->IsImportedFromLDAP && !$this->owner->config()->update_ldap_from_local) { |
115
|
|
|
// Transform the automatically mapped fields into read-only. This doesn't |
116
|
|
|
// apply if updating LDAP from local is enabled, as changing data locally can be written back. |
117
|
|
|
foreach ($this->owner->config()->ldap_field_mappings as $name) { |
118
|
|
|
$field = $fields->dataFieldByName($name); |
119
|
|
|
if (!empty($field)) { |
120
|
|
|
// Set to readonly, but not disabled so that the data is still sent to the |
121
|
|
|
// server and doesn't break Member_Validator |
122
|
|
|
$field->setReadonly(true); |
123
|
|
|
$field->setTitle($field->Title()._t('LDAPMemberExtension.IMPORTEDFIELD', ' (imported)')); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
$message = _t( |
127
|
|
|
'LDAPMemberExtension.INFOIMPORTED', |
128
|
|
|
'This user is automatically imported from LDAP. '. |
129
|
|
|
'Manual changes to imported fields will be removed upon sync.' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
if ($message) { |
|
|
|
|
133
|
|
|
$fields->addFieldToTab( |
134
|
|
|
'Root.Main', |
135
|
|
|
new LiteralField( |
136
|
|
|
'Info', |
137
|
|
|
sprintf('<p class="message warning">%s</p>', $message) |
138
|
|
|
), |
139
|
|
|
'FirstName' |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function validate(ValidationResult $validationResult) |
145
|
|
|
{ |
146
|
|
|
if (!$this->owner->config()->create_users_in_ldap) { |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// We allow empty Username for registration purposes, as we need to |
151
|
|
|
// create Member records with empty Username temporarily. Forms should explicitly |
152
|
|
|
// check for Username not being empty if they require it not to be. |
153
|
|
|
if (empty($this->owner->Username)) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!preg_match('/^[a-z0-9\.]+$/', $this->owner->Username)) { |
158
|
|
|
$validationResult->error( |
159
|
|
|
'Username must only contain lowercase alphanumeric characters and dots.', |
160
|
|
|
'bad' |
161
|
|
|
); |
162
|
|
|
throw new ValidationException($validationResult); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create the user in LDAP, provided this configuration is enabled |
168
|
|
|
* and a username was passed to a new Member record. |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function onBeforeWrite() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$service = Injector::inst()->get('LDAPService'); |
173
|
|
|
if (!$service->enabled()) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
if (!$this->owner->config()->create_users_in_ldap) { |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
if ($this->owner->Username && !$this->owner->IsImportedFromLDAP) { |
180
|
|
|
$service->createLDAPUser($this->owner); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Update the local data with LDAP, and ensure local membership is also set in |
186
|
|
|
* LDAP too. This writes into LDAP, provided that feature is enabled. |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function onAfterWrite() |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$service = Injector::inst()->get('LDAPService'); |
191
|
|
|
if (!$service->enabled()) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
if (!$this->owner->config()->update_ldap_from_local) { |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
if ($this->owner->IsImportedFromLDAP) { |
198
|
|
|
$service->updateLDAPFromMember($this->owner); |
199
|
|
|
$service->updateLDAPGroupsForMember($this->owner); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
View Code Duplication |
public function onAfterDelete() { |
|
|
|
|
204
|
|
|
$service = Injector::inst()->get('LDAPService'); |
205
|
|
|
if (!$service->enabled()) { |
206
|
|
|
return; |
207
|
|
|
} |
208
|
|
|
if (!$this->owner->config()->delete_users_in_ldap) { |
209
|
|
|
return; |
210
|
|
|
} |
211
|
|
|
if ($this->owner->IsImportedFromLDAP) { |
212
|
|
|
$service->deleteLDAPMember($this->owner); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Triggered by {@link Member::logIn()} when successfully logged in, |
218
|
|
|
* this will update the Member record from AD data. |
219
|
|
|
*/ |
220
|
|
|
public function memberLoggedIn() |
221
|
|
|
{ |
222
|
|
|
if ($this->owner->GUID) { |
223
|
|
|
Injector::inst()->get('LDAPService')->updateMemberFromLDAP($this->owner); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.