Completed
Pull Request — master (#12)
by Simon
01:30
created

PasswordForm::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\PartialUserforms\Forms;
4
5
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Forms\PasswordField;
11
use SilverStripe\Forms\RequiredFields;
12
use SilverStripe\Forms\Validator;
13
use SilverStripe\UserForms\Control\UserDefinedFormController;
14
15
class PasswordForm extends Form
16
{
17
18
    public const PASSWORD_SESSION_KEY = 'PartialFormSession';
19
20
    /**
21
     * PasswordForm constructor.
22
     * @param UserDefinedFormController|null $controller
23
     * @param string $name
24
     * @param FieldList|null $fields
25
     * @param FieldList|null $actions
26
     * @param Validator|null $validator
27
     */
28
    public function __construct(
29
        UserDefinedFormController $controller = null,
30
        $name = self::DEFAULT_NAME,
31
        FieldList $fields = null,
32
        FieldList $actions = null,
33
        Validator $validator = null
34
    ) {
35
        if (!$fields) {
36
            $fields = $this->getFields();
37
        }
38
        if (!$actions) {
39
            $actions = $this->getActions();
40
        }
41
        if (!$validator) {
42
            $validator = $this->getFormValidator();
43
        }
44
        parent::__construct($controller, $name, $fields, $actions, $validator);
45
    }
46
47
    /**
48
     * @return FieldList
49
     */
50
    public function getFields()
51
    {
52
        return FieldList::create([
53
            PasswordField::create('Password', _t(__CLASS__ . '.PasswordField', 'Password'))
54
        ]);
55
    }
56
57
    /**
58
     * @return FieldList
59
     */
60
    public function getActions()
61
    {
62
        return FieldList::create([
63
            FormAction::create(['doValidate', _t(__CLASS__ . '.Validate', 'Submit')])
64
        ]);
65
    }
66
67
    public function getFormValidator()
68
    {
69
        return RequiredFields::create(['Password']);
70
    }
71
72
    /**
73
     * @param array $data
74
     * @param $this $form
75
     * @return HTTPResponse
76
     */
77
    public function doValidate($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        $password = hash('SHA256', $data['Password']);
80
81
        /** @var PartialFormSubmission $partialForm */
82
        $partialForm = $this->controller->getPartialForm();
83
84
        if (hash_equals($partialForm->Password, $password)) {
85
            $request = $this->controller->getRequest();
86
            $request->getSession()->set(
87
                static::PASSWORD_SESSION_KEY,
88
                [
89
                    $request->param('ID'),
90
                    $request->param('OtherID')
91
                ]
92
            );
93
        }
94
95
        return $this->controller->redirectBack();
96
    }
97
}
98