2016-10-01 21 views
1

我试图在发布之前审核应用程序的所有权限,并且我想确保没有角色具有比需要更多的权限。我已经看过不同的功能和系统表,但一切都很零碎。如何查看角色的所有数据库和对象授权?

是否有一个很好的查询或方法能够转储每个特定角色的授予?

我正在使用第9.5页。

回答

2

系统目录pg_class的列relacl包含有关权限的所有信息。在通过postgres与补助资架构public

实施例的数据newuser

create table test(id int); 
create view test_view as select * from test; 

grant select, insert, update on test to newuser; 
grant select on test_view to newuser; 

查询pg_class

select 
    relname, 
    relkind, 
    coalesce(nullif(s[1], ''), 'public') as grantee, 
    s[2] as privileges 
from 
    pg_class c 
    join pg_namespace n on n.oid = relnamespace 
    join pg_roles r on r.oid = relowner, 
    unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl, 
    regexp_split_to_array(acl, '=|/') s 
where nspname = 'public' 
and relname like 'test%'; 

    relname | relkind | grantee | privileges 
-----------+---------+----------+------------ 
test  | r  | postgres | arwdDxt  <- owner postgres has all privileges on the table 
test  | r  | newuser | arw   <- newuser has append/read/write privileges 
test_view | v  | postgres | arwdDxt  <- owner postgres has all privileges on the view 
test_view | v  | newuser | r   <- newuser has read privilege 
(4 rows) 

评论:

  • coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)) - 空在relacl意味着拥有者拥有所有特权;
  • unnest(...) acl - relacl是一个数组aclitem,一个用户的数组元素;
  • regexp_split_to_array(acl, '=|/') s - 将aclitem拆分为:s [1] username,s [2]特权;
  • coalesce(nullif(s[1], ''), 'public') as grantee - 空的用户名表示public

修改查询以选择个别用户或特定的一种关系或其他模式,等...

阅读文档:

以类似的方式,你可以得到关于授予特权模式(列nspacl in pg_namespace)和数据库(datacl in pg_database

+0

这是伟大的信息。我花费了太多时间使用has_xxx_privilege()函数和各种pg_tables,pg_proc等表和视图。 – deinspanjer

相关问题