|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Requests\ChangePasswordRequest; |
|
6
|
|
|
use App\Models\User; |
|
7
|
|
|
use App\Services\UserTableService; |
|
8
|
|
|
use Illuminate\Support\Facades\Auth; |
|
9
|
|
|
use Illuminate\Support\Facades\Hash; |
|
10
|
|
|
|
|
11
|
|
|
class UserController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
protected $userTableService; |
|
14
|
|
|
|
|
15
|
4 |
|
public function __construct(UserTableService $userTableService) |
|
16
|
|
|
{ |
|
17
|
4 |
|
$this->userTableService = $userTableService; |
|
18
|
4 |
|
} |
|
19
|
|
|
|
|
20
|
2 |
|
public function index() |
|
21
|
|
|
{ |
|
22
|
2 |
|
$title = trans('general.users'); |
|
23
|
|
|
|
|
24
|
2 |
|
$dataset = User::select('id', 'name', 'email', 'created_at') |
|
25
|
2 |
|
->paginate($this->getItemsPerPage()); |
|
26
|
|
|
|
|
27
|
2 |
|
if ($dataset->isEmpty()) { |
|
28
|
1 |
|
$this->addFlashMessage(trans('general.no_users_in_database'), 'alert-danger'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$viewData = [ |
|
32
|
2 |
|
'columns' => $this->userTableService->getColumns(), |
|
33
|
2 |
|
'dataset' => $dataset, |
|
34
|
2 |
|
'routeName' => $this->userTableService->getRouteName(), |
|
35
|
2 |
|
'title' => $title, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
2 |
|
return view('list', $viewData); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function delete($objectId) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$object = User::find($objectId); |
|
44
|
1 |
|
$data = ['class' => 'alert-success', 'message' => trans('general.deleted')]; |
|
45
|
|
|
|
|
46
|
1 |
|
if (!$object || $object->id === 1 || $object->id = Auth::user()->id) { |
|
|
|
|
|
|
47
|
|
|
$data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_object')]; |
|
48
|
|
|
} else { |
|
49
|
1 |
|
$object->delete(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
return response()->json($data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function changePassword() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return view('auth.passwords.change'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function postChangePassword(ChangePasswordRequest $request) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$user = Auth::user(); |
|
63
|
1 |
|
$user->password = Hash::make($request->new_password); |
|
|
|
|
|
|
64
|
1 |
|
$user->save(); |
|
65
|
|
|
|
|
66
|
1 |
|
return redirect()->route($this->userTableService->getRouteName().'.change_password') |
|
67
|
1 |
|
->with([ |
|
68
|
1 |
|
'message' => trans('general.saved'), |
|
69
|
1 |
|
'alert-class' => 'alert-success', |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|