From a2ec2967d275a46a0147bfc89b362e85e77fddca Mon Sep 17 00:00:00 2001 From: dogukanoksuz Date: Fri, 18 Oct 2024 16:11:04 +0300 Subject: [PATCH] refactor: Simplify conditionals in RoleController.php and add extension variables endpoint Simplified the conditionals in the `extensions` method of `RoleController.php` to improve readability and maintainability. Also added a new endpoint `getExtensionVariables` to retrieve extension variables from `db.json`. --- .../API/Settings/RoleController.php | 49 +++++++++++++++---- routes/api.php | 1 + 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/API/Settings/RoleController.php b/app/Http/Controllers/API/Settings/RoleController.php index 22fdb746..eb35bd76 100644 --- a/app/Http/Controllers/API/Settings/RoleController.php +++ b/app/Http/Controllers/API/Settings/RoleController.php @@ -234,13 +234,24 @@ public function setServers(Request $request) public function extensions(Request $request) { $extensions = Extension::all(); - $selected = Extension::find( - Role::find($request->role_id) - ->permissions - ->where('type', 'extension') - ->pluck('value') - ->toArray() - ); + $roles = Role::find($request->role_id) + ->permissions + ->where('type', 'extension') + ->pluck('value') + ->toArray(); + $selected = $extensions->filter(function ($extension) use ($roles) { + return in_array($extension->id, $roles); + })->values(); + + if ($request->variable_selector) { + $selected = [ + ...$selected, + [ + 'id' => 'default', + 'name' => 'Default KV', + ], + ]; + } return response()->json([ 'extensions' => $extensions, @@ -555,6 +566,17 @@ public function variables(Request $request) return response()->json($permissions); } + /** + * Get extension variables from db.json + */ + public function getExtensionVariables(Request $request) + { + $extension = Extension::find($request->extension_id); + $extJson = getExtensionJson($extension->name); + + return response()->json($extJson['variables'] ?? []); + } + /** * Add variable * @@ -562,12 +584,21 @@ public function variables(Request $request) */ public function setVariables(Request $request) { + switch ($request->type) { + case 'multiselect': + $value = json_encode($request->value); + break; + default: + $value = $request->value; + break; + } + Permission::grant( $request->role_id, 'variable', $request->key, - $request->value, - $request->type ?? null, + $value, + $request->type ?? 'string', 'roles' ); diff --git a/routes/api.php b/routes/api.php index 0d71e456..6b39f3e7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -210,6 +210,7 @@ Route::post('/{extension_id}/license', [Settings\ExtensionController::class, 'license']); Route::get('/{extension_id}/download', [Settings\ExtensionController::class, 'download']); Route::get('/{extension_id}/functions', [Settings\RoleController::class, 'getExtensionFunctions']); + Route::get('/{extension_id}/variables', [Settings\RoleController::class, 'getExtensionVariables']); }); // Users