Search code examples
oracle-databasetriggersddl-trigger

How do I create a Oracle trigger that grants permissions


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 our database, I want to grant select permissions to a role. Basically this:

grant select on TABLENAME to READROLE;

So far my trigger looks something like this:

CREATE OR REPLACE TRIGGER osmm_grant_on_creation

AFTER CREATE ON OSMM.SCHEMA

BEGIN

    //grant goes here

END

Problem is, I can't figure out how to join the two together by getting the name of the newly created table and referencing it through the trigger to the grant. Any suggestions? Thanks.


Solution

  • 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