feat(admin): add controller of scope

This commit is contained in:
FoskyM 2023-10-01 19:41:17 +08:00
parent 91a8f950ec
commit bcddcfc468
No known key found for this signature in database
GPG key ID: 42C0ED6994AD7E9C
6 changed files with 153 additions and 1 deletions

View file

@ -33,7 +33,12 @@ return [
->post('/oauth-clients', 'oauth.clients.create', Api\Controller\CreateClientController::class)
->get('/oauth-clients/{client_id}', 'oauth.clients.show', Api\Controller\ShowClientController::class)
->patch('/oauth-clients/{id}', 'oauth.clients.update', Api\Controller\UpdateClientController::class)
->delete('/oauth-clients/{id}', 'oauth.clients.delete', Api\Controller\DeleteClientController::class),
->delete('/oauth-clients/{id}', 'oauth.clients.delete', Api\Controller\DeleteClientController::class)
->get('/oauth-scopes', 'oauth.clients.list', Api\Controller\ListClientController::class)
->post('/oauth-scopes', 'oauth.clients.create', Api\Controller\CreateClientController::class)
->patch('/oauth-scopes/{id}', 'oauth.clients.update', Api\Controller\UpdateClientController::class)
->delete('/oauth-scopes/{id}', 'oauth.clients.delete', Api\Controller\DeleteClientController::class),
(new Extend\Settings)
->serializeToForum('foskym-oauth-center.allow_implicit', 'foskym-oauth-center.allow_implicit', 'boolval')

View file

@ -0,0 +1,25 @@
<?php
namespace FoskyM\OAuthCenter\Api\Controller;
use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use FoskyM\OAuthCenter\Models\Scope;
use FoskyM\OAuthCenter\Api\Serializer\ScopeSerializer;
class CreateScopeController extends AbstractListController
{
public $serializer = ScopeSerializer::class;
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = RequestUtil::getActor($request);
$actor->assertAdmin();
$attributes = Arr::get($request->getParsedBody(), 'data.attributes');
return Scope::create([]);
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace FoskyM\OAuthCenter\Api\Controller;
use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use FoskyM\OAuthCenter\Models\Scope;
use FoskyM\OAuthCenter\Api\Serializer\ScopeSerializer;
class DeleteScopeController extends AbstractListController
{
public $serializer = ScopeSerializer::class;
protected function data(ServerRequestInterface $request, Document $document)
{
$id = Arr::get($request->getQueryParams(), 'id');
RequestUtil::getActor($request)
->assertAdmin();
$client = Scope::find($id);
$client->delete();
return $client;
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace FoskyM\OAuthCenter\Api\Controller;
use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use FoskyM\OAuthCenter\Models\Scope;
use FoskyM\OAuthCenter\Api\Serializer\ScopeSerializer;
class ListScopeController extends AbstractListController
{
public $serializer = ScopeSerializer::class;
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = RequestUtil::getActor($request);
$actor->assertAdmin();
return Scope::all();
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace FoskyM\OAuthCenter\Api\Controller;
use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use FoskyM\OAuthCenter\Models\Scope;
use FoskyM\OAuthCenter\Api\Serializer\ScopeSerializer;
class UpdateScopeController extends AbstractListController
{
public $serializer = ScopeSerializer::class;
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = RequestUtil::getActor($request);
$actor->assertAdmin();
$id = Arr::get($request->getQueryParams(), 'id');
$client = Scope::find($id);
$attributes = Arr::get($request->getParsedBody(), 'data.attributes', []);
collect(['scope', 'resource_path', 'method', 'is_default', 'scope_name', 'scope_icon', 'scope_desc'])
->each(function (string $attribute) use ($client, $attributes) {
if (($val = Arr::get($attributes, $attribute)) !== null) {
$client->$attribute = $val;
}
});
$client->save();
return $client;
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace FoskyM\OAuthCenter\Api\Serializer;
use Flarum\Api\Serializer\AbstractSerializer;
use FoskyM\OAuthCenter\Models\Scope;
use InvalidArgumentException;
class ScopeSerializer extends AbstractSerializer
{
protected $type = 'oauth-scopes';
protected function getDefaultAttributes($model)
{
if (!($model instanceof Scope)) {
throw new InvalidArgumentException(
get_class($this) . ' can only serialize instances of ' . Scope::class
);
}
// See https://docs.flarum.org/extend/api.html#serializers for more information.
return [
"id" => $model->id,
"scope" => $model->scope,
"resource_path" => $model->resource_path,
"method" => $model->method,
"is_default" => $model->is_default,
"scope_name" => $model->scope_name,
"scope_icon" => $model->scope_icon,
"scope_desc" => $model->scope_desc,
];
}
}