I need to query an Oracle database and get back which privileges are granted which roles and on which objects. Because I am going to either revoke or grant privileges to roles on objects according to configurations in a database management system I'm creating.
When I loop over the objects I'm going to process, I need to know if a role has already been granted any of the privileges on that object. This needs to be done before queuing up the GRANT
or REVOKE
to be performed.
The ODP.NET provider doesn't support running multiple Oracle statements,
they must be run one by one or encapsulated in a BEGIN END
block (which then counts as a single statement). Some statements even have to be put inside EXECUTE IMMEDIATE
statements inside the block for it to work.
I know you can grant the same privilege on an object to a role multiple times without breaking anything, but (correct me if I'm wrong) revoking a privilege from role on an object which the role doesn't have will raise an exception and break the whole block execution.
Before performing any action on the Oracle database I first query for all the information I need to do my work, as I've found that to be best for performance, not playing ping pong with the server by querying for each object, role or privilege...
While processing the Oracle database I then compare the local information I just queried for, with the configured information, and perform different actions based on that.
I used SQLTracker and found Toad using this query for finding privileges granted on objects for a role and removed the roles where clause
SELECT dtp.PRIVILEGE,
dtp.grantable ADMIN,
dtp.grantee,
dtp.grantor,
dbo.owner,
dbo.object_type,
dbo.object_name,
dbo.status,
'' column_name
FROM ALL_TAB_PRIVS dtp, DBA_OBJECTS dbo
WHERE dtp.TABLE_SCHEMA = dbo.owner AND dtp.table_name = dbo.object_name
AND dbo.object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION')
UNION ALL
SELECT dcp.PRIVILEGE,
dcp.grantable ADMIN,
dcp.grantee,
dcp.grantor,
dbo.owner,
dbo.object_type,
dbo.object_name,
dbo.status,
dcp.column_name
FROM ALL_COL_PRIVS dcp, DBA_OBJECTS dbo
WHERE dcp.TABLE_SCHEMA = dbo.owner AND dcp.table_name = dbo.object_name
AND dbo.object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION');
I think this query will give me the information I need, but I'm afraid of performance issues if working with a very large Oracle database with A LOT of objects.
What is the best possible way to query for which privileges are granted to which roles on which objects on an Oracle database?
The query I posted is just one of my own suggestions, but since I'm asking this question here I'm obviously not sure about it, and would like some input on the query.
Am I on the right track? Does it need to be modified? Should I scrap it and use a totally different approach?