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

setPartialFormSubmission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace Firesphere\PartialUserforms\Controllers;
5
6
use Firesphere\PartialUserforms\Forms\PasswordForm;
7
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
8
use Page;
9
use PageController;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Control\HTTPResponse_Exception;
12
use SilverStripe\UserForms\Model\UserDefinedForm;
13
14
/**
15
 * Class \Firesphere\PartialUserforms\Controllers\PartialUserFormVerifyController
16
 *
17
 */
18
class PartialUserFormVerifyController extends PageController
19
{
20
21
    /**
22
     * @var array
23
     */
24
    private static $allowed_actions = [
1 ignored issue
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
25
        'getForm'
26
    ];
27
    /**
28
     * @var PartialFormSubmission
29
     */
30
    protected $partialFormSubmission;
31
32
    /**
33
     * @var PasswordForm
34
     */
35
    protected $form;
36
37
    /**
38
     * @return PartialUserFormVerifyController|void
39
     * @throws HTTPResponse_Exception
40
     */
41
    public function init()
42
    {
43
        parent::init();
44
        $session = $this->getRequest()->getSession();
45
        // Set the session if the last session has expired
46
        if (!$session->get(PartialSubmissionController::SESSION_KEY)) {
47
            return $this->httpError(404);
48
        }
49
50
        /** @var PartialFormSubmission $partial */
51
        $partial = PartialFormSubmission::get()->byID($session->get(PartialSubmissionController::SESSION_KEY));
52
53
        $this->setPartialFormSubmission($partial);
54
        // Set data record and load the form
55
        /** @var UserDefinedForm dataRecord */
56
        $this->dataRecord = Page::create();
57
    }
58
59
    /**
60
     * @return PasswordForm
61
     */
62
    public function getForm()
63
    {
64
        return PasswordForm::create($this, __FUNCTION__);
65
    }
66
67
68
    /**
69
     * @param array $data
70
     * @param PasswordForm $form
71
     * @return HTTPResponse
72
     */
73
    public function doValidate($data, $form)
74
    {
75
        /** @var PartialFormSubmission $partial */
76
        $partial = $this->getPartialFormSubmission();
77
78
        $password = hash_pbkdf2('SHA256', $data['Password'], $partial->TokenSalt, 1000);
79
        if (!hash_equals($password, $partial->Password)) {
80
            $form->sessionError(
81
                _t(
82
                    PasswordForm::class . '.PASSWORDERROR',
83
                    'Password incorrect, please check your password and try again'
84
                )
85
            );
86
87
            return $this->redirectBack();
88
        }
89
90
        $request = $this->getRequest();
91
        $request->getSession()->set(PasswordForm::PASSWORD_SESSION_KEY, $partial->ID);
92
93
        $token = $partial->Token;
94
        $key = $partial->generateKey($token);
0 ignored issues
show
Bug introduced by
It seems like $token defined by $partial->Token on line 93 can also be of type boolean; however, Firesphere\PartialUserfo...bmission::generateKey() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95
96
        return $this->redirect(sprintf('/%s/%s/%s', 'partial', $key, $token));
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getPartialFormSubmission()
103
    {
104
        return $this->partialFormSubmission;
105
    }
106
107
    /**
108
     * @param mixed $partialFormSubmission
109
     */
110
    public function setPartialFormSubmission($partialFormSubmission): void
111
    {
112
        $this->partialFormSubmission = $partialFormSubmission;
113
    }
114
}
115