laravel-acl
laravel-acl copied to clipboard
assignRole() does not work, but roles->attach() does
I am attempting to set up laravel acl, and ran across an issue with assignRole().
I am running laravel 5.2 and acl 0.1.3
Here is unit test code to illustrate the issue:
$roleAdmin = new Role();
$roleAdmin->name = 'Administrator';
$roleAdmin->slug = 'administrator';
$roleAdmin->description = 'manage administration privileges';
$roleAdmin->save();
$user = User::find(1);
$user->roles()->attach($roleAdmin->id);
$this->assertTrue($user->is('administrator')); // test passes
$user = User::find(2);
$user->assignRole($roleAdmin);
$this->assertTrue($user->is('administrator')); // test fails
Note that this includes the most recent pull request that updates HasRole::is() to use contains() rather than in_array(), otherwise it throws an exception.
Please let me know what the issue might be or if more information is needed to figure out the issue.
I'm having this issue too. syncRoles() works, but assignRole() doesn't. 😞
Well... I noticed I had to re-query the user to get the updated roles:
<?php
$user = User::first();
$user->syncRoles(['dev']);
$user->assignRole('customer');
$user2 = User::first();
dd($user->getRoles(), $user2->getRoles());
Outputs:
array:1 [
0 => "dev"
]
array:2 [
0 => "dev"
1 => "customer"
]