Passed
Pull Request — master (#1713)
by Darko
07:01
created

ForgotPasswordController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use Illuminate\Routing\Controllers\HasMiddleware;
6
use Illuminate\Routing\Controllers\Middleware;
7
use App\Http\Controllers\Controller;
8
use App\Jobs\SendPasswordForgottenEmail;
9
use App\Models\User;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Validator;
12
use Illuminate\Support\Str;
13
14
class ForgotPasswordController extends Controller implements HasMiddleware
15
{
16
    public static function middleware(): array
17
    {
18
        return [
19
            'guest',
20
        ];
21
    }
22
23
    /**
24
     * @throws \Exception
25
     */
26
    public function showLinkRequestForm(Request $request): void
27
    {
28
        $sent = '';
29
        $email = $request->input('email') ?? '';
30
        $rssToken = $request->input('apikey') ?? '';
31
        if (empty($email) && empty($rssToken)) {
32
            app('smarty.view')->assign('error', 'Missing parameter (email and/or apikey) to send password reset');
33
        } else {
34
            if (config('captcha.enabled') === true && (! empty(config('captcha.secret')) && ! empty(config('captcha.sitekey')))) {
35
                $validate = Validator::make($request->all(), [
36
                    'g-recaptcha-response' => 'required|captcha',
37
                ]);
38
                if ($validate->fails()) {
39
                    app('smarty.view')->assign('error', 'Captcha validation failed.');
40
                }
41
            }
42
            //
43
            // Check users exists and send an email
44
            //
45
            $ret = ! empty($rssToken) ? User::getByRssToken($rssToken) : User::getByEmail($email);
46
            if ($ret === null) {
47
                app('smarty.view')->assign('error', 'The email or apikey are not recognised.');
48
            } else {
49
                //
50
                // Generate a forgottenpassword guid, store it in the user table
51
                //
52
                $guid = Str::random(32);
53
                User::updatePassResetGuid($ret['id'], $guid);
54
                //
55
                // Send the email
56
                //
57
                $resetLink = url('/').'/resetpassword?guid='.$guid;
58
                SendPasswordForgottenEmail::dispatch($ret, $resetLink);
59
                app('smarty.view')->assign('success', 'Password reset email has been sent!');
60
            }
61
            $sent = true;
62
        }
63
64
        $theme = 'Gentele';
65
66
        $title = 'Forgotten Password';
67
        $meta_title = 'Forgotten Password';
68
        $meta_keywords = 'forgotten,password,signup,registration';
69
        $meta_description = 'Forgotten Password';
70
71
        $content = app('smarty.view')->fetch($theme.'/forgottenpassword.tpl');
72
73
        app('smarty.view')->assign(
74
            [
75
                'content' => $content,
76
                'title' => $title,
77
                'meta_title' => $meta_title,
78
                'meta_keywords' => $meta_keywords,
79
                'meta_description' => $meta_description,
80
                'email' => $email,
81
                'apikey' => $rssToken,
82
                'sent' => $sent,
83
            ]
84
        );
85
        app('smarty.view')->display($theme.'/basepage.tpl');
86
    }
87
}
88