0

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 ?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Mohamed DS
  • 11
  • 3
  • As far I have understand, if you go with spatie's permissions package, you still need to manage user's permission for the particlular event. e.g. you have created events XYZ and ABC, now there is a user called John, you want to mange access of John in XYZ and ABC event. is that what you want? – Ram Chander Mar 07 '23 at 13:51
  • Yeah, but the problem with this approach is that a user can't have different roles in different events, Cuz he can only have one role in one organization (along with it's events). – Mohamed DS Mar 07 '23 at 15:33
  • can you try creating a new table? like `user_event_roles` and table column would be like as `id(PK&AI)`, `user_id`. `event_id`, `role_id`, `created_at`, `updated_at`, `deleted_at`. will that work for you? – Ram Chander Mar 10 '23 at 05:23

1 Answers1

0

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.

Ram Chander
  • 1,088
  • 2
  • 18
  • 36
  • I did consider that approach, The problem is when you want to assign a user a specific role in an organization (not in an event) that's where the problem is. So basically I need a way to assign a user to organizations and events along with his role of course. NOTE: - example 01: " I wanna assign james as an admin of the organization (Relation with the organization). - Exmaple 02: "I wanna assign jimmy as a manager of an event (Relation with the event). – Mohamed DS Mar 12 '23 at 07:54