|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App\Http\Controllers; |
|
5
|
|
|
|
|
6
|
|
|
use App\Http\Requests\IndexUsersRequest; |
|
7
|
|
|
use App\User; |
|
8
|
|
|
use DB; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Yajra\DataTables\DataTables; |
|
11
|
|
|
|
|
12
|
|
|
class UsersController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new controller instance. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware("not_student")->only("index"); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Display a listing of the resource. |
|
25
|
|
|
* |
|
26
|
|
|
* @param IndexUsersRequest $request |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\View\View |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function index(IndexUsersRequest $request) |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
$userRole = $request->get("role"); |
|
36
|
|
|
|
|
37
|
|
|
switch ($userRole) { |
|
38
|
|
|
case User::ROLE_ADMINISTRATOR: |
|
39
|
|
|
$users = User::administrators()->get(); |
|
40
|
|
|
break; |
|
41
|
|
|
case User::ROLE_VIEWER: |
|
42
|
|
|
$users = User::viewers()->get(); |
|
43
|
|
|
break; |
|
44
|
|
|
case User::ROLE_STUDENT: |
|
45
|
|
|
// Students, since can be many, are handled in a different way. |
|
46
|
|
|
return $this->indexStudents(); |
|
47
|
|
|
default: |
|
48
|
|
|
return view("users.index_no_role"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return view( |
|
52
|
|
|
"users.index", |
|
53
|
|
|
[ |
|
54
|
|
|
"users" => $users, |
|
55
|
|
|
"user_role" => $userRole |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Display a listing of the resource (student users), via DataTables. |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\View\View |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
private function indexStudents() |
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
|
|
if (request()->ajax()) { |
|
71
|
|
|
$userQuery = User::students() |
|
72
|
|
|
->with(["student", "student.compilations"]) |
|
73
|
|
|
->select("users.*"); |
|
74
|
|
|
return DataTables::of($userQuery) |
|
75
|
|
|
->addColumn( |
|
76
|
|
|
"student.number_of_compilations", |
|
77
|
|
|
function ($user) { |
|
78
|
|
|
// A blank space is appended as workaround |
|
79
|
|
|
// to the still-unexplicable error occurring when returning zero. |
|
80
|
|
|
return $user->student->compilations->count() . " "; |
|
81
|
|
|
} |
|
82
|
|
|
) |
|
83
|
|
|
->make(true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return view("users.index_students"); |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Display the specified resource. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \App\User $user |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Illuminate\View\View |
|
96
|
|
|
* |
|
97
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function show(User $user) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->authorize("view", $user); |
|
102
|
|
|
|
|
103
|
|
|
if ($user->role === User::ROLE_STUDENT) { |
|
104
|
|
|
$user->load(User::ROLE_STUDENT); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return view( |
|
108
|
|
|
"users.show", |
|
109
|
|
|
["user" => $user] |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Show the form for editing the specified resource. |
|
115
|
|
|
* |
|
116
|
|
|
* @param \App\User $user |
|
117
|
|
|
* |
|
118
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
119
|
|
|
* |
|
120
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function edit(User $user) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->authorize("update", $user); |
|
125
|
|
|
|
|
126
|
|
|
// @todo implement edit logic |
|
127
|
|
|
return redirect(route("home")); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Update the specified resource in storage. |
|
132
|
|
|
* |
|
133
|
|
|
* @param \App\User $user |
|
134
|
|
|
* |
|
135
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function update(User $user) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->authorize("update", $user); |
|
142
|
|
|
|
|
143
|
|
|
// @todo implement edit logic |
|
144
|
|
|
return redirect(route("home")); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Remove the specified resource from storage. |
|
149
|
|
|
* |
|
150
|
|
|
* @param \App\User $user |
|
151
|
|
|
* |
|
152
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
153
|
|
|
* |
|
154
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
155
|
|
|
*/ |
|
156
|
|
|
public function destroy(User $user) |
|
157
|
|
|
{ |
|
158
|
|
|
|
|
159
|
|
|
$this->authorize("delete", $user); |
|
160
|
|
|
|
|
161
|
|
|
$userRole = $user->role; |
|
162
|
|
|
|
|
163
|
|
|
DB::transaction(function () use ($user) { |
|
164
|
|
|
|
|
165
|
|
|
if ($user->role === User::ROLE_STUDENT) { |
|
166
|
|
|
$user->student()->delete(); |
|
167
|
|
|
} |
|
168
|
|
|
$user->delete(); |
|
169
|
|
|
|
|
170
|
|
|
}); |
|
171
|
|
|
|
|
172
|
|
|
return \Redirect::route("users.index", ["role" => $userRole]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|