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
[Improvement] Set scope pivot value by default to enable easy relationship updates #551
Comments
My proposed solution does not work yet because of the following (apparently unsolvable) issue: laravel/framework#29741 Another approach (without side-effects) is to introduce a pivot model:
|
Roles and abilities in Bouncer are never meant to be done directly on the relation yourself. Always use the In your case, where you're saving all of the user's settings at once, use the Bouncer::sync($user)->abilities($abilities);
Bouncer::sync($user)->roles($roles); |
@JosephSilber thanks for the quick response! |
As background info: I am using https://github.com/cloudcreativity/laravel-json-api for my api. It would be really dirty to overwrite the pre-build logic for updating relationships. In the background the default Laravel sync method is invoked which basically does the same thing as |
As you've already discovered with Again, always use the |
Ok, I understand your point but will probably keep it like it is as it works and I don't need all the complexity of Bouncer. |
What does this improvement solve?
When managing roles and abilities in a SPA, you need to communicate updates with the API. If the api (like mine) uses traditional Laravel/Eloquent relation handling to update the assigned roles and permissions of a user, you cannot use the Bouncer facade (which does all the magic).
This results in relations being created but the pivot values for the scope not being filled.
I already have a middleware to set the global Bouncer scope before the update and a hook to refresh Bouncer after the update. Whats missing is setting the scope on the
permissions
andassigned_roles
table automatically.How can it be solved?
After digging deeply into Laravel I found out that you can actually set default pivot values (laravel/framework#22867).
So you could easily update the
HasRoles
andHasAbilities
trait like follows:I already created a new trait which overwrites those methods and it works perfectly. Still, I think others would be happy to have this, too.
Let me know what you think and if there are any side-effects I did not think of?
The text was updated successfully, but these errors were encountered: