CRUD зон/устройств/правил автоматизации + RBAC owner/viewer
Policies (ZonePolicy/DevicePolicy/AutomationRulePolicy): не multi-tenant изоляция по user_id, а household-wide RBAC — все авторизованные видят одни и те же данные, только owner может создавать/менять/удалять. user_id на записи — это created_by, а не граница видимости. Form Requests с authorize() через политики; DeviceRequest учитывает unique external_id с ignore на update. AutomationRuleRequest: action_type остаётся строкой (не enum), т.к. это открытый список из device_types.capabilities; для действия set_level в форме есть отдельное поле "level" (Alpine.js переключает его видимость), которое контроллер мапит в action_params — без сырого JSON-редактора для MVP. Контроллеры без show (index+create/edit достаточно), Blade-формы на базе Breeze-компонентов. Навигация и дашборд (счётчики зон/устройств/активных правил) обновлены. Добавлен DemoGrowboxSeeder (демо-зона с датчиком, вентилятором и правилом) и viewer-пользователь в сидер. Восстановлен трейт AuthorizesRequests в базовом Controller (Laravel 11+ убрал его по умолчанию) — нужен для $this->authorize(). Проверено: 33/33 теста (включая новые ZoneControllerTest, AutomationRuleControllerTest — маппинг level→action_params, 403 для viewer), полный CRUD-цикл вживую через браузер под owner (создание зоны, правила с set_level, Alpine-переключение поля "Уровень"), доступ viewer подтверждён как read-only (нет кнопок изменения, прямой заход на /zones/create отдаёт 403).
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\ConditionOperator;
|
||||
use App\Models\AutomationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AutomationRuleRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$rule = $this->route('automation_rule');
|
||||
|
||||
return $rule
|
||||
? $this->user()->can('update', $rule)
|
||||
: $this->user()->can('create', AutomationRule::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'zone_id' => ['required', 'exists:zones,id'],
|
||||
'target_device_id' => ['required', 'exists:devices,id'],
|
||||
'condition_source_device_id' => ['required', 'exists:devices,id'],
|
||||
'condition_sensor_type' => ['required', 'string', 'max:255'],
|
||||
'condition_operator' => ['required', Rule::enum(ConditionOperator::class)],
|
||||
'condition_value' => ['required', 'numeric'],
|
||||
'action_type' => ['required', 'string', 'max:255'],
|
||||
'level' => ['nullable', 'numeric', 'required_if:action_type,set_level'],
|
||||
'is_active' => ['boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Device;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class DeviceRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$device = $this->route('device');
|
||||
|
||||
return $device
|
||||
? $this->user()->can('update', $device)
|
||||
: $this->user()->can('create', Device::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$device = $this->route('device');
|
||||
|
||||
return [
|
||||
'zone_id' => ['required', 'exists:zones,id'],
|
||||
'device_type_id' => ['required', 'exists:device_types,id'],
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'external_id' => [
|
||||
'required', 'string', 'max:255',
|
||||
Rule::unique('devices', 'external_id')->ignore($device),
|
||||
],
|
||||
'protocol' => ['required', 'string', 'max:255'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Zone;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ZoneRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$zone = $this->route('zone');
|
||||
|
||||
return $zone
|
||||
? $this->user()->can('update', $zone)
|
||||
: $this->user()->can('create', Zone::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'description' => ['nullable', 'string'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user