Laravel-permission: Want to add a status(active/inactive) column to the roles table

Created on 20 Dec 2019  路  11Comments  路  Source: spatie/laravel-permission

Hi,

I am in situation where I need to add a status column for roles table. So that admin person can active or inactive a role. Also I want to know what will be the modifications in other functions like @can etc.

Please help, its urgent.
Thanks in advance.

support

All 11 comments

In the docs there's a section on Extending this package. In there it talks about "Replacing" the Role model. You'd want to do that. You'd need to also add your column to the db, and then update all the methods to check the status when returning a response.
Unfortunately I think you'll also need to override some of the methods in the HasRoles trait, as well as the middleware. Maybe even the ServiceProvider.

Potential shortcut: If your app uses only one Guard, then you could perhaps explore using the guard_name field as a status field, and just change the guard_name to something like 'disabled' until you want to re-enable it.

I am very thankful for your response.
I have added a column in role table and modified some core functions in HasRole. It is working fine for checking the permissions. But I am not able not find the required method for @can. Could you please tell me which function is need to modify for that?

The @can() only works on "permissions", not roles.
It appears you will need to add your custom restriction into the query that determines the permissions from the user's roles:
The PermissionServiceProvider reads the permissions from the user's roles, and then registers them onto Laravel's Gate, which then allows @can and ->can() to respond.

Thank you for this explanations. It helps me a lot. Ok I will modify the functions there.

@suparnaCC r u facing any problem after adding column to role.?

@segmatehire02 No, I am not facing any problem after adding column. Everything is working fine.

what kind of changes that you have made in HasRoles Trait and Service Provider, that @drbyte has mentioned?.

I have added an extra where condition for sql queries.
Like:
Before: $this->roles->where('guard_name', $guard)->contains('name', $roles)
After: $this->roles->where('guard_name', $guard)->where('status','active')->contains('name', $roles)

In my case i have to add column(foreign key of some table) in permission table. but let see what and how i have to made changes in HasPermission Trait. Thanks for the quick response.

Welcome. Happy coding.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dylangeorgeharbour picture dylangeorgeharbour  路  3Comments

Dreambox13 picture Dreambox13  路  3Comments

ergonomicus picture ergonomicus  路  3Comments

holymp2006 picture holymp2006  路  4Comments

feliperoan picture feliperoan  路  3Comments