user(); return $user && $user->hasRole(['profit-center', 'root', 'developer']) && Feature::for($user)->active(self::FEATURE); } /** * A Product modellre a Laravel automatikusan a ProductPolicy-t köti, az viszont a * (beszállítói/admin) szerkesztésre készült és a profit-center szerepkört nem ismeri. * A rekord-szintű szűrést a getEloquentQuery() végzi (idegen termék 404), ezért itt * a resource-szintű jogosultság dönt. */ public static function canView($record): bool { return static::canViewAny(); } public static function canCreate(): bool { return false; } public static function canEdit($record): bool { return false; } public static function canDelete($record): bool { return false; } public static function canDeleteAny(): bool { return false; } public static function getEloquentQuery(): Builder { $query = parent::getEloquentQuery() ->with(['Supplier', 'Producer', 'ProductGroup']) ->inActivePriceList() ->withActivePrice(); /** @var User|null $user */ $user = auth()->user(); if ($user?->hasRole('profit-center')) { $query->whereIn('products.supplier_id', static::profitCenterSupplierIds($user)); } return $query; } /** * A profitcenter beszállítói: a felhasználó első profitcentere (1:1 használat), csak * a rendelésnél is látható beszállítók. Profitcenter nélküli felhasználó nem lát semmit. * * @return array */ public static function profitCenterSupplierIds(User $user): array { return $user->profitCenters()->first() ?->suppliers() ->where('suppliers.canSeeInOrder', 1) ->pluck('suppliers.id') ->all() ?? []; } public static function infolist(Schema $schema): Schema { return ProductInfolist::configure($schema); } public static function table(Table $table): Table { return ProductsTable::configure($table); } public static function getPages(): array { return [ 'index' => ListProducts::route('/'), 'view' => ViewProduct::route('/{record}'), ]; } }