GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UsersManagementController   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 251
rs 10
c 0
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 1
A index() 0 6 1
A edit() 0 16 2
A store() 0 47 3
A __construct() 0 3 1
A show() 0 5 1
B search() 0 35 3
B update() 0 39 5
A destroy() 0 15 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Profile;
6
use App\Models\User;
7
use App\Traits\CaptureIpTrait;
8
use Auth;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
use jeremykenedy\LaravelRoles\Models\Role;
12
use Validator;
13
14
class UsersManagementController extends Controller
15
{
16
    /**
17
     * Create a new controller instance.
18
     *
19
     * @return void
20
     */
21
    public function __construct()
22
    {
23
        $this->middleware('auth');
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index()
32
    {
33
        $users = User::paginate(config('settings.userListPaginationSize'));
34
        $roles = Role::all();
35
36
        return View('usersmanagement.show-users', compact('users', 'roles'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return View('usersmanage...pact('users', 'roles')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
37
    }
38
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function create()
45
    {
46
        $roles = Role::all();
47
48
        $data = [
49
            'roles' => $roles,
50
        ];
51
52
        return view('usersmanagement.create-user')->with($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('usersmanage...ate-user')->with($data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param \Illuminate\Http\Request $request
59
     *
60
     * @return \Illuminate\Http\Response
61
     */
62
    public function store(Request $request)
63
    {
64
        $validator = Validator::make($request->all(),
65
            [
66
                'scout_name'            => 'max:255',
67
                'first_name'            => 'required|max:255',
68
                'last_name'             => 'required|max:255',
69
                'password'              => 'required|min:6|max:20|confirmed',
70
                'password_confirmation' => 'required|same:password',
71
                'role'                  => 'required',
72
            ],
73
            [
74
                'scout_name.unique'         => trans('auth.userNameTaken'),
75
                'scout_name.required'       => trans('auth.userNameRequired'),
76
                'first_name.required'       => trans('auth.fNameRequired'),
77
                'last_name.required'        => trans('auth.lNameRequired'),
78
                'password.required'         => trans('auth.passwordRequired'),
79
                'password.min'              => trans('auth.PasswordMin'),
80
                'password.max'              => trans('auth.PasswordMax'),
81
                'role.required'             => trans('auth.roleRequired'),
82
            ]
83
        );
84
85
        if ($validator->fails()) {
86
            return back()->withErrors($validator)->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->withError...validator)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
87
        }
88
89
        $ipAddress = new CaptureIpTrait();
90
        $profile = new Profile();
91
        $name_gen = (($request->input('scout_name') != null) ? $request->input('first_name').'_'.$request->input('scout_name').'_'.$request->input('last_name') : $request->input('first_name').'_'.$request->input('last_name'));
92
93
        $user = User::create([
94
            'scout_name'             => $request->input('scout_name'),
95
            'first_name'             => $request->input('first_name'),
96
            'last_name'              => $request->input('last_name'),
97
            'name_gen'               => $name_gen,
98
            'password'               => bcrypt($request->input('password')),
99
            'token'                  => str_random(64),
100
            'admin_ip_address'       => $ipAddress->getClientIp(),
101
            'activated'              => 1,
102
        ]);
103
104
        $user->profile()->save($profile);
105
        $user->attachRole($request->input('role'));
106
        $user->save();
107
108
        return redirect('users')->with('success', trans('usersmanagement.createSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('users')...gement.createSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
109
    }
110
111
    /**
112
     * Display the specified resource.
113
     *
114
     * @param int $id
115
     *
116
     * @return \Illuminate\Http\Response
117
     */
118
    public function show($id)
119
    {
120
        $user = User::find($id);
121
122
        return view('usersmanagement.show-user')->withUser($user);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('usersmanage...user')->withUser($user) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
123
    }
124
125
    /**
126
     * Show the form for editing the specified resource.
127
     *
128
     * @param int $id
129
     *
130
     * @return \Illuminate\Http\Response
131
     */
132
    public function edit($id)
133
    {
134
        $user = User::findOrFail($id);
135
        $roles = Role::all();
136
137
        foreach ($user->roles as $user_role) {
138
            $currentRole = $user_role;
139
        }
140
141
        $data = [
142
            'user'        => $user,
143
            'roles'       => $roles,
144
            'currentRole' => $currentRole,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $currentRole seems to be defined by a foreach iteration on line 137. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
145
        ];
146
147
        return view('usersmanagement.edit-user')->with($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('usersmanage...dit-user')->with($data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
148
    }
149
150
    /**
151
     * Update the specified resource in storage.
152
     *
153
     * @param Request $request
154
     * @param $id
155
     *
156
     * @return \Illuminate\Http\RedirectResponse
157
     */
158
    public function update(Request $request, $id)
159
    {
160
        $user = User::find($id);
161
        $ipAddress = new CaptureIpTrait();
162
163
        $validator = Validator::make($request->all(), [
164
            'scout_name'    => 'max:255',
165
            'first_name'    => 'required|max:255',
166
            'last_name'     => 'required|max:255',
167
            'password'      => 'nullable|confirmed|min:6',
168
        ]);
169
170
        if ($validator->fails()) {
171
            return back()->withErrors($validator)->withInput();
172
        }
173
174
        $name_gen = (($request->input('scout_name') != null) ? $request->input('first_name').'_'.$request->input('scout_name').'_'.$request->input('last_name') : $request->input('first_name').'_'.$request->input('last_name'));
175
176
        $user->scout_name = $request->input('scout_name');
177
        $user->first_name = $request->input('first_name');
178
        $user->last_name = $request->input('last_name');
179
        $user->name_gen = $name_gen;
180
        $user->activated = 1;
181
182
        if ($request->input('password') != null) {
183
            $user->password = bcrypt($request->input('password'));
184
        }
185
186
        $userRole = $request->input('role');
187
        if ($userRole != null) {
188
            $user->detachAllRoles();
189
            $user->attachRole($userRole);
190
        }
191
192
        $user->updated_ip_address = $ipAddress->getClientIp();
193
194
        $user->save();
195
196
        return back()->with('success', trans('usersmanagement.updateSuccess'));
197
    }
198
199
    /**
200
     * Remove the specified resource from storage.
201
     *
202
     * @param int $id
203
     *
204
     * @return \Illuminate\Http\Response
205
     */
206
    public function destroy($id)
207
    {
208
        $currentUser = Auth::user();
209
        $user = User::findOrFail($id);
210
        $ipAddress = new CaptureIpTrait();
211
212
        if ($user->id != $currentUser->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
213
            $user->deleted_ip_address = $ipAddress->getClientIp();
214
            $user->save();
215
            $user->delete();
216
217
            return redirect('users')->with('success', trans('usersmanagement.deleteSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('users')...gement.deleteSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
218
        }
219
220
        return back()->with('error', trans('usersmanagement.deleteSelfError'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->with('err...ment.deleteSelfError')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
221
    }
222
223
    /**
224
     * Method to search the users.
225
     *
226
     * @param Request $request
227
     *
228
     * @return \Illuminate\Http\Response
229
     */
230
    public function search(Request $request)
231
    {
232
        $searchTerm = $request->input('user_search_box');
233
        $searchRules = [
234
            'user_search_box' => 'required|string|max:255',
235
        ];
236
        $searchMessages = [
237
            'user_search_box.required' => 'Search term is required',
238
            'user_search_box.string'   => 'Search term has invalid characters',
239
            'user_search_box.max'      => 'Search term has too many characters - 255 allowed',
240
        ];
241
242
        $validator = Validator::make($request->all(), $searchRules, $searchMessages);
243
244
        if ($validator->fails()) {
245
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...P_UNPROCESSABLE_ENTITY) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
246
                json_encode($validator),
247
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
248
        }
249
250
        $results = User::where('id', 'like', $searchTerm.'%')
251
                            ->orWhere('name', 'like', $searchTerm.'%')
252
                            ->orWhere('email', 'like', $searchTerm.'%')->get();
253
254
        // Attach roles to results
255
        foreach ($results as $result) {
256
            $roles = [
257
                'roles' => $result->roles,
258
            ];
259
            $result->push($roles);
260
        }
261
262
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
263
            json_encode($results),
264
        ], Response::HTTP_OK);
265
    }
266
}
267