Passed
Push — Auth ( 26ff7a...6598c8 )
by Stone
01:58
created

Password::reset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 23
rs 9.9332
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
56
        //verify if token is valid
57
        if(!$this->userModel->getUserDetailsByToken($token))
58
        {
59
            $this->alertBox->setAlert('Invalid reset token, please request a new password', 'error');
60
            $this->response->redirect();
61
        }
62
63
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
64
        $this->data['navigation'] = $this->siteConfig->getMenu();
65
66
        $this->data["token"] = $token;
67
        $this->renderView('resetPassword');
68
    }
69
70
    public function resetPassword()
71
    {
72
        $this->onlyPost();
73
        $request = $this->request->getDataFull();
74
        $password = $request["forgotPassword"];
75
        $confirm = $request["forgotConfirm"];
76
        $token = $request["token"];
77
78
        $error = false;
79
        $registerErrors = new \stdClass();
80
        if($password !== $confirm)
81
        {
82
            $error = true;
83
            $registerErrors->forgotPassword = "password and confirmation do not match";
84
            $registerErrors->forgotConfirm = "password and confirmation do not match";
85
        }
86
87
        $passwordError = $this->isPasswordComplex($password);
88
        if (!$passwordError["success"]) {
89
            $error = true;
90
            $registerErrors->forgotPassword = $passwordError["message"];
91
        }
92
93
        if ($error) {
94
            $this->session->set("registrationErrors", $registerErrors);
95
            $this->response->redirect('/password/reset/get?token='.$token);
96
        }
97
98
        $user = $this->userModel->getUserDetailsByToken($token);
99
        if (!$user) {
100
101
            $this->alertBox->setAlert('Invalid reset token', 'error');
102
            $this->response->redirect();
103
        }
104
105
        $this->userModel->resetPassword($user->idusers, $password);
106
107
        $this->alertBox->setAlert('Password reset, please login');
108
        $this->response->redirect("/login");
109
110
    }
111
112
    /**
113
     * @throws \Exception
114
     */
115
    public function sendResetMail()
116
    {
117
        $this->onlyPost();
118
        $request = $this->request->getDataFull();
119
        $email = $request["forgotEmail"];
120
121
        $error = false;
122
        $registerErrors = new \stdClass();
123
        $user = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
124
125
        try {
126
            $user = $this->userModel->getUserDetailsByEmail($email);
127
            if (!$user) {
128
                $error = true;
129
                $registerErrors->forgotEmail = "email not found";
130
            }
131
        } catch (BlogocException $e) {
132
            $error = true;
133
            $registerErrors->forgotEmail = $e->getMessage();
134
        }
135
136
        if ($error) {
137
            $this->session->set("registrationInfo", $request);
138
            $this->session->set("registrationErrors", $registerErrors);
139
            $this->response->redirect('/password');
140
        }
141
142
        $token = $this->userModel->generatePasswordHash($user->idusers);
143
        $this->sendMail->sendResetPasswordMail($email, $token);
144
145
        $this->alertBox->setAlert('Password reset link sent to your mailbox');
146
        $this->response->redirect();
147
    }
148
149
150
}