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

PartialUserFormVerifyController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 98
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 18 2
A getForm() 0 4 1
A doValidate() 0 23 2
A getPartialFormSubmission() 0 4 1
A setPartialFormSubmission() 0 4 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
    public const PASSWORD_KEY = 'FormPassword';
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
        $sessionKey = PartialSubmissionController::SESSION_KEY;
47
        // Set the session if the last session has expired
48
        if (!$session->get($sessionKey)) {
49
            return $this->httpError(404);
50
        }
51
52
        /** @var PartialFormSubmission $partial */
53
        $partial = PartialFormSubmission::get()->byID($session->get($sessionKey));
54
55
        $this->setPartialFormSubmission($partial);
56
        // Set data record and load the form
57
        /** @var UserDefinedForm dataRecord */
58
        $this->dataRecord = Page::create();
59
    }
60
61
    /**
62
     * @return PasswordForm
63
     */
64
    public function getForm()
65
    {
66
        return PasswordForm::create($this, __FUNCTION__);
67
    }
68
69
70
    /**
71
     * @param array $data
72
     * @param PasswordForm $form
73
     * @return HTTPResponse
74
     * @throws \Exception
75
     */
76
    public function doValidate($data, $form)
77
    {
78
        /** @var PartialFormSubmission $partial */
79
        $partial = $this->getPartialFormSubmission();
80
81
        $password = hash_pbkdf2('SHA256', $data['Password'], $partial->TokenSalt, 1000);
0 ignored issues
show
Unused Code introduced by
$password is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
        if (false) {//!hash_equals($password, $partial->Password)) {
83
            $form->sessionError(
84
                _t(
85
                    PasswordForm::class . '.PASSWORDERROR',
86
                    'Password incorrect, please check your password and try again'
87
                )
88
            );
89
90
            return $this->redirectBack();
91
        }
92
93
        $request = $this->getRequest();
94
        $request->getSession()->set(PasswordForm::PASSWORD_SESSION_KEY, $partial->ID);
95
        $request->getSession()->set(self::PASSWORD_KEY, $data['Password']);
96
97
        return $this->redirect($partial->getPartialLink());
98
    }
99
100
    /**
101
     * @return PartialFormSubmission
102
     */
103
    public function getPartialFormSubmission()
104
    {
105
        return $this->partialFormSubmission;
106
    }
107
108
    /**
109
     * @param PartialFormSubmission $partialFormSubmission
110
     */
111
    public function setPartialFormSubmission($partialFormSubmission): void
112
    {
113
        $this->partialFormSubmission = $partialFormSubmission;
114
    }
115
}
116