Passed
Push — master ( 957889...d2fe6b )
by Karel
08:00
created

UserRoleController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Chuck\UserRepository;
6
use Chuckbe\Chuckcms\Models\User;
7
use ChuckSite;
0 ignored issues
show
Bug introduced by
The type ChuckSite 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...
8
9
use Spatie\Permission\Models\Role;
10
use Spatie\Permission\Models\Permission;
11
12
use Illuminate\Http\Request;
13
use Illuminate\Foundation\Bus\DispatchesJobs;
14
use Illuminate\Routing\Controller as BaseController;
15
use Illuminate\Foundation\Validation\ValidatesRequests;
16
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
17
18
class UserRoleController extends BaseController
19
{
20
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
21
22
    private $user;
23
    private $userRepository;
24
    /**
25
     * Create a new controller instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct(User $user, UserRepository $userRepository)
30
    {
31
        $this->user = $user;
32
        $this->userRepository = $userRepository;
33
    }
34
35
    /**
36
     * Show the dashboard -> roles.
37
     *
38
     * @return \Illuminate\View\View
39
     */
40
    public function index()
41
    {
42
        $roles = Role::all();
43
        
44
        return view('chuckcms::backend.users.roles.index', compact('roles'));
45
    }
46
47
    public function create(Request $request)
48
    {
49
        $this->validate(request(), [//@todo create custom Request class for page validation
50
            'role_name' => 'max:185|required',
51
            'role_redirect' => 'max:255|required'
52
        ]);
53
54
        $role = Role::firstOrCreate(['name' => $request->role_name],['redirect' => $request->role_redirect]);
55
56
        return redirect()->route('dashboard.users.roles.edit', ['role' => $role->id])->with('notification', 'Rol aangemaakt!');
57
    }
58
59
    /**
60
     * Show the edit user page.
61
     *
62
     * @return \Illuminate\View\View
63
     */
64
    public function edit(Role $role)
65
    {
66
        $permissions = Permission::all();
67
        return view('chuckcms::backend.users.roles.edit', compact('role', 'permissions'));
68
    }
69
70
    public function save(Request $request)
71
    {
72
        $this->validate(request(), [//@todo create custom Request class for page validation
73
            'role_name' => 'max:185|required',
74
            'role_redirect' => 'max:255|required',
75
            'role_id' => 'required',
76
            'permissions_name.*' => 'required',
77
            'permissions_active.*' => 'required'
78
        ]);
79
80
        $role = Role::findById($request->role_id);
81
        $role->name = $request->role_name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Spatie\Permission\Models\Role. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
82
        $role->redirect = $request->role_redirect;
83
        $role->save();
84
85
        $permissions = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $permissions is dead and can be removed.
Loading history...
86
        $countPermissions = count($request->permissions_name);
87
        for ($i=0; $i < $countPermissions; $i++) { 
88
            if($request->permissions_active[$i] == 1) {
89
                $role->givePermissionTo($request->permissions_name[$i]);
90
            } else {
91
                $role->revokePermissionTo($request->permissions_name[$i]);
92
            }
93
        }
94
95
        //redirect back
96
        return redirect()->route('dashboard.users.roles')->with('notification', 'Rol gewijzigd!');
97
    }
98
99
    /**
100
     * Delete the role.
101
     *
102
     * @return string $status
103
     */
104
    public function delete(Request $request)
105
    {
106
        $this->validate(request(), [
107
            'role_id' => 'required',
108
        ]);
109
110
        $role = Role::findById($request->get('role_id'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('role_id') can also be of type null; however, parameter $id of Spatie\Permission\Models\Role::findById() does only seem to accept integer, 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

110
        $role = Role::findById(/** @scrutinizer ignore-type */ $request->get('role_id'));
Loading history...
111
        if ( $role->delete() ) {
112
            return redirect()->route('dashboard.users.roles')->with('notification', 'Rol verwijderd!');
113
        } else {
114
            return redirect()->route('dashboard.users.roles')->with('whoops', 'Er is iets misgegaan, probeer het later nog eens!');
115
        }
116
    }
117
}
118