2011-09-21 5 views

risposta

40

Un utente in un database Oracle ha solo i privilegi che si concedono. Quindi puoi creare un utente di sola lettura semplicemente non concedendo altri privilegi.

Quando si crea un utente

CREATE USER ro_user 
IDENTIFIED BY ro_user 
DEFAULT TABLESPACE users 
TEMPORARY TABLESPACE temp; 

l'utente non ha nemmeno il permesso di accedere al database. È possibile concedere che

GRANT CREATE SESSION to ro_user 

e quindi si può andare sulla concessione di qualsiasi privilegio di lettura che si desidera. Ad esempio, se si vuole RO_USER essere in grado di interrogare SCHEMA_NAME.TABLE_NAME, si potrebbe fare qualcosa di simile

GRANT SELECT ON schema_name.table_name TO ro_user 

In generale, è meglio creare un ruolo, tuttavia, e concedere i privilegi di oggetto per il ruolo in modo da può quindi concedere il ruolo a diversi utenti. Qualcosa di simile

Creare il ruolo

CREATE ROLE ro_role; 

concedere il ruolo SELEZIONA accesso su ogni tavolo in uno schema particolare

BEGIN 
    FOR x IN (SELECT * FROM dba_tables WHERE owner='SCHEMA_NAME') 
    LOOP 
    EXECUTE IMMEDIATE 'GRANT SELECT ON schema_name.' || x.table_name || 
            ' TO ro_role'; 
    END LOOP; 
END; 

e quindi concedere il ruolo per l'utente

GRANT ro_role TO ro_user; 
8
create user ro_role identified by ro_role; 
grant create session, select any table, select any dictionary to ro_role; 
-1

È n ot rigorosamente possibile in default db a causa dei molti esecuzioni pubbliche che ogni utente guadagna automaticamente attraverso il pubblico.

1

Eseguire la seguente procedura ad esempio come sistema utente.

Impostare p_owner sul proprietario dello schema e p_readonly sul nome dell'utente di sola lettura.

create or replace 
procedure createReadOnlyUser(p_owner in varchar2, p_readonly in varchar2) 
AUTHID CURRENT_USER is 
BEGIN 
    execute immediate 'create user '||p_readonly||' identified by '||p_readonly; 
    execute immediate 'grant create session to '||p_readonly; 
    execute immediate 'grant select any dictionary to '||p_readonly; 
    execute immediate 'grant create synonym to '||p_readonly; 

    FOR R IN (SELECT owner, object_name from all_objects where object_type in('TABLE', 'VIEW') and owner=p_owner) LOOP 
     execute immediate 'grant select on '||p_owner||'.'||R.object_name||' to '||p_readonly; 
    END LOOP; 
    FOR R IN (SELECT owner, object_name from all_objects where object_type in('FUNCTION', 'PROCEDURE') and owner=p_owner) LOOP 
     execute immediate 'grant execute on '||p_owner||'.'||R.object_name||' to '||p_readonly; 
    END LOOP; 
    FOR R IN (SELECT owner, object_name FROM all_objects WHERE object_type in('TABLE', 'VIEW') and owner=p_owner) LOOP 
     EXECUTE IMMEDIATE 'create synonym '||p_readonly||'.'||R.object_name||' for '||R.owner||'."'||R.object_name||'"'; 
    END LOOP; 
    FOR R IN (SELECT owner, object_name from all_objects where object_type in('FUNCTION', 'PROCEDURE') and owner=p_owner) LOOP 
     execute immediate 'create synonym '||p_readonly||'.'||R.object_name||' for '||R.owner||'."'||R.object_name||'"'; 
    END LOOP; 
END; 
0

è possibile creare utenti e privilegi concessione

Crea utente read_only identificato da read_only; sovvenzione crea sessione, seleziona qualsiasi tabella in read_only;