Passed
Branch master (acb862)
by Simon
03:16
created

MemberExtension::updateCMSFields()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 8
nop 1
dl 0
loc 31
rs 9.2222
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
    /**
30
     * @param FieldList $fields
31
     */
32
    public function updateCMSFields(FieldList $fields)
33
    {
34
        // PwndDisabled always needs to be false
35
        $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...
36
37
        $fields->removeByName(['BreachedSites', 'PasswordIsPwnd']);
38
        if ($this->owner->BreachedSites || $this->owner->PasswordIsPwnd) {
39
            $fields->findOrMakeTab('Root.HaveIBeenPwned', _t(self::class . '.PWNDTAB', 'Have I Been Pwnd?'));
40
        }
41
        if ($this->owner->PasswordIsPwnd > 0 || $this->owner->BreachedSites) {
42
            $text = _t(
43
                self::class . '.PWNDHelp',
44
                '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. ' .
45
                'Therefore, we can not accept your password, because it is insecure or known to have been breached. ' .
46
                'Before a password is safely stored in our database, we test if the password has been breached. We do not share your password. ' .
47
                'We run a safe test against the HaveIBeenPwned database to. None of your data is shared or stored at HaveIBeenPwned. ' .
48
                'For more information, you can read up on "Password safety", and we strongly recommend installing a password manager if you haven\'t already. ' .
49
                'Several options are LastPass, BitWarden and 1Password. These services are also able to test your passwords against the HaveIBeenPwned database, ' .
50
                'to see if your passwords are secure and safe.<br />' .
51
                '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>.'
52
            );
53
54
            $help = LiteralField::create('Helptext', '<p>' . $text . '</p>');
55
            $fields->addFieldToTab('Root.HaveIBeenPwned', $help);
56
        }
57
58
        if ($this->owner->BreachedSites) {
59
            $fields->addFieldToTab(
60
                'Root.HaveIBeenPwned',
61
                ReadonlyField::create('BreachedSites', _t(self::class . '.BREACHEDSITES', 'Breached sites'))
62
            );
63
        }
64
65
        $fields->addFieldToTab('Root.Main', $countField = ReadonlyField::create('PasswordIsPwnd', 'Pwnd Count'));
66
        $countField->setDescription(_t(
67
            self::class . '.AMOUNT',
68
            'Amount of times the password appears in the Have I Been Pwnd database'
69
        ));
70
71
        $fields->addFieldToTab('Root.Main', $tmpDisable = CheckboxField::create(
72
            'PwndDisabled',
73
            _t(self::class . '.TMPDISABLE', 'Disable "Have I Been Pwnd" temporarily')
74
        ));
75
        $tmpDisable->setDescription(_t(
76
            self::class . '.TMPDISABLEDESCR',
77
            '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.'
78
        ));
79
    }
80
}
81