Passed
Push — dev6 ( 6e2ff7...b01572 )
by Ron
18:29
created

UserController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 0
c 2
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Actions\GetUserRoles;
6
use App\Events\Admin\NewUserCreated;
7
use App\Events\Admin\UserDeactivatedEvent;
8
use App\Events\Admin\UserUpdatedEvent;
9
use App\Http\Controllers\Controller;
10
use App\Http\Requests\User\UserRequest;
11
use App\Models\User;
12
use App\Models\UserRoles;
13
use App\Models\UserSetting;
14
use App\Models\UserSettingType;
15
use Illuminate\Http\Request;
16
use Illuminate\Support\Facades\Auth;
17
use Inertia\Inertia;
18
19
class UserController extends Controller
20
{
21
    /**
22
     * Display a listing of all active users
23
     */
24
    public function index()
25
    {
26
        $this->authorize('create', User::class);
27
28
        return Inertia::render('Admin/User/Index', [
29
            'users' => User::with('UserRoles')->get(),
30
        ]);
31
    }
32
33
    /**
34
     * Show the new user form
35
     */
36
    public function create()
37
    {
38
        $this->authorize('create', User::class);
39
40
        return Inertia::render('Admin/User/Create', [
41
            'roles' => (new GetUserRoles)->run(Auth::user()),
1 ignored issue
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $user of App\Actions\GetUserRoles::run() does only seem to accept App\Models\User, 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

41
            'roles' => (new GetUserRoles)->run(/** @scrutinizer ignore-type */ Auth::user()),
Loading history...
42
        ]);
43
    }
44
45
    /**
46
     * Store a newly created user
47
     */
48
    public function store(UserRequest $request)
49
    {
50
        $newUser = User::create($request->toArray());
51
52
        //  Add the users settings data
53
        $settings = UserSettingType::all();
54
        foreach($settings as $setting)
55
        {
56
            UserSetting::create([
57
                'user_id'         => $newUser->user_id,
58
                'setting_type_id' => $setting->setting_type_id,
59
                'value'           => true,
60
            ]);
61
        }
62
63
        event(new NewUserCreated($newUser));
64
        return back()->with([
65
            'message' => 'New User Created',
66
            'type'    => 'success',
67
        ]);
68
    }
69
70
    /**
71
     * Show form for editing an existing user
72
     */
73
    public function edit($id)
74
    {
75
        $this->authorize('create', User::class);
76
77
        return Inertia::render('Admin/User/Edit', [
78
            'user'  => User::where('username', $id)->firstOrFail()->makeVisible(['user_id', 'role_id']),
79
            'roles' => UserRoles::all(),
80
        ]);
81
    }
82
83
    /**
84
     * Update a user's information
85
     */
86
    public function update(UserRequest $request, $id)
87
    {
88
        $user = User::findOrFail($id);
89
90
        if(Auth::user()->role_id > $user->role_id)
1 ignored issue
show
Bug introduced by
Accessing role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
91
        {
92
            return back()->with([
93
                'message' => 'You cannot modify a user with higher permissions than you',
94
                'type'    => 'danger'
95
            ]);
96
        }
97
98
        $user->update($request->toArray());
99
100
        event(new UserUpdatedEvent($user));
101
        return redirect(route('admin.user.index'))->with([
102
            'message' => 'User Details Updated',
103
            'type'    => 'success'
104
        ]);
105
    }
106
107
    /**
108
     * Deactivate a user
109
     */
110
    public function destroy($id)
111
    {
112
        $user = User::where('username', $id)->firstOrFail();
113
        $this->authorize('create', $user);
114
        $user->delete();
115
116
        //  TODO - Verify that the user does not have more permissions than the one doing the delete
117
118
        event(new UserDeactivatedEvent($user));
119
        return back()->with([
120
            'message' => 'User has been deactivated',
121
            'type'    => 'danger',
122
        ]);
123
    }
124
}
125