UsersManagementController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A create() 0 9 1
A __construct() 0 3 1
B update() 0 44 6
A edit() 0 16 2
A store() 0 52 2
A destroy() 0 15 2
A show() 0 6 1
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 jeremykenedy\LaravelRoles\Models\Role;
11
use Validator;
12
13
class UsersManagementController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     *
18
     * @return void
19
     */
20
    public function __construct()
21
    {
22
        $this->middleware('auth');
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        $users = User::all();
33
        $roles = Role::all();
34
35
        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...
36
    }
37
38
    /**
39
     * Show the form for creating a new resource.
40
     *
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function create()
44
    {
45
        $roles = Role::all();
46
47
        $data = [
48
            'roles' => $roles,
49
        ];
50
51
        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...
52
    }
53
54
    /**
55
     * Store a newly created resource in storage.
56
     *
57
     * @param \Illuminate\Http\Request $request
58
     *
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function store(Request $request)
62
    {
63
        $validator = Validator::make(
64
            $request->all(),
65
            [
66
                'name'                  => 'required|max:255|unique:users',
67
                'first_name'            => '',
68
                'last_name'             => '',
69
                'email'                 => 'required|email|max:255|unique:users',
70
                'password'              => 'required|min:6|max:20|confirmed',
71
                'password_confirmation' => 'required|same:password',
72
                'role'                  => 'required',
73
            ],
74
            [
75
                'name.unique'         => trans('auth.userNameTaken'),
76
                'name.required'       => trans('auth.userNameRequired'),
77
                'first_name.required' => trans('auth.fNameRequired'),
78
                'last_name.required'  => trans('auth.lNameRequired'),
79
                'email.required'      => trans('auth.emailRequired'),
80
                'email.email'         => trans('auth.emailInvalid'),
81
                'password.required'   => trans('auth.passwordRequired'),
82
                'password.min'        => trans('auth.PasswordMin'),
83
                'password.max'        => trans('auth.PasswordMax'),
84
                'role.required'       => trans('auth.roleRequired'),
85
            ]
86
        );
87
88
        if ($validator->fails()) {
89
            $this->throwValidationException(
0 ignored issues
show
Bug introduced by
The method throwValidationException() does not exist on App\Http\Controllers\UsersManagementController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

89
            $this->/** @scrutinizer ignore-call */ 
90
                   throwValidationException(
Loading history...
90
                $request,
91
                $validator
92
            );
93
        } else {
94
            $ipAddress = new CaptureIpTrait();
95
            $profile = new Profile();
96
97
            $user = User::create([
98
                'name'             => $request->input('name'),
99
                'first_name'       => $request->input('first_name'),
100
                'last_name'        => $request->input('last_name'),
101
                'email'            => $request->input('email'),
102
                'password'         => bcrypt($request->input('password')),
103
                'token'            => str_random(64),
104
                'admin_ip_address' => $ipAddress->getClientIp(),
105
                'activated'        => 1,
106
            ]);
107
108
            $user->profile()->save($profile);
109
            $user->attachRole($request->input('role'));
110
            $user->save();
111
112
            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...
113
        }
114
    }
115
116
    /**
117
     * Display the specified resource.
118
     *
119
     * @param int $id
120
     *
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function show($id)
124
    {
125
        $user = User::find($id);
126
        $roles = Role::all();
127
128
        return view('usersmanagement.show-user', compact('user', 'roles'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('usersmanage...mpact('user', 'roles')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
129
    }
130
131
    /**
132
     * Show the form for editing the specified resource.
133
     *
134
     * @param int $id
135
     *
136
     * @return \Illuminate\Http\Response
137
     */
138
    public function edit($id)
139
    {
140
        $user = User::findOrFail($id);
141
        $roles = Role::all();
142
143
        foreach ($user->roles as $user_role) {
144
            $currentRole = $user_role;
145
        }
146
147
        $data = [
148
            'user'        => $user,
149
            'roles'       => $roles,
150
            'currentRole' => $currentRole,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $currentRole seems to be defined by a foreach iteration on line 143. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
151
        ];
152
153
        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...
154
    }
155
156
    /**
157
     * Update the specified resource in storage.
158
     *
159
     * @param \Illuminate\Http\Request $request
160
     * @param int                      $id
161
     *
162
     * @return \Illuminate\Http\Response
163
     */
164
    public function update(Request $request, $id)
165
    {
166
        $currentUser = Auth::user();
0 ignored issues
show
Unused Code introduced by
The assignment to $currentUser is dead and can be removed.
Loading history...
167
        $user = User::find($id);
168
        $emailCheck = ($request->input('email') != '') && ($request->input('email') != $user->email);
169
        $ipAddress = new CaptureIpTrait();
170
171
        if ($emailCheck) {
172
            $validator = Validator::make($request->all(), [
173
                'name'     => 'required|max:255',
174
                'email'    => 'email|max:255|unique:users',
175
                'password' => 'present|confirmed|min:6',
176
            ]);
177
        } else {
178
            $validator = Validator::make($request->all(), [
179
                'name'     => 'required|max:255',
180
                'password' => 'nullable|confirmed|min:6',
181
            ]);
182
        }
183
        if ($validator->fails()) {
184
            $this->throwValidationException(
185
                $request,
186
                $validator
187
            );
188
        } else {
189
            $user->name = $request->input('name');
190
191
            if ($emailCheck) {
192
                $user->email = $request->input('email');
193
            }
194
195
            if ($request->input('password') != null) {
196
                $user->password = bcrypt($request->input('password'));
197
            }
198
199
            $user->detachAllRoles();
200
            $user->attachRole($request->input('role'));
201
            //$user->activated = 1;
202
203
            $user->updated_ip_address = $ipAddress->getClientIp();
204
205
            $user->save();
206
207
            return back()->with('success', trans('usersmanagement.updateSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->with('suc...gement.updateSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
208
        }
209
    }
210
211
    /**
212
     * Remove the specified resource from storage.
213
     *
214
     * @param int $id
215
     *
216
     * @return \Illuminate\Http\Response
217
     */
218
    public function destroy($id)
219
    {
220
        $currentUser = Auth::user();
221
        $user = User::findOrFail($id);
222
        $ipAddress = new CaptureIpTrait();
223
224
        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...
225
            $user->deleted_ip_address = $ipAddress->getClientIp();
226
            $user->save();
227
            $user->delete();
228
229
            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...
230
        }
231
232
        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...
233
    }
234
}
235