Completed
Pull Request — master (#12)
by Simon
01:14
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 self $form
71
     * @return HTTPResponse
72
     */
73
    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...
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
            $request = $this->getRequest();
81
            $request->getSession()->set(PasswordForm::PASSWORD_SESSION_KEY, true);
82
        }
83
84
        $token = $partial->Token;
85
        $key = $partial->generateKey($token);
0 ignored issues
show
Bug introduced by
It seems like $token defined by $partial->Token on line 84 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...
86
87
        return $this->redirect(sprintf('/%s/%s/%s', 'partial', $key, $token));
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function getPartialFormSubmission()
94
    {
95
        return $this->partialFormSubmission;
96
    }
97
98
    /**
99
     * @param mixed $partialFormSubmission
100
     */
101
    public function setPartialFormSubmission($partialFormSubmission): void
102
    {
103
        $this->partialFormSubmission = $partialFormSubmission;
104
    }
105
}
106