-
app/Rules/FirewallPort.php
Open in GitHubuse Closure; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Str; class FirewallPort implements ValidationRule { public function validate(string $attribute, mixed $value, Closure $fail): void { if (is_numeric($value)) { if ($value < 1 || $value > 65535) { $fail(__('The :attribute field must be between :min and :max.', ['min' => 1, 'max' => 65535])); } return; } if (Str::substrCount($value, ':') !== 1) { $fail(__('The :attribute field is invalid.')); return; } [$fromPort, $toPort] = explode(':', $value); if ($toPort < $fromPort) { $fail(__('The range is invalid.')); } if ($toPort < 1 || $toPort > 65535) { $fail(__('The :attribute field must be between :min and :max.', ['min' => 1, 'max' => 65535])); } if ($fromPort < 1 || $fromPort > 65535) { $fail(__('The :attribute field must be between :min and :max.', ['min' => 1, 'max' => 65535])); } } }
-
app/Http/Controllers/FirewallRuleController.php
Open in GitHubuse App\Enum; use App\Models\Server; use App\Rules\FirewallPort; use App\Server\Firewall\RuleAction; use Illuminate\Http\Request; class FirewallRuleController extends Controller { // ... public function store(Server $server, Request $request) { $data = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'action' => ['required', 'string', Enum::rule(RuleAction::class)], 'port' => ['required', new FirewallPort], 'from_ipv4' => ['nullable', 'string', 'ipv4'], ]); // ... } // ... }