Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

sql - How to apply RLS on table using value from same table

I have a table like below and two users Soni and Garve. I want to show few orders only to users based on their role and using value in string_to_match column.

enter image description here

user Soni can see order_id like below, all product_keys of an order_id if order_id has even one string_to_match=pending rows/orders.

enter image description here

similarly role Garve can see all product_keys of an order_id if order_id has string_to_match=waiting orders.

enter image description here

Is it possible in Postgres?

question from:https://stackoverflow.com/questions/66053005/how-to-apply-rls-on-table-using-value-from-same-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If everybody has a roleid and based on you can decide ,so

-- roleid  1 = access to 'pending'
-- roleid 2 = access to 'waiting'

declare roleid integer;
select * from 
yourordertable t1
where  exists ( select 1 from yourordertable t2 
                where t2.order_id = t1.order_id
                and case roleid 
                   when 1 then t2.string_to_match = 'pending'
                   when 2 then t2.string_to_match = 'waiting'                         
              ) 
and case roleid when 2 then t2.string_to_match <> 'pending' 

not sure about last condition but based on your last output screenshot, it filters out 'pending' status for 'waiting' status .if It is not needed you can remove it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...