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