SupabaseS
Supabase4y ago
lp

Advanced role systems similar to discord

Hey, I'm creating a chat app which shares a lot of features with Discord for a client. specifically, the roles feature
I want to create dynamic roles that can be added to users that allow the users to do certain actions in certain channels. For example, a role called "Advanced" would allow users to add reactions to messages in Channel A and Channel B, or create threads in Channel B, depending on configuration set by chat admins.

I'm wondering how to set up access policies for this. Here's where I'm heading...

I'd create these tables...
  • role table with
    id
    and
    name
  • role_user_assignment table with role_id,
    user_id
    to give a user a role
  • role_channel_permission table with permission enum "react" "reply_in_thread" and pointer to channel_id, role_id to give users of a certain role a permission in a certain channel.
So for example to check if user can create a reaction to a message in a channel... I'd have role named "Reactors" with an assignment to a user, and there'd be a role_channel_permission for permission to "react" on Reactors role_id in a given channel.

Then the question is how to model the row level security for this?

I want to only allow insertion of the message_reaction on a message for users who have a role that has a role_channel_permission in the given channel to "react".

Anyone have any examples of these "nested access policies with joins"?

Otherwise I know i could just write supabase functions to do this but it seems like something I could model with access policies...
Was this page helpful?