2019-04-25 15:33:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2019-04-28 12:49:10 +00:00
|
|
|
use App\Exceptions\HTTPException;
|
2019-04-25 15:33:15 +00:00
|
|
|
use App\Exceptions\NoPermissionException;
|
|
|
|
use App\Exceptions\NotLoggedInException;
|
|
|
|
use App\Exceptions\ResourceNotFound;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Laravel\Lumen\Routing\Controller as BaseController;
|
|
|
|
use TaGeSo\APIResponse\Response;
|
|
|
|
|
|
|
|
class AccountController extends BaseController
|
|
|
|
{
|
|
|
|
public function getUsers(Response $response) {
|
2019-04-28 12:49:10 +00:00
|
|
|
|
2019-04-25 15:33:15 +00:00
|
|
|
if(!Auth::check()) {
|
|
|
|
throw new NotLoggedInException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Auth::user()->admin) {
|
|
|
|
throw new NoPermissionException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = User::query()->paginate(100);
|
|
|
|
$response->setPagination(
|
|
|
|
$users->currentPage(),
|
|
|
|
$users->lastPage(),
|
|
|
|
$users->perPage()
|
|
|
|
);
|
2019-06-20 14:46:50 +00:00
|
|
|
|
2019-04-25 15:33:15 +00:00
|
|
|
return $response->withData(\App\Http\Resources\API\User::collection(($users)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser(Response $response, $id) {
|
|
|
|
if(!Auth::check()) {
|
|
|
|
throw new NotLoggedInException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(Auth::user()->admin || Auth::user()->id == $id)) {
|
|
|
|
throw new NoPermissionException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::query()->where("id", "=", $id)->first();
|
|
|
|
|
|
|
|
if($user == null) {
|
|
|
|
throw new ResourceNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withData(new \App\Http\Resources\API\User($user));
|
|
|
|
}
|
|
|
|
}
|