Password::resetPassword()   B
last analyzed

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