Passed
Push — master ( 284e72...816a06 )
by Iman
04:12
created

AuthController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getForgot() 0 7 2
A getLogin() 0 7 2
A postForgot() 0 15 1
A table() 0 4 2
A postUnlockScreen() 0 10 2
A getLockscreen() 0 9 2
A getIndex() 0 3 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;
12
13
class AuthController extends Controller
14
{
15
    /**
16
     * @param string $tableName
17
     * @return mixed
18
     */
19
    public function table($tableName = null)
20
    {
21
        $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...
22
        return \DB::table($tableName);
23
    }
24
25
    function getIndex()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        return view('CbAuth::home', ['page_title' => '<strong>Dashboard</strong>']);
28
    }
29
30
    public function getLockscreen()
31
    {
32
        if (! CRUDBooster::myId()) {
33
            Session::flush();
34
            return redirect()->route('getLogin')->with('message', cbTrans('alert_session_expired'));
35
        }
36
37
        Session::put('admin_lock', 1);
38
        return view('CbAuth::lockscreen');
39
    }
40
41
    public function postUnlockScreen()
42
    {
43
        $user = CbUsersRepo::find(CRUDBooster::myId());
44
45
        if (\Hash::check(request('password'), $user->password)) {
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::check() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        if (\Hash::check(/** @scrutinizer ignore-type */ request('password'), $user->password)) {
Loading history...
46
            Session::put('admin_lock', 0);
47
48
            return redirect()->route('AuthControllerGetIndex');
49
        }
50
        echo "<script>alert('".cbTrans('alert_password_wrong')."');history.go(-1);</script>";
51
    }
52
53
    public function getLogin()
54
    {
55
        if (CRUDBooster::myId()) {
56
            return redirect(cbAdminPath());
57
        }
58
59
        return view('CbAuth::login');
60
    }
61
62
    public function getForgot()
63
    {
64
        if (CRUDBooster::myId()) {
65
            return redirect()->action('\\'.AuthController::class.'@getIndex');
66
        }
67
68
        return view('CbAuth::forgot');
69
    }
70
71
    public function postForgot()
72
    {
73
        $this->validateForgotPass();
74
75
        $randString = str_random(5);
76
        CbUsersRepo::updateByMail(request('email'), ['password' => \Hash::make($randString)]);
77
78
        //$appname = cbGetsetting('appname');
79
        $user = CbUsersRepo::findByMail(request('email'));
80
        $user->password = $randString;
81
        (new Mailer())->send(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']);
82
83
        CRUDBooster::insertLog(cbTrans('log_forgot', ['email' => request('email'), 'ip' => Request::server('REMOTE_ADDR')]));
84
85
        return redirect()->route('getLogin')->with('message', cbTrans('message_forgot_password'));
86
    }
87
88
    public function getLogout()
89
    {
90
        CRUDBooster::insertLog(cbTrans('log_logout', ['email' => CRUDBooster::me()->email]));
91
        Session::flush();
92
93
        return redirect()->route('getLogin')->with('message', cbTrans('message_after_logout'));
94
    }
95
96
    private function validateForgotPass()
97
    {
98
        $validator = Validator::make(request()->all(), ['email' => 'required|email|exists:cms_users',]);
99
100
        if ($validator->fails()) {
101
            $message = $validator->errors()->all();
102
            backWithMsg(implode(', ', $message), 'danger');
103
        }
104
    }
105
}
106