Skip to content

Commit

Permalink
refactor: Simplify conditionals in RoleController.php and add extensi…
Browse files Browse the repository at this point in the history
…on 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`.
  • Loading branch information
dogukanoksuz committed Oct 18, 2024
1 parent 45afe73 commit a2ec296
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
49 changes: 40 additions & 9 deletions app/Http/Controllers/API/Settings/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -555,19 +566,39 @@ 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
*
* @return JsonResponse|Response
*/
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'
);

Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2ec296

Please sign in to comment.