This commit is contained in:
Kekskurse 2019-06-20 19:07:17 +02:00
parent c8cf8f705d
commit 2f6ee03dfc
5 changed files with 78 additions and 8 deletions

View file

@ -7,7 +7,9 @@ use App\Exceptions\NoPermissionException;
use App\Exceptions\NotLoggedInException; use App\Exceptions\NotLoggedInException;
use App\Exceptions\ResourceNotFound; use App\Exceptions\ResourceNotFound;
use App\Http\Resources\API\App; use App\Http\Resources\API\App;
use App\Http\Resources\API\AppAccess;
use App\Http\Resources\API\AppForOwner; use App\Http\Resources\API\AppForOwner;
use App\Http\Resources\API\AppUser;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use http\Env\Request; use http\Env\Request;
@ -63,6 +65,54 @@ class AppController extends BaseController
$response->setPagination($apps->currentPage(), $apps->lastPage(), $apps->perPage()); $response->setPagination($apps->currentPage(), $apps->lastPage(), $apps->perPage());
return $response->withData(App::collection($apps)); return $response->withData(App::collection($apps));
}
public function updateApp(Response $response, \Illuminate\Http\Request $request, $id) {
if(!Auth::check()) {
throw new NotLoggedInException();
}
$this->validate($request, [
'name' => 'required|max:255|min:3|regex:@^[a-zA-Z0-9]*$@',
'description' => 'required|min:3',
'url' => 'required|url',
'direct_url' => 'url'
]);
$app = \App\Models\App::query()->where("id", "=", $id)->firstOrFail();
if($app->owner_id != Auth::user()->id) {
throw new NoPermissionException(403, "Not your app");
}
$app->name = $request->input("name");
$app->description = $request->input("description");
$app->url = $request->input("url");
$app->direct_url = $request->input("direct_url");
$app->saveOrFail();
return $response->withData(new AppForOwner($app));
}
public function getUsers(Response $response, $id) {
if(!Auth::check()) {
throw new NotLoggedInException();
}
$app = \App\Models\App::query()->where("id", "=", $id)->firstOrFail();
if($app->user_id != Auth::user()->id) {
throw new NoPermissionException(403, "Not your app");
}
$access = \App\Models\AppAccess::query()->where("status", "=", "allowed")->where("app_id", "=", $id)->paginate(100);
$response->setPagination(
$access->currentPage(),
$access->lastPage(),
$access->perPage()
);
return $response->withData(AppUser::collection($access));
} }
} }

View file

@ -19,7 +19,7 @@ class CorsMiddleware
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE', 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Credentials' => 'true', 'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => '86400', 'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With, Access-Control-Allow-Origin'
]; ];
if ($request->isMethod('OPTIONS')) if ($request->isMethod('OPTIONS'))

View file

@ -0,0 +1,23 @@
<?php
namespace App\Http\Resources\API;
use Illuminate\Http\Resources\Json\JsonResource;
class AppUser extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'user_id' => $this->user_id,
'username' => $this->getUser()->username,
'status' => $this->status
];
}
}

View file

@ -32,12 +32,6 @@ class AppPermission extends Migration
$app->access_read_apps = true; $app->access_read_apps = true;
$app->saveOrFail(); $app->saveOrFail();
$setting = new \App\Models\Setting();
$setting->name = "gui_url";
$setting->description = "GUI Url for redirect User from API/PHP-GUI to WebGui";
$setting->typ = "textinput";
$setting->value = "http://localhost:8080";
$setting->saveOrFail();
} }
/** /**
@ -58,6 +52,5 @@ class AppPermission extends Migration
$table->dropColumn('access_read_profile'); $table->dropColumn('access_read_profile');
}); });
\App\Models\Setting::query()->where("name", "=", "gui_url")->delete();
} }
} }

View file

@ -51,8 +51,12 @@ $router->group(['prefix' => 'api'], function () use ($router) {
$router->get("/find", ['uses' => 'API\AppController@findApp']); $router->get("/find", ['uses' => 'API\AppController@findApp']);
$router->group(['prefix' => '{id}'], function () use ($router) { $router->group(['prefix' => '{id}'], function () use ($router) {
$router->get("/", ['uses' => 'API\AppController@appDetails']); $router->get("/", ['uses' => 'API\AppController@appDetails']);
$router->put("/", ['uses' => 'API\AppController@updateApp']);
$router->get("/access", ["uses" => "API\oAuthController@getAccess"]); $router->get("/access", ["uses" => "API\oAuthController@getAccess"]);
$router->post("/access/allow", ["uses" => "API\oAuthController@allowAccess"]); $router->post("/access/allow", ["uses" => "API\oAuthController@allowAccess"]);
$router->get("/user", ["uses" => "API\AppController@getUsers"]);
}); });
}); });