Passed
Push — master ( 70d075...35e9ea )
by Ferry
08:21 queued 12s
created

AdminController::postLogin()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 29
dl 0
loc 43
rs 9.456
c 1
b 1
f 0
cc 4
nc 4
nop 0
1
<?php namespace crocodicstudio\crudbooster\controllers;
2
3
use CRUDBooster;
0 ignored issues
show
Bug introduced by
The type CRUDBooster 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...
4
use Illuminate\Support\Facades\DB;
5
use Illuminate\Support\Facades\Request;
6
use Illuminate\Support\Facades\Session;
7
use Illuminate\Support\Facades\Validator;
8
9
class AdminController extends CBController
10
{
11
    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...
12
    {
13
        $data = [];
14
        $data['page_title'] = '<strong>Dashboard</strong>';
15
16
        return view('crudbooster::home', $data);
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

16
        return /** @scrutinizer ignore-call */ view('crudbooster::home', $data);
Loading history...
17
    }
18
19
    public function getLockscreen()
20
    {
21
22
        if (! CRUDBooster::myId()) {
23
            Session::flush();
24
25
            return redirect()->route('getLogin')->with('message', trans('crudbooster.alert_session_expired'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

25
            return /** @scrutinizer ignore-call */ redirect()->route('getLogin')->with('message', trans('crudbooster.alert_session_expired'));
Loading history...
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

25
            return redirect()->route('getLogin')->with('message', /** @scrutinizer ignore-call */ trans('crudbooster.alert_session_expired'));
Loading history...
26
        }
27
28
        Session::put('admin_lock', 1);
29
30
        return view('crudbooster::lockscreen');
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

30
        return /** @scrutinizer ignore-call */ view('crudbooster::lockscreen');
Loading history...
31
    }
32
33
    public function postUnlockScreen()
34
    {
35
        $id = CRUDBooster::myId();
36
        $password = Request::input('password');
37
        $users = DB::table(config('crudbooster.USER_TABLE'))->where('id', $id)->first();
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

37
        $users = DB::table(/** @scrutinizer ignore-call */ config('crudbooster.USER_TABLE'))->where('id', $id)->first();
Loading history...
38
39
        if (\Hash::check($password, $users->password)) {
40
            Session::put('admin_lock', 0);
41
42
            return redirect(CRUDBooster::adminPath());
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

42
            return /** @scrutinizer ignore-call */ redirect(CRUDBooster::adminPath());
Loading history...
43
        } else {
44
            echo "<script>alert('".trans('crudbooster.alert_password_wrong')."');history.go(-1);</script>";
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
            echo "<script>alert('"./** @scrutinizer ignore-call */ trans('crudbooster.alert_password_wrong')."');history.go(-1);</script>";
Loading history...
45
        }
46
    }
47
48
    public function getLogin()
49
    {
50
51
        if (CRUDBooster::myId()) {
52
            return redirect(CRUDBooster::adminPath());
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
            return /** @scrutinizer ignore-call */ redirect(CRUDBooster::adminPath());
Loading history...
53
        }
54
55
        return view('crudbooster::login');
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

55
        return /** @scrutinizer ignore-call */ view('crudbooster::login');
Loading history...
56
    }
57
58
    public function postLogin()
59
    {
60
61
        $validator = Validator::make(Request::all(), [
62
            'email' => 'required|email|exists:'.config('crudbooster.USER_TABLE'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
            'email' => 'required|email|exists:'./** @scrutinizer ignore-call */ config('crudbooster.USER_TABLE'),
Loading history...
63
            'password' => 'required',
64
        ]);
65
66
        if ($validator->fails()) {
67
            $message = $validator->errors()->all();
68
69
            return redirect()->back()->with(['message' => implode(', ', $message), 'message_type' => 'danger']);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

69
            return /** @scrutinizer ignore-call */ redirect()->back()->with(['message' => implode(', ', $message), 'message_type' => 'danger']);
Loading history...
70
        }
71
72
        $email = Request::input("email");
73
        $password = Request::input("password");
74
        $users = DB::table(config('crudbooster.USER_TABLE'))->where("email", $email)->first();
75
76
        if (\Hash::check($password, $users->password)) {
77
            $priv = DB::table("cms_privileges")->where("id", $users->id_cms_privileges)->first();
78
79
            $roles = DB::table('cms_privileges_roles')->where('id_cms_privileges', $users->id_cms_privileges)->join('cms_moduls', 'cms_moduls.id', '=', 'id_cms_moduls')->select('cms_moduls.name', 'cms_moduls.path', 'is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete')->get();
80
81
            $photo = ($users->photo) ? asset($users->photo) : asset('vendor/crudbooster/avatar.jpg');
0 ignored issues
show
Bug introduced by
The function asset was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

81
            $photo = ($users->photo) ? /** @scrutinizer ignore-call */ asset($users->photo) : asset('vendor/crudbooster/avatar.jpg');
Loading history...
82
            Session::put('admin_id', $users->id);
83
            Session::put('admin_is_superadmin', $priv->is_superadmin);
84
            Session::put('admin_name', $users->name);
85
            Session::put('admin_photo', $photo);
86
            Session::put('admin_privileges_roles', $roles);
87
            Session::put("admin_privileges", $users->id_cms_privileges);
88
            Session::put('admin_privileges_name', $priv->name);
89
            Session::put('admin_lock', 0);
90
            Session::put('theme_color', $priv->theme_color);
91
            Session::put("appname", CRUDBooster::getSetting('appname'));
92
93
            CRUDBooster::insertLog(trans("crudbooster.log_login", ['email' => $users->email, 'ip' => Request::server('REMOTE_ADDR')]));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

93
            CRUDBooster::insertLog(/** @scrutinizer ignore-call */ trans("crudbooster.log_login", ['email' => $users->email, 'ip' => Request::server('REMOTE_ADDR')]));
Loading history...
94
95
            $cb_hook_session = new \App\Http\Controllers\CBHook;
96
            $cb_hook_session->afterLogin();
97
98
            return redirect(CRUDBooster::adminPath());
99
        } else {
100
            return redirect()->route('getLogin')->with('message', trans('crudbooster.alert_password_wrong'));
101
        }
102
    }
103
104
    public function getForgot()
105
    {
106
        if (CRUDBooster::myId()) {
107
            return redirect(CRUDBooster::adminPath());
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

107
            return /** @scrutinizer ignore-call */ redirect(CRUDBooster::adminPath());
Loading history...
108
        }
109
110
        return view('crudbooster::forgot');
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

110
        return /** @scrutinizer ignore-call */ view('crudbooster::forgot');
Loading history...
111
    }
112
113
    public function postForgot()
114
    {
115
        $validator = Validator::make(Request::all(), [
116
            'email' => 'required|email|exists:'.config('crudbooster.USER_TABLE'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

116
            'email' => 'required|email|exists:'./** @scrutinizer ignore-call */ config('crudbooster.USER_TABLE'),
Loading history...
117
        ]);
118
119
        if ($validator->fails()) {
120
            $message = $validator->errors()->all();
121
122
            return redirect()->back()->with(['message' => implode(', ', $message), 'message_type' => 'danger']);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

122
            return /** @scrutinizer ignore-call */ redirect()->back()->with(['message' => implode(', ', $message), 'message_type' => 'danger']);
Loading history...
123
        }
124
125
        $rand_string = str_random(5);
126
        $password = \Hash::make($rand_string);
127
128
        DB::table(config('crudbooster.USER_TABLE'))->where('email', Request::input('email'))->update(['password' => $password]);
129
130
        $appname = CRUDBooster::getSetting('appname');
0 ignored issues
show
Unused Code introduced by
The assignment to $appname is dead and can be removed.
Loading history...
131
        $user = CRUDBooster::first(config('crudbooster.USER_TABLE'), ['email' => g('email')]);
132
        $user->password = $rand_string;
133
        CRUDBooster::sendEmail(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']);
134
135
        CRUDBooster::insertLog(trans("crudbooster.log_forgot", ['email' => g('email'), 'ip' => Request::server('REMOTE_ADDR')]));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

135
        CRUDBooster::insertLog(/** @scrutinizer ignore-call */ trans("crudbooster.log_forgot", ['email' => g('email'), 'ip' => Request::server('REMOTE_ADDR')]));
Loading history...
136
137
        return redirect()->route('getLogin')->with('message', trans("crudbooster.message_forgot_password"));
138
    }
139
140
    public function getLogout()
141
    {
142
143
        $me = CRUDBooster::me();
144
        CRUDBooster::insertLog(trans("crudbooster.log_logout", ['email' => $me->email]));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

144
        CRUDBooster::insertLog(/** @scrutinizer ignore-call */ trans("crudbooster.log_logout", ['email' => $me->email]));
Loading history...
145
146
        Session::flush();
147
148
        return redirect()->route('getLogin')->with('message', trans("crudbooster.message_after_logout"));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

148
        return /** @scrutinizer ignore-call */ redirect()->route('getLogin')->with('message', trans("crudbooster.message_after_logout"));
Loading history...
149
    }
150
}
151