65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Customer;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreVmRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user()->can('create', Customer::class);
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$baseDomain = config('hosting.plesk.base_domain');
|
|
|
|
return [
|
|
'name' => ['required', 'string', 'max:100', 'regex:/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/'],
|
|
'subdomain' => [
|
|
Rule::requiredIf(fn () => $this->boolean('behind_traefik')),
|
|
'nullable',
|
|
'string',
|
|
'max:63',
|
|
'regex:/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/',
|
|
],
|
|
'behind_traefik' => ['boolean'],
|
|
'user_id' => [
|
|
Rule::requiredIf(fn () => $this->user()->isAdmin()),
|
|
'nullable',
|
|
'exists:users,id',
|
|
],
|
|
'ip_pool_id' => ['nullable', 'exists:ip_pools,id'],
|
|
'cpu' => ['required', 'integer', 'min:1', 'max:32'],
|
|
'ram' => ['required', 'integer', 'min:512', 'max:131072'],
|
|
'disk' => ['required', 'integer', 'min:10', 'max:2048'],
|
|
'devices' => ['nullable', 'array'],
|
|
'devices.*.type' => ['required_with:devices', Rule::in(array_keys(\App\Models\VmDevice::typesFor($this->user())))],
|
|
'devices.*.slot' => ['nullable', 'string', 'max:32'],
|
|
'devices.*.config' => ['nullable', 'array'],
|
|
'install_iso' => ['nullable', 'string', 'max:255'],
|
|
];
|
|
}
|
|
|
|
public function domain(): ?string
|
|
{
|
|
if (! $this->boolean('behind_traefik') || ! $this->filled('subdomain')) {
|
|
return null;
|
|
}
|
|
|
|
return strtolower($this->input('subdomain')).'.'.config('hosting.plesk.base_domain');
|
|
}
|
|
|
|
public function ownerId(): int
|
|
{
|
|
if ($this->user()->isAdmin() && $this->filled('user_id')) {
|
|
return (int) $this->input('user_id');
|
|
}
|
|
|
|
return $this->user()->id;
|
|
}
|
|
}
|