Rollen für Langutor

Aus Vokabulabor
Zur Navigation springen Zur Suche springen

Links

Zielsetzung

Es soll ein Rollen-/Rechtesystem mittels des Paketes spatie/laravel-permission eingerichtet werden.

Vorgehen

composer require spatie/laravel-permission

app/Http/Kernel.php

protected $middlewareAliases = [
        ....
        'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
        'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
        'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
    ];

app\Models\User.php

...
  use HasApiTokens, HasFactory, Notifiable, HasRoles;
...
php artisan make:seeder PermissionSeeder
php artisan make:seeder RoleSeeder
php artisan make:seeder SuperAdminSeeder
  • database\seeders\PermissionSeeder.php
  use Spatie\Permission\Models\Permission;
...
    public function run(): void
    {
        $permissions = [
           'create-role',
            'edit-role',
            'delete-role',
            'create-user',
            'edit-user',
            'delete-user',
            'create-data',
            'edit-data',
            'delete-data'
        ];
        // Looping and Inserting Array's Permissions into Permission Table
        foreach ($permissions as $permission) {
            Permission::create(['name' => $permission]);
        }
    }

database\seeders\RoleSeeder.php

  use Spatie\Permission\Models\Role;
...
    public function run(): void
    {
        Role::create(['name' => 'Super Admin']);
        $admin = Role::create(['name' => 'Admin']);
        $manager = Role::create(['name' => 'Manager']);
        $student = Role::create(['name' => 'Student']);
        $admin->givePermissionTo([
            'create-user',
            'edit-user',
            'delete-user',
            'create-product',
            'edit-product',
            'delete-product',
            'create-data',
            'edit-data',
            'delete-data'
        ]);
        $manager->givePermissionTo([
            'create-data',
            'edit-data',
            'delete-data'
        ]);
        $student->givePermissionTo([
            'create-data',
            'edit-data'
        ]);
    }

database\seeders\SuperAdminSeeder.php

  use App\Models\User;
  use Illuminate\Support\Facades\Hash;
...
    public function run(): void
    {
        // Creating Super Admin User
        $superAdmin = User::create([
            'name' => 'Langutor',
            'email' => 'langutor@hamatoma.de',
            'password' => Hash::make('topSecret')
        ]);
        $superAdmin->assignRole('Super Admin');

        // Creating Admin User
        $admin = User::create([
            'name' => 'administrator',
            'email' => 'langutor@hamatoma.de',
            'password' => Hash::make('topSecret')
        ]);
        $admin->assignRole('Admin');

        // Creating Product Manager User
        $manager = User::create([
            'name' => 'Alice',
            'email' => 'alice@hamatoma.de',
            'password' => Hash::make('topSecret')
        ]);
        $manager->assignRole('Manager');
    }

database\seeders\DatabaseSeeder.php

use Database\Seeders\RoleSeeder;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\SuperAdminSeeder;
...
    public function run(): void
    {
        // \App\Models\User::factory(10)->create();
        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);
        $this->call([
            PermissionSeeder::class,
            RoleSeeder::class,
            SuperAdminSeeder::class,
        ]);
    }

Terminal

php artisan migrate:fresh --seed

app\Providers\AuthServiceProvider.php

use Illuminate\Support\Facades\Gate;
...
    public function boot(): void
    {
        Gate::before(function ($user, $ability) {
            return $user->hasRole('Super Admin') ? true : null;
        });
    }

Enable Bootstrap 5

  • app\Providers\AppServiceProvider.php
use Illuminate\Pagination\Paginator;
...
    public function boot(): void
    {
        Paginator::useBootstrapFive();
    }

routes/web.php

Route::resources([
    'roles' => RoleController::class,
    'users' => UserController::class,
]);