|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use App\Http\Controllers\Controller; |
|
8
|
|
|
use App\DataTables\UsersDataTable; |
|
9
|
|
|
use Illuminate\Support\Facades\Auth; |
|
10
|
|
|
use Illuminate\Validation\Rule; |
|
11
|
|
|
use App\User; |
|
12
|
|
|
|
|
13
|
|
|
class UserController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new controller instance. |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
|
|
|
|
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->middleware('auth'); |
|
23
|
|
|
// TODO: Setup logging |
|
24
|
|
|
// $this->middleware('log')->only('index'); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Display index page and process dataTable ajax request. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \App\DataTables\UsersDataTable $dataTable |
|
31
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
32
|
|
|
*/ |
|
33
|
|
|
public function index(UsersDataTable $dataTable) |
|
34
|
|
|
{ |
|
35
|
|
|
return $dataTable->render('user.index'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Show create user page. |
|
40
|
|
|
* |
|
41
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
42
|
|
|
*/ |
|
43
|
|
|
public function create() |
|
44
|
|
|
{ |
|
45
|
|
|
return view('user.create'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Store a new user. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Request $request |
|
52
|
|
|
* @return Response |
|
53
|
|
|
*/ |
|
54
|
|
|
public function store(Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
$validator = Validator::make($request->all(), [ |
|
57
|
|
|
'name' => 'required|string|max:255', |
|
58
|
|
|
'email' => 'required|string|email|max:255|unique:users', |
|
59
|
|
|
'password' => 'required|string|min:6|confirmed', |
|
60
|
|
|
'role' => 'required|integer|max:3', |
|
61
|
|
|
'phone' => 'numeric|phone|nullable', |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
if ($validator->fails()) { |
|
65
|
|
|
return redirect('user/create')->withErrors($validator)->withInput(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$user = new User; |
|
69
|
|
|
|
|
70
|
|
|
$user->name = $request->input('name'); |
|
|
|
|
|
|
71
|
|
|
$user->email = $request->input('email'); |
|
|
|
|
|
|
72
|
|
|
$user->password = bcrypt($request->input('password')); |
|
|
|
|
|
|
73
|
|
|
$user->role = $request->input('role'); |
|
|
|
|
|
|
74
|
|
|
$user->phone = $request->input('phone'); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$user->save(); |
|
77
|
|
|
|
|
78
|
|
|
return redirect('user'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Show the given user. |
|
83
|
|
|
* |
|
84
|
|
|
* @param Request $request |
|
85
|
|
|
* @param string $id |
|
86
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function show(Request $request, $id) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$user = User::findOrFail($id); |
|
91
|
|
|
$user->password = ""; |
|
92
|
|
|
|
|
93
|
|
|
return view('user.show', ['user' => $user]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Edit the given user. |
|
98
|
|
|
* |
|
99
|
|
|
* @param Request $request |
|
100
|
|
|
* @param string $id |
|
101
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
public function edit(Request $request, $id) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$user = User::findOrFail($id); |
|
106
|
|
|
$user->password = ""; |
|
107
|
|
|
|
|
108
|
|
|
return view('user.edit', ['user' => $user]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Update the given user. |
|
113
|
|
|
* |
|
114
|
|
|
* @param Request $request |
|
115
|
|
|
* @param string $id |
|
116
|
|
|
* @return Response |
|
117
|
|
|
*/ |
|
118
|
|
|
public function update(Request $request, $id) |
|
119
|
|
|
{ |
|
120
|
|
|
// TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need |
|
121
|
|
|
// to add a hidden _method field to spoof these HTTP verbs. The |
|
122
|
|
|
// method_field helper can create this field for you: |
|
123
|
|
|
// {{ method_field('PUT') }} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$user = User::findOrFail($id); |
|
126
|
|
|
|
|
127
|
|
|
$validator = Validator::make($request->all(), [ |
|
128
|
|
|
'name' => 'required|string|max:255', |
|
129
|
|
|
'email' => 'required|string|email|max:255|unique:users,email,'.$user->id, |
|
130
|
|
|
'role' => 'required|integer|max:3', |
|
131
|
|
|
'phone' => 'numeric|phone|nullable', |
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
|
|
if ($validator->fails()) { |
|
135
|
|
|
return redirect('user/'.$id.'/edit')->withErrors($validator)->withInput(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$user->name = $request->input('name'); |
|
139
|
|
|
$user->email = $request->input('email'); |
|
140
|
|
|
$user->role = $request->input('role'); |
|
141
|
|
|
$user->phone = $request->input('phone'); |
|
142
|
|
|
|
|
143
|
|
|
$user->save(); |
|
144
|
|
|
|
|
145
|
|
|
return redirect('user'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Deletes a user. |
|
150
|
|
|
* |
|
151
|
|
|
* @param Request $request |
|
152
|
|
|
* @param string $id |
|
153
|
|
|
* @return Response |
|
154
|
|
|
*/ |
|
155
|
|
|
public function destroy(Request $request, $id) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
$user = User::findOrFail($id); |
|
158
|
|
|
|
|
159
|
|
|
if ($user->trashed()) { |
|
160
|
|
|
// if the user was already deleted then permananetly delete it |
|
161
|
|
|
User::destroy($id); |
|
162
|
|
|
} else { |
|
163
|
|
|
// soft delete the user the first time |
|
164
|
|
|
$user->delete(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return redirect('user'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Confirms deletion a user. |
|
172
|
|
|
* |
|
173
|
|
|
* @param Request $request |
|
174
|
|
|
* @param string $id |
|
175
|
|
|
* @return Response |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function remove(Request $request, $id) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$user = User::findOrFail($id); |
|
180
|
|
|
$user->password = ""; |
|
181
|
|
|
|
|
182
|
|
|
return view('user.remove', ['user' => $user]); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.