Laravel 13 в laravel-app/, Breeze (Blade) для сессионного логина + Sanctum для API-токенов (задел под Vue/Flutter на этапе 3). Схема Postgres перенесена из сырого SQL в Laravel-миграции (владелец схемы теперь Laravel, Go-сервисы как читали/писали эти таблицы, так и продолжают). Роли/категории/ операторы условий — закрытые PHP-enum'ы (UserRole, DeviceCategory, ConditionOperator); action_type правил намеренно остаётся строкой — открытый, управляемый данными список, а не код. Сидер device_types с growbox-типами как пример данных, не бизнес-логика. Docker: один контейнер (php-fpm + nginx через supervisord, multi-stage сборка vendor/assets). Образ пока не --no-dev — сидер использует fake() для демо-аккаунта. Проверено: миграции применяются на реальном Postgres (в докере и локально), дефолтные тесты Breeze проходят (25/25), Breeze-логин подтверждён вживую через браузер, Sanctum корректно отдаёт 401 на защищённый /api/user без токена.
87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Auth;
|
|
|
|
use Illuminate\Auth\Events\Lockout;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class LoginRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'email' => ['required', 'string', 'email'],
|
|
'password' => ['required', 'string'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Attempt to authenticate the request's credentials.
|
|
*
|
|
* @throws ValidationException
|
|
*/
|
|
public function authenticate(): void
|
|
{
|
|
$this->ensureIsNotRateLimited();
|
|
|
|
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
|
|
RateLimiter::hit($this->throttleKey());
|
|
|
|
throw ValidationException::withMessages([
|
|
'email' => trans('auth.failed'),
|
|
]);
|
|
}
|
|
|
|
RateLimiter::clear($this->throttleKey());
|
|
}
|
|
|
|
/**
|
|
* Ensure the login request is not rate limited.
|
|
*
|
|
* @throws ValidationException
|
|
*/
|
|
public function ensureIsNotRateLimited(): void
|
|
{
|
|
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
|
|
return;
|
|
}
|
|
|
|
event(new Lockout($this));
|
|
|
|
$seconds = RateLimiter::availableIn($this->throttleKey());
|
|
|
|
throw ValidationException::withMessages([
|
|
'email' => trans('auth.throttle', [
|
|
'seconds' => $seconds,
|
|
'minutes' => ceil($seconds / 60),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the rate limiting throttle key for the request.
|
|
*/
|
|
public function throttleKey(): string
|
|
{
|
|
return Str::transliterate(Str::lower($this->string('email')).'|'.$this->ip());
|
|
}
|
|
}
|