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