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 __construct() |
15
|
|
|
{ |
16
|
|
|
parent::__construct(); |
17
|
|
|
|
18
|
|
|
$this->crud->setModel("App\User"); |
19
|
|
|
$this->crud->setEntityNameStrings('user', 'users'); |
20
|
|
|
$this->crud->setRoute('admin/user'); |
21
|
|
|
$this->crud->setColumns([ |
22
|
|
|
[ |
23
|
|
|
'name' => 'name', |
24
|
|
|
'label' => 'Name', |
25
|
|
|
'type' => 'text', |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
'name' => 'email', |
29
|
|
|
'label' => 'Email', |
30
|
|
|
'type' => 'email', |
31
|
|
|
], |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->crud->addColumn([ // n-n relationship (with pivot table) |
35
|
|
|
'label' => "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' => "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' => 'Name', |
56
|
|
|
'type' => 'text', |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'name' => 'email', |
60
|
|
|
'label' => 'Email', |
61
|
|
|
'type' => 'email', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'name' => 'password', |
65
|
|
|
'label' => 'Password', |
66
|
|
|
'type' => 'password', |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
'name' => 'password_confirmation', |
70
|
|
|
'label' => 'Password Confirmation', |
71
|
|
|
'type' => 'password', |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
// two interconnected entities |
75
|
|
|
'label' => 'User Role Permissions', |
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' => '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' => 'Permission', |
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
|
|
|
$item = $this->crud->create(\Request::except(['redirect_after_save', 'password'])); |
118
|
|
|
|
119
|
|
|
//encrypt password |
120
|
|
|
if ($request->input('password')) { |
121
|
|
|
$item->password = bcrypt($request->input('password')); |
122
|
|
|
$item->save(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// show a success message |
126
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
127
|
|
|
|
128
|
|
|
// redirect the user where he chose to be redirected |
129
|
|
|
switch (\Request::input('redirect_after_save')) { |
130
|
|
|
case 'current_item_edit': |
131
|
|
|
return \Redirect::to($this->crud->route.'/'.$item->id.'/edit'); |
132
|
|
|
|
133
|
|
|
default: |
134
|
|
|
return \Redirect::to(\Request::input('redirect_after_save')); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function update(UpdateRequest $request) |
139
|
|
|
{ |
140
|
|
|
//encrypt password and set it to request |
141
|
|
|
$this->crud->hasAccessOrFail('update'); |
142
|
|
|
|
143
|
|
|
$dataToUpdate = \Request::except(['redirect_after_save', 'password']); |
144
|
|
|
|
145
|
|
|
//encrypt password |
146
|
|
|
if ($request->input('password')) { |
147
|
|
|
$dataToUpdate['password'] = bcrypt($request->input('password')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// update the row in the db |
151
|
|
|
$this->crud->update(\Request::get('id'), $dataToUpdate); |
152
|
|
|
|
153
|
|
|
// show a success message |
154
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
155
|
|
|
|
156
|
|
|
return \Redirect::to($this->crud->route); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.