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

PartialUserFormVerifyController::getForm()   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 0
1
<?php
2
3
4
namespace Firesphere\PartialUserforms\Controllers;
5
6
7
use Firesphere\PartialUserforms\Forms\PasswordForm;
8
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
9
use Page;
10
use PageController;
11
use SilverStripe\Control\HTTPResponse;
12
use SilverStripe\Control\HTTPResponse_Exception;
13
use SilverStripe\UserForms\Model\UserDefinedForm;
14
15
/**
16
 * Class \Firesphere\PartialUserforms\Controllers\PartialUserFormVerifyController
17
 *
18
 */
19
class PartialUserFormVerifyController extends PageController
20
{
21
22
    /**
23
     * @var array
24
     */
25
    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...
26
        'getForm'
27
    ];
28
    /**
29
     * @var PartialFormSubmission
30
     */
31
    protected $partialFormSubmission;
32
33
    /**
34
     * @var PasswordForm
35
     */
36
    protected $form;
37
38
    /**
39
     * @return PartialUserFormVerifyController|void
40
     * @throws HTTPResponse_Exception
41
     */
42
    public function init()
43
    {
44
        parent::init();
45
        $session = $this->getRequest()->getSession();
46
        // Set the session if the last session has expired
47
        if (!$session->get(PartialSubmissionController::SESSION_KEY)) {
48
            return $this->httpError(404);
49
        }
50
51
        /** @var PartialFormSubmission $partial */
52
        $partial = PartialFormSubmission::get()->byID($session->get(PartialSubmissionController::SESSION_KEY));
53
54
        $this->setPartialFormSubmission($partial);
55
        // Set data record and load the form
56
        /** @var UserDefinedForm dataRecord */
57
        $this->dataRecord = Page::create();
58
    }
59
60
    /**
61
     * @return PasswordForm
62
     */
63
    public function getForm()
64
    {
65
        return PasswordForm::create($this, __FUNCTION__);
66
    }
67
68
69
    /**
70
     * @param array $data
71
     * @param self $form
72
     * @return HTTPResponse
73
     */
74
    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...
75
    {
76
        /** @var PartialFormSubmission $partial */
77
        $partial = $this->getPartialFormSubmission();
78
79
        $password = hash_pbkdf2('SHA256', $data['Password'], $partial->TokenSalt, 1000);
80
        if (hash_equals($password, $partial->Password)) {
81
            $request = $this->getRequest();
82
            $request->getSession()->set(PasswordForm::PASSWORD_SESSION_KEY, true);
83
        }
84
85
        $token = $partial->Token;
86
        $key = $partial->generateKey($token);
0 ignored issues
show
Bug introduced by
It seems like $token defined by $partial->Token on line 85 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...
87
88
        return $this->redirect(sprintf('/%s/%s/%s', 'partial', $key, $token));
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    public function getPartialFormSubmission()
95
    {
96
        return $this->partialFormSubmission;
97
    }
98
99
    /**
100
     * @param mixed $partialFormSubmission
101
     */
102
    public function setPartialFormSubmission($partialFormSubmission): void
103
    {
104
        $this->partialFormSubmission = $partialFormSubmission;
105
    }
106
107
}
108