Skip to content

Commit

Permalink
Allow users to change the server description (pterodactyl#4420)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 authored Oct 31, 2022
1 parent b1abae8 commit f2095e8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function rename(RenameServerRequest $request, Server $server): JsonRespon
{
$this->repository->update($server->id, [
'name' => $request->input('name'),
'description' => $request->input('description') ?? '',
]);

if ($server->name !== $request->input('name')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RenameServerRequest extends ClientApiRequest implements ClientPermissionsR
{
/**
* Returns the permissions string indicating which permission should be used to
* validate that the authenticated user has permission to perform this action aganist
* validate that the authenticated user has permission to perform this action against
* the given resource (server).
*/
public function permission(): string
Expand All @@ -26,6 +26,7 @@ public function rules(): array
{
return [
'name' => Server::getRules()['name'],
'description' => 'string|nullable',
];
}
}
2 changes: 1 addition & 1 deletion app/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class Permission extends Model
'settings' => [
'description' => 'Permissions that control a user\'s access to the settings for this server.',
'keys' => [
'rename' => 'Allows a user to rename this server.',
'rename' => 'Allows a user to rename this server and change the description of it.',
'reinstall' => 'Allows a user to trigger a reinstall of this server.',
],
],
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
],
'settings' => [
'rename' => 'Renamed the server from :old to :new',
'description' => 'Changed the server description from :old to :new',
],
'startup' => [
'edit' => 'Changed the :variable variable from ":old" to ":new"',
Expand Down
4 changes: 2 additions & 2 deletions resources/scripts/api/server/renameServer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import http from '@/api/http';

export default (uuid: string, name: string): Promise<void> => {
export default (uuid: string, name: string, description?: string): Promise<void> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/servers/${uuid}/settings/rename`, { name })
http.post(`/api/client/servers/${uuid}/settings/rename`, { name, description })
.then(() => resolve())
.catch(reject);
});
Expand Down
22 changes: 17 additions & 5 deletions resources/scripts/components/server/settings/RenameServerBox.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { ServerContext } from '@/state/server';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Field as FormikField, Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Actions, useStoreActions } from 'easy-peasy';
import renameServer from '@/api/server/renameServer';
import Field from '@/components/elements/Field';
Expand All @@ -11,19 +11,29 @@ import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http';
import { Button } from '@/components/elements/button/index';
import tw from 'twin.macro';
import Label from '@/components/elements/Label';
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
import { Textarea } from '@/components/elements/Input';

interface Values {
name: string;
description: string;
}

const RenameServerBox = () => {
const { isSubmitting } = useFormikContext<Values>();

return (
<TitledGreyBox title={'Change Server Name'} css={tw`relative`}>
<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>
<div css={tw`mt-6 text-right`}>
<Button type={'submit'}>Save</Button>
</div>
Expand All @@ -37,10 +47,10 @@ export default () => {
const setServer = ServerContext.useStoreActions((actions) => actions.server.setServer);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);

const submit = ({ name }: Values, { setSubmitting }: FormikHelpers<Values>) => {
const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers<Values>) => {
clearFlashes('settings');
renameServer(server.uuid, name)
.then(() => setServer({ ...server, name }))
renameServer(server.uuid, name, description)
.then(() => setServer({ ...server, name, description }))
.catch((error) => {
console.error(error);
addError({ key: 'settings', message: httpErrorToHuman(error) });
Expand All @@ -53,9 +63,11 @@ export default () => {
onSubmit={submit}
initialValues={{
name: server.name,
description: server.description,
}}
validationSchema={object().shape({
name: string().required().min(1),
description: string().nullable(),
})}
>
<RenameServerBox />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@ public function testServerNameCanBeChanged(array $permissions)
/** @var \Pterodactyl\Models\Server $server */
[$user, $server] = $this->generateTestAccount($permissions);
$originalName = $server->name;
$originalDescription = $server->description;

$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/settings/rename", [
'name' => '',
'description' => '',
]);

$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.meta.rule', 'required');

$server = $server->refresh();
$this->assertSame($originalName, $server->name);
$this->assertSame($originalDescription, $server->description);

$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/settings/rename", [
'name' => 'Test Server Name',
'description' => 'This is a test server.',
])
->assertStatus(Response::HTTP_NO_CONTENT);

$server = $server->refresh();
$this->assertSame('Test Server Name', $server->name);
$this->assertSame('This is a test server.', $server->description);
}

/**
Expand Down

0 comments on commit f2095e8

Please sign in to comment.