Grant insert/update access to schema - oracle

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?

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.

Right way to create tables in public schema in Oracle DB

I have worked on PostgreSQL, which has a concept of public schema. By default all objects goes into public schema, if no schema is defined explicitly. This allows all the users in that database to access all objects in public schema.
I want to know what is the equivalent of this in Oracle DB. How do I create objects using ADMIN user and make them available to all the users in that db. Currently, objects created using ADMIN are available to ADMIN user only. We have to explicitly grant permissions to other users to objects in ADMIN schema.
We have to explicitly grant permissions to other users to objects in ADMIN schema.
Exactly, that's how Oracle works.
Though, you don't have to do it "manually". A good option is to create a role, then grant privileges on tables to that role, and - finally - grant role to every user you want. What's good in it? If new user is created, just grant the same role to it and everything works.
Also, you can write a script to do the job for you. If you're connected as ADMIN, then e.g.
SQL> begin
2 for cur_r in (select table_name from user_tables) loop
3 execute immediate 'grant select on ' || cur_r.table_name ||' to admin_role';
4 end loop;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
In a matter of seconds everything is done.

Truncate table privilege without stored procedures

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;

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 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