postgresql – 如何获取用户所属的所有角色(包括继承的角色)?
发布时间:2020-12-13 16:22:19 所属栏目:百科 来源:网络整理
导读:假设我有两个 Postgresql数据库组,“作者”和“编辑”,以及两个用户,“maxwell”和“ernest”. create role authors;create role editors;create user maxwell;create user ernest;grant authors to editors; --editors can do what authors can dogrant edi
假设我有两个
Postgresql数据库组,“作者”和“编辑”,以及两个用户,“maxwell”和“ernest”.
create role authors; create role editors; create user maxwell; create user ernest; grant authors to editors; --editors can do what authors can do grant editors to maxwell; --maxwell is an editor grant authors to ernest; --ernest is an author 我想编写一个高性能函数,它返回maxwell所属的角色列表(最好是它们的oid),如下所示: create or replace function get_all_roles() returns oid[] ... 它应该返回maxwell,作者和编辑的oids(但不是ernest). 但是,当有继承时,我不确定如何做到这一点.
您可以使用
recursive query查询系统目录,尤其是
pg_auth_members :
WITH RECURSIVE cte AS ( SELECT oid FROM pg_roles WHERE rolname = 'maxwell' UNION ALL SELECT m.roleid FROM cte JOIN pg_auth_members m ON m.member = cte.oid ) SELECT oid FROM cte; BTW,INHERIT是 BTW2:循环依赖是不可能的. Postgres不允许这样做.所以我们不必检查. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |