Manage redirects, missing URLs, repositories and settings through the API
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
68
app/Http/Requests/Api/V1/MissingUrlRedirectRequest.php
Normal file
68
app/Http/Requests/Api/V1/MissingUrlRedirectRequest.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Models\MissingUrl;
|
||||
use App\Models\Redirect;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Where a missing address should go. The origin is the address itself.
|
||||
*/
|
||||
class MissingUrlRedirectRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if (is_string($this->input('to')) && filled($this->input('to'))) {
|
||||
$this->merge(['to' => Redirect::normalise($this->input('to'))]);
|
||||
}
|
||||
|
||||
// The language it was seen in, unless told otherwise; null for all.
|
||||
if (! $this->exists('locale')) {
|
||||
$this->merge(['locale' => $this->missingUrl()->locale]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$path = $this->missingUrl()->path;
|
||||
$locale = $this->input('locale');
|
||||
|
||||
return [
|
||||
'to' => ['required', 'string', 'max:500', Rule::notIn([$path])],
|
||||
'status' => ['sometimes', 'integer', Rule::in([301, 302])],
|
||||
'locale' => [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::in(array_keys((array) config('locales.supported'))),
|
||||
// Already answered by a rule: edit that one instead.
|
||||
function (string $attribute, mixed $value, \Closure $fail) use ($path, $locale): void {
|
||||
$exists = Redirect::query()
|
||||
->where('from', $path)
|
||||
->when($locale === null, fn ($q) => $q->whereNull('locale'), fn ($q) => $q->where('locale', $locale))
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
$fail(__('validation.unique', ['attribute' => 'from']));
|
||||
}
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function missingUrl(): MissingUrl
|
||||
{
|
||||
return $this->route('missing_url');
|
||||
}
|
||||
}
|
||||
67
app/Http/Requests/Api/V1/RedirectRequest.php
Normal file
67
app/Http/Requests/Api/V1/RedirectRequest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Models\Redirect;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* The panel's redirect form, as rules. Both ends are normalised before they
|
||||
* are checked, so `/a/` and `/a` are the same rule however it was typed.
|
||||
*/
|
||||
class RedirectRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
foreach (['from', 'to'] as $end) {
|
||||
if (is_string($this->input($end)) && filled($this->input($end))) {
|
||||
$this->merge([$end => Redirect::normalise($this->input($end))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$redirect = $this->route('redirect');
|
||||
$creating = ! $redirect instanceof Redirect;
|
||||
$sometimes = $creating ? 'required' : 'sometimes';
|
||||
|
||||
$from = $this->input('from', $redirect?->from);
|
||||
$locale = $this->exists('locale') ? $this->input('locale') : $redirect?->locale;
|
||||
|
||||
return [
|
||||
'from' => [
|
||||
$sometimes,
|
||||
'string',
|
||||
'max:500',
|
||||
// One rule per address and language, as the table's index says.
|
||||
Rule::unique('redirects', 'from')
|
||||
->ignore($redirect?->id)
|
||||
->where(fn ($q) => $locale === null ? $q->whereNull('locale') : $q->where('locale', $locale)),
|
||||
],
|
||||
'to' => [$sometimes, 'string', 'max:500', Rule::notIn([$from])],
|
||||
'status' => ['sometimes', 'integer', Rule::in([301, 302])],
|
||||
'locale' => ['sometimes', 'nullable', 'string', Rule::in(array_keys((array) config('locales.supported')))],
|
||||
'is_enabled' => ['sometimes', 'boolean'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return ['to.not_in' => __('validation.different', ['attribute' => 'to', 'other' => 'from'])];
|
||||
}
|
||||
}
|
||||
50
app/Http/Requests/Api/V1/RepositoryRequest.php
Normal file
50
app/Http/Requests/Api/V1/RepositoryRequest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Enums\GitProvider;
|
||||
use App\Models\Repository;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* The panel's repository form, as rules. The synced columns are not here on
|
||||
* purpose: nothing a caller sends may fake a repository's numbers.
|
||||
*/
|
||||
class RepositoryRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$repository = $this->route('repository');
|
||||
$creating = ! $repository instanceof Repository;
|
||||
$sometimes = $creating ? 'required' : 'sometimes';
|
||||
|
||||
$provider = $this->input('provider', $repository?->provider?->value);
|
||||
|
||||
return [
|
||||
'provider' => [$sometimes, Rule::enum(GitProvider::class)],
|
||||
'full_name' => [
|
||||
$sometimes,
|
||||
'string',
|
||||
'max:255',
|
||||
'regex:/^[\w.-]+\/[\w.-]+$|^[\w.-]+$/',
|
||||
Rule::unique('repositories', 'full_name')
|
||||
->ignore($repository?->id)
|
||||
->where('provider', $provider),
|
||||
],
|
||||
'project_id' => ['sometimes', 'nullable', 'integer', Rule::exists('projects', 'id')->whereNull('deleted_at')],
|
||||
'is_primary' => ['sometimes', 'boolean'],
|
||||
'show_publicly' => ['sometimes', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user