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