Schema Privileges in Oracle Database 23ai/26ai
Schema privileges allow us to simplify grants where a user or role needs privileges on all objects in a schema.
oracle 23configurationintermediate
by OracleDba
36 views
Schema privileges allow us to simplify grants where a user or role needs privileges on all objects in a schema.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
drop user if exists testuser1 cascade;
drop user if exists testuser2 cascade;
drop role if exists t1_schema_role;
create user testuser1 identified by testuser1 quota unlimited on users;
grant db_developer_role to testuser1;
create user testuser2 identified by testuser2 quota unlimited on users;
grant create session to testuser2;
create role t1_schema_role;
conn testuser1/testuser1@//localhost:1521/freepdb1
-- Sequences
create sequence t1_seq;
create sequence t2_seq;
-- Tables
create table t1 (id number);
insert into t1 values (t1_seq.nextval);
commit;
create table t2 (id number);
insert into t2 values (t2_seq.nextval);
commit;
-- Views
create view t1_v as select * from t1;
create view t2_v as select * from t2;
-- Procedures
create or replace procedure p1 as
begin
null;
end;
/
create or replace procedure p2 as
begin
null;
end;
/12345678910111213141516171819
conn testuser1/testuser1@//localhost:1521/freepdb1
-- Sequences
grant select any sequence on schema testuser1 to testuser2;
grant select any sequence on schema testuser1 to t1_schema_role;
-- Tables, views, materialized views
grant select any table on schema testuser1 to testuser2;
grant insert any table on schema testuser1 to testuser2;
grant update any table on schema testuser1 to testuser2;
grant delete any table on schema testuser1 to testuser2;
grant select any table on schema testuser1 to t1_schema_role;
grant insert any table on schema testuser1 to t1_schema_role;
grant update any table on schema testuser1 to t1_schema_role;
grant delete any table on schema testuser1 to t1_schema_role;
-- Procedures, functions and packages
grant execute any procedure on schema testuser1 to testuser2;
grant execute any procedure on schema testuser1 to t1_schema_role;1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
conn testuser2/testuser2@//localhost:1521/freepdb1
-- Sequences
select testuser1.t1_seq.nextval;
NEXTVAL
----------
2
SQL>
select testuser1.t2_seq.nextval;
NEXTVAL
----------
2
SQL>
-- Tables
select count(*) from testuser1.t1;
COUNT(*)
----------
1
SQL>
select count(*) from testuser1.t2;
COUNT(*)
----------
1
SQL>
-- Views
select * from testuser1.t1_v;
ID
----------
1
SQL>
select * from testuser1.t2_v;
ID
----------
1
SQL>
-- Procedures
exec testuser1.p1;
PL/SQL procedure successfully completed.
SQL>
exec testuser1.p2;
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
noaudit policy test_audit_policy;
drop audit policy test_audit_policy;
create audit policy test_audit_policy
actions delete on testuser1.t1,
insert on testuser1.t1,
update on testuser1.t1,
select on testuser1.t1_seq
when 'sys_context(''userenv'', ''session_user'') = ''TESTUSER2'''
evaluate per session
container = current;
audit policy test_audit_policy;
conn testuser2/testuser2@//localhost:1521/freepdb1
insert into testuser1.t1 (id) values (testuser1.t1_seq.nextval);
commit;
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
-- You might need to flush the audit information before it is visible.
-- exec dbms_audit_mgmt.flush_unified_audit_trail;
column event_timestamp format a30
column dbusername format a10
column action_name format a20
column object_schema format a10
column object_name format a20
select event_timestamp,
dbusername,
action_name,
object_schema,
object_name
from unified_audit_trail
where dbusername = 'TESTUSER2'
order BY event_timestamp;
EVENT_TIMESTAMP DBUSERNAME ACTION_NAME OBJECT_SCH OBJECT_NAME
------------------------------ ---------- -------------------- ---------- --------------------
30-APR-23 05.00.39.944494 PM TESTUSER2 SELECT TESTUSER1 T1_SEQ
30-APR-23 05.00.39.948816 PM TESTUSER2 INSERT TESTUSER1 T1
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
conn testuser2/testuser2@//localhost:1521/freepdb1
column username format a10
column privilege format a25
column schema format a10
select * from user_schema_privs;
USERNAME PRIVILEGE SCHEMA ADM COM INH
---------- ------------------------- ---------- --- --- ---
TESTUSER2 EXECUTE ANY PROCEDURE TESTUSER1 NO NO NO
TESTUSER2 SELECT ANY SEQUENCE TESTUSER1 NO NO NO
TESTUSER2 DELETE ANY TABLE TESTUSER1 NO NO NO
TESTUSER2 UPDATE ANY TABLE TESTUSER1 NO NO NO
TESTUSER2 INSERT ANY TABLE TESTUSER1 NO NO NO
TESTUSER2 SELECT ANY TABLE TESTUSER1 NO NO NO
6 rows selected.
SQL>
select * from session_schema_privs;
PRIVILEGE SCHEMA
------------------------- ----------
EXECUTE ANY PROCEDURE TESTUSER1
SELECT ANY SEQUENCE TESTUSER1
DELETE ANY TABLE TESTUSER1
UPDATE ANY TABLE TESTUSER1
INSERT ANY TABLE TESTUSER1
SELECT ANY TABLE TESTUSER1
6 rows selected.
SQL>
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
column role format a20
column privilege format a25
column schema format a10
select * from role_schema_privs;
ROLE PRIVILEGE SCHEMA ADM COM INH
-------------------- ------------------------- ---------- --- --- ---
T1_SCHEMA_ROLE EXECUTE ANY PROCEDURE TESTUSER1 NO NO NO
T1_SCHEMA_ROLE SELECT ANY SEQUENCE TESTUSER1 NO NO NO
T1_SCHEMA_ROLE DELETE ANY TABLE TESTUSER1 NO NO NO
T1_SCHEMA_ROLE UPDATE ANY TABLE TESTUSER1 NO NO NO
T1_SCHEMA_ROLE INSERT ANY TABLE TESTUSER1 NO NO NO
T1_SCHEMA_ROLE SELECT ANY TABLE TESTUSER1 NO NO NO
6 rows selected.
SQL>12345678910111213141516171819
conn testuser1/testuser1@//localhost:1521/freepdb1
-- Sequences
revoke select any sequence on schema testuser1 from testuser2;
revoke select any sequence on schema testuser1 from t1_schema_role;
-- Tables, views, materialized views
revoke select any table on schema testuser1 from testuser2;
revoke insert any table on schema testuser1 from testuser2;
revoke update any table on schema testuser1 from testuser2;
revoke delete any table on schema testuser1 from testuser2;
revoke select any table on schema testuser1 from t1_schema_role;
revoke insert any table on schema testuser1 from t1_schema_role;
revoke update any table on schema testuser1 from t1_schema_role;
revoke delete any table on schema testuser1 from t1_schema_role;
-- Procedures, functions and packages
revoke execute any procedure on schema testuser1 from testuser2;
revoke execute any procedure on schema testuser1 from t1_schema_role;Please to add comments
No comments yet. Be the first to comment!