AdminController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 16
eloc 70
dl 0
loc 140
rs 10
c 2
b 2
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getLockscreen() 0 12 2
A getForgot() 0 7 2
A getLogin() 0 8 2
A postForgot() 0 25 2
A postUnlockScreen() 0 12 2
A postLogin() 0 43 4
A getIndex() 0 6 1
A getLogout() 0 9 1
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);
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'));
26
        }
27
28
        Session::put('admin_lock', 1);
29
30
        return view('crudbooster::lockscreen');
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();
38
39
        if (\Hash::check($password, $users->password)) {
40
            Session::put('admin_lock', 0);
41
42
            return redirect(CRUDBooster::adminPath());
43
        } else {
44
            echo "<script>alert('".trans('crudbooster.alert_password_wrong')."');history.go(-1);</script>";
0 ignored issues
show
Bug introduced by
Are you sure trans('crudbooster.alert_password_wrong') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

44
            echo "<script>alert('"./** @scrutinizer ignore-type */ 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());
53
        }
54
55
        return view('crudbooster::login');
56
    }
57
58
    public function postLogin()
59
    {
60
61
        $validator = Validator::make(Request::all(), [
62
            'email' => 'required|email|exists:'.config('crudbooster.USER_TABLE'),
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']);
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');
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')]));
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());
108
        }
109
110
        return view('crudbooster::forgot');
111
    }
112
113
    public function postForgot()
114
    {
115
        $validator = Validator::make(Request::all(), [
116
            'email' => 'required|email|exists:'.config('crudbooster.USER_TABLE'),
117
        ]);
118
119
        if ($validator->fails()) {
120
            $message = $validator->errors()->all();
121
122
            return redirect()->back()->with(['message' => implode(', ', $message), 'message_type' => 'danger']);
123
        }
124
125
        $rand_string = str_random(5);
0 ignored issues
show
Deprecated Code introduced by
The function str_random() has been deprecated: Str::random() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

125
        $rand_string = /** @scrutinizer ignore-deprecated */ str_random(5);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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')]));
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]));
145
146
        Session::flush();
147
148
        return redirect()->route('getLogin')->with('message', trans("crudbooster.message_after_logout"));
149
    }
150
}
151