1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PermissionManager\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests; |
6
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
7
|
|
|
use Backpack\PermissionManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest; |
8
|
|
|
// VALIDATION |
9
|
|
|
use Backpack\PermissionManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
class UserCrudController extends CrudController |
13
|
|
|
{ |
14
|
|
|
public function setup() |
15
|
|
|
{ |
16
|
|
|
$this->crud->setModel(config('backpack.permissionmanager.user_model')); |
17
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users')); |
|
|
|
|
18
|
|
|
$this->crud->setRoute(config('backpack.base.route_prefix').'/user'); |
19
|
|
|
$this->crud->enableAjaxTable(); |
20
|
|
|
|
21
|
|
|
$this->crud->setColumns([ |
22
|
|
|
[ |
23
|
|
|
'name' => 'name', |
24
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
25
|
|
|
'type' => 'text', |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
'name' => 'email', |
29
|
|
|
'label' => trans('backpack::permissionmanager.email'), |
30
|
|
|
'type' => 'email', |
31
|
|
|
], |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->crud->addColumn([ // n-n relationship (with pivot table) |
35
|
|
|
'label' => trans('backpack::permissionmanager.roles'), // Table column heading |
36
|
|
|
'type' => 'select_multiple', |
37
|
|
|
'name' => 'roles', // the method that defines the relationship in your Model |
38
|
|
|
'entity' => 'roles', // the method that defines the relationship in your Model |
39
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
40
|
|
|
'model' => "Backpack\PermissionManager\app\Models\Roles", // foreign key model |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$this->crud->addColumn([ // n-n relationship (with pivot table) |
44
|
|
|
'label' => trans('backpack::permissionmanager.extra_permissions'), // Table column heading |
45
|
|
|
'type' => 'select_multiple', |
46
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
47
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
48
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
49
|
|
|
'model' => "Backpack\PermissionManager\app\Models\Permission", // foreign key model |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$this->crud->addFields([ |
53
|
|
|
[ |
54
|
|
|
'name' => 'name', |
55
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
56
|
|
|
'type' => 'text', |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'name' => 'email', |
60
|
|
|
'label' => trans('backpack::permissionmanager.email'), |
61
|
|
|
'type' => 'email', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'name' => 'password', |
65
|
|
|
'label' => trans('backpack::permissionmanager.password'), |
66
|
|
|
'type' => 'password', |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
'name' => 'password_confirmation', |
70
|
|
|
'label' => trans('backpack::permissionmanager.password_confirmation'), |
71
|
|
|
'type' => 'password', |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
// two interconnected entities |
75
|
|
|
'label' => trans('backpack::permissionmanager.user_role_permission'), |
76
|
|
|
'field_unique_name' => 'user_role_permission', |
77
|
|
|
'type' => 'checklist_dependency', |
78
|
|
|
'name' => 'roles_and_permissions', // the methods that defines the relationship in your Model |
79
|
|
|
'subfields' => [ |
80
|
|
|
'primary' => [ |
81
|
|
|
'label' => trans('backpack::permissionmanager.roles'), |
82
|
|
|
'name' => 'roles', // the method that defines the relationship in your Model |
83
|
|
|
'entity' => 'roles', // the method that defines the relationship in your Model |
84
|
|
|
'entity_secondary' => 'permissions', // the method that defines the relationship in your Model |
85
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
86
|
|
|
'model' => "Backpack\PermissionManager\app\Models\Role", // foreign key model |
87
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] |
88
|
|
|
'number_columns' => 3, //can be 1,2,3,4,6 |
|
|
|
|
89
|
|
|
], |
90
|
|
|
'secondary' => [ |
91
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_singular')), |
92
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
93
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
94
|
|
|
'entity_primary' => 'roles', // the method that defines the relationship in your Model |
95
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
96
|
|
|
'model' => "Backpack\PermissionManager\app\Models\Permission", // foreign key model |
97
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] |
98
|
|
|
'number_columns' => 3, //can be 1,2,3,4,6 |
|
|
|
|
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Store a newly created resource in the database. |
107
|
|
|
* |
108
|
|
|
* @param StoreRequest $request - type injection used for validation using Requests |
109
|
|
|
* |
110
|
|
|
* @return \Illuminate\Http\RedirectResponse |
111
|
|
|
*/ |
112
|
|
|
public function store(StoreRequest $request) |
113
|
|
|
{ |
114
|
|
|
$this->crud->hasAccessOrFail('create'); |
115
|
|
|
|
116
|
|
|
// insert item in the db |
117
|
|
|
if ($request->input('password')) { |
118
|
|
|
$item = $this->crud->create(\Request::except(['redirect_after_save'])); |
119
|
|
|
|
120
|
|
|
// now bcrypt the password |
121
|
|
|
$item->password = bcrypt($request->input('password')); |
|
|
|
|
122
|
|
|
$item->save(); |
123
|
|
|
} else { |
124
|
|
|
$item = $this->crud->create(\Request::except(['redirect_after_save', 'password'])); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// show a success message |
128
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
129
|
|
|
|
130
|
|
|
// save the redirect choice for next time |
131
|
|
|
$this->setSaveAction(); |
132
|
|
|
|
133
|
|
|
return $this->performSaveAction($item->getKey()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function update(UpdateRequest $request) |
137
|
|
|
{ |
138
|
|
|
//encrypt password and set it to request |
139
|
|
|
$this->crud->hasAccessOrFail('update'); |
140
|
|
|
|
141
|
|
|
$dataToUpdate = \Request::except(['redirect_after_save', 'password']); |
142
|
|
|
|
143
|
|
|
//encrypt password |
144
|
|
|
if ($request->input('password')) { |
145
|
|
|
$dataToUpdate['password'] = bcrypt($request->input('password')); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// update the row in the db |
149
|
|
|
$this->crud->update(\Request::get($this->crud->model->getKeyName()), $dataToUpdate); |
150
|
|
|
|
151
|
|
|
// show a success message |
152
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
153
|
|
|
|
154
|
|
|
// save the redirect choice for next time |
155
|
|
|
$this->setSaveAction(); |
156
|
|
|
|
157
|
|
|
return $this->performSaveAction(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.