From 27ea12901e305f4a66de0c8e71c40e43ada4fca3 Mon Sep 17 00:00:00 2001 From: Lars Stommer Date: Thu, 22 Oct 2026 10:13:55 +0200 Subject: [PATCH] Benutzerrollen (Administrator, Vermietung, Inhalte) und Benutzerverwaltung MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Rollenbasierte Policies, Navigationsgruppen - Media Library (Spatie) für die kommende Bildverwaltung installiert Co-Authored-By: Claude Opus 5.5 --- app/Enums/UserRole.php | 31 + .../Resources/Bookings/BookingResource.php | 3 + .../Resources/Guests/GuestResource.php | 3 + .../Resources/Properties/PropertyResource.php | 3 + .../Resources/Users/Pages/CreateUser.php | 11 + .../Resources/Users/Pages/EditUser.php | 19 + .../Resources/Users/Pages/ListUsers.php | 19 + .../Resources/Users/Schemas/UserForm.php | 41 ++ .../Resources/Users/Tables/UsersTable.php | 25 + app/Filament/Resources/Users/UserResource.php | 55 ++ app/Models/User.php | 13 +- app/Policies/BookingPolicy.php | 13 + app/Policies/GuestPolicy.php | 13 + app/Policies/PropertyPolicy.php | 13 + app/Policies/RolePolicy.php | 57 ++ app/Policies/UserPolicy.php | 21 + app/Providers/Filament/AdminPanelProvider.php | 1 + composer.json | 1 + composer.lock | 572 +++++++++++++++--- config/media-library.php | 363 +++++++++++ database/factories/UserFactory.php | 7 + .../2026_10_22_101129_create_media_table.php | 32 + ...6_10_22_101148_add_role_to_users_table.php | 28 + database/seeders/DatabaseSeeder.php | 2 + tests/Feature/AdminAccessTest.php | 76 +++ 25 files changed, 1341 insertions(+), 81 deletions(-) create mode 100644 app/Enums/UserRole.php create mode 100644 app/Filament/Resources/Users/Pages/CreateUser.php create mode 100644 app/Filament/Resources/Users/Pages/EditUser.php create mode 100644 app/Filament/Resources/Users/Pages/ListUsers.php create mode 100644 app/Filament/Resources/Users/Schemas/UserForm.php create mode 100644 app/Filament/Resources/Users/Tables/UsersTable.php create mode 100644 app/Filament/Resources/Users/UserResource.php create mode 100644 app/Policies/BookingPolicy.php create mode 100644 app/Policies/GuestPolicy.php create mode 100644 app/Policies/PropertyPolicy.php create mode 100644 app/Policies/RolePolicy.php create mode 100644 app/Policies/UserPolicy.php create mode 100644 config/media-library.php create mode 100644 database/migrations/2026_10_22_101129_create_media_table.php create mode 100644 database/migrations/2026_10_22_101148_add_role_to_users_table.php create mode 100644 tests/Feature/AdminAccessTest.php diff --git a/app/Enums/UserRole.php b/app/Enums/UserRole.php new file mode 100644 index 0000000..91f02e1 --- /dev/null +++ b/app/Enums/UserRole.php @@ -0,0 +1,31 @@ + 'Administrator', + self::Rentals => 'Vermietung', + self::Content => 'Inhalte', + }; + } + + public function getDescription(): string + { + return match ($this) { + self::Admin => 'Alles, inklusive Benutzerverwaltung', + self::Rentals => 'Buchungen, Gäste, Preise, Angebote und Bewertungen', + self::Content => 'Bilder, Texte, FAQ, Hinweise und Bewertungen', + }; + } +} diff --git a/app/Filament/Resources/Bookings/BookingResource.php b/app/Filament/Resources/Bookings/BookingResource.php index 1c98fe9..b7446a6 100644 --- a/app/Filament/Resources/Bookings/BookingResource.php +++ b/app/Filament/Resources/Bookings/BookingResource.php @@ -15,6 +15,7 @@ use Filament\Schemas\Schema; use Filament\Support\Icons\Heroicon; use Filament\Tables\Table; +use UnitEnum; class BookingResource extends Resource { @@ -22,6 +23,8 @@ class BookingResource extends Resource protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCalendarDays; + protected static string|UnitEnum|null $navigationGroup = 'Vermietung'; + protected static ?string $modelLabel = 'Buchung'; protected static ?string $pluralModelLabel = 'Anfragen & Buchungen'; diff --git a/app/Filament/Resources/Guests/GuestResource.php b/app/Filament/Resources/Guests/GuestResource.php index 911b51a..f9c7f1b 100644 --- a/app/Filament/Resources/Guests/GuestResource.php +++ b/app/Filament/Resources/Guests/GuestResource.php @@ -13,6 +13,7 @@ use Filament\Schemas\Schema; use Filament\Support\Icons\Heroicon; use Filament\Tables\Table; +use UnitEnum; class GuestResource extends Resource { @@ -20,6 +21,8 @@ class GuestResource extends Resource protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedUsers; + protected static string|UnitEnum|null $navigationGroup = 'Vermietung'; + protected static ?string $modelLabel = 'Gast'; protected static ?string $pluralModelLabel = 'Gäste'; diff --git a/app/Filament/Resources/Properties/PropertyResource.php b/app/Filament/Resources/Properties/PropertyResource.php index cfb838a..c997963 100644 --- a/app/Filament/Resources/Properties/PropertyResource.php +++ b/app/Filament/Resources/Properties/PropertyResource.php @@ -17,6 +17,7 @@ use Filament\Schemas\Schema; use Filament\Support\Icons\Heroicon; use Filament\Tables\Table; +use UnitEnum; class PropertyResource extends Resource { @@ -24,6 +25,8 @@ class PropertyResource extends Resource protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedHomeModern; + protected static string|UnitEnum|null $navigationGroup = 'Vermietung'; + protected static ?string $modelLabel = 'Wohnung'; protected static ?string $pluralModelLabel = 'Wohnungen'; diff --git a/app/Filament/Resources/Users/Pages/CreateUser.php b/app/Filament/Resources/Users/Pages/CreateUser.php new file mode 100644 index 0000000..125b3ff --- /dev/null +++ b/app/Filament/Resources/Users/Pages/CreateUser.php @@ -0,0 +1,11 @@ +columns(2) + ->components([ + TextInput::make('name') + ->label('Name') + ->required() + ->maxLength(255), + TextInput::make('email') + ->label('E-Mail (Anmeldename)') + ->email() + ->required() + ->unique(ignoreRecord: true) + ->maxLength(255), + Radio::make('role') + ->label('Rolle') + ->options(UserRole::class) + ->required() + ->columnSpanFull(), + TextInput::make('password') + ->label(fn (string $operation): string => $operation === 'create' ? 'Passwort' : 'Neues Passwort (leer lassen = unverändert)') + ->password() + ->revealable() + ->minLength(10) + ->required(fn (string $operation): bool => $operation === 'create') + ->dehydrated(fn (?string $state): bool => filled($state)), + ]); + } +} diff --git a/app/Filament/Resources/Users/Tables/UsersTable.php b/app/Filament/Resources/Users/Tables/UsersTable.php new file mode 100644 index 0000000..ba3a7e6 --- /dev/null +++ b/app/Filament/Resources/Users/Tables/UsersTable.php @@ -0,0 +1,25 @@ +columns([ + TextColumn::make('name')->label('Name')->searchable(), + TextColumn::make('email')->label('E-Mail')->searchable(), + TextColumn::make('role')->label('Rolle')->badge(), + ]) + ->recordActions([ + EditAction::make(), + DeleteAction::make(), + ]); + } +} diff --git a/app/Filament/Resources/Users/UserResource.php b/app/Filament/Resources/Users/UserResource.php new file mode 100644 index 0000000..7e46f6a --- /dev/null +++ b/app/Filament/Resources/Users/UserResource.php @@ -0,0 +1,55 @@ + ListUsers::route('/'), + 'create' => CreateUser::route('/create'), + 'edit' => EditUser::route('/{record}/edit'), + ]; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index c68d797..fb48bbf 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,6 +3,7 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; +use App\Enums\UserRole; use Database\Factories\UserFactory; use Filament\Models\Contracts\FilamentUser; use Filament\Panel; @@ -12,7 +13,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; -#[Fillable(['name', 'email', 'password'])] +#[Fillable(['name', 'email', 'role', 'password'])] #[Hidden(['password', 'remember_token'])] class User extends Authenticatable implements FilamentUser { @@ -20,11 +21,16 @@ class User extends Authenticatable implements FilamentUser use HasFactory, Notifiable; /** - * Only administrators have accounts; there is no public registration. + * Accounts are created by administrators only; an account without a role has no access. */ public function canAccessPanel(Panel $panel): bool { - return true; + return $this->role !== null; + } + + public function hasRole(UserRole ...$roles): bool + { + return in_array($this->role, $roles, true); } /** @@ -37,6 +43,7 @@ protected function casts(): array return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', + 'role' => UserRole::class, ]; } } diff --git a/app/Policies/BookingPolicy.php b/app/Policies/BookingPolicy.php new file mode 100644 index 0000000..d63de69 --- /dev/null +++ b/app/Policies/BookingPolicy.php @@ -0,0 +1,13 @@ + + */ + abstract protected function roles(): array; + + public function viewAny(User $user): bool + { + return $this->allows($user); + } + + public function view(User $user): bool + { + return $this->allows($user); + } + + public function create(User $user): bool + { + return $this->allows($user); + } + + public function update(User $user): bool + { + return $this->allows($user); + } + + public function delete(User $user): bool + { + return $this->allows($user); + } + + public function deleteAny(User $user): bool + { + return $this->allows($user); + } + + public function reorder(User $user): bool + { + return $this->allows($user); + } + + protected function allows(User $user): bool + { + return $user->hasRole(UserRole::Admin, ...$this->roles()); + } +} diff --git a/app/Policies/UserPolicy.php b/app/Policies/UserPolicy.php new file mode 100644 index 0000000..5a5789e --- /dev/null +++ b/app/Policies/UserPolicy.php @@ -0,0 +1,21 @@ +allows($user) && ! $user->is($model); + } +} diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index 4f7778d..33467e0 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -28,6 +28,7 @@ public function panel(Panel $panel): Panel ->path('admin') ->login() ->brandName('FeWo Stommer · Verwaltung') + ->navigationGroups(['Vermietung', 'Webseite', 'System']) ->colors([ 'primary' => Color::Sky, ]) diff --git a/composer.json b/composer.json index 95259d4..6b7f57c 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "php": "^8.3", "barryvdh/laravel-dompdf": "^3.1", "filament/filament": "^5.10", + "filament/spatie-laravel-media-library-plugin": "^5.0", "laravel/framework": "^13.17", "laravel/tinker": "^3.0" }, diff --git a/composer.lock b/composer.lock index 4fa982e..7e8cd4a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b824e8039e614f6ba029ba896794c96a", + "content-hash": "1f5e3ea95226f9ee49c3baddc64e655e", "packages": [ { "name": "anourvalar/eloquent-serialize", @@ -739,6 +739,83 @@ ], "time": "2026-06-07T11:47:49+00:00" }, + { + "name": "composer/semver", + "version": "3.5.1", + "source": { + "type": "git", + "url": "https://github.com/composer/semver.git", + "reference": "528948210cc46f967a4b98e83b19e76f2b504993" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/semver/zipball/528948210cc46f967a4b98e83b19e76f2b504993", + "reference": "528948210cc46f967a4b98e83b19e76f2b504993", + "shasum": "" + }, + "require": { + "php": "^5.3.2 || ^7.0 || ^8.0" + }, + "require-dev": { + "phpstan/phpstan": "^1.11", + "symfony/phpunit-bridge": "^3 || ^7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\Semver\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nils Adermann", + "email": "naderman@naderman.de", + "homepage": "http://www.naderman.de" + }, + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + }, + { + "name": "Rob Bast", + "email": "rob.bast@gmail.com", + "homepage": "http://robbast.nl" + } + ], + "description": "Version comparison library that offers utilities, version constraint parsing and validation.", + "keywords": [ + "semantic", + "semver", + "validation", + "versioning" + ], + "support": { + "irc": "ircs://irc.libera.chat:6697/composer", + "issues": "https://github.com/composer/semver/issues", + "source": "https://github.com/composer/semver/tree/3.5.1" + }, + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://github.com/composer", + "type": "github" + } + ], + "time": "2026-10-05T13:12:12+00:00" + }, { "name": "danharrin/date-format-converter", "version": "v0.3.1", @@ -1713,6 +1790,43 @@ }, "time": "2026-10-20T18:28:40+00:00" }, + { + "name": "filament/spatie-laravel-media-library-plugin", + "version": "v5.10.3", + "source": { + "type": "git", + "url": "https://github.com/filamentphp/spatie-laravel-media-library-plugin.git", + "reference": "062f9bfdc64cbe9ec1c65e0754a44d52514445ff" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/filamentphp/spatie-laravel-media-library-plugin/zipball/062f9bfdc64cbe9ec1c65e0754a44d52514445ff", + "reference": "062f9bfdc64cbe9ec1c65e0754a44d52514445ff", + "shasum": "" + }, + "require": { + "filament/support": "self.version", + "php": "^8.2", + "spatie/laravel-medialibrary": "^11.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Filament\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Filament support for `spatie/laravel-medialibrary`.", + "homepage": "https://github.com/filamentphp/filament", + "support": { + "issues": "https://github.com/filamentphp/filament/issues", + "source": "https://github.com/filamentphp/filament" + }, + "time": "2026-10-06T18:18:24+00:00" + }, { "name": "filament/support", "version": "v5.10.3", @@ -3704,6 +3818,84 @@ ], "time": "2026-10-21T12:53:46+00:00" }, + { + "name": "maennchen/zipstream-php", + "version": "3.2.2", + "source": { + "type": "git", + "url": "https://github.com/maennchen/ZipStream-PHP.git", + "reference": "77bebeb4c6c340bb3c11c843b2cffd8bbfde4d5e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maennchen/ZipStream-PHP/zipball/77bebeb4c6c340bb3c11c843b2cffd8bbfde4d5e", + "reference": "77bebeb4c6c340bb3c11c843b2cffd8bbfde4d5e", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "ext-zlib": "*", + "php-64bit": "^8.3" + }, + "require-dev": { + "brianium/paratest": "^7.7", + "ext-zip": "*", + "friendsofphp/php-cs-fixer": "^3.86", + "guzzlehttp/guzzle": "^7.5", + "mikey179/vfsstream": "^1.6", + "php-coveralls/php-coveralls": "^2.5", + "phpunit/phpunit": "^12.0", + "vimeo/psalm": "^6.0" + }, + "suggest": { + "guzzlehttp/psr7": "^2.4", + "psr/http-message": "^2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "ZipStream\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paul Duncan", + "email": "pabs@pablotron.org" + }, + { + "name": "Jonatan Männchen", + "email": "jonatan@maennchen.ch" + }, + { + "name": "Jesse Donat", + "email": "donatj@gmail.com" + }, + { + "name": "András Kolesár", + "email": "kolesar@kolesar.hu" + } + ], + "description": "ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.", + "keywords": [ + "stream", + "zip" + ], + "support": { + "issues": "https://github.com/maennchen/ZipStream-PHP/issues", + "source": "https://github.com/maennchen/ZipStream-PHP/tree/3.2.2" + }, + "funding": [ + { + "url": "https://github.com/maennchen", + "type": "github" + } + ], + "time": "2026-04-11T18:38:28+00:00" + }, { "name": "masterminds/html5", "version": "2.11.0", @@ -5603,6 +5795,136 @@ ], "time": "2022-12-17T21:53:22+00:00" }, + { + "name": "spatie/image", + "version": "3.9.9", + "source": { + "type": "git", + "url": "https://github.com/spatie/image.git", + "reference": "929c00fdab6c0c929e69524fffd1a658167cbcc2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/image/zipball/929c00fdab6c0c929e69524fffd1a658167cbcc2", + "reference": "929c00fdab6c0c929e69524fffd1a658167cbcc2", + "shasum": "" + }, + "require": { + "ext-exif": "*", + "ext-json": "*", + "ext-mbstring": "*", + "php": "^8.4", + "spatie/image-optimizer": "^1.7.5", + "spatie/temporary-directory": "^2.2", + "symfony/process": "^6.4|^7.0|^8.0" + }, + "require-dev": { + "ext-ffi": "*", + "ext-gd": "*", + "ext-imagick": "*", + "jcupitt/vips": "^2.4", + "laravel/sail": "^1.34", + "pestphp/pest": "^5.0", + "phpstan/phpstan": "^1.10.50", + "spatie/pest-plugin-snapshots": "^2.4", + "spatie/pixelmatch-php": "^1.0", + "spatie/ray": "^1.40.1", + "symfony/var-dumper": "^6.4|^7.0|^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\Image\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Manipulate images with an expressive API", + "homepage": "https://github.com/spatie/image", + "keywords": [ + "image", + "spatie" + ], + "support": { + "source": "https://github.com/spatie/image/tree/3.9.9" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2026-10-20T06:00:43+00:00" + }, + { + "name": "spatie/image-optimizer", + "version": "1.10.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/image-optimizer.git", + "reference": "333c03952289dc2df0a91874636a0dffeb5b6aec" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/image-optimizer/zipball/333c03952289dc2df0a91874636a0dffeb5b6aec", + "reference": "333c03952289dc2df0a91874636a0dffeb5b6aec", + "shasum": "" + }, + "require": { + "ext-fileinfo": "*", + "php": "^7.4|^8.0", + "psr/log": "^1.0 | ^2.0 | ^3.0", + "symfony/process": "^4.2|^5.0|^6.0|^7.0|^8.0" + }, + "require-dev": { + "pestphp/pest": "^1.21|^2.0|^3.0|^4.0", + "phpunit/phpunit": "^8.5.21|^9.4.4|^10.0|^11.0|^12.0", + "symfony/var-dumper": "^4.2|^5.0|^6.0|^7.0|^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\ImageOptimizer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Easily optimize images using PHP", + "homepage": "https://github.com/spatie/image-optimizer", + "keywords": [ + "image-optimizer", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/image-optimizer/issues", + "source": "https://github.com/spatie/image-optimizer/tree/1.10.0" + }, + "time": "2026-06-29T08:28:30+00:00" + }, { "name": "spatie/invade", "version": "2.2.0", @@ -5670,6 +5992,116 @@ ], "time": "2026-10-19T16:16:47+00:00" }, + { + "name": "spatie/laravel-medialibrary", + "version": "11.23.10", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-medialibrary.git", + "reference": "e32803a1295c001ad53a1e9c8468f5a39b4df37f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-medialibrary/zipball/e32803a1295c001ad53a1e9c8468f5a39b4df37f", + "reference": "e32803a1295c001ad53a1e9c8468f5a39b4df37f", + "shasum": "" + }, + "require": { + "composer/semver": "^3.4", + "ext-exif": "*", + "ext-fileinfo": "*", + "ext-json": "*", + "illuminate/bus": "^12.0|^13.0", + "illuminate/conditionable": "^12.0|^13.0", + "illuminate/console": "^12.0|^13.0", + "illuminate/database": "^12.0|^13.0", + "illuminate/pipeline": "^12.0|^13.0", + "illuminate/support": "^12.0|^13.0", + "maennchen/zipstream-php": "^3.1", + "php": "^8.2", + "spatie/image": "^3.3.2", + "spatie/laravel-package-tools": "^1.16.1", + "spatie/temporary-directory": "^2.2", + "symfony/console": "^6.4.1|^7.0|^8.0" + }, + "conflict": { + "php-ffmpeg/php-ffmpeg": "<0.6.1" + }, + "require-dev": { + "aws/aws-sdk-php": "^3.293.10", + "ext-imagick": "*", + "ext-pdo_sqlite": "*", + "ext-zip": "*", + "guzzlehttp/guzzle": "^7.8.1", + "larastan/larastan": "^3.13", + "league/flysystem-aws-s3-v3": "^3.22", + "mockery/mockery": "^1.6.7", + "orchestra/testbench": "^10.12|^11.3", + "pestphp/pest": "^3.8.7|^4.7.8|^5.3.3", + "phpstan/extension-installer": "^1.3.1", + "spatie/laravel-ray": "^1.33", + "spatie/pdf-to-image": "^2.2|^3.0", + "spatie/pest-expectations": "^1.13", + "spatie/pest-plugin-snapshots": "^2.3.1" + }, + "suggest": { + "league/flysystem-aws-s3-v3": "Required to use AWS S3 file storage", + "php-ffmpeg/php-ffmpeg": "Required for generating video thumbnails", + "spatie/pdf-to-image": "Required for generating thumbnails of PDFs and SVGs" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\MediaLibrary\\MediaLibraryServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\MediaLibrary\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Associate files with Eloquent models", + "homepage": "https://github.com/spatie/laravel-medialibrary", + "keywords": [ + "cms", + "conversion", + "downloads", + "images", + "laravel", + "laravel-medialibrary", + "media", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-medialibrary/issues", + "source": "https://github.com/spatie/laravel-medialibrary/tree/11.23.10" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2026-10-19T12:45:53+00:00" + }, { "name": "spatie/laravel-package-tools", "version": "1.93.3", @@ -5796,6 +6228,67 @@ ], "time": "2026-04-27T14:27:52+00:00" }, + { + "name": "spatie/temporary-directory", + "version": "2.4.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/temporary-directory.git", + "reference": "32cbb9645b28839cf4f476708e99a2c70e6802c9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/temporary-directory/zipball/32cbb9645b28839cf4f476708e99a2c70e6802c9", + "reference": "32cbb9645b28839cf4f476708e99a2c70e6802c9", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\TemporaryDirectory\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Easily create, use and destroy temporary directories", + "homepage": "https://github.com/spatie/temporary-directory", + "keywords": [ + "php", + "spatie", + "temporary-directory" + ], + "support": { + "issues": "https://github.com/spatie/temporary-directory/issues", + "source": "https://github.com/spatie/temporary-directory/tree/2.4.0" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2026-06-22T07:55:44+00:00" + }, { "name": "symfony/clock", "version": "v8.1.9", @@ -8795,83 +9288,6 @@ } ], "packages-dev": [ - { - "name": "composer/semver", - "version": "3.5.1", - "source": { - "type": "git", - "url": "https://github.com/composer/semver.git", - "reference": "528948210cc46f967a4b98e83b19e76f2b504993" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/composer/semver/zipball/528948210cc46f967a4b98e83b19e76f2b504993", - "reference": "528948210cc46f967a4b98e83b19e76f2b504993", - "shasum": "" - }, - "require": { - "php": "^5.3.2 || ^7.0 || ^8.0" - }, - "require-dev": { - "phpstan/phpstan": "^1.11", - "symfony/phpunit-bridge": "^3 || ^7" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "3.x-dev" - } - }, - "autoload": { - "psr-4": { - "Composer\\Semver\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nils Adermann", - "email": "naderman@naderman.de", - "homepage": "http://www.naderman.de" - }, - { - "name": "Jordi Boggiano", - "email": "j.boggiano@seld.be", - "homepage": "http://seld.be" - }, - { - "name": "Rob Bast", - "email": "rob.bast@gmail.com", - "homepage": "http://robbast.nl" - } - ], - "description": "Version comparison library that offers utilities, version constraint parsing and validation.", - "keywords": [ - "semantic", - "semver", - "validation", - "versioning" - ], - "support": { - "irc": "ircs://irc.libera.chat:6697/composer", - "issues": "https://github.com/composer/semver/issues", - "source": "https://github.com/composer/semver/tree/3.5.1" - }, - "funding": [ - { - "url": "https://packagist.com", - "type": "custom" - }, - { - "url": "https://github.com/composer", - "type": "github" - } - ], - "time": "2026-10-05T13:12:12+00:00" - }, { "name": "fakerphp/faker", "version": "v1.24.1", diff --git a/config/media-library.php b/config/media-library.php new file mode 100644 index 0000000..df8aad6 --- /dev/null +++ b/config/media-library.php @@ -0,0 +1,363 @@ + env('MEDIA_DISK', 'public'), + + /* + * The disk on which to store conversions (thumbnails, etc.) and responsive images + * when no disk is specified explicitly on the media collection or via + * `storingConversionsOnDisk()`. When left null, conversions are stored on the + * same disk as the original media — preserving previous behavior. + * + * This is useful when the originals live on a remote disk (e.g. S3) but the + * generated derivatives should stay local for faster access and lower egress. + */ + 'conversions_disk_name' => env('MEDIA_CONVERSIONS_DISK', null), + + /* + * The maximum file size of an item in bytes. + * Adding a larger file will result in an exception. + */ + 'max_file_size' => 1024 * 1024 * 10, // 10MB + + /* + * Uploads whose file name contains any of these extensions will be rejected. + * The check looks at every extension in the file name, so a file named + * `malicious.php.jpg` is blocked as well. Matching is case-insensitive + * and a leading dot is optional. + * + * The default list lives on the `FileAdder` class so the shipped config + * and the in-code fallback (used when the config is cached without the + * key) cannot drift. Override here to extend or shrink it. + */ + 'disallowed_extensions' => FileAdder::$defaultDisallowedExtensions, + + /* + * When this is set to an array of extensions, only uploads whose final + * extension is in the list will be accepted. Matching is case-insensitive + * and a leading dot is optional. The `disallowed_extensions` list above + * is still enforced, so an interior dangerous segment (such as the `php` + * in `shell.php.jpg`) is rejected even if the final extension is allowed. + * Leave `null` to disable allowlisting. + */ + 'allowed_extensions' => null, + + /* + * This queue connection will be used to generate derived and responsive images. + * Leave empty to use the default queue connection. + */ + 'queue_connection_name' => env('QUEUE_CONNECTION', 'sync'), + + /* + * This queue will be used to generate derived and responsive images. + * Leave empty to use the default queue. + */ + 'queue_name' => env('MEDIA_QUEUE', ''), + + /* + * By default all conversions will be performed on a queue. + */ + 'queue_conversions_by_default' => env('QUEUE_CONVERSIONS_BY_DEFAULT', false), + + /* + * Should database transactions be run after database commits? + */ + 'queue_conversions_after_database_commit' => env('QUEUE_CONVERSIONS_AFTER_DB_COMMIT', true), + + /* + * The fully qualified class name of the media model. + */ + 'media_model' => Media::class, + + /* + * The fully qualified class name of the media observer. + */ + 'media_observer' => MediaObserver::class, + + /* + * When enabled, media collections will be serialised using the default + * laravel model serialization behaviour. + * + * Keep this option disabled if using Media Library Pro components (https://medialibrary.pro) + */ + 'use_default_collection_serialization' => false, + + /* + * The fully qualified class name of the model used for temporary uploads. + * + * This model is only used in Media Library Pro (https://medialibrary.pro) + */ + 'temporary_upload_model' => TemporaryUpload::class, + + /* + * When enabled, Media Library Pro will only process temporary uploads that were uploaded + * in the same session. You can opt to disable this for stateless usage of + * the pro components. + */ + 'enable_temporary_uploads_session_affinity' => true, + + /* + * When enabled, Media Library pro will generate thumbnails for uploaded file. + */ + 'generate_thumbnails_for_temporary_uploads' => true, + + /* + * This is the class that is responsible for naming generated files. + */ + 'file_namer' => DefaultFileNamer::class, + + /* + * The class that contains the strategy for determining a media file's path. + */ + 'path_generator' => DefaultPathGenerator::class, + + /* + * The class that contains the strategy for determining how to remove files. + */ + 'file_remover_class' => DefaultFileRemover::class, + + /* + * Here you can specify which path generator should be used for the given class. + */ + 'custom_path_generators' => [ + // Model::class => PathGenerator::class + // or + // 'model_morph_alias' => PathGenerator::class + ], + + /* + * When urls to files get generated, this class will be called. Use the default + * if your files are stored locally above the site root or on s3. + */ + 'url_generator' => DefaultUrlGenerator::class, + + /* + * Moves media on updating to keep path consistent. Enable it only with a custom + * PathGenerator that uses, for example, the media UUID. + */ + 'moves_media_on_update' => false, + + /* + * Whether to activate versioning when urls to files get generated. + * When activated, this attaches a ?v=xx query string to the URL. + */ + 'version_urls' => false, + + /* + * The media library will try to optimize all converted images by removing + * metadata and applying a little bit of compression. These are + * the optimizers that will be used by default. + */ + 'image_optimizers' => [ + Jpegoptim::class => [ + '-m85', // set maximum quality to 85% + '--force', // ensure that progressive generation is always done also if a little bigger + '--strip-all', // this strips out all text information such as comments and EXIF data + '--all-progressive', // this will make sure the resulting image is a progressive one + ], + Pngquant::class => [ + '--force', // required parameter for this package + ], + Optipng::class => [ + '-i0', // this will result in a non-interlaced, progressive scanned image + '-o2', // this set the optimization level to two (multiple IDAT compression trials) + '-quiet', // required parameter for this package + ], + Svgo::class => [ + '--disable=cleanupIDs', // disabling because it is known to cause troubles + ], + Gifsicle::class => [ + '-b', // required parameter for this package + '-O3', // this produces the slowest but best results + ], + Cwebp::class => [ + '-m 6', // for the slowest compression method in order to get the best compression. + '-pass 10', // for maximizing the amount of analysis pass. + '-mt', // multithreading for some speed improvements. + '-q 90', // quality factor that brings the least noticeable changes. + ], + Avifenc::class => [ + '-a cq-level=23', // constant quality level, lower values mean better quality and greater file size (0-63). + '-j all', // number of jobs (worker threads, "all" uses all available cores). + '--min 0', // min quantizer for color (0-63). + '--max 63', // max quantizer for color (0-63). + '--minalpha 0', // min quantizer for alpha (0-63). + '--maxalpha 63', // max quantizer for alpha (0-63). + '-a end-usage=q', // rate control mode set to Constant Quality mode. + '-a tune=ssim', // SSIM as tune the encoder for distortion metric. + ], + ], + + /* + * These generators will be used to create an image of media files. + */ + 'image_generators' => [ + Image::class, + Webp::class, + Avif::class, + Pdf::class, + Svg::class, + Video::class, + ], + + /* + * The path where to store temporary files while performing image conversions. + * If set to null, storage_path('media-library/temp') will be used. + */ + 'temporary_directory_path' => null, + + /* + * The engine that should perform the image conversions. + * Should be either `gd`, `imagick` or `vips`. + */ + 'image_driver' => env('IMAGE_DRIVER', 'gd'), + + /* + * FFMPEG & FFProbe binaries paths, only used if you try to generate video + * thumbnails and have installed the php-ffmpeg/php-ffmpeg composer + * dependency. + */ + 'ffmpeg_path' => env('FFMPEG_PATH', '/usr/bin/ffmpeg'), + 'ffprobe_path' => env('FFPROBE_PATH', '/usr/bin/ffprobe'), + + /* + * The timeout (in seconds) that will be used when generating video + * thumbnails via FFMPEG. + */ + 'ffmpeg_timeout' => env('FFMPEG_TIMEOUT', 900), + + /* + * The number of threads that FFMPEG should use. 0 means that FFMPEG + * may decide itself. + */ + 'ffmpeg_threads' => env('FFMPEG_THREADS', 0), + + /* + * Here you can override the class names of the jobs used by this package. Make sure + * your custom jobs extend the ones provided by the package. + */ + 'jobs' => [ + 'perform_conversions' => PerformConversionsJob::class, + 'generate_responsive_images' => GenerateResponsiveImagesJob::class, + ], + + /* + * When using the addMediaFromUrl method you may want to replace the default downloader. + * This is particularly useful when the url of the image is behind a firewall and + * need to add additional flags, possibly using curl. + */ + 'media_downloader' => DefaultDownloader::class, + + /* + * When using the addMediaFromUrl method the SSL is verified by default. + * This is option disables SSL verification when downloading remote media. + * Please note that this is a security risk and should only be false in a local environment. + */ + 'media_downloader_ssl' => env('MEDIA_DOWNLOADER_SSL', true), + + /* + * The default lifetime in minutes for temporary urls. + * This is used when you call the `getLastTemporaryUrl` or `getLastTemporaryUrl` method on a media item. + */ + 'temporary_url_default_lifetime' => env('MEDIA_TEMPORARY_URL_DEFAULT_LIFETIME', 5), + + 'remote' => [ + /* + * Any extra headers that should be included when uploading media to + * a remote disk. Even though supported headers may vary between + * different drivers, a sensible default has been provided. + * + * Supported by S3: CacheControl, Expires, StorageClass, + * ServerSideEncryption, Metadata, ACL, ContentEncoding + */ + 'extra_headers' => [ + 'CacheControl' => 'max-age=604800', + ], + ], + + 'responsive_images' => [ + /* + * This class is responsible for calculating the target widths of the responsive + * images. By default we optimize for filesize and create variations that each are 30% + * smaller than the previous one. More info in the documentation. + * + * https://docs.spatie.be/laravel-medialibrary/v9/advanced-usage/generating-responsive-images + */ + 'width_calculator' => FileSizeOptimizedWidthCalculator::class, + + /* + * By default rendering media to a responsive image will add some javascript and a tiny placeholder. + * This ensures that the browser can already determine the correct layout. + * When disabled, no tiny placeholder is generated. + */ + 'use_tiny_placeholders' => true, + + /* + * This class will generate the tiny placeholder used for progressive image loading. By default + * the media library will use a tiny blurred jpg image. + */ + 'tiny_placeholder_generator' => Blurred::class, + ], + + /* + * When enabling this option, a route will be registered that will enable + * the Media Library Pro Vue and React components to move uploaded files + * in a S3 bucket to their right place. + */ + 'enable_vapor_uploads' => env('ENABLE_MEDIA_LIBRARY_VAPOR_UPLOADS', false), + + /* + * When converting Media instances to response the media library will add + * a `loading` attribute to the `img` tag. Here you can set the default + * value of that attribute. + * + * Possible values: 'lazy', 'eager', 'auto' or null if you don't want to set any loading instruction. + * + * More info: https://css-tricks.com/native-lazy-loading/ + */ + 'default_loading_attribute_value' => null, + + /* + * You can specify a prefix for that is used for storing all media. + * If you set this to `/my-subdir`, all your media will be stored in a `/my-subdir` directory. + */ + 'prefix' => env('MEDIA_PREFIX', ''), + + /* + * When forcing lazy loading, media will be loaded even if you don't eager load media and you have + * disabled lazy loading globally in the service provider. + */ + 'force_lazy_loading' => env('FORCE_MEDIA_LIBRARY_LAZY_LOADING', true), +]; diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index c4ceb07..464c802 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Enums\UserRole; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; @@ -30,9 +31,15 @@ public function definition(): array 'email_verified_at' => now(), 'password' => static::$password ??= Hash::make('password'), 'remember_token' => Str::random(10), + 'role' => UserRole::Admin, ]; } + public function role(?UserRole $role): static + { + return $this->state(fn (): array => ['role' => $role]); + } + /** * Indicate that the model's email address should be unverified. */ diff --git a/database/migrations/2026_10_22_101129_create_media_table.php b/database/migrations/2026_10_22_101129_create_media_table.php new file mode 100644 index 0000000..47a4be9 --- /dev/null +++ b/database/migrations/2026_10_22_101129_create_media_table.php @@ -0,0 +1,32 @@ +id(); + + $table->morphs('model'); + $table->uuid()->nullable()->unique(); + $table->string('collection_name'); + $table->string('name'); + $table->string('file_name'); + $table->string('mime_type')->nullable(); + $table->string('disk'); + $table->string('conversions_disk')->nullable(); + $table->unsignedBigInteger('size'); + $table->json('manipulations'); + $table->json('custom_properties'); + $table->json('generated_conversions'); + $table->json('responsive_images'); + $table->unsignedInteger('order_column')->nullable()->index(); + + $table->nullableTimestamps(); + }); + } +}; diff --git a/database/migrations/2026_10_22_101148_add_role_to_users_table.php b/database/migrations/2026_10_22_101148_add_role_to_users_table.php new file mode 100644 index 0000000..2be4164 --- /dev/null +++ b/database/migrations/2026_10_22_101148_add_role_to_users_table.php @@ -0,0 +1,28 @@ +string('role')->nullable()->after('email'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('role'); + }); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0f7a957..ea08279 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Enums\UserRole; use App\Models\User; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; @@ -18,6 +19,7 @@ public function run(): void User::factory()->create([ 'name' => 'Admin', 'email' => 'admin@example.com', + 'role' => UserRole::Admin, ]); $this->call(PropertySeeder::class); diff --git a/tests/Feature/AdminAccessTest.php b/tests/Feature/AdminAccessTest.php new file mode 100644 index 0000000..c2d30bf --- /dev/null +++ b/tests/Feature/AdminAccessTest.php @@ -0,0 +1,76 @@ + + */ + public static function roleProvider(): array + { + return [ + 'Administrator' => [UserRole::Admin, true, true, true], + 'Vermietung' => [UserRole::Rentals, true, true, false], + 'Inhalte' => [UserRole::Content, false, false, false], + ]; + } + + #[DataProvider('roleProvider')] + public function test_roles_see_only_their_areas(UserRole $role, bool $bookings, bool $properties, bool $users): void + { + $this->actingAs(User::factory()->role($role)->create()); + + $this->get(BookingResource::getUrl('index'))->assertStatus($bookings ? 200 : 403); + $this->get(PropertyResource::getUrl('index'))->assertStatus($properties ? 200 : 403); + $this->get(UserResource::getUrl('index'))->assertStatus($users ? 200 : 403); + } + + public function test_account_without_role_cannot_enter_the_admin(): void + { + $this->actingAs(User::factory()->role(null)->create()); + + $this->get(BookingResource::getUrl('index'))->assertForbidden(); + } + + public function test_administrator_creates_a_user_with_role(): void + { + $this->actingAs(User::factory()->create()); + + Livewire::test(CreateUser::class) + ->fillForm([ + 'name' => 'Brigitte Stommer', + 'email' => 'brigitte@example.com', + 'role' => UserRole::Rentals->value, + 'password' => 'sehr-geheim-123', + ]) + ->call('create') + ->assertHasNoFormErrors(); + + $user = User::where('email', 'brigitte@example.com')->sole(); + $this->assertSame(UserRole::Rentals, $user->role); + $this->assertTrue(Hash::check('sehr-geheim-123', $user->password)); + } + + public function test_administrators_cannot_delete_themselves(): void + { + $admin = User::factory()->create(); + + $this->assertFalse($admin->can('delete', $admin)); + $this->assertTrue($admin->can('delete', User::factory()->create())); + } +}