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

PasswordForm::getActions()   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
    public const PASSWORD_SESSION_KEY = 'PartialFormSession';
18
19
    /**
20
     * PasswordForm constructor.
21
     * @param UserDefinedFormController|null $controller
22
     * @param string $name
23
     * @param FieldList|null $fields
24
     * @param FieldList|null $actions
25
     * @param Validator|null $validator
26
     */
27
    public function __construct(
28
        UserDefinedFormController $controller = null,
29
        $name = self::DEFAULT_NAME,
30
        FieldList $fields = null,
31
        FieldList $actions = null,
32
        Validator $validator = null
33
    ) {
34
        if (!$fields) {
35
            $fields = $this->getFields();
36
        }
37
        if (!$actions) {
38
            $actions = $this->getActions();
39
        }
40
        if (!$validator) {
41
            $validator = $this->getFormValidator();
42
        }
43
        parent::__construct($controller, $name, $fields, $actions, $validator);
44
    }
45
46
    /**
47
     * @return FieldList
48
     */
49
    protected function getFields()
50
    {
51
        return FieldList::create([
52
            PasswordField::create('Password', _t(__CLASS__ . '.PasswordField', 'Password'))
53
        ]);
54
    }
55
56
    /**
57
     * @return FieldList
58
     */
59
    protected function getActions()
60
    {
61
        return FieldList::create([
62
            FormAction::create('doValidate', _t(__CLASS__ . '.Validate', 'Submit'))
63
        ]);
64
    }
65
66
    public function getFormValidator()
67
    {
68
        return RequiredFields::create(['Password']);
69
    }
70
71
    /**
72
     * @param array $data
73
     * @param self $form
74
     * @return HTTPResponse
75
     */
76
    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...
77
    {
78
79
        /** @var PartialFormSubmission $partialForm */
80
        $partialForm = $this->controller->getPartialForm();
81
82
        $password = hash_pbkdf2('SHA256', $data['Password'], $partialForm->TokenSalt, 1000);
83
        if (hash_equals($partialForm, $password)) {
84
            $request = $this->controller->getRequest();
85
            $request->getSession()->set(
86
                static::PASSWORD_SESSION_KEY,
87
                [
88
                    $request->param('ID'),
89
                    $request->param('OtherID')
90
                ]
91
            );
92
        }
93
94
        return $this->controller->redirectBack();
95
    }
96
}
97