skip to Main Content

In my application, an organization has multiple events, and a user can have multiple roles in different events, Until now it’s all good I can just do a pivot relation btw user_id, role_id, event_id, But the problem is that I have some roles that are focused to the organization ex: Owner, Admin and they’re not related to the event. And therefore the previous DB structure cannot fix my problem cuz it’s limited to users having roles in an event.
I’ve tried going for a polymorphic relationship which basically means a user can have a role either in an event or an organization like so: user_id, role_id, entity_id, entity_type, But it’s a lot of work and I don’t know if it’s the ideal solution.

What I’ve tried :

The one solution that I found is working with spatie’s permissions package which supports teams.
So basically a user can have a role in an event, also a user can have permission in an event, So I figured that I work with the organization’s scope instead of the event’s scope and then give permission to the user to access a specific event. Ex: I invite a user to the organization to be an Admin and then give him the right (permission) to manage a specific event or multi events.
This seems to work, but the problem with it is that I’m limited to assigning one role to the user in that organization which means in all of the events, Cuz now the DB relationship is :
user_id, role_id, organization_id.

Is there any way I can assign diffrent roles in diffrent events for the user, along with the ability to assign some roles outside of the events for some users ?

2

Answers


  1. Chosen as BEST ANSWER

    Eventually, after searching and asking other senior engineers I ended up going with the polymorphic solution, I even proposed my solution to ChatGPT and it agreed with it. though it gave another answer at first, after proposing my solution it agreed that it was the optimal one. I'm posting its explanation for the solution I think it's well described :

    enter image description here

    DISCLAIMER: I'm the one who came up with that solution!! I just asked ChatGPT if there is a better solution and she gave me a recap of what I suggested.


  2. Will you try following if this works for you?

    create a talbe user_event_roles

    its structure will be like

    user_event_roles
    
    id | user_id | event_id | role_id | created_at | updated_at | updated_at |
    --------------------------------------------------------------------------
    

    you can store user’s id in user_id along with its event’s id event_id (in which event user has access), and stored the role_id, the role you want to assign to user.

    Please share your feedback if this approach doesn’t work so we can find some other solution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search