Tricket er at lave en rekursiv forespørgsel over systemkatalogrelationerne pg_roles
og pg_auth_members
:
WITH RECURSIVE membership_tree(grpid, userid) AS (
-- Get all roles and list them as their own group as well
SELECT pg_roles.oid, pg_roles.oid
FROM pg_roles
UNION ALL
-- Now add all group membership
SELECT m_1.roleid, t_1.userid
FROM pg_auth_members m_1, membership_tree t_1
WHERE m_1.member = t_1.grpid
)
SELECT DISTINCT t.userid, r.rolname AS usrname, t.grpid, m.rolname AS grpname
FROM membership_tree t, pg_roles r, pg_roles m
WHERE t.grpid = m.oid AND t.userid = r.oid
ORDER BY r.rolname, m.rolname;
Dette giver et overblik over alle brugere i systemet med alle nedarvede rollemedlemskaber. Pak dette ind, så du altid har dette værktøj ved hånden.
Skål, Patrick