Passed
Push — master ( 41c09c...5f5681 )
by Iman
06:15
created

AuthController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A table() 0 4 2
A __construct() 0 3 1
A getForgot() 0 7 2
A getLogin() 0 7 2
A postForgot() 0 15 1
A validateForgotPass() 0 7 2
A getLogout() 0 6 1
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\helpers\Mailer;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Session;
10
use Illuminate\Support\Facades\Validator;
11
use crocodicstudio\crudbooster\helpers\CRUDBooster, CB;
0 ignored issues
show
Bug introduced by
The type CB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class AuthController extends Controller
14
{
15
    /**
16
     * @var \crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo
17
     */
18
    private $usersRepo;
19
20
    /**
21
     * AuthController constructor.
22
     *
23
     * @param \crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo $usersRepo
24
     */
25
    public function __construct(CbUsersRepo $usersRepo)
26
    {
27
        $this->usersRepo = $usersRepo;
28
    }
29
30
    /**
31
     * @param string $tableName
32
     * @return mixed
33
     */
34
    public function table($tableName = null)
35
    {
36
        $tableName = $tableName ?: $this->table;
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on crocodicstudio\crudboost...thModule\AuthController. Did you maybe forget to declare it?
Loading history...
37
        return \DB::table($tableName);
38
    }
39
40
    public function getLogin()
41
    {
42
        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...
43
            return redirect(cbAdminPath());
44
        }
45
46
        return view('CbAuth::login');
47
    }
48
49
    public function getForgot()
50
    {
51
        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...
52
            return redirect()->route('CbDashboard');
53
        }
54
55
        return view('CbAuth::forgot');
56
    }
57
58
    public function postForgot()
59
    {
60
        $this->validateForgotPass();
61
62
        $randString = str_random(5);
63
        $this->usersRepo->updateByMail(request('email'), ['password' => \Hash::make($randString)]);
64
65
        //$appname = cbGetsetting('appname');
66
        $user = $this->usersRepo->findByMail(request('email'));
67
        $user->password = $randString;
68
        (new Mailer())->send(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']);
69
70
        CRUDBooster::insertLog(trans('crudbooster_logging.log_forgot', ['email' => request('email'), 'ip' => Request::server('REMOTE_ADDR')]));
71
72
        return redirect()->route('getLogin')->with('message', cbTrans('message_forgot_password'));
73
    }
74
75
    public function getLogout()
76
    {
77
        CRUDBooster::insertLog(trans('crudbooster_logging.log_logout', ['email' => auth('cbAdmin')->user()->email]));
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
        auth('cbAdmin')->logout();
79
80
        return redirect()->route('getLogin')->with('message', cbTrans('message_after_logout'));
81
    }
82
83
    private function validateForgotPass()
84
    {
85
        $validator = Validator::make(request()->all(), ['email' => 'required|email|exists:cms_users',]);
86
87
        if ($validator->fails()) {
88
            $message = $validator->errors()->all();
89
            backWithMsg(implode(', ', $message), 'danger');
90
        }
91
    }
92
}
93