Passed
Pull Request — master (#57)
by Stone
03:27
created

Password::resetPassword()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 17
nop 0
dl 0
loc 45
rs 8.5386
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers;
4
5
use App\Models\UserModel;
6
use Core\BlogocException;
7
use Core\Controller;
8
use Core\Container;
9
use Core\Traits\PasswordFunctions;
10
use Core\Traits\StringFunctions;
11
12
class Password extends Controller
13
{
14
15
    use PasswordFunctions;
16
    use StringFunctions;
1 ignored issue
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Password.
Loading history...
17
18
    protected $siteConfig;
19
    protected $sendMail;
20
21
    private $userModel;
22
23
    public function __construct(Container $container)
24
    {
25
        $this->loadModules[] = 'SiteConfig';
26
        $this->loadModules[] = 'SendMail';
27
        parent::__construct($container);
28
29
        $this->userModel = new UserModel($this->container);
30
    }
31
32
    public function index()
33
    {
34
        if ($this->session->isParamSet("user")) {
35
            //we are already connected, redirect
36
            $this->response->redirect();
37
        }
38
39
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
40
        $this->data['navigation'] = $this->siteConfig->getMenu();
41
42
        $this->renderView('forgotPassword');
43
    }
44
45
    public function reset($get)
46
    {
47
        //only get messages here
48
        if(!$this->startsWith(strtolower($get),"get"))
49
        {
50
            throw new \Exception("invalid call");
51
        }
52
53
        //grab the token and ID
54
        $token = $this->request->getData("token");
55
        $userId = $this->request->getData("userId");
56
57
        //verify if token is valid
58
        if(!$this->isHexa($token)|| !$this->isInt($userId))
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type null; however, parameter $string of App\Controllers\Password::isHexa() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        if(!$this->isHexa(/** @scrutinizer ignore-type */ $token)|| !$this->isInt($userId))
Loading history...
59
        {
60
            $this->alertBox->setAlert('Badly formatted Token', 'error');
61
            $this->response->redirect();
62
        }
63
        if(!$this->userModel->getUserDetailsByToken($token, $userId))
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type null; however, parameter $token of App\Models\UserModel::getUserDetailsByToken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        if(!$this->userModel->getUserDetailsByToken(/** @scrutinizer ignore-type */ $token, $userId))
Loading history...
Bug introduced by
It seems like $userId can also be of type null; however, parameter $userId of App\Models\UserModel::getUserDetailsByToken() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        if(!$this->userModel->getUserDetailsByToken($token, /** @scrutinizer ignore-type */ $userId))
Loading history...
64
        {
65
            $this->alertBox->setAlert('Invalid reset token, please request a new password', 'error');
66
            $this->response->redirect();
67
        }
68
69
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
70
        $this->data['navigation'] = $this->siteConfig->getMenu();
71
72
        $this->data["token"] = $token;
73
        $this->data["userId"] = $userId;
74
        $this->renderView('resetPassword');
75
    }
76
77
    public function resetPassword()
78
    {
79
        $this->onlyPost();
80
        $request = $this->request->getDataFull();
81
        $password = $request["forgotPassword"];
82
        $confirm = $request["forgotConfirm"];
83
        $token = $request["token"];
84
        $userId = $request["userId"];
85
86
        if(!$this->isHexa($token) || !$this->isInt($userId) )
87
        {
88
            throw new \Exception("Bad Token or ID request");
89
        }
90
91
        $error = false;
92
        $registerErrors = new \stdClass();
93
        if($password !== $confirm)
94
        {
95
            $error = true;
96
            $registerErrors->forgotPassword = "password and confirmation do not match";
97
            $registerErrors->forgotConfirm = "password and confirmation do not match";
98
        }
99
100
        $passwordError = $this->isPasswordComplex($password);
101
        if (!$passwordError["success"]) {
102
            $error = true;
103
            $registerErrors->forgotPassword = $passwordError["message"];
104
        }
105
106
        if ($error) {
107
            $this->session->set("registrationErrors", $registerErrors);
108
            $this->response->redirect('/password/reset/get?token='.$token);
109
        }
110
111
        $user = $this->userModel->getUserDetailsByToken($token, $userId);
112
        if (!$user) {
113
114
            $this->alertBox->setAlert('Invalid reset token', 'error');
115
            $this->response->redirect();
116
        }
117
118
        $this->userModel->resetPassword($user->idusers, $password);
119
120
        $this->alertBox->setAlert('Password reset, please login');
121
        $this->response->redirect("/login");
122
123
    }
124
125
    /**
126
     * @throws \Exception
127
     */
128
    public function sendResetMail()
129
    {
130
        $this->onlyPost();
131
        $request = $this->request->getDataFull();
132
        $email = $request["forgotEmail"];
133
134
        $error = false;
135
        $registerErrors = new \stdClass();
136
        $user = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
137
138
        try {
139
            $user = $this->userModel->getUserDetailsByEmail($email);
140
            if (!$user) {
141
                $error = true;
142
                $registerErrors->forgotEmail = "email not found";
143
            }
144
        } catch (BlogocException $e) {
145
            $error = true;
146
            $registerErrors->forgotEmail = $e->getMessage();
147
        }
148
149
        if ($error) {
150
            $this->session->set("registrationInfo", $request);
151
            $this->session->set("registrationErrors", $registerErrors);
152
            $this->response->redirect('/password');
153
        }
154
155
        $token = $this->userModel->generatePasswordHash($user->idusers);
156
        $this->sendMail->sendResetPasswordMail($email, $token, $user->idusers);
157
158
        $this->alertBox->setAlert('Password reset link sent to your mailbox');
159
        $this->response->redirect();
160
    }
161
162
163
}