Add config option to disable server descriptions for users (#581)

* add config option to disable server descriptions

* only disable server descriptions for users but not for admins

* Add ,

* invert

* unset description in server transformer if disabled

* remove testing leftover

---------

Co-authored-by: notCharles <charles@pelican.dev>
This commit is contained in:
Boy132 2024-09-29 00:35:57 +02:00 committed by GitHub
parent aafe17174f
commit af4cba341a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 61 additions and 24 deletions

View File

@ -522,6 +522,25 @@ class Settings extends Page implements HasForms
->suffix('Requests Per Minute')
->default(env('APP_API_APPLICATION_RATELIMIT', config('http.rate_limit.application'))),
]),
Section::make('Server')
->description('Settings for Servers.')
->columns()
->collapsible()
->collapsed()
->schema([
Toggle::make('PANEL_EDITABLE_SERVER_DESCRIPTIONS')
->label('Allow Users to edit Server Descriptions?')
->onIcon('tabler-check')
->offIcon('tabler-x')
->onColor('success')
->offColor('danger')
->live()
->columnSpanFull()
->formatStateUsing(fn ($state): bool => (bool) $state)
->afterStateUpdated(fn ($state, Set $set) => $set('PANEL_EDITABLE_SERVER_DESCRIPTIONS', (bool) $state))
->default(env('PANEL_EDITABLE_SERVER_DESCRIPTIONS', config('panel.editable_server_descriptions'))),
]),
];
}

View File

@ -297,7 +297,7 @@ class CreateServer extends CreateRecord
'md' => 6,
'lg' => 6,
])
->label('Notes'),
->label('Description'),
]),
Wizard\Step::make('Egg Configuration')

View File

@ -2,16 +2,16 @@
namespace App\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\Response;
use App\Models\Server;
use Illuminate\Http\JsonResponse;
use App\Facades\Activity;
use App\Services\Servers\ReinstallServerService;
use App\Http\Controllers\Api\Client\ClientApiController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
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\SetDockerImageRequest;
use App\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest;
use App\Models\Server;
use App\Services\Servers\ReinstallServerService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class SettingsController extends ClientApiController
{
@ -33,7 +33,11 @@ class SettingsController extends ClientApiController
$description = $request->has('description') ? (string) $request->input('description') : $server->description;
$server->name = $name;
$server->description = $description;
if (config('panel.editable_server_descriptions')) {
$server->description = $description;
}
$server->save();
if ($server->name !== $name) {

View File

@ -2,8 +2,8 @@
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use App\Services\Helpers\AssetHashService;
use Illuminate\View\View;
class AssetComposer
{
@ -28,6 +28,7 @@ class AssetComposer
'siteKey' => config('recaptcha.website_key') ?? '',
],
'usesSyncDriver' => config('queue.default') === 'sync',
'serverDescriptionsEditable' => config('panel.editable_server_descriptions'),
]);
}
}

View File

@ -2,17 +2,17 @@
namespace App\Transformers\Api\Client;
use App\Models\Allocation;
use App\Models\Egg;
use App\Models\EggVariable;
use App\Models\Permission;
use App\Models\Server;
use App\Models\Subuser;
use League\Fractal\Resource\Item;
use App\Models\Allocation;
use App\Models\Permission;
use Illuminate\Container\Container;
use App\Models\EggVariable;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use App\Services\Servers\StartupCommandService;
use Illuminate\Container\Container;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
class ServerTransformer extends BaseClientTransformer
{
@ -36,7 +36,7 @@ class ServerTransformer extends BaseClientTransformer
$user = $this->request->user();
return [
$data = [
'server_owner' => $user->id === $server->owner_id,
'identifier' => $server->uuid_short,
'internal_id' => $server->id,
@ -76,6 +76,12 @@ class ServerTransformer extends BaseClientTransformer
'is_installing' => !$server->isInstalled(),
'is_transferring' => !is_null($server->transfer),
];
if (!config('panel.editable_server_descriptions')) {
unset($data['description']);
}
return $data;
}
/**

View File

@ -169,4 +169,6 @@ return [
],
'use_binary_prefix' => env('PANEL_USE_BINARY_PREFIX', true),
'editable_server_descriptions' => env('PANEL_EDITABLE_SERVER_DESCRIPTIONS', true),
];

View File

@ -2,7 +2,7 @@ import React from 'react';
import { ServerContext } from '@/state/server';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import { Field as FormikField, Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Actions, useStoreActions } from 'easy-peasy';
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
import renameServer from '@/api/server/renameServer';
import Field from '@/components/elements/Field';
import { object, string } from 'yup';
@ -23,17 +23,21 @@ interface Values {
const RenameServerBox = () => {
const { isSubmitting } = useFormikContext<Values>();
const serverDescriptionsEditable = useStoreState((state) => state.settings.data!.serverDescriptionsEditable);
return (
<TitledGreyBox title={'Change Server Details'} css={tw`relative`}>
<SpinnerOverlay visible={isSubmitting} />
<Form css={tw`mb-0`}>
<Field id={'name'} name={'name'} label={'Server Name'} type={'text'} />
<div css={tw`mt-6`}>
<Label>Server Description</Label>
<FormikFieldWrapper name={'description'}>
<FormikField as={Textarea} name={'description'} rows={3} />
</FormikFieldWrapper>
</div>
{serverDescriptionsEditable && (
<div css={tw`mt-6`}>
<Label>Server Description</Label>
<FormikFieldWrapper name={'description'}>
<FormikField as={Textarea} name={'description'} rows={3} />
</FormikFieldWrapper>
</div>
)}
<div css={tw`mt-6 text-right`}>
<Button type={'submit'}>Save</Button>
</div>

View File

@ -8,6 +8,7 @@ export interface SiteSettings {
siteKey: string;
};
usesSyncDriver: boolean;
serverDescriptionsEditable: boolean;
}
export interface SettingsStore {