Passed
Push — master ( 284933...d63a45 )
by Iman
08:48
created

ForgetPasswordController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Modules\AuthModule;
4
5
use Crocodicstudio\Crudbooster\CBCoreModule\CbUsersRepo;
6
use Crocodicstudio\Crudbooster\controllers\Controller;
7
use Crocodicstudio\Crudbooster\Modules\EmailTemplates\Mailer;
8
9
class ForgetPasswordController extends Controller
10
{
11
    private $usersRepo;
12
13
    public function __construct(CbUsersRepo $usersRepo)
14
    {
15
        $this->usersRepo = $usersRepo;
16
    }
17
18
    public function getForgot()
19
    {
20
        if (auth('cbAdmin')->id()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression auth('cbAdmin')->id() of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
21
            return redirect()->route('CbDashboard');
22
        }
23
24
        return view('CbAuth::forgot');
25
    }
26
27
    public function postForgot()
28
    {
29
        $this->validateForgotPass();
30
31
        $randString = str_random(5);
32
        $this->usersRepo->updateByMail(request('email'), ['password' => bcrypt($randString)]);
33
34
        //$appname = cbGetsetting('appname');
35
        $user = $this->usersRepo->findByMail(request('email'));
36
        $user->password = $randString;
37
        (new Mailer())->send(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']);
38
39
        event('cb.forgottenPasswordRequested', [request('email'), request()->ip()]);
40
        return redirect()->route('getLogin')->with('message', cbTrans('message_forgot_password'));
41
    }
42
43
    private function validateForgotPass()
44
    {
45
        $validator = \Validator::make(request()->all(), ['email' => 'required|email|exists:cms_users',]);
46
47
        if ($validator->fails()) {
48
            $message = $validator->errors()->all();
49
            backWithMsg(implode(', ', $message), 'danger');
50
        }
51
    }
52
}
53