This commit is contained in:
Kekskurse 2019-11-22 18:22:26 +01:00
parent 21ed7565d6
commit f48da1a642
2 changed files with 107 additions and 0 deletions

View file

@ -0,0 +1,102 @@
<?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;
}
}

View file

@ -84,6 +84,11 @@ $router->group(['prefix' => 'api'], function () use ($router) {
$router->group(['prefix' => 'admin'], function () use ($router) { $router->group(['prefix' => 'admin'], function () use ($router) {
$router->post("/settings", ["uses" => "API\ServerController@saveSettings"]); $router->post("/settings", ["uses" => "API\ServerController@saveSettings"]);
$router->get("/user", ["uses" => "API\AdminController@listAllUsers"]);
$router->get("/user/{id}", ["uses" => "API\AdminController@getUserDetails"]);
$router->put("/user/{id}", ["uses" => "API\AdminController@saveUserDetails"]);
$router->post("/mail/{id}/resend", ["uses" => "API\AdminController@resendValidationMail"]);
$router->put("/mail/{id}/status", ["uses" => "API\AdminController@changeMailStatus"]);
}); });
}); });
}); });