Lokesh M
Lokesh M

Reputation: 76

Laravel Spatie/Permission specific permission for each individual location/branch

I am trying to provide user role/permission using spatie/laravel.

Currently, it's working without issues, but I need to specify the user permission for each branch/locations (sub_locations table) for single user.

e.g.,

user A can edit something on sub_location 1, but can't edit that from sub_location 2 login.

Same, user B has an admin role in 1 & 4 locations and non admin in all other locations.

So, I need to provide permissions/role based on sub_locations.

I am not sure how to do that.

I need to control user permissions in both controller and views.

Also, I have not yet changed any default functions of spatie/laravel and it's tables.

Below are the user table structure.

Users Table

user_id (UQ),
password,
status, ("Y"=> Active, "N"=> Inactive)
//other fields

User_Locations Table

id (PK, AI),
user_id (FK),
location_id (FK),
sub_location_id (FK),
status ("Y"=> Active, "N"=> Inactive)

Locations Table

id (PK, AI),
location_id,
location_name

Sub_Locations Table

id (PK, AI),
location_id (FK),
sub_location_id,
sub_location_name

Upvotes: 1

Views: 90

Answers (1)

Lokesh M
Lokesh M

Reputation: 76

I have enabled the teams option in spatie and for each location I have linked the team's ID as foreign key it resolved the issue. Whenever the user switch location I have used setPermissionsTeamID() option to set the team id and corresponding permissions. I hope this will also help others in this case.

Upvotes: 0

Related Questions