varius
varius

Reputation: 31

Laravel 9 : wrong roles (spatie/laravel-permission)

I use Laravel 9, breeze and the package spatie/laravel-permission and I made some modifications to be able to assign a role after the authentification: (I check the number in the column "id_role" from my table User to known the role )

AuthenticatedSessionController:

public function store(LoginRequest $request)
    {
        $request->authenticate();

        $request->session()->regenerate();
        $user = Auth::user();

        if ($user->id_role==2) {
            $user->assignRole('admin');
         }
        if($user->id_role==1)
         {
            $user->assignRole('user');
         }
         //test
        return redirect('/');
        //return redirect()->intended(RouteServiceProvider::HOME);
    }

And I used the following seeder to created those role:

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // Reset cached roles and permissions
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        // create permissions
        Permission::create(['name' => 'edit comments']);
        Permission::create(['name' => 'delete comments']);
        Permission::create(['name' => 'create comments']);

        Permission::create(['name' => 'edit projects']);
        Permission::create(['name' => 'delete projects']);
        Permission::create(['name' => 'create projects']);

        Permission::create(['name' => 'delete images']);
        Permission::create(['name' => 'create images']);

        // create roles and assign existing permissions
        $role1 = Role::create(['name' => 'user']);
        $role1->givePermissionTo('edit comments');
        $role1->givePermissionTo('delete comments');
        $role1->givePermissionTo('create comments');

        $role2 = Role::create(['name' => 'Super-Admin']);

        
    }

class Database2Seeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $role1 = Role::create(['name' => 'admin']);
        $role1->givePermissionTo('edit comments');
        $role1->givePermissionTo('delete comments');
        $role1->givePermissionTo('create comments');
    }
}

But the problem is than every time I'm login as a 'admin' I have the role 'admin' and 'user' .

Like in this blade (view) in see the content for the both role in the same time:

@foreach ($comments as $comment)
        <tr>
            <td>"{{ $comment->com }}"</td>
            <br>

            @role('user')
                USER
            @endrole

            @role('admin')
            <a class="btn btn-primary" href="{{ route('comments.destroy',$comment->id)  }}">Delete</a>
            @endrole
@foreach ($comments as $comment)
        <tr>
            <td>"{{ $comment->com }}"</td>
            <br>

            @role('user')
                USER
            @endrole

            @role('admin')
            <a class="btn btn-primary" href="{{ route('comments.destroy',$comment->id)  }}">Delete</a>
            @endrole

Upvotes: 1

Views: 649

Answers (1)

Mahdi Rashidi
Mahdi Rashidi

Reputation: 437

It seems that you assigned both roles to the user.

Check models_has_roles tables and remove the extra assigned role.

Upvotes: 0

Related Questions