This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Http/Controllers/API/AccountController.php

52 lines
1.3 KiB
PHP
Raw Normal View History

2019-04-25 15:33:15 +00:00
<?php
namespace App\Http\Controllers\API;
2019-12-02 13:26:32 +00:00
use App\Data\Repository\UserRepository;
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;
2019-07-23 15:48:11 +00:00
use http\Env\Request;
2019-04-25 15:33:15 +00:00
use Illuminate\Support\Facades\Auth;
use Laravel\Lumen\Routing\Controller as BaseController;
use TaGeSo\APIResponse\Response;
class AccountController extends BaseController
{
2019-12-02 13:26:32 +00:00
public function getUsers(Response $response, UserRepository $userRepository) {
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();
}
2019-12-02 13:26:32 +00:00
$users = $userRepository->getAllUsers();
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)));
}
2019-12-02 13:26:32 +00:00
public function getUser(Response $response, UserRepository $userRepository, $id) {
2019-04-25 15:33:15 +00:00
if(!Auth::check()) {
throw new NotLoggedInException();
}
if(!(Auth::user()->admin || Auth::user()->id == $id)) {
throw new NoPermissionException();
}
2019-12-02 13:26:32 +00:00
$user = $userRepository->findById($id);
2019-04-25 15:33:15 +00:00
if($user == null) {
throw new ResourceNotFound();
}
return $response->withData(new \App\Http\Resources\API\User($user));
}
2019-07-23 15:48:11 +00:00
2019-04-25 15:33:15 +00:00
}