2016-10-01 20 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

相關問題