mirror of
https://github.com/pelican-dev/panel.git
synced 2025-06-15 07:01:08 +02:00
Create new description endpoint (#1136)
Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com>
This commit is contained in:
parent
34865d4288
commit
65deffc6e6
@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\Client\Servers;
|
|||||||
|
|
||||||
use App\Facades\Activity;
|
use App\Facades\Activity;
|
||||||
use App\Http\Controllers\Api\Client\ClientApiController;
|
use App\Http\Controllers\Api\Client\ClientApiController;
|
||||||
|
use App\Http\Requests\Api\Client\Servers\Settings\DescriptionServerRequest;
|
||||||
use App\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest;
|
use App\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest;
|
||||||
use App\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest;
|
use App\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest;
|
||||||
use App\Http\Requests\Api\Client\Servers\Settings\SetDockerImageRequest;
|
use App\Http\Requests\Api\Client\Servers\Settings\SetDockerImageRequest;
|
||||||
@ -34,24 +35,36 @@ class SettingsController extends ClientApiController
|
|||||||
public function rename(RenameServerRequest $request, Server $server): JsonResponse
|
public function rename(RenameServerRequest $request, Server $server): JsonResponse
|
||||||
{
|
{
|
||||||
$name = $request->input('name');
|
$name = $request->input('name');
|
||||||
$description = $request->has('description') ? (string) $request->input('description') : $server->description;
|
|
||||||
|
|
||||||
if ($server->name !== $name) {
|
$server->update(['name' => $name]);
|
||||||
|
|
||||||
|
if ($server->wasChanged('name')) {
|
||||||
Activity::event('server:settings.rename')
|
Activity::event('server:settings.rename')
|
||||||
->property(['old' => $server->name, 'new' => $name])
|
->property(['old' => $server->getOriginal('name'), 'new' => $name])
|
||||||
->log();
|
->log();
|
||||||
$server->name = $name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($server->description !== $description && config('panel.editable_server_descriptions')) {
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update server description
|
||||||
|
*/
|
||||||
|
public function description(DescriptionServerRequest $request, Server $server): JsonResponse
|
||||||
|
{
|
||||||
|
if (!config('panel.editable_server_descriptions')) {
|
||||||
|
return new JsonResponse([], Response::HTTP_FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
$description = $request->input('description');
|
||||||
|
$server->update(['description' => $description ?? '']);
|
||||||
|
|
||||||
|
if ($server->wasChanged('description')) {
|
||||||
Activity::event('server:settings.description')
|
Activity::event('server:settings.description')
|
||||||
->property(['old' => $server->description, 'new' => $description])
|
->property(['old' => $server->getOriginal('description'), 'new' => $description])
|
||||||
->log();
|
->log();
|
||||||
$server->description = $description;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$server->save();
|
|
||||||
|
|
||||||
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +93,7 @@ class SettingsController extends ClientApiController
|
|||||||
*/
|
*/
|
||||||
public function dockerImage(SetDockerImageRequest $request, Server $server): JsonResponse
|
public function dockerImage(SetDockerImageRequest $request, Server $server): JsonResponse
|
||||||
{
|
{
|
||||||
if (!in_array($server->image, array_values($server->egg->docker_images))) {
|
if (!in_array($server->image, $server->egg->docker_images)) {
|
||||||
throw new BadRequestHttpException('This server\'s Docker image has been manually set by an administrator and cannot be updated.');
|
throw new BadRequestHttpException('This server\'s Docker image has been manually set by an administrator and cannot be updated.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Api\Client\Servers\Settings;
|
||||||
|
|
||||||
|
use App\Contracts\Http\ClientPermissionsRequest;
|
||||||
|
use App\Http\Requests\Api\Client\ClientApiRequest;
|
||||||
|
use App\Models\Permission;
|
||||||
|
|
||||||
|
class DescriptionServerRequest extends ClientApiRequest implements ClientPermissionsRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns the permissions string indicating which permission should be used to
|
||||||
|
* validate that the authenticated user has permission to perform this action against
|
||||||
|
* the given resource (server).
|
||||||
|
*/
|
||||||
|
public function permission(): string
|
||||||
|
{
|
||||||
|
return Permission::ACTION_SETTINGS_DESCRIPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The rules to apply when validating this request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'description' => 'string|nullable',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -26,7 +26,6 @@ class RenameServerRequest extends ClientApiRequest implements ClientPermissionsR
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => Server::getRules()['name'],
|
'name' => Server::getRules()['name'],
|
||||||
'description' => 'string|nullable',
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,8 @@ class Permission extends Model implements Validatable
|
|||||||
|
|
||||||
public const ACTION_SETTINGS_RENAME = 'settings.rename';
|
public const ACTION_SETTINGS_RENAME = 'settings.rename';
|
||||||
|
|
||||||
|
public const ACTION_SETTINGS_DESCRIPTION = 'settings.description';
|
||||||
|
|
||||||
public const ACTION_SETTINGS_REINSTALL = 'settings.reinstall';
|
public const ACTION_SETTINGS_REINSTALL = 'settings.reinstall';
|
||||||
|
|
||||||
public const ACTION_ACTIVITY_READ = 'activity.read';
|
public const ACTION_ACTIVITY_READ = 'activity.read';
|
||||||
@ -176,7 +178,7 @@ class Permission extends Model implements Validatable
|
|||||||
[
|
[
|
||||||
'name' => 'settings',
|
'name' => 'settings',
|
||||||
'icon' => 'tabler-settings',
|
'icon' => 'tabler-settings',
|
||||||
'permissions' => ['rename', 'reinstall'],
|
'permissions' => ['rename', 'description', 'reinstall'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'activity',
|
'name' => 'activity',
|
||||||
|
@ -16,7 +16,8 @@ return [
|
|||||||
'startup_update' => 'Allows a user to modify the startup variables for the server.',
|
'startup_update' => 'Allows a user to modify the startup variables for the server.',
|
||||||
'startup_docker_image' => 'Allows a user to modify the Docker image used when running the server.',
|
'startup_docker_image' => 'Allows a user to modify the Docker image used when running the server.',
|
||||||
'settings_reinstall' => 'Allows a user to trigger a reinstall of this server.',
|
'settings_reinstall' => 'Allows a user to trigger a reinstall of this server.',
|
||||||
'settings_rename' => 'Allows a user to rename this server and change the description of it.',
|
'settings_rename' => 'Allows a user to rename this server.',
|
||||||
|
'settings_description' => 'Allows a user to change the description of this server.',
|
||||||
'activity_read' => 'Allows a user to view the activity logs for the server.',
|
'activity_read' => 'Allows a user to view the activity logs for the server.',
|
||||||
'websocket_*' => 'Allows a user access to the websocket for this server.',
|
'websocket_*' => 'Allows a user access to the websocket for this server.',
|
||||||
'control_console' => 'Allows a user to send data to the server console.',
|
'control_console' => 'Allows a user to send data to the server console.',
|
||||||
|
@ -129,6 +129,7 @@ Route::prefix('/servers/{server:uuid}')->middleware([ServerSubject::class, Authe
|
|||||||
|
|
||||||
Route::prefix('/settings')->group(function () {
|
Route::prefix('/settings')->group(function () {
|
||||||
Route::post('/rename', [Client\Servers\SettingsController::class, 'rename']);
|
Route::post('/rename', [Client\Servers\SettingsController::class, 'rename']);
|
||||||
|
Route::post('/description', [Client\Servers\SettingsController::class, 'description']);
|
||||||
Route::post('/reinstall', [Client\Servers\SettingsController::class, 'reinstall']);
|
Route::post('/reinstall', [Client\Servers\SettingsController::class, 'reinstall']);
|
||||||
Route::put('/docker-image', [Client\Servers\SettingsController::class, 'dockerImage']);
|
Route::put('/docker-image', [Client\Servers\SettingsController::class, 'dockerImage']);
|
||||||
});
|
});
|
||||||
|
@ -21,11 +21,9 @@ class SettingsControllerTest extends ClientApiIntegrationTestCase
|
|||||||
/** @var \App\Models\Server $server */
|
/** @var \App\Models\Server $server */
|
||||||
[$user, $server] = $this->generateTestAccount($permissions);
|
[$user, $server] = $this->generateTestAccount($permissions);
|
||||||
$originalName = $server->name;
|
$originalName = $server->name;
|
||||||
$originalDescription = $server->description;
|
|
||||||
|
|
||||||
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/settings/rename", [
|
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/settings/rename", [
|
||||||
'name' => '',
|
'name' => '',
|
||||||
'description' => '',
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||||
@ -33,18 +31,15 @@ class SettingsControllerTest extends ClientApiIntegrationTestCase
|
|||||||
|
|
||||||
$server = $server->refresh();
|
$server = $server->refresh();
|
||||||
$this->assertSame($originalName, $server->name);
|
$this->assertSame($originalName, $server->name);
|
||||||
$this->assertSame($originalDescription, $server->description);
|
|
||||||
|
|
||||||
$this->actingAs($user)
|
$this->actingAs($user)
|
||||||
->postJson("/api/client/servers/$server->uuid/settings/rename", [
|
->postJson("/api/client/servers/$server->uuid/settings/rename", [
|
||||||
'name' => 'Test Server Name',
|
'name' => 'Test Server Name',
|
||||||
'description' => 'This is a test server.',
|
|
||||||
])
|
])
|
||||||
->assertStatus(Response::HTTP_NO_CONTENT);
|
->assertStatus(Response::HTTP_NO_CONTENT);
|
||||||
|
|
||||||
$server = $server->refresh();
|
$server = $server->refresh();
|
||||||
$this->assertSame('Test Server Name', $server->name);
|
$this->assertSame('Test Server Name', $server->name);
|
||||||
$this->assertSame('This is a test server.', $server->description);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user