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

176 lines
5.2 KiB
PHP
Raw Normal View History

2019-11-22 17:22:26 +00:00
<?php
namespace App\Http\Controllers\API;
2019-12-02 13:26:32 +00:00
use App\Data\Repository\AppRepository;
2019-11-22 17:22:26 +00:00
use App\Exceptions\HTTPException;
2019-12-02 13:26:32 +00:00
use App\Exceptions\NoPermissionException;
use App\Exceptions\NotLoggedInException;
2019-11-22 17:22:26 +00:00
use App\Jobs\Mails\ValidateMailAddressJob;
2019-11-26 16:29:59 +00:00
use App\Models\App;
use App\Models\Invite;
2019-11-22 17:22:26 +00:00
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
{
2019-12-02 13:26:32 +00:00
public function listAllApps(Request $request, Response $response, AppRepository $appRepository) {
2019-11-26 16:29:59 +00:00
if(!Auth::check()) {
2019-12-02 13:26:32 +00:00
throw new NotLoggedInException();
2019-11-26 16:29:59 +00:00
}
if(!Auth::user()->admin) {
2019-12-02 13:26:32 +00:00
throw new NoPermissionException();
2019-11-26 16:29:59 +00:00
}
2019-12-02 13:26:32 +00:00
$apps = $appRepository->getAllApps();
2019-11-26 16:29:59 +00:00
return $response->withData(\App\Http\Resources\API\App::collection($apps));
}
2019-12-02 13:26:32 +00:00
public function saveAppProperties(Request $request, Response $response, AppRepository $appRepository, $id) {
if(!Auth::check()) {
throw new NotLoggedInException();
}
2019-11-26 16:29:59 +00:00
if(!Auth::user()->admin) {
2019-12-02 13:26:32 +00:00
throw new NoPermissionException();
2019-11-26 16:29:59 +00:00
}
2019-12-02 13:26:32 +00:00
$app = $appRepository->findById($id);
2019-11-26 16:29:59 +00:00
$app->auto_accept = (bool)$request->input("autoAccept", false);
$app->testing_warning = (bool)$request->input("testingWarning", false);
$app->untrusted_warning = (bool)$request->input("untrustedWarning", false);
$app->show_on_webpage = (bool)$request->input("showOnWebpage", false);
$app->hidden_in_app_list = (bool)$request->input("hideInAppList", false);
$app->user_cant_remove_app = (bool)$request->input("userCantRemoveApp", false);
$app->stop_auto_redirect = (bool)$request->input("stopAutoRedirect", false);
2019-12-02 13:26:32 +00:00
$appRepository->update($app);
2019-11-26 16:29:59 +00:00
return $response;
}
2019-11-22 17:22:26 +00:00
public function listAllUsers(Request $request, Response $response) {
2019-12-02 13:26:32 +00:00
// @todo replace with /api/v1/account call
2019-11-22 17:22:26 +00:00
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) {
2019-12-02 13:26:32 +00:00
// @todo replace with /api/v1/account/:id call
2019-11-22 17:22:26 +00:00
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;
}
2019-11-26 16:29:59 +00:00
public function listInvites(Request $request, Response $response) {
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$invites = Invite::query()->get();
return $response->withData($invites);
}
2019-11-26 16:52:26 +00:00
public function inviteNew(Request $request, Response $response) {
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$invite = new Invite();
$invite->user_id = Auth::user()->id;
$invite->username = $request->input("username", null);
$invite->comment = $request->input("comment", null);
$invite->status = "active";
$invite->createToken();
$invite->saveOrFail();
return $response->withData($invite);
}
2019-11-22 17:22:26 +00:00
}