简体   繁体   中英

Filter rows of query based on group role in postgresql

I have a query as follows:

SELECT id, type, name
FROM users

This gives the result:

1  member jack
2  member sarah
3  manager ron

I want to modify the query that type manager will be shown only to users that has hr role.

Basically hr user should see:

1  member  jack
2  member  sarah
3  manager ron

non hr user should see

1  member  jack
2  member  sarah

I have a boolean query which gives the true false value if user has hr role or not:

select pg_has_role("current_user"(), 'hr'::name, 'MEMBER'::text)

but i cant seem to integrate it to the select query in order to filter the rows correctly.

I'm trying something like

SELECT id,type,name
FROM users
WHERE case when select pg_has_role("current_user"(), 'hr'::name, 
'MEMBER'::text) then ___________ else    _______________ end

How can I do that?

You can try following query (you don't need select when using pg_has_role inside WHERE)

select id,type,name
from users
WHERE type NOT IN (
     CASE WHEN pg_has_role("current_user"(), 'hr'::name, 'MEMBER'::text) 
     THEN '' 
     ELSE 'manager' END)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM