|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ActiveDirectory\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\ActiveDirectory\Authenticators\LDAPAuthenticator; |
|
7
|
|
|
use SilverStripe\ActiveDirectory\Services\LDAPService; |
|
8
|
|
|
use SilverStripe\Control\Director; |
|
9
|
|
|
use SilverStripe\Control\HTTP; |
|
10
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
11
|
|
|
use SilverStripe\Core\Config\Config; |
|
12
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
13
|
|
|
use SilverStripe\Forms\FieldList; |
|
14
|
|
|
use SilverStripe\Forms\TextField; |
|
15
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
16
|
|
|
use SilverStripe\Security\Member; |
|
17
|
|
|
use SilverStripe\Security\MemberAuthenticator\ChangePasswordForm; |
|
18
|
|
|
use SilverStripe\Security\Security; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @package activedirectory |
|
22
|
|
|
*/ |
|
23
|
|
|
class LDAPChangePasswordForm extends ChangePasswordForm |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The sole purpose for overriding the constructor is surfacing the username to the user. |
|
27
|
|
|
* @param \SilverStripe\Control\RequestHandler $controller |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* @param FieldList $fields |
|
30
|
|
|
* @param FieldList $actions |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($controller, $name, $fields = null, $actions = null) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
|
35
|
|
|
|
|
36
|
|
|
// Obtain the Member object. If the user got this far, they must have already been synced. |
|
37
|
|
|
$member = Security::getCurrentUser(); |
|
38
|
|
|
if (!$member) { |
|
39
|
|
|
if ($this->getSession()->get('AutoLoginHash')) { |
|
40
|
|
|
$member = Member::member_from_autologinhash($this->getSession()->get('AutoLoginHash')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// The user is not logged in and no valid auto login hash is available |
|
44
|
|
|
if (!$member) { |
|
45
|
|
|
$this->getSession()->clear('AutoLoginHash'); |
|
46
|
|
|
return $this->controller->redirect($this->controller->Link('login')); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$data = Injector::inst() |
|
51
|
|
|
->get(LDAPService::class) |
|
52
|
|
|
->getUserByGUID($member->GUID, ['samaccountname']); |
|
53
|
|
|
|
|
54
|
|
|
$emailField = null; |
|
55
|
|
|
$usernameField = null; |
|
56
|
|
|
if (Config::inst()->get( |
|
57
|
|
|
LDAPAuthenticator::class, |
|
58
|
|
|
'allow_email_login' |
|
59
|
|
|
) === 'yes' |
|
60
|
|
|
&& !empty($member->Email) |
|
61
|
|
|
) { |
|
62
|
|
|
$emailField = TextField::create( |
|
63
|
|
|
'Email', |
|
64
|
|
|
_t(__CLASS__ . '.USERNAMEOREMAIL', 'Email'), |
|
65
|
|
|
$member->Email, |
|
66
|
|
|
null, |
|
67
|
|
|
$this |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
if (!empty($data['samaccountname'])) { |
|
71
|
|
|
$usernameField = TextField::create( |
|
72
|
|
|
'Username', |
|
73
|
|
|
_t(__CLASS__ . '.USERNAME', 'Username'), |
|
74
|
|
|
$data['samaccountname'], |
|
75
|
|
|
null, |
|
76
|
|
|
$this |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($emailField) { |
|
81
|
|
|
$emailFieldReadonly = $emailField->performDisabledTransformation(); |
|
82
|
|
|
$this->Fields()->unshift($emailFieldReadonly); |
|
83
|
|
|
} |
|
84
|
|
|
if ($usernameField) { |
|
85
|
|
|
$usernameFieldReadonly = $usernameField->performDisabledTransformation(); |
|
86
|
|
|
$this->Fields()->unshift($usernameFieldReadonly); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|