|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\PermissionManager\app\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
|
6
|
|
|
// VALIDATION |
|
7
|
|
|
use Backpack\PermissionManager\app\Http\Requests\RoleCrudRequest as StoreRequest; |
|
8
|
|
|
use Backpack\PermissionManager\app\Http\Requests\RoleCrudRequest as UpdateRequest; |
|
9
|
|
|
|
|
10
|
|
|
class RoleCrudController extends CrudController |
|
11
|
|
|
{ |
|
12
|
|
|
public function setup() |
|
13
|
|
|
{ |
|
14
|
|
|
$role_model = config('backpack.permissionmanager.models.role'); |
|
15
|
|
|
$permission_model = config('backpack.permissionmanager.models.permission'); |
|
16
|
|
|
|
|
17
|
|
|
$this->crud->setModel($role_model); |
|
18
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::permissionmanager.role'), trans('backpack::permissionmanager.roles')); |
|
|
|
|
|
|
19
|
|
|
$this->crud->setRoute(backpack_url('role')); |
|
20
|
|
|
|
|
21
|
|
|
$this->crud->addColumn([ |
|
22
|
|
|
'name' => 'name', |
|
23
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
|
24
|
|
|
'type' => 'text', |
|
25
|
|
|
]); |
|
26
|
|
|
|
|
27
|
|
View Code Duplication |
if (config('backpack.permissionmanager.multiple_guards')) { |
|
|
|
|
|
|
28
|
|
|
$this->crud->addColumn([ |
|
29
|
|
|
'name' => 'guard_name', |
|
30
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
|
31
|
|
|
'type' => 'text', |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->crud->addColumn([ |
|
36
|
|
|
// n-n relationship (with pivot table) |
|
37
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_plural')), |
|
38
|
|
|
'type' => 'select_multiple', |
|
39
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
|
40
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
|
41
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
|
42
|
|
|
'model' => $permission_model, // foreign key model |
|
43
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries? |
|
44
|
|
|
]); |
|
45
|
|
|
|
|
46
|
|
|
$this->crud->addField([ |
|
47
|
|
|
'name' => 'name', |
|
48
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
|
49
|
|
|
'type' => 'text', |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
if (config('backpack.permissionmanager.multiple_guards')) { |
|
|
|
|
|
|
53
|
|
|
$this->crud->addField([ |
|
54
|
|
|
'name' => 'guard_name', |
|
55
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
|
56
|
|
|
'type' => 'select_from_array', |
|
57
|
|
|
'options' => $this->getGuardTypes(), |
|
58
|
|
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->crud->addField([ |
|
62
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_plural')), |
|
63
|
|
|
'type' => 'checklist', |
|
64
|
|
|
'name' => 'permissions', |
|
65
|
|
|
'entity' => 'permissions', |
|
66
|
|
|
'attribute' => 'name', |
|
67
|
|
|
'model' => $permission_model, |
|
68
|
|
|
'pivot' => true, |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
if (config('backpack.permissionmanager.allow_role_create') == false) { |
|
72
|
|
|
$this->crud->denyAccess('create'); |
|
73
|
|
|
} |
|
74
|
|
|
if (config('backpack.permissionmanager.allow_role_update') == false) { |
|
75
|
|
|
$this->crud->denyAccess('update'); |
|
76
|
|
|
} |
|
77
|
|
|
if (config('backpack.permissionmanager.allow_role_delete') == false) { |
|
78
|
|
|
$this->crud->denyAccess('delete'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function store(StoreRequest $request) |
|
83
|
|
|
{ |
|
84
|
|
|
//otherwise, changes won't have effect |
|
85
|
|
|
\Cache::forget('spatie.permission.cache'); |
|
86
|
|
|
|
|
87
|
|
|
return parent::storeCrud(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function update(UpdateRequest $request) |
|
91
|
|
|
{ |
|
92
|
|
|
//otherwise, changes won't have effect |
|
93
|
|
|
\Cache::forget('spatie.permission.cache'); |
|
94
|
|
|
|
|
95
|
|
|
return parent::updateCrud(); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/* |
|
99
|
|
|
* Get an array list of all available guard types |
|
100
|
|
|
* that have been defined in app/config/auth.php |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
**/ |
|
104
|
|
View Code Duplication |
private function getGuardTypes() |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$guards = config('auth.guards'); |
|
107
|
|
|
|
|
108
|
|
|
$returnable = []; |
|
109
|
|
|
foreach ($guards as $key => $details) { |
|
110
|
|
|
$returnable[$key] = $key; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $returnable; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.