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