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

PartialUserFormVerifyController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getForm() 0 4 1
A init() 0 18 2
A doValidate() 0 22 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
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
        $sessionKey = PartialSubmissionController::SESSION_KEY;
46
        // Set the session if the last session has expired
47
        if (!$session->get($sessionKey)) {
48
            return $this->httpError(404);
49
        }
50
51
        /** @var PartialFormSubmission $partial */
52
        $partial = PartialFormSubmission::get()->byID($session->get($sessionKey));
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 PasswordForm $form
72
     * @return HTTPResponse
73
     * @throws \Exception
74
     */
75
    public function doValidate($data, $form)
76
    {
77
        /** @var PartialFormSubmission $partial */
78
        $partial = $this->getPartialFormSubmission();
79
80
        $password = hash_pbkdf2('SHA256', $data['Password'], $partial->TokenSalt, 1000);
81
        if (!hash_equals($password, $partial->Password)) {
82
            $form->sessionError(
83
                _t(
84
                    PasswordForm::class . '.PASSWORDERROR',
85
                    'Password incorrect, please check your password and try again'
86
                )
87
            );
88
89
            return $this->redirectBack();
90
        }
91
92
        $request = $this->getRequest();
93
        $request->getSession()->set(PasswordForm::PASSWORD_SESSION_KEY, $partial->ID);
94
95
        return $this->redirect($partial->getPartialLink());
96
    }
97
98
    /**
99
     * @return PartialFormSubmission
100
     */
101
    public function getPartialFormSubmission()
102
    {
103
        return $this->partialFormSubmission;
104
    }
105
106
    /**
107
     * @param PartialFormSubmission $partialFormSubmission
108
     */
109
    public function setPartialFormSubmission($partialFormSubmission): void
110
    {
111
        $this->partialFormSubmission = $partialFormSubmission;
112
    }
113
}
114