1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Http\Controllers; |
9
|
|
|
|
10
|
|
|
use App\Models\UserRole; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use App\Models\UserPermission; |
13
|
|
|
|
14
|
|
|
class UserPermissionController extends Controller |
15
|
|
|
{ |
16
|
|
|
public function createRole() |
17
|
|
|
{ |
18
|
|
|
$roles = UserRole::all(); |
19
|
|
|
|
20
|
|
|
return view('users.entrust.addrole', [ |
21
|
|
|
'roles' => $roles, |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function storeRole(Request $request) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$r = new UserRole(); |
|
|
|
|
28
|
|
|
$r->name = $request->get('name'); |
|
|
|
|
29
|
|
|
$r->display_name = $request->get('dname'); |
30
|
|
|
$r->description = $request->get('desc'); |
|
|
|
|
31
|
|
|
$r->save(); |
32
|
|
|
|
33
|
|
|
return redirect()->action('UserPermissionController@createRole'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function createPermission() |
37
|
|
|
{ |
38
|
|
|
$perms = UserPermission::all(); |
39
|
|
|
|
40
|
|
|
return view('users.entrust.addperm', [ |
41
|
|
|
'perms' => $perms, |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function storePermission(Request $request) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$p = new UserPermission(); |
|
|
|
|
48
|
|
|
$p->name = $request->get('name'); |
|
|
|
|
49
|
|
|
$p->display_name = $request->get('dname'); |
50
|
|
|
$p->description = $request->get('desc'); |
|
|
|
|
51
|
|
|
$p->save(); |
52
|
|
|
|
53
|
|
|
return redirect()->action('UserPermissionController@createPermission'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function showRole($id) |
57
|
|
|
{ |
58
|
|
|
$p = \DB::table('user_permission_role as pr') |
59
|
|
|
->leftJoin('user_permissions as p', 'pr.permission_id', '=', 'p.id') |
60
|
|
|
->where('pr.role_id', '=', $id) |
61
|
|
|
->get(); |
62
|
|
|
|
63
|
|
|
$ptoadd = UserPermission::all(); |
64
|
|
|
|
65
|
|
|
return view('users.entrust.showrole', [ |
66
|
|
|
'perms' => $p, |
67
|
|
|
'roleid' => $id, |
68
|
|
|
'permstoadd' => $ptoadd, |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function addPermToRole(Request $request, $roleid) |
73
|
|
|
{ |
74
|
|
|
$role = UserRole::all()->where('id', '=', $roleid)->first(); |
75
|
|
|
$perm = UserPermission::all()->where('id', '=', $request->get('perm'))->first(); |
76
|
|
|
|
77
|
|
|
$role->attachPermission($perm); |
78
|
|
|
|
79
|
|
|
return redirect()->action('UserPermissionController@showRole', $roleid); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function removePermFromRole($roleid, $permid) |
83
|
|
|
{ |
84
|
|
|
$role = UserRole::all()->where('id', '=', $roleid)->first(); |
85
|
|
|
$perm = UserPermission::all()->where('id', '=', $permid)->first(); |
86
|
|
|
|
87
|
|
|
$role->detachPermission($perm); |
88
|
|
|
|
89
|
|
|
return redirect()->action('UserPermissionController@showRole', $roleid); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function showPermission($id) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.