Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync Roles detaches from other models with different entity_type #529

Closed
o-zaengle opened this issue Jul 2, 2020 · 1 comment
Closed
Labels

Comments

@o-zaengle
Copy link

o-zaengle commented Jul 2, 2020

Given two models that utilize the HasRolesAndAbilities trait., let's call them Member and User. A User is assigned the role of Admin, and a Member (with the same id as the User) is also assigned the role of Admin. When syncing roles to a Member I am experiencing that any role being detached from the Member l is also being detached from the User .

The following test fails due to the assigned_roles table not containing the role for the User.

I would expect that it would exist and syncing the roles on the Member model would not impact the roles on the User model.

  public function testBasicTest()
    {
        $role = Bouncer::role()->firstOrCreate([
            'name' => 'admin',
            'title' => 'Administrator',
        ]);

        $user = factory(User::class)->create();
        $user->assign('admin');

        $member = factory(Member::class)->create();
        $member->assign('admin');

        $this->assertDatabaseHas('assigned_roles', [
            'role_id' => $role->getKey(),
            'entity_id' => $user->getKey(),
            'entity_type' => $user->getMorphClass(),
        ]);

        $this->assertDatabaseHas('assigned_roles', [
            'role_id' => $role->getKey(),
            'entity_id' => $member->getKey(),
            'entity_type' => $member->getMorphClass(),
        ]);

        Bouncer::sync($member)->roles([]);

        $this->assertDatabaseHas('assigned_roles', [
            'role_id' => $role->getKey(),
            'entity_id' => $user->getKey(),
            'entity_type' => $user->getMorphClass(),
        ]);

        $this->assertDatabaseMissing('assigned_roles', [
            'role_id' => $role->getKey(),
            'entity_id' => $member->getKey(),
            'entity_type' => $member->getMorphClass(),
        ]);
    }

  • PHP version: 7.4.7

  • Laravel Framework: 7.18.0

  • PostgreSQL: 12.3

  • Bouncer: 1.0.0-rc.8

This also occurs on earlier versions of the package.

@JosephSilber
Copy link
Owner

Thanks for the detailed report 👍

Let me know if it's fixed for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants