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()) { |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() The call to
SilverStripe\ORM\DataExtension::canEdit() has too few arguments starting with member .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
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(); |
||||||
0 ignored issues
–
show
The method
isPasswordExpired() does not exist on SilverStripe\SecurityExt...tension\MemberExtension .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
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()) { |
||||||
0 ignored issues
–
show
The expression
$member->canEdit() of type boolean|null is loosely compared to false ; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.
If an expression can have both $a = canBeFalseAndNull();
// Instead of
if ( ! $a) { }
// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
![]() The call to
SilverStripe\ORM\DataExtension::canEdit() has too few arguments starting with member .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
57 | return $member; |
||||||
0 ignored issues
–
show
|
|||||||
58 | } |
||||||
59 | |||||||
60 | $currentValue = $member->PasswordExpiry; |
||||||
61 | $currentDate = $member->dbObject('PasswordExpiry'); |
||||||
0 ignored issues
–
show
The method
dbObject() does not exist on SilverStripe\SecurityExt...tension\MemberExtension .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
62 | |||||||
63 | if ($dataValue && (!$currentValue || $currentDate->inFuture())) { |
||||||
0 ignored issues
–
show
The expression
$dataValue of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||||
64 | // Only alter future expiries - this way an admin could see how long ago a password expired still |
||||||
65 | $member->PasswordExpiry = DBDatetime::now()->Rfc2822(); |
||||||
0 ignored issues
–
show
|
|||||||
66 | } elseif (!$dataValue && $member->isPasswordExpired()) { |
||||||
0 ignored issues
–
show
The expression
$dataValue of type integer|null is loosely compared to false ; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||||
67 | // Only unset if the expiry date is in the past |
||||||
68 | $member->PasswordExpiry = null; |
||||||
69 | } |
||||||
70 | |||||||
71 | return $member; |
||||||
0 ignored issues
–
show
|
|||||||
72 | } |
||||||
73 | } |
||||||
74 |