From cf2abfccf76bb107e0c477c74c57510c4cea1392 Mon Sep 17 00:00:00 2001 From: FoskyM Date: Sun, 1 Oct 2023 16:55:27 +0800 Subject: [PATCH] feat(admin): clients controller --- extend.php | 6 ++- js/dist/admin.js | Bin 8327 -> 8366 bytes js/dist/admin.js.map | Bin 25169 -> 25319 bytes js/src/admin/pages/ClientsPage.js | 9 ++-- src/Api/Controller/CreateClientController.php | 39 ++++++++++++++++++ src/Api/Controller/DeleteClientController.php | 28 +++++++++++++ src/Api/Controller/ListClientController.php | 4 +- src/Api/Controller/UpdateClientController.php | 37 +++++++++++++++++ 8 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 src/Api/Controller/CreateClientController.php create mode 100644 src/Api/Controller/DeleteClientController.php create mode 100644 src/Api/Controller/UpdateClientController.php diff --git a/extend.php b/extend.php index bad30c4..926568e 100644 --- a/extend.php +++ b/extend.php @@ -27,8 +27,12 @@ return [ (new Extend\Routes('forum')) ->post('/oauth/authorize', 'oauth.authorize.post', Controllers\AuthorizeController::class), + (new Extend\Routes('api')) - ->get('/oauth-clients', 'oauth.clients.list', Api\Controller\ListClientController::class), + ->get('/oauth-clients', 'oauth.clients.list', Api\Controller\ListClientController::class) + ->post('/oauth-clients', 'oauth.clients.create', Api\Controller\CreateClientController::class) + ->patch('/oauth-clients/{id}', 'oauth.clients.update', Api\Controller\UpdateClientController::class) + ->delete('/oauth-clients/{id}', 'oauth.clients.delete', Api\Controller\DeleteClientController::class), (new Extend\Middleware('api'))->add(ResourceScopeMiddleware::class), ]; diff --git a/js/dist/admin.js b/js/dist/admin.js index 589a21ae64ba68aee1b50010ef57e7c5ecd50726..4459328aab2dfb48f4a64068cfc30fc22278765a 100644 GIT binary patch delta 231 zcmZp7T<5sKf=!^zwnQ&aBSq6XIX|zsL?M0h1vb&i3G70X^VtL$(>FJ=-Q{2`+Z@lo zft@jbvVgcXkn|CkGEdd4F3~GaEK7CH$xO{F@ytuh*T~b!*GbhYNi0fFEzv7W%qdOP ztgY451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27Bcz-`8cPktP!}$Vc3*K($)V(-Ick&rjGc47tN?hpBXOfPHbTcUxN(C* z8kh~yL5)XZ3gLb-bvevT?U{T{=!taw`pFzDMv{q~Yba2)8AMWzm+tqawk9s>&vQ5b zAuWug4|sA#xJL|y$dIMLKO I)Neb*U!@txvj6}9 diff --git a/js/src/admin/pages/ClientsPage.js b/js/src/admin/pages/ClientsPage.js index 594f0a1..ea80fe8 100644 --- a/js/src/admin/pages/ClientsPage.js +++ b/js/src/admin/pages/ClientsPage.js @@ -46,7 +46,7 @@ export default class ClientsPage extends Page { type: 'text', value: client[key]() || '', onchange: (event) => { - this.saveClientInfo(client.id(), key, event.target.value); + this.saveClientInfo(index, key, event.target.value); }, })) ), @@ -98,7 +98,10 @@ export default class ClientsPage extends Page { return pwd; } - saveClientInfo(id, key, value) { - console.log(id, key, value); + saveClientInfo(index, key, value) { + console.log(index, key, value); + this.clients[index].save({ + [key]: value, + }); } } diff --git a/src/Api/Controller/CreateClientController.php b/src/Api/Controller/CreateClientController.php new file mode 100644 index 0000000..5093c0c --- /dev/null +++ b/src/Api/Controller/CreateClientController.php @@ -0,0 +1,39 @@ +assertAdmin(); + + $data = Arr::get($request->getParsedBody(), 'data', []); + + $client = Client::build( + Arr::get($data, 'attributes.name'), + $actor->id, + Arr::get($data, 'attributes.icon'), + Arr::get($data, 'attributes.description'), + Arr::get($data, 'attributes.actions'), + Arr::get($data, 'attributes.metrics'), + Arr::get($data, 'attributes.requirements'), + + ); + + + $client->save(); + + return $client; + } +} diff --git a/src/Api/Controller/DeleteClientController.php b/src/Api/Controller/DeleteClientController.php new file mode 100644 index 0000000..17907d0 --- /dev/null +++ b/src/Api/Controller/DeleteClientController.php @@ -0,0 +1,28 @@ +getQueryParams(), 'id'); + RequestUtil::getActor($request) + ->assertAdmin(); + + $client = Client::find($id); + + $client->delete(); + + return $client; + } +} diff --git a/src/Api/Controller/ListClientController.php b/src/Api/Controller/ListClientController.php index caa3307..b0e10bf 100644 --- a/src/Api/Controller/ListClientController.php +++ b/src/Api/Controller/ListClientController.php @@ -16,9 +16,7 @@ class ListClientController extends AbstractListController protected function data(ServerRequestInterface $request, Document $document) { $actor = RequestUtil::getActor($request); - if (!$actor->isAdmin()) { - return []; - } + $actor->assertAdmin(); return Client::get(); } diff --git a/src/Api/Controller/UpdateClientController.php b/src/Api/Controller/UpdateClientController.php new file mode 100644 index 0000000..1bc2478 --- /dev/null +++ b/src/Api/Controller/UpdateClientController.php @@ -0,0 +1,37 @@ +assertAdmin(); + + $id = Arr::get($request->getQueryParams(), 'id'); + $client = Client::find($id); + + $data = Arr::get($request->getParsedBody(), 'data', []); + + collect(['client_id', 'client_secret', 'redirect_uri', 'grant_types', 'scope', 'client_name', 'client_desc', 'client_icon', 'client_home']) + ->each(function (string $attribute) use ($client, $data) { + if (($val = Arr::get($data, "attributes.$attribute")) !== null) { + $client->$attribute = $val; + } + }); + + $client->save(); + + return $client; + } +}