Skip to content

RLS: allow to select all team members of teams that user is also part of #4509

Answered by b2m9
b2m9 asked this question in Questions
Discussion options

You must be logged in to vote

The issue is that the naive solution causes the following error: infinite recursion detected in policy for relation

team_id in (select team_id from members where auth.uid() = user_id)

Now, it seems that using a security definer function works around this issue - for a reason I don't fully understand (it seems that function bypass RLS, hence no recursion). To stick with the example from above, you can solve the problem like this:

CREATE OR REPLACE FUNCTION get_teams_for_user(user_id BIGINT)
RETURNS SETOF BIGINT
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
  SELECT team_id from members where user_id = $1
$$;

All it does is to outsource the select statement from the st…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@walton-alex
Comment options

Answer selected by b2m9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants