Completed
Push — master ( 0b9e95...0208b2 )
by Damian
42s queued 20s
created

ChangePasswordForm::getFormFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security\MemberAuthenticator;
4
5
use SilverStripe\Control\RequestHandler;
6
use SilverStripe\Control\Session;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Forms\FormField;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Forms\PasswordField;
13
use SilverStripe\Security\Security;
14
15
/**
16
 * Standard Change Password Form
17
 */
18
class ChangePasswordForm extends Form
19
{
20
    /**
21
     * Constructor
22
     *
23
     * @param RequestHandler $controller The parent controller, necessary to create the appropriate form action tag.
24
     * @param string $name The method on the controller that will return this form object.
25
     * @param FieldList|FormField $fields All of the fields in the form - a {@link FieldList} of
26
     * {@link FormField} objects.
27
     * @param FieldList|FormAction $actions All of the action buttons in the form - a {@link FieldList} of
28
     */
29
    public function __construct($controller, $name, $fields = null, $actions = null)
30
    {
31
        $backURL = $controller->getBackURL()
32
            ?: $controller->getRequest()->getSession()->get('BackURL');
33
34
        if (!$fields) {
35
            $fields = $this->getFormFields();
36
        }
37
        if (!$actions) {
38
            $actions = $this->getFormActions();
39
        }
40
41
        if ($backURL) {
42
            $fields->push(HiddenField::create('BackURL', false, $backURL));
0 ignored issues
show
Bug introduced by
The method push() does not exist on SilverStripe\Forms\FormField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            $fields->/** @scrutinizer ignore-call */ 
43
                     push(HiddenField::create('BackURL', false, $backURL));
Loading history...
43
        }
44
45
        parent::__construct($controller, $name, $fields, $actions);
0 ignored issues
show
Bug introduced by
It seems like $fields can also be of type SilverStripe\Forms\FormField; however, parameter $fields of SilverStripe\Forms\Form::__construct() does only seem to accept null|SilverStripe\Forms\FieldList, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        parent::__construct($controller, $name, /** @scrutinizer ignore-type */ $fields, $actions);
Loading history...
Bug introduced by
It seems like $actions can also be of type SilverStripe\Forms\FormAction; however, parameter $actions of SilverStripe\Forms\Form::__construct() does only seem to accept null|SilverStripe\Forms\FieldList, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        parent::__construct($controller, $name, $fields, /** @scrutinizer ignore-type */ $actions);
Loading history...
46
    }
47
48
    /**
49
     * @return FieldList
50
     */
51
    protected function getFormFields()
52
    {
53
        $fields = FieldList::create();
54
55
        // Security/changepassword?h=XXX redirects to Security/changepassword
56
        // without GET parameter to avoid potential HTTP referer leakage.
57
        // In this case, a user is not logged in, and no 'old password' should be necessary.
58
        if (Security::getCurrentUser()) {
59
            $fields->push(PasswordField::create(
60
                'OldPassword',
61
                _t('SilverStripe\\Security\\Member.YOUROLDPASSWORD', 'Your old password')
62
            ));
63
        }
64
65
        $fields->push(PasswordField::create(
66
            'NewPassword1',
67
            _t('SilverStripe\\Security\\Member.NEWPASSWORD', 'New Password')
68
        ));
69
        $fields->push(PasswordField::create(
70
            'NewPassword2',
71
            _t('SilverStripe\\Security\\Member.CONFIRMNEWPASSWORD', 'Confirm New Password')
72
        ));
73
74
        return $fields;
75
    }
76
77
    /**
78
     * @return FieldList
79
     */
80
    protected function getFormActions()
81
    {
82
        $actions = FieldList::create(
83
            FormAction::create(
84
                'doChangePassword',
85
                _t('SilverStripe\\Security\\Member.BUTTONCHANGEPASSWORD', 'Change Password')
86
            )
87
        );
88
89
        return $actions;
90
    }
91
}
92