Home How to structure DB tables to store user permissions over model instances?
Reply: 0

How to structure DB tables to store user permissions over model instances?

ezekkiel
1#
ezekkiel Published in 2018-01-13 05:38:31Z

I have to create a DB structure (Relational) and then the permissions check for a system where a user can have multiple roles (and inherit permissions from a Role) but over certain instances.

So a user can have a role of editor over the instance with id 1 but can also have the role of viewer over the instance with id of 2.

I'm using laravel and the idea at the end would be to have gates/policies

At the moment I did this using the following tables:

  • users (id, name, ...)
  • roles (id, label, name)
  • role_user (id, role_id, user_id)
  • permissions (id, label, name)
  • permission_role (permission_id, role_id)
  • instances (id, name, ...)
  • instance_role_user (role_user_id, instance_id, ...)

So I have the relation of User and Role, and the relation of that with the specific Instance is on instance_role_user table but I'm having troubles getting this relations work in Laravel so I can have a simple way of checking it like this:

$user = User::find(1);
$instance = Instance::find(1);

$canEdit = $user->can('edit')->on($instance);

I tried with packages like Spatie/laravel-permission and Bouncer but couldn't get those to work as I expect.

Any ideas or experience dealing with this using Laravel?

Thanks!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319715 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO