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/AdminController.php
2019-11-22 18:22:26 +01:00

102 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Exceptions\HTTPException;
use App\Jobs\Mails\ValidateMailAddressJob;
use App\Models\Mail;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use TaGeSo\APIResponse\Response;
class AdminController extends BaseController
{
public function listAllUsers(Request $request, Response $response) {
if(!Auth::check()) {
abort(401);
}
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$user = User::all();
return $response->withData(\App\Http\Resources\API\User::collection($user));
}
public function getUserDetails(Request $request, Response $response, $id) {
if(!Auth::check()) {
abort(401);
}
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$user = User::query()->where("id", "=", $id)->firstOrFail();
$mails = Mail::query()->where("user_id", "=", $id)->get();
$data = [];
$data["details"] = new \App\Http\Resources\API\User($user);
$data["mails"] = $mails;
return $response->withData($data);
}
public function saveUserDetails(Request $request, Response $response, $id) {
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$user = User::query()->where("id", "=", $id)->firstOrFail();
$user->admin = (bool)$request->input("admin", false);
$user->developer = (bool)$request->input("developer", false);
$user->username = $request->input("username");
$user->saveOrFail();
return $response;
}
public function resendValidationMail(Request $request, Response $response, $id) {
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$mail = Mail::query()->where("id", "=", $id)->firstOrFail();
$this->dispatch(new ValidateMailAddressJob($mail));
$response->setStatusCode(201);
return $response;
}
public function changeMailStatus(Request $request, Response $response, $id) {
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$mail = Mail::query()->where("id", "=", $id)->firstOrFail();
$mail->status = $request->input("status");
if($mail->status == "valide") {
$user = User::query()->where("id", "=", $mail->user_id)->firstOrFail();
if($user->getMail() == null) {
$mail->primary = true;
}
}
$mail->saveOrFail();
$response->setStatusCode(200);
return $response;
}
}