Passed
Pull Request — master (#6)
by
unknown
03:54
created

saveRequirePasswordChangeOnNextLogin()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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()) {
0 ignored issues
show
Bug 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 ignore-call  annotation

26
        if ($currentUser && ($currentUser->ID !== $this->owner->ID) && $this->owner->/** @scrutinizer ignore-call */ canEdit()) {

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.

Loading history...
Bug Best Practice introduced by
The property ID does not exist on SilverStripe\SecurityExt...tension\MemberExtension. Did you maybe forget to declare it?
Loading history...
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();
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

41
        return $this->owner->/** @scrutinizer ignore-call */ isPasswordExpired();

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.

Loading history...
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()) {
0 ignored issues
show
Bug Best Practice introduced by
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 false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
Bug 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 ignore-call  annotation

54
        if (!$member->/** @scrutinizer ignore-call */ canEdit()) {

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.

Loading history...
55
            return $member;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $member also could return the type SilverStripe\SecurityExt...tension\MemberExtension which is incompatible with the documented return type SilverStripe\Security\Member.
Loading history...
56
        }
57
58
        $currentValue = $member->PasswordExpiry;
59
        $currentDate = $member->dbObject('PasswordExpiry');
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

59
        /** @scrutinizer ignore-call */ 
60
        $currentDate = $member->dbObject('PasswordExpiry');

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.

Loading history...
60
61
        if ($dataValue && (!$currentValue || $currentDate->inFuture())) {
0 ignored issues
show
Bug Best Practice introduced by
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 ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

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
Loading history...
62
            // Only alter future expiries - this way an admin could see how long ago a password expired still
63
            $member->PasswordExpiry = DBDatetime::now()->Rfc2822();
0 ignored issues
show
Bug Best Practice introduced by
The property PasswordExpiry does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
        } elseif (!$dataValue && $member->isPasswordExpired()) {
0 ignored issues
show
Bug Best Practice introduced by
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 ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

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
Loading history...
65
            // Only unset if the expiry date is in the past
66
            $member->PasswordExpiry = null;
67
        }
68
69
        return $member;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $member also could return the type SilverStripe\SecurityExt...tension\MemberExtension which is incompatible with the documented return type SilverStripe\Security\Member.
Loading history...
70
    }
71
}
72