Passed
Push — dev6 ( e24a5d...ab8d6f )
by Ron
20:36
created

UserRolesController::store()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Events\Admin\UserRoleCreatedEvent;
6
use App\Events\Admin\UserRoleDeletedEvent;
7
use App\Events\Admin\UserRoleUpdatedEvent;
8
use App\Http\Controllers\Controller;
9
use App\Http\Requests\User\UserRoleRequest;
10
use App\Models\User;
11
use App\Models\UserRolePermissions;
12
use App\Models\UserRolePermissionTypes;
13
use App\Models\UserRoles;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\Support\Facades\Log;
17
use Inertia\Inertia;
18
19
class UserRolesController extends Controller
20
{
21
    /**
22
     * Show the existing User Roles
23
     */
24
    public function index()
25
    {
26
        $this->authorize('viewAny', UserRoles::class);
27
28
        return Inertia::render('Admin/Roles/Index', [
29
            'roles' => UserRoles::all()->makeVisible('allow_edit'),
30
        ]);
31
    }
32
33
    /**
34
     * Show the form to create a new Role
35
     */
36
    public function create()
37
    {
38
        $this->authorize('create', UserRoles::class);
39
40
        return Inertia::render('Admin/Roles/Create', [
41
            'permissions' => UserRolePermissionTypes::all(),
42
        ]);
43
    }
44
45
    /**
46
     * Create the new User Role
47
     */
48
    public function store(UserRoleRequest $request)
49
    {
50
        //  Create the new role
51
        $newRole = UserRoles::create($request->only(['name', 'description']));
52
53
        //  Insert the permissions for the role
54
        foreach($request->user_role_permissions as $perm)
55
        {
56
            UserRolePermissions::create([
57
                'role_id'      => $newRole->role_id,
58
                'perm_type_id' => $perm['perm_type_id'],
59
                'allow'        => isset($perm['allow']) ? $perm['allow'] : false,
60
            ]);
61
        }
62
63
        event(new UserRoleCreatedEvent($newRole));
64
        return redirect(route('admin.user-roles.index'))->with([
65
            'message' => 'New role created',
66
            'type'    => 'success',
67
        ]);
68
    }
69
70
    /**
71
     * Show the form for editing the User Role
72
     */
73
    public function edit($id)
74
    {
75
        $role = UserRoles::with('UserRolePermissions.UserRolePermissionTypes')->where('role_id', $id)->firstOrFail();
76
        $this->authorize('update', $role);
77
78
        return Inertia::render('Admin/Roles/Edit', [
79
            'role_data' => $role,
80
        ]);
81
    }
82
83
    /**
84
     * Update the user Role
85
     */
86
    public function update(UserRoleRequest $request, $id)
87
    {
88
        //  Block a user from trying to update one of the default roles
89
        if($id <= 4)
90
        {
91
            report('User '.$request->user()->username.' is trying to modify a default role');
92
            abort(403, 'You cannot modify a default User Role');
93
        }
94
95
        //  Update the role details
96
        $role = UserRoles::find($id);
97
        $role->update($request->only(['name', 'description']));
98
99
        //  Update the role permissions
100
        foreach($request->user_role_permissions as $perm)
101
        {
102
            UserRolePermissions::where(['role_id' => $perm['role_id'], 'perm_type_id' => $perm['perm_type_id']])->update([
103
                'allow' => $perm['allow']
104
            ]);
105
        }
106
107
        event(new UserRoleUpdatedEvent($role));
108
        return redirect(route('admin.user-roles.index'))->with([
109
            'message' => 'Role Updated',
110
            'type'    => 'success'
111
        ]);
112
    }
113
114
    /**
115
     * Remove a User Role
116
     */
117
    public function destroy($id)
118
    {
119
        $role = UserRoles::findOrFail($id);
120
        $this->authorize('forceDelete', $role);
121
122
        //  verify this is not a default role
123
        $role = UserRoles::find($id);
124
        if(!$role->allow_edit)
125
        {
126
            report('User '.Auth::user()->full_name.' is trying to delete one of the default User Roles');
1 ignored issue
show
Bug introduced by
Accessing full_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
127
            return back()->with([
128
                'message' => 'You cannot delete one of the default User Roles',
129
                'type' => 'danger'
130
            ]);
131
        }
132
133
        //  Verify that it is not in use
134
        $inUse = User::where('role_id', $id)->count();
135
        if($inUse)
136
        {
137
            report('User '.Auth::user()->username.' is trying to delete a role that is stil in use.  Details');
1 ignored issue
show
Bug introduced by
Accessing username on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
138
            return back()->with([
139
                'message' => 'This User Role is in use.  Please remove all users from this role before deleting',
140
                'type' => 'danger'
141
            ]);
142
        }
143
144
        $role->delete();
145
146
        event(new UserRoleDeletedEvent($role));
1 ignored issue
show
Bug introduced by
It seems like $role can also be of type null; however, parameter $role of App\Events\Admin\UserRol...tedEvent::__construct() does only seem to accept App\Models\UserRoles, 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

146
        event(new UserRoleDeletedEvent(/** @scrutinizer ignore-type */ $role));
Loading history...
147
        return redirect(route('admin.user-roles.index'));
148
    }
149
}
150