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\RoleStoreCrudRequest as StoreRequest; |
8
|
|
|
use Backpack\PermissionManager\app\Http\Requests\RoleUpdateCrudRequest as UpdateRequest; |
9
|
|
|
|
10
|
|
|
class RoleCrudController extends CrudController |
11
|
|
|
{ |
12
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; |
13
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation; |
14
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation; |
15
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; |
16
|
|
|
|
17
|
|
|
public function setup() |
18
|
|
|
{ |
19
|
|
|
$role_model = config('backpack.permissionmanager.models.role'); |
20
|
|
|
$permission_model = config('backpack.permissionmanager.models.permission'); |
21
|
|
|
|
22
|
|
|
$this->crud->setModel($role_model); |
23
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::permissionmanager.role'), trans('backpack::permissionmanager.roles')); |
24
|
|
|
$this->crud->setRoute(backpack_url('role')); |
|
|
|
|
25
|
|
|
|
26
|
|
|
// deny access according to configuration file |
27
|
|
|
if (config('backpack.permissionmanager.allow_role_create') == false) { |
28
|
|
|
$this->crud->denyAccess('create'); |
29
|
|
|
} |
30
|
|
|
if (config('backpack.permissionmanager.allow_role_update') == false) { |
31
|
|
|
$this->crud->denyAccess('update'); |
32
|
|
|
} |
33
|
|
|
if (config('backpack.permissionmanager.allow_role_delete') == false) { |
34
|
|
|
$this->crud->denyAccess('delete'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->crud->operation('list', function() use ($permission_model) { |
|
|
|
|
38
|
|
|
$this->crud->addColumn([ |
39
|
|
|
'name' => 'name', |
40
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
41
|
|
|
'type' => 'text', |
42
|
|
|
]); |
43
|
|
View Code Duplication |
if (config('backpack.permissionmanager.multiple_guards')) { |
|
|
|
|
44
|
|
|
$this->crud->addColumn([ |
45
|
|
|
'name' => 'guard_name', |
46
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
47
|
|
|
'type' => 'text', |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
$this->crud->addColumn([ |
51
|
|
|
// n-n relationship (with pivot table) |
52
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_plural')), |
53
|
|
|
'type' => 'select_multiple', |
54
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
55
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
56
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
57
|
|
|
'model' => $permission_model, // foreign key model |
58
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries? |
59
|
|
|
]); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$this->crud->operation(['create', 'update'], function() use ($permission_model) { |
|
|
|
|
63
|
|
|
$this->crud->addField([ |
64
|
|
|
'name' => 'name', |
65
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
66
|
|
|
'type' => 'text', |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
View Code Duplication |
if (config('backpack.permissionmanager.multiple_guards')) { |
|
|
|
|
70
|
|
|
$this->crud->addField([ |
71
|
|
|
'name' => 'guard_name', |
72
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
73
|
|
|
'type' => 'select_from_array', |
74
|
|
|
'options' => $this->getGuardTypes(), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->crud->addField([ |
79
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_plural')), |
80
|
|
|
'type' => 'checklist', |
81
|
|
|
'name' => 'permissions', |
82
|
|
|
'entity' => 'permissions', |
83
|
|
|
'attribute' => 'name', |
84
|
|
|
'model' => $permission_model, |
85
|
|
|
'pivot' => true, |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
//otherwise, changes won't have effect |
89
|
|
|
\Cache::forget('spatie.permission.cache'); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* |
94
|
|
|
* Get an array list of all available guard types |
95
|
|
|
* that have been defined in app/config/auth.php |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
**/ |
99
|
|
View Code Duplication |
private function getGuardTypes() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$guards = config('auth.guards'); |
102
|
|
|
|
103
|
|
|
$returnable = []; |
104
|
|
|
foreach ($guards as $key => $details) { |
105
|
|
|
$returnable[$key] = $key; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $returnable; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.