1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Http\Resources\User as UserResource; |
7
|
|
|
use App\Models\User; |
8
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
9
|
|
|
use Illuminate\Support\Facades\Gate; |
10
|
|
|
|
11
|
|
|
class UserController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Class constructor. |
15
|
|
|
* |
16
|
|
|
* @throws AuthorizationException Handled by App\Exceptions\Handler.php. |
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
// Only accept logged in Users. |
21
|
|
|
$this->middleware(function ($request, $next) { |
22
|
|
|
if ($request->user() === null) { |
23
|
|
|
throw new AuthorizationException(); |
24
|
|
|
} |
25
|
|
|
return $next($request); |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return all users as an array |
31
|
|
|
* |
32
|
|
|
* @throws AuthorizationException Handled by App\Exceptions\Handler.php. |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function index() |
37
|
|
|
{ |
38
|
|
|
$users = User::with(['applicant', 'manager', 'hr_advisor'])->get(); |
39
|
|
|
$viewableUsers = $users->filter(function ($user) { |
40
|
|
|
return Gate::allows('view-user', $user); |
41
|
|
|
})->values(); |
42
|
|
|
if (empty($viewableUsers)) { |
43
|
|
|
throw new AuthorizationException(); |
44
|
|
|
} |
45
|
|
|
return UserResource::collection($viewableUsers); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Display the specified resource. |
50
|
|
|
* |
51
|
|
|
* @param \App\Models\User $user Incoming User. |
52
|
|
|
* @return \Illuminate\Http\Response |
53
|
|
|
*/ |
54
|
|
|
public function show(User $user) |
55
|
|
|
{ |
56
|
|
|
$this->authorize('view-user', $user); |
57
|
|
|
return new UserResource($user); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|