Passed
Push — master ( 8677c2...c8946b )
by Simon
01:42
created

MemberExtension::updateCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 24
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\HaveIBeenPwned\Extensions;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\LiteralField;
8
use SilverStripe\Forms\ReadonlyField;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\Security\Member;
11
12
/**
13
 * Class \Firesphere\HaveIBeenPwned\Extensions\MemberExtension
14
 *
15
 * @property Member|MemberExtension $owner
16
 * @property int $PasswordIsPwnd
17
 * @property string $BreachedSites
18
 */
19
class MemberExtension extends DataExtension
20
{
21
    /**
22
     * @var array
23
     */
24
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'PasswordIsPwnd' => 'Int',
26
        'BreachedSites'  => 'Text'
27
    ];
28
29
    protected $fallbackHelp = 'If the error says that you "have been Pwnd", it means your password appears in the <a href="https://haveibeenpwned.com/Privacy">Have I Been Pwnd</a> database. ' .
30
    'Therefore, we can not accept your password, because it is insecure or known to have been breached. ' .
31
    'Before a password is safely stored in our database, we test if the password has been breached. We do not share your password. ' .
32
    'We run a safe test against the HaveIBeenPwned database to. None of your data is shared or stored at HaveIBeenPwned. ' .
33
    'For more information, you can read up on "Password safety", and we strongly recommend installing a password manager if you haven\'t already. ' .
34
    'Several options are LastPass, BitWarden and 1Password. These services are also able to test your passwords against the HaveIBeenPwned database, ' .
35
    'to see if your passwords are secure and safe.<br />' .
36
    'Furthermore, <a href="https://www.troyhunt.com/introducing-306-million-freely-downloadable-pwned-passwords/">Troy Hunt explains why and how this service is important</a>.';
37
38
    /**
39
     * @param FieldList $fields
40
     */
41
    public function updateCMSFields(FieldList $fields)
42
    {
43
        // PwndDisabled always needs to be false
44
        $this->owner->PwndDisabled = false;
0 ignored issues
show
Bug Best Practice introduced by
The property PwndDisabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
46
        $fields->removeByName(['BreachedSites', 'PasswordIsPwnd']);
47
        $this->breachFound($fields);
48
49
        $this->breachedSites($fields);
50
51
        $fields->addFieldsToTab('Root.Main', [
52
            ReadonlyField::create(
53
                'PasswordIsPwnd',
54
                _t(self::class . '.PWNCOUNT', 'Pwnd Count')
55
            )->setDescription(_t(
56
                self::class . '.AMOUNT',
57
                'Amount of times the password appears in the Have I Been Pwnd database'
58
            )),
59
            CheckboxField::create(
60
                'PwndDisabled',
61
                _t(self::class . '.TMPDISABLE', 'Disable "Have I Been Pwnd" temporarily')
62
            )->setDescription(_t(
63
                self::class . '.TMPDISABLEDESCR',
64
                'Allow the password to be a compromised password once (only from the CMS), to reset a users password manually and let the user reset the password on first login.'
65
            ))
66
        ]);
67
    }
68
69
    /**
70
     * @param FieldList $fields
71
     */
72
    protected function breachFound(FieldList $fields)
73
    {
74
        if ($this->owner->BreachedSites || $this->owner->PasswordIsPwnd) {
75
            $fields->findOrMakeTab('Root.HaveIBeenPwned', _t(self::class . '.PWNDTAB', 'Have I Been Pwnd?'));
76
            $text = _t(
77
                self::class . '.PWNDHelp',
78
                $this->fallbackHelp
79
            );
80
81
            $help = LiteralField::create('Helptext', '<p>' . $text . '</p>');
82
            $fields->addFieldToTab('Root.HaveIBeenPwned', $help);
83
        }
84
    }
85
86
    /**
87
     * @param FieldList $fields
88
     */
89
    protected function breachedSites(FieldList $fields)
90
    {
91
        if ($this->owner->BreachedSites) {
92
            $fields->addFieldToTab(
93
                'Root.HaveIBeenPwned',
94
                ReadonlyField::create(
95
                    'BreachedSites',
96
                    _t(self::class . '.BREACHEDSITES', 'Known breaches')
97
                )->setDescription(_t(
98
                    self::class . '.BREACHEDDESCRIPTION',
99
                    'Sites on which your email address or username has been found in known breaches.'
100
                ))
101
            );
102
        }
103
    }
104
}
105