userController.php
9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Validator;
use App\Models\User;
use App\Models\Loginlog;
use App\Models\Aksesgrup;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Yajra\DataTables\Facades\DataTables;
class userController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
/*public function masuk(Request $request)
{
$validator = Validator::make($request->all(), [
'username' => 'required',
'password' => 'required',
]);
if ($validator->fails()) {
$respon = array('status'=>false, 'pesan' => $validator->messages());
} else {
$userdata = array(
'username' => $request->input('username'),
'password' => $request->input('password'),
);
if (\Auth::attempt($userdata, $request->ingatkansaya)) {
Loginlog::create(['username'=>$request->input('username'), 'ip'=>$this->alamatIp()]);
$respon = array('status'=>true, 'pesan' => ['msg' => 'Berhasil login']);
$request->session()->flash('selamat-datang', 'Selamat Datang di Halaman Administrator');
} else {
$respon = array("status"=>false,"pesan"=> ['msg' => 'Gagal Login, Username atau Password salah!!']);
}
}
return response()->json($respon);
}*/
public function index()
{
return view('backend.user.index');
}
public function data(Request $request)
{
if ($request->ajax()) {
$user = User::byLevel();
return Datatables::of($user)
->addIndexColumn()
->addColumn(
'action',
'<center>
<a class="edit ubah" data-toggle="tooltip" data-placement="top" title="Edit" user-id="{{ $id }}" href="#edit-{{ $id }}">
<i class="fa fa-pencil text-warning"></i>
</a>
<a class="delete hidden-xs hidden-sm hapus" data-toggle="tooltip" data-placement="top" title="Delete" href="#hapus-{{ $id }}" user-id="{{ $id }}">
<i class="fa fa-trash text-danger"></i>
</a>
</center>'
)
->editColumn('status', function ($user) {
$status = $user->status == 1 ? '<span class="badge badge-pill badge-info">Aktif</span>':'<span class="badge badge-pill badge-danger">Tidak Aktif</span>';
return '<center>'.$status.'</center>';
})
->editColumn('aksesgrup', function ($user) {
$aksesgrup = $user->aksesgrup === NULL ? '-':$user->aksesgrup->nama;
return '<center>'.$aksesgrup.'</center>';
})
->addColumn('level', function ($user) {
if($user->level == 1)
{
$badge = 'success';
} elseif ($user->level==2) {
$badge = 'primary';
} elseif ($user->level==3) {
$badge = 'secondary';
} else {
$badge = 'danger';
}
return '<center><span class="badge badge-pill badge-'.$badge.'">'.config("master.level.".$user->level).'</span></center>';
})
->rawColumns(['status','level', 'action', 'aksesgrup'])->make(true);
} else {
exit("Not an AJAX request -_-");
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$aksesgrup = Aksesgrup::byLevel()->pluck('nama', 'id');
return view('backend.user.tambah', compact('aksesgrup'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'nama' => 'required|string|max:255',
'username' => 'required|string|max:50|unique:users',
'email' => 'required|email',
'password' => 'min:6',
'aksesgrup_id' => 'required',
'level' => 'required',
]);
if ($validator->fails()) {
$respon = array('status'=>false, 'pesan' => $validator->messages());
} else {
if (User::create($request->all())) {
$respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil disimpan']);
} else {
$respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal disimpan']);
}
}
return response()->json($respon);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::find($id);
$aksesgrup = Aksesgrup::byLevel()->pluck('nama', 'id');
return view('backend.user.ubah', compact('user', 'aksesgrup'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'nama' => 'required|string|max:255',
'password' => 'nullable|min:6',
'email' => 'required|email',
'aksesgrup_id' => 'required',
'level' => 'required',
]);
if ($validator->fails()) {
$respon = array('status'=>false, 'pesan' => $validator->messages());
} else {
$user = User::find($id);
if ($request->has('password')) {
$update = $user->update($request->all());
} else {
$update = $user->update($request->except('password'));
}
if ($update) {
$respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil diubah']);
} else {
$respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal diubah']);
}
}
return response()->json($respon);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function hapus($id)
{
$user = User::find($id);
return view('backend.user.hapus', ['user' => $user]);
}
public function destroy($id)
{
$user = User::find($id);
if ($user->id == \Auth::id()) {
return response()->json(array('status'=>false, 'pesan' => ['msg' => 'Maaf, tidak bisa menghapus akun sendiri. Silahkan Hubungi Administrator..']));
}
if ($user->delete()) {
$respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil dihapus']);
} else {
$respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal dihapus']);
}
return response()->json($respon);
}
public function ubahpassword($id)
{
if ($id == \Auth::user()->id) {
$user = User::find($id);
return view('backend.user.ubah_password', compact('user'));
}
// return view('backend.main.404');
}
public function resetpassword(Request $request)
{
if (\Auth::user()->id != $request->input('id')) {
$respon = array('status'=>false, 'pesan' => ['msg' => 'Tidak ada akses']);
} else {
$user = User::find($request->input('id'));
$nullable = $user->password == null ? 'nullable|':'';
$validator = Validator::make($request->all(), [
'password_lama' => $nullable.'required',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required|min:6',
]);
if ($validator->fails()) {
$respon = array('status'=>false, 'pesan' => $validator->messages());
} else {
if ($user->password == null) {
if (User::find($request->input('id'))->update($request->all())) {
$respon = array('status'=>true, 'next_url'=> $request->next_url, 'pesan' => ['msg' => 'Password Berhasil diubah']);
} else {
$respon = array('status'=>false, 'pesan' => ['msg' => 'Password Gagal diubah']);
}
} elseif (\Hash::check(trim($request->input('password_lama')), \Auth::user()->password) && $user->password != null) {
if (User::find($request->input('id'))->update($request->all())) {
$respon = array('status'=>true, 'next_url'=> $request->next_url, 'pesan' => ['msg' => 'Password Berhasil diubah']);
} else {
$respon = array('status'=>false, 'pesan' => ['msg' => 'Password Gagal diubah']);
}
} else {
$respon = array('status'=>false, 'pesan' => ['password_lama' => ['Password lama salah']]);
}
}
}
return response()->json($respon);
}
}