Truncate table privilege without stored procedures - oracle

SYSTEM creates several tables, and would like to grant userA the ability to truncate tables. On oracle's docs, the minimum privilege is :
GRANT DROP ANY TABLE TO userA
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10007.htm#SQLRF01707
DROP ANY TABLE as stated is too powerful of a privilege to be granted to a user, and from looking around there is no way to limit the tables the user can truncate with it.
The proper way as previous topics has touched on is to use stored procedures:
CREATE OR REPLACE procedure truncateTables
AS
BEGIN
execute immediate 'TRUNCATE TABLE table1';
end;
/
GRANT EXECUTE on system.truncateTables TO userA;
However, if I want to avoid using any stored procedures at all, is there an alternative way to allow a user that is not the table owner to truncate tables, but not with a privilege that is potentially destructive like "DROP ANY TABLE" ?

You cant do it without procedure.
you can do it dynamically like this:
CREATE OR REPLACE procedure pr_truncate_table(p_table_name varchar2) is
begin
execute immediate 'truncate table ' || p_table_name || '';
end;
/

A work-around that I have used is to grant delete to the other user, then use a delete instead of truncate to remove the rows. This is NOT a proper truncate as it does not clear the used storage, but it allows the same tasks to be achieved.
With the table owner account
GRANT delete on Table_Name to "User_Account";
With the other account
Delete from Table_Name where 1=1;

Related

grant Truncate priviledge on another User in Oracle

I have created below Oracle Procedure where i am granting DML priviledges on all tables to TST user. Now i also want to grant Truncate priviledge to TST user in the same Procedure but dont know how to do it.
CREATE OR REPLACE PROCEDURE "TBL_MER"."PROCEDURE_GRANT_PRIV"
IS
BEGIN
FOR tab IN (SELECT table_name
FROM all_tables
where owner = USER
order by table_name) LOOP
EXECUTE IMMEDIATE 'GRANT SELECT, INSERT, UPDATE, DELETE ON '||tab.table_name||' TO TST';
END LOOP;
COMMIT;
END;
Did you read the documentation on the TRUNCATE command?
Quoting from the 19c SQL Language Reference
Prerequisites
To truncate a table, the table must be in your schema or you must have
the DROP ANY TABLE system privilege.
TRUNCATE is not DML.

Grant insert/update access to schema

I have a request to grant user TOM Insert/Update access to all the tables on a schema JERRY.
I use the following query achieve it.
FOR x IN (SELECT * FROM user_tables)
LOOP
EXECUTE IMMEDIATE 'GRANT SELECT ON ' || x.table_name || ' TO <<someone>>';
END LOOP;
After granting the rights on the existing tables, i have to create few more tables on JERRY schema. Will TOM have insert/update access to those newly created tables?
If NO do i need to execute the above statement every time i create a new table?
or is there a way to grant access to the whole schema?

Revoke ANY Privileges Oracle

Hi I have this question.
Is posibble GRANT ANY privileges excluding some tables of the same schema.
For Example:
EXECUTE IMMEDIATE
'CREATE USER USREJECUTA_SUI_ABAS
IDENTIFIED BY VALUES ''test''
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP
PROFILE DEFAULT
ACCOUNT UNLOCK';
-- 2 Roles for USREJECUTA_SUI
EXECUTE IMMEDIATE 'GRANT CONNECT TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT RESOURCE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'ALTER USER USREJECUTA_SUI_ABAS DEFAULT ROLE ALL';
-- 1 System Privileges for USREJECUTA_SUI
EXECUTE IMMEDIATE 'GRANT SELECT ANY TABLE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT UPDATE ANY TABLE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT INSERT ANY TABLE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT UNLIMITED TABLESPACE TO USREJECUTA_SUI_ABAS';
Now revoke the permissions from some tables
REVOKE INSERT,UPDATE ON VELITTDA.TAPROVEEDORESXPAIS FROM USREJECUTA_SUI';
but the system launch this error:
ORA-01927: cannot REVOKE privileges you did not grant.
Do you kow some wat to do this? I will apreciate a lot your help.
No, you can't. If you give someone SELECT ANY TABLE, you've given them the ability to query any table in the database. You can't revoke object-level privileges when you haven't granted object-level privileges.
The proper approach is almost always to create a role that has object-level privileges on the actual set of objects that the user needs access to. Grant the role to the user (and any other users that need a similar set of privileges). The various ANY roles are really only appropriate for folks like DBAs.
Granting the SELECT ANY TABLE (or any other ANY TABLE) privilege is generally the wrong thing to do and is almost as bad as granting DBA to arbitrary schema users.
If you are trying to avoid issuing a lot of grant statements, then use the simple trick of generating your DDL from the data dictionary.
set head off
set pagesize 0
spool grant_foo.sql
select 'GRANT SELECT ON '||table_name||' TO FOO_ROLE;'
from all_tables where owner = 'FOO'
order by table_name
;
spool off
Then edit grant_foo.sql as needed before executing.

Create table on another schema

Situation is that user1 gives permision to user2:
GRANT CREATE ANY TABLE, SELECT ANY TABLE TO user2;
And after logging on user2, I'm trying to create table:
CREATE TABLE user1.test(id NUMBER PRIMARY KEY);
the result is ORA-01031 - insufficient privileges
I can create table on own schema and select tables from other schemas. I thought that CREATE ANY TABLE solves the problem, but it looks other way. Ah, and both users have unlimited tablespace. What else should I guarantee?
Perhaps you need to also grant CREATE ANY INDEX? You are creating an index when you add the primary key. You can quickly test this by omitting the PK constraint.
"create any table" is too powerful a privilege to be granting to non-DBA's. A better approach would be to create a "create table" procedure in the target schema that accepts sanitised components of the required DDL, and grant execute privilege on that to the required users.
A suitable interface would be something like ...
create procedure
create_table(
table_name varchar2,
columns varchar2,
etc varchar2)
... so that you can ...
begin
user1.create_table(
table_name => 'TEST',
columns => 'id NUMBER PRIMARY KEY',
etc => '');
end;
... and have the procedure construct and execute the DDL for you.
user
GRANT CREATE ANY INDEX TO SCHEMA_NAME

Create a user who can only query data from a tablespace (oracle)

I have 3 users in my oracle Database
user_admin
user
user_query
The user_admin have dba rol.
user have connect and resource rol
and user_query the same as user.
I want to configure the last one to deny him of all kind of insert,update,delete but form the full tablespace not from a table, view or procedure...
The point is to have a secure-only-querying data user.
But i can't do it for a tablespace, at least as far i know.
Any idea?
You can loop through all table that use the tablespace in question and grant select. I would try to stay away from powerful privs like "SELECT ANY TABLE", as that would apply to the entire database.
For example, if your tablespace is named XXX then:
BEGIN
FOR tbl IN (SELECT owner, table_name
FROM dba_tables dt
WHERE dt.tablespace_name = 'XXX') LOOP
EXECUTE IMMEDIATE 'GRANT SELECT ON ' || tbl.owner || '.' || tbl.table_name || ' TO USER_QUERY';
END LOOP;
END;
there is the following which grants select to any table or view (except those owned by SYS):
grant select any table to user_query;
It doesn't restrict to a single tablespace though - any table in the entire database would be available for select.
Firstly, the use of the CONNECT and RESOURCE roles is discouraged, and documented as such.
That aside, no, there is no privilege for granting by tablespace, or even by user. For one thing, a partitioned table or index can use multiple tablespaces, none of which might be the default for that object.
Grants are at the object level. You could create a procedure to grant privileges to a user (or better to a role) based on the tablespace of a table though.

Resources