1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SilverStripe\SecurityExtensions\Extension; |
6
|
|
|
|
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\ORM\DataExtension; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
11
|
|
|
use SilverStripe\Security\Member; |
12
|
|
|
use SilverStripe\Security\Security; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Extend Member to add relationship to registered methods and track some specific preferences |
16
|
|
|
* |
17
|
|
|
* @property Member|MemberExtension owner |
18
|
|
|
*/ |
19
|
|
|
class MemberExtension extends DataExtension |
20
|
|
|
{ |
21
|
|
|
public function updateCMSFields(FieldList $fields) |
22
|
|
|
{ |
23
|
|
|
$currentUser = Security::getCurrentUser(); |
24
|
|
|
|
25
|
|
|
// We can allow an admin to require a user to change their password however. But: |
26
|
|
|
// - Don't show a read only field if the user cannot edit this record |
27
|
|
|
// - Don't show if a user views their own profile (just let them reset their own password) |
28
|
|
|
if ($currentUser && ($currentUser->ID !== $this->owner->ID) && $this->owner->canEdit()) { |
|
|
|
|
29
|
|
|
$requireNewPassword = CheckboxField::create( |
30
|
|
|
'RequiresPasswordChangeOnNextLogin', |
31
|
|
|
_t(__CLASS__ . '.RequiresPasswordChangeOnNextLogin', 'Requires password change on next log in') |
32
|
|
|
); |
33
|
|
|
$fields->insertAfter('Password', $requireNewPassword); |
34
|
|
|
|
35
|
|
|
$fields->dataFieldByName('Password')->addExtraClass('form-field--no-divider mb-0 pb-0'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $fields; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getRequiresPasswordChangeOnNextLogin() |
42
|
|
|
{ |
43
|
|
|
return $this->owner->isPasswordExpired(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set password expiry to now to enforce a change of password next log in |
48
|
|
|
* |
49
|
|
|
* @param int|null $dataValue boolean representation checked/not checked {@see CheckboxField::dataValue} |
50
|
|
|
* @return Member |
51
|
|
|
*/ |
52
|
|
|
public function saveRequiresPasswordChangeOnNextLogin($dataValue) |
53
|
|
|
{ |
54
|
|
|
$member = $this->owner; |
55
|
|
|
|
56
|
|
|
if (!$member->canEdit()) { |
|
|
|
|
57
|
|
|
return $member; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$currentValue = $member->PasswordExpiry; |
61
|
|
|
$currentDate = $member->dbObject('PasswordExpiry'); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if ($dataValue && (!$currentValue || $currentDate->inFuture())) { |
|
|
|
|
64
|
|
|
// Only alter future expiries - this way an admin could see how long ago a password expired still |
65
|
|
|
$member->PasswordExpiry = DBDatetime::now()->Rfc2822(); |
|
|
|
|
66
|
|
|
} elseif (!$dataValue && $member->isPasswordExpired()) { |
|
|
|
|
67
|
|
|
// Only unset if the expiry date is in the past |
68
|
|
|
$member->PasswordExpiry = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $member; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|