Files
lotteryLaravel/app/Http/Requests/Admin/AdminAgentLineStoreRequest.php
kang 395e1c7400 feat: refactor super admin to use is_super_admin flag and enhance site deletion logic
- Changed super admin detection from role-based to `is_super_admin` flag in AdminUser model
- Added `requireDefaultAdminSiteId()` method to throw validation error when no integration site exists
- Enhanced site deletion to migrate platform role bindings to fallback site and auto-delete site-specific admin accounts
- Made agent line code optional with auto-generation fallback using `{site_code}-agent-{counter}` format
2026-06-12 20:47:40 +08:00

77 lines
2.4 KiB
PHP

<?php
namespace App\Http\Requests\Admin;
use App\Http\Requests\Admin\Concerns\AgentProfileFieldRules;
use App\Http\Requests\ApiFormRequest;
use App\Models\AdminSite;
use App\Models\AgentNode;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
final class AdminAgentLineStoreRequest extends ApiFormRequest
{
use AgentProfileFieldRules;
public function authorize(): bool
{
return true;
}
protected function prepareForValidation(): void
{
$this->prepareAgentProfileFieldsForValidation();
if ($this->has('site_code')) {
$this->merge([
'site_code' => strtolower(trim((string) $this->input('site_code'))),
]);
}
if ($this->has('code')) {
$this->merge([
'code' => strtolower(trim((string) $this->input('code'))),
]);
}
}
/** @return array<string, mixed> */
public function rules(): array
{
return [
'site_code' => ['required', 'string', 'max:64', 'regex:/^[a-z0-9][a-z0-9_-]*$/', Rule::exists('admin_sites', 'code')],
'code' => ['sometimes', 'nullable', 'string', 'max:64', 'regex:/^[a-z0-9][a-z0-9_-]*$/', Rule::unique('agent_nodes', 'code')],
'name' => ['required', 'string', 'max:128'],
'username' => ['required', 'string', 'max:64', Rule::unique('admin_users', 'username')],
'password' => ['required', 'string', 'min:8', 'max:128'],
'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('admin_users', 'email')],
'status' => ['sometimes', 'integer', 'in:0,1'],
...$this->agentProfileFieldRules(),
];
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
if ($validator->errors()->isNotEmpty()) {
return;
}
$siteCode = (string) $this->input('site_code');
$site = AdminSite::query()->where('code', $siteCode)->first();
if ($site === null) {
return;
}
$hasRoot = AgentNode::query()
->where('admin_site_id', $site->id)
->where('depth', 0)
->exists();
if ($hasRoot) {
$validator->errors()->add('site_code', 'site_root_exists');
}
});
}
}