oracle權限體系中有個default role,比較難以理解。下面用實例說明一下作用。
我們可以給某個用戶分配一些角色,比如role r1,r2,r3,r4,而其中可以將某些角色比如r1設置爲default role,其他的不設置成default role,這樣,當該用戶登錄時,自動具有default role中所包含的權限,其他的角色所具有的權限要通過set role 角色來獲得。
下面我們舉個例子:
(1)sys用戶作爲sysdba登錄,創建4個角色:
create role r1;
create role r2 identified by r2;
create role r3 identified by r3;
create role r4 identified by r4;
(2)sys用戶賦予這四個角色對應的權限:
grant create session to r1;
grant select on hr.test to r2; (這裏hr.test是我新創建的一個表,裏面有ID和name兩列)
grant update(name) on hr.test to r3;
grant insert on hr.test to r3;
grant delete on hr.test to r4;
(3)sys用戶創建一個用戶u3
create user u3 identified by u3;
(4)將角色r1,r2,r3,r4賦予用戶u3
grant r1,r2,r3,r4 to u3;
在修改用戶u3的默認角色前,r1,r2,r3,r4 角色均爲u3的 default role,以u3用戶登錄,查詢、增刪改hr.test,都沒有問題。
(5)現在sys用戶修改用戶u3的default role,僅將r1作爲u3的默認角色:
alter user u3 default role r1; --此時將覆蓋原來的設置,u3 的default role =r1,僅僅有登錄權限。
(6)用戶u3 log off ,然後再log on,進去後發現,
查詢、增刪改hr.test都不能進行。
(7) 用戶自己打開role權限
set role r2 identified by r2;
這時執行 select * from hr.test,發現沒有問題。增刪改不行。
將對應的角色打開:
set role r3 identified by r3;
此時修改和插入記錄沒有問題,但是select * from hr.test 確發現不行了。證明此時用戶所屬的角色僅僅是默認角色r1,和剛剛打開的角色r3,而r2被set role r3 identified by r3;覆蓋掉了。
那要同時有r2,r3,r4的權限怎麼辦呢?
set role r2 identified by r2,r3 identified by r3,r4 identified by r4。此時就同時對hr.test可以進行查詢,增刪改了。
不過set role 的效果是臨時的,只是當前session有效,其他的session無效,當結束當前session後再登錄,又只有default role 的權限了。