Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions app/Http/Controllers/Admin/ServersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,30 @@ public function __construct(
*
* @throws DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws ValidationException
*/
public function setDetails(Request $request, Server $server): RedirectResponse
{
$this->detailsModificationService->handle($server, $request->only([
'owner_id', 'external_id', 'name', 'description',
]));
if ($request->input('external_id')) {
$externalId = $request->input('external_id');
$exists = Server::where('external_id', $externalId)
->where('id', '!=', $server->id)
->exists();

if ($exists) {
throw ValidationException::withMessages([
'external_id' => 'The external identifier must be unique among all servers.',
]);
}
}

try {
$this->detailsModificationService->handle($server, $request->only([
'owner_id', 'external_id', 'name', 'description',
]));
} catch (DataValidationException $exception) {
throw new ValidationException($exception->getValidator());
}

$this->alert->success(trans('admin/server.alerts.details_updated'))->flash();

Expand Down
15 changes: 15 additions & 0 deletions app/Http/Requests/Admin/ServerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public function withValidator(Validator $validator): void
], function ($input) {
return !$input->auto_deploy;
});

if ($this->input('external_id')) {
$server = $this->route('server');
$externalId = $this->input('external_id');

$query = Server::where('external_id', $externalId);

if ($server) {
$query->where('id', '!=', $server->id);
}

if ($query->exists()) {
$validator->errors()->add('external_id', 'The external identifier must be unique among all servers.');
}
}
});
}
}
30 changes: 30 additions & 0 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
use Illuminate\Validation\Rule;

/**
* \Pterodactyl\Models\Server.
Expand Down Expand Up @@ -217,6 +218,35 @@ public function isSuspended(): bool
return $this->status === self::STATUS_SUSPENDED;
}

public static function getRulesForUpdate($model, string $column = 'id'): array
{
if ($model instanceof Model) {
[$id, $column] = [$model->getKey(), $model->getKeyName()];
}

$rules = static::getRules();
foreach ($rules as $key => &$data) {
foreach ($data as &$datum) {
if (!is_string($datum) || !str_starts_with($datum, 'unique')) {
continue;
}

[, $args] = explode(':', $datum);
$args = explode(',', $args);

if ($key === 'external_id') {
$datum = Rule::unique($args[0], $args[1] ?? $key)
->ignore($id ?? $model, $column)
->whereNotNull('external_id');
} else {
$datum = Rule::unique($args[0], $args[1] ?? $key)->ignore($id ?? $model, $column);
}
}
}

return $rules;
}

/**
* Gets the user who owns the server.
*
Expand Down