MemberExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequiresPasswordChangeOnNextLogin() 0 3 1
A updateCMSFields() 0 18 4
B saveRequiresPasswordChangeOnNextLogin() 0 20 7
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 property ID does not exist on SilverStripe\SecurityExt...tension\MemberExtension. Did you maybe forget to declare it?
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

28
        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...
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
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

43
        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...
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
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

56
        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...
57
            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...
58
        }
59
60
        $currentValue = $member->PasswordExpiry;
61
        $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

61
        /** @scrutinizer ignore-call */ 
62
        $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...
62
63
        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...
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
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...
66
        } 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...
67
            // Only unset if the expiry date is in the past
68
            $member->PasswordExpiry = null;
69
        }
70
71
        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...
72
    }
73
}
74