I have the following tables:
CREATE TABLE public.permissions (
"user" varchar ,
"action" permissions_action_enum, -- 'read'/'write'
id ltree
);
CREATE TABLE public.items (
"path" ltree NULL,
id uuid NOT NULL,
);
I want to return all rows that the user has permission to see, a user has permission to an item if he has permission on the item or on one of its ancestors. for example:
public.permissions:
user |action|id |
-----------|------|---|
1 |read |a |
1 |read |d |
public.items:
path |id |
-----------|---|
a.b |b |
a.c |c |
e.d |d |
g.f |f |
According to the above data, the user has permissions to items (b, c, and d).
I want to create a query which joins the above tables and return all items that the user has permission to read
, the expected result is:
path |id |
-----------|---|
a.b |b |
a.c |c |
e.d |d |
There is an efficient way to search whether ltree
contains other ltree
? Or if ltree
is-a-child of another entry? Something like *.a.*
in lquery
but between columns.
I tried to use <@/@>
but they are only good for checks on root/item:
select 'a.b'::ltree <@ 'b'::ltree -- false
select 'a.b'::ltree <@ 'a'::ltree -- true
select 'a.b'::ltree @> 'b'::ltree -- false
select 'a.b'::ltree @> 'a'::ltree -- false