--MARS-770

set verify off
SET serveroutput ON
--SET TIMING ON
whenever sqlerror exit sql.sqlcode rollback;

column owner format a15
column object_name format a30
column spoolname new_value spoolname 

select 'install_package_MarsGeneralRelease_MARS770_grants_' || name || '_' ||to_char(sysdate,'YYYYMMDD_HH24MISS')||'.log' spoolname from v$pdbs;
spool '&spoolname'


prompt ##### started at time #####
select systimestamp from dual;
prompt ##### database name #####
select name pdb_name from v$pdbs;

--set echo on
--=============================================================================================================================
-- start install --MARS-770 payload grants specific part
--=============================================================================================================================

-- GRANT SELECT ANY TABLE ON SCHEMA CT_MRDS TO MRDS_LOADER;
-- GRANT INSERT ANY TABLE ON SCHEMA CT_MRDS TO MRDS_LOADER;
-- GRANT UPDATE ANY TABLE ON SCHEMA CT_MRDS TO MRDS_LOADER;
-- GRANT DELETE ANY TABLE ON SCHEMA CT_MRDS TO MRDS_LOADER;
-- GRANT EXECUTE ANY PROCEDURE ON SCHEMA CT_MRDS TO MRDS_LOADER;
-- GRANT SELECT ANY SEQUENCE ON SCHEMA CT_MRDS TO MRDS_LOADER;
-- GRANT SELECT ANY TABLE ON SCHEMA CT_MRDS TO MOPDB_PUBLIC_GL;

-- GRANT SELECT ANY TABLE ON SCHEMA CT_ET_TEMPLATES TO MRDS_LOADER;
-- GRANT INSERT ANY TABLE ON SCHEMA CT_ET_TEMPLATES TO MRDS_LOADER;
-- GRANT UPDATE ANY TABLE ON SCHEMA CT_ET_TEMPLATES TO MRDS_LOADER;
-- GRANT DELETE ANY TABLE ON SCHEMA CT_ET_TEMPLATES TO MRDS_LOADER;
-- GRANT CREATE ANY TABLE ON SCHEMA CT_ET_TEMPLATES TO MRDS_LOADER;
-- GRANT DROP ANY TABLE ON SCHEMA CT_ET_TEMPLATES TO MRDS_LOADER;

-- GRANT SELECT ANY TABLE ON SCHEMA OU_RQSD TO RQSD;

-- BEGIN
--     FOR user_rec IN (SELECT username FROM dba_users WHERE oracle_maintained != 'Y' AND common != 'YES') LOOP
--         DECLARE
--             schema_name VARCHAR2(128) := user_rec.username;
--         BEGIN

--             EXECUTE IMMEDIATE 'GRANT SELECT ON CT_MRDS.A_WORKFLOW_HISTORY TO "'|| schema_name ||'" WITH GRANT OPTION';
--             EXECUTE IMMEDIATE 'GRANT SELECT ANY TABLE ON SCHEMA ODS TO '|| schema_name ;

--         EXCEPTION
--             WHEN OTHERS THEN
--                 DBMS_OUTPUT.PUT_LINE('Error processing schema ' || schema_name || ': ' || SQLERRM);
--         END;
--     END LOOP;
-- END;

GRANT SELECT ANY TABLE TO OU_RQSD;
GRANT SELECT ANY TABLE TO RQSD;

BEGIN
    FOR user_rec IN (SELECT username FROM dba_users WHERE oracle_maintained != 'Y' AND common != 'YES') LOOP
        DECLARE
            schema_name VARCHAR2(128) := user_rec.username;
        BEGIN

            EXECUTE IMMEDIATE 'GRANT SELECT ON CT_MRDS.A_WORKFLOW_HISTORY TO "'|| schema_name ||'" WITH GRANT OPTION';

        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('Error processing schema ' || schema_name || ': ' || SQLERRM);
        END;
    END LOOP;
END;