How do I create a Oracle trigger that grants permissions

前端 未结 2 1161
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 22:24

I want to do something that\'s conceptually simple but seems to be a lot more complex in reality.

Basically, whenever a new table is created for a couple of users in

相关标签:
2条回答
  • 2020-12-21 22:50

    you probabply need to do something like:

    CREATE OR REPLACE TRIGGER osmm_grant_on_creation
    
    AFTER CREATE ON OSMM.SCHEMA
    DECLARE
    new_obj_name varchar2(30);
    BEGIN
    SELECT ora_dict_obj_name
    INTO new_obj_name
    FROM dual
    WHERE ora_dict_obj_type = 'TABLE';
    
    execute immediate 'grant select on ' || new_obj_name || ' to READROLE';
    END
    

    but I can't check if it works

    0 讨论(0)
  • 2020-12-21 23:01

    It's likely more complex than you're even thinking. The GRANT statement is DDL which means that it issues implicit commits which means that you cannot put it in a trigger directly. Your trigger would need to submit a job which ran in a separate session after the triggering transaction committed which would actually do the grant. And that means that you have to use the older DBMS_JOB package to schedule the job since the more modern DBMS_SCHEDULER also implicitly commits.

    Since you shouldn't be creating tables on the fly in Oracle in the first place, the proper place for this sort of grant is in the build scripts that you run to create the table in the first place. Relying on triggers to do things like grants just tends to make it more difficult to do builds properly because running exactly the same script in two different environments may generate two different results because of differences in the trigger.

    If you're determined to go down this path, however, you'd probably want something like

    A procedure that grants the privilege

    CREATE OR REPLACE PROCEDURE grant_select_to_readrole( p_table_name IN VARCHAR2 )
    AS
    BEGIN
      EXECUTE IMMEDIATE 'grant select on ' || p_table_name || ' to readrole';
    END;
    

    And a trigger that submits a job that calls this procedure

    CREATE OR REPLACE TRIGGER osmm_grant_on_creation
      AFTER CREATE ON OSMM.SCHEMA
    AS
      l_jobno PLS_INTEGER;
    BEGIN
      dbms_job.submit( l_jobno,
                       'BEGIN grant_select_to_readrole( ''' || ora_dict_obj_name || ''' ); END;',
                       sysdate + interval '10' second );
    END;
    

    If you were to try to issue DDL in the schema-level trigger itself, you'd get an error

    SQL> ed
    Wrote file afiedt.buf
    
      1  create or replace trigger after_create_on_scott
      2    after create on schema
      3  declare
      4  begin
      5    execute immediate 'grant select on scott.emp to hr';
      6* end;
    SQL> /
    
    Trigger created.
    
    SQL> create table foo( col1 number );
    create table foo( col1 number )
    *
    ERROR at line 1:
    ORA-00604: error occurred at recursive SQL level 1
    ORA-30511: invalid DDL operation in system triggers
    ORA-06512: at line 3
    
    0 讨论(0)
提交回复
热议问题