1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ActiveDirectory\Authenticators; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Control\Email\Email; |
7
|
|
|
use SilverStripe\Control\Session; |
8
|
|
|
use SilverStripe\Core\Config\Config; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\Security\Authenticator; |
12
|
|
|
use SilverStripe\Security\Member; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LDAPAuthenticator |
16
|
|
|
* |
17
|
|
|
* Authenticate a user against LDAP, without the single sign-on component. |
18
|
|
|
* |
19
|
|
|
* See SAMLAuthenticator for further information. |
20
|
|
|
* |
21
|
|
|
* @package activedirectory |
22
|
|
|
*/ |
23
|
|
|
class LDAPAuthenticator extends Authenticator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $name = 'LDAP'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set to 'yes' to indicate if this module should look up usernames in LDAP by matching the email addresses. |
32
|
|
|
* |
33
|
|
|
* CAVEAT #1: only set to 'yes' for systems that enforce email uniqueness. |
34
|
|
|
* Otherwise only the first LDAP user with matching email will be accessible. |
35
|
|
|
* |
36
|
|
|
* CAVEAT #2: this is untested for systems that use LDAP with principal style usernames (i.e. [email protected]). |
37
|
|
|
* The system will misunderstand emails for usernames with uncertain outcome. |
38
|
|
|
* |
39
|
|
|
* @var string 'no' or 'yes' |
40
|
|
|
*/ |
41
|
|
|
private static $allow_email_login = 'no'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set to 'yes' to fallback login attempts to {@link $fallback_authenticator}. |
45
|
|
|
* This will occur if LDAP fails to authenticate the user. |
46
|
|
|
* |
47
|
|
|
* @var string 'no' or 'yes' |
48
|
|
|
*/ |
49
|
|
|
private static $fallback_authenticator = 'no'; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The class of {@link Authenticator} to use as the fallback authenticator. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private static $fallback_authenticator_class = 'SilverStripe\\Security\\MemberAuthenticator'; |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public static function get_name() |
62
|
|
|
{ |
63
|
|
|
return Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'name'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Controller $controller |
68
|
|
|
* @return LDAPLoginForm |
69
|
|
|
*/ |
70
|
|
|
public static function get_login_form(Controller $controller) |
71
|
|
|
{ |
72
|
|
|
return new LDAPLoginForm($controller, 'LoginForm'); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Performs the login, but will also create and sync the Member record on-the-fly, if not found. |
77
|
|
|
* |
78
|
|
|
* @param array $data |
79
|
|
|
* @param Form $form |
80
|
|
|
* @return bool|Member|void |
81
|
|
|
* @throws HTTPResponse_Exception |
82
|
|
|
*/ |
83
|
|
|
public static function authenticate($data, Form $form = null) |
84
|
|
|
{ |
85
|
|
|
$service = Injector::inst()->get('SilverStripe\\ActiveDirectory\\Services\\LDAPService'); |
86
|
|
|
$login = trim($data['Login']); |
87
|
|
|
if (Email::is_valid_address($login)) { |
88
|
|
|
if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'allow_email_login') != 'yes') { |
89
|
|
|
$form->sessionMessage( |
|
|
|
|
90
|
|
|
_t( |
91
|
|
|
'LDAPAuthenticator.PLEASEUSEUSERNAME', |
92
|
|
|
'Please enter your username instead of your email to log in.' |
93
|
|
|
), |
94
|
|
|
'bad' |
95
|
|
|
); |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$username = $service->getUsernameByEmail($login); |
100
|
|
|
|
101
|
|
|
// No user found with this email. |
102
|
|
View Code Duplication |
if (!$username) { |
|
|
|
|
103
|
|
|
if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'fallback_authenticator') === 'yes') { |
104
|
|
|
$fallbackMember = self::fallback_authenticate($data, $form); |
105
|
|
|
if ($fallbackMember) { |
106
|
|
|
return $fallbackMember; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$form->sessionMessage(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials'), 'bad'); |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$username = $login; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$result = $service->authenticate($username, $data['Password']); |
118
|
|
|
$success = $result['success'] === true; |
119
|
|
View Code Duplication |
if (!$success) { |
|
|
|
|
120
|
|
|
if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'fallback_authenticator') === 'yes') { |
121
|
|
|
$fallbackMember = self::fallback_authenticate($data, $form); |
122
|
|
|
if ($fallbackMember) { |
123
|
|
|
return $fallbackMember; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($form) { |
128
|
|
|
$form->sessionMessage($result['message'], 'bad'); |
129
|
|
|
} |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$data = $service->getUserByUsername($result['identity']); |
134
|
|
|
if (!$data) { |
135
|
|
|
if ($form) { |
136
|
|
|
$form->sessionMessage( |
137
|
|
|
_t('LDAPAuthenticator.PROBLEMFINDINGDATA', 'There was a problem retrieving your user data'), |
138
|
|
|
'bad' |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields |
145
|
|
|
$member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
146
|
|
View Code Duplication |
if (!($member && $member->exists())) { |
|
|
|
|
147
|
|
|
$member = new Member(); |
148
|
|
|
$member->GUID = $data['objectguid']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Update the users from LDAP so we are sure that the email is correct. |
152
|
|
|
// This will also write the Member record. |
153
|
|
|
$service->updateMemberFromLDAP($member); |
154
|
|
|
|
155
|
|
|
Session::clear('BackURL'); |
156
|
|
|
|
157
|
|
|
return $member; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Try to authenticate using the fallback authenticator. |
162
|
|
|
* |
163
|
|
|
* @param array $data |
164
|
|
|
* @param null|Form $form |
165
|
|
|
* @return null|Member |
166
|
|
|
*/ |
167
|
|
|
protected static function fallback_authenticate($data, Form $form = null) |
168
|
|
|
{ |
169
|
|
|
return call_user_func( |
170
|
|
|
[ |
171
|
|
|
Config::inst()->get( |
172
|
|
|
'SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', |
173
|
|
|
'fallback_authenticator_class' |
174
|
|
|
), |
175
|
|
|
'authenticate' |
176
|
|
|
], |
177
|
|
|
array_merge($data, ['Email' => $data['Login']]), |
178
|
|
|
$form |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.