--MARS-757

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_MarsDEVOreplicator_MARS757_create_user_' || 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-757 create user
--=============================================================================================================================


DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP;
ALTER USER MRDS_LOADER QUOTA UNLIMITED ON DATA;

GRANT CREATE SESSION TO MRDS_LOADER;
GRANT SELECT ANY DICTIONARY TO MRDS_LOADER;
GRANT SELECT_CATALOG_ROLE TO MRDS_LOADER;
--GRANT SELECT ON SYS.ALL_TABLES TO MRDS_LOADER;
GRANT READ ANY TABLE TO MRDS_LOADER;
CREATE ROLE MRDS_LOADER_ROLE;
GRANT MRDS_LOADER_ROLE TO MRDS_LOADER;
GRANT READ, WRITE ON DIRECTORY DATA_PUMP_DIR TO MRDS_LOADER_ROLE;
GRANT EXECUTE ON DBMS_CLOUD TO MRDS_LOADER_ROLE;
GRANT CREATE JOB TO MRDS_LOADER;
GRANT EXECUTE ON DBMS_PARALLEL_EXECUTE TO MRDS_LOADER;
EXEC DBMS_CLOUD_ADMIN.ENABLE_RESOURCE_PRINCIPAL(username => 'MRDS_LOADER');
GRANT EXECUTE ON C##CLOUD$SERVICE.DBMS_CLOUD TO MRDS_LOADER;


-- 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;
--             role_name   VARCHAR2(128) := schema_name || '_ROLE_RW';
--         BEGIN
--             -- Step 1: Drop the role if it exists
--             BEGIN
--                 EXECUTE IMMEDIATE 'DROP ROLE ' || role_name;
--             EXCEPTION
--                 WHEN OTHERS THEN
--                     IF SQLCODE != -1919 THEN -- ORA-01918: role does not exist
--                         RAISE;
--                     END IF;
--             END;

            
--             EXECUTE IMMEDIATE 'GRANT SELECT ANY TABLE ON SCHEMA CT_MRDS TO ' || schema_name;
--             EXECUTE IMMEDIATE 'GRANT SELECT ANY TABLE ON SCHEMA ODS TO ' || schema_name;

--             -- Step 2: Create the role
--             EXECUTE IMMEDIATE 'CREATE ROLE ' || role_name;

--             -- Step 3: Grant schema-level privileges to the role            
--             EXECUTE IMMEDIATE 'GRANT CREATE ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT DROP ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT CREATE ANY VIEW ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT DROP ANY VIEW ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT SELECT ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT INSERT ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT UPDATE ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT DELETE ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT ALTER ANY TABLE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT EXECUTE ANY PROCEDURE ON SCHEMA ' || schema_name || ' TO ' || role_name;
--             EXECUTE IMMEDIATE 'GRANT SELECT ANY SEQUENCE ON SCHEMA ' || schema_name || ' TO ' || role_name;


--             -- Step 4: Grant the role to MRDS_LOADER
--             EXECUTE IMMEDIATE 'GRANT ' || role_name || ' TO MRDS_LOADER';

--             -- Print success message
--             DBMS_OUTPUT.PUT_LINE('Role ' || role_name || ' created and privileges granted successfully.');
--         EXCEPTION
--             WHEN OTHERS THEN
--                 DBMS_OUTPUT.PUT_LINE('Error processing schema ' || schema_name || ': ' || SQLERRM);
--         END;
--     END LOOP;
-- END;


GRANT SELECT ANY TABLE TO CT_MRDS;

GRANT CREATE ANY TABLE  TO MRDS_LOADER;
GRANT DROP ANY TABLE TO MRDS_LOADER;
GRANT CREATE ANY VIEW TO MRDS_LOADER;
GRANT DROP ANY VIEW TO MRDS_LOADER;
GRANT SELECT ANY TABLE TO MRDS_LOADER;
GRANT INSERT ANY TABLE TO MRDS_LOADER;
GRANT UPDATE ANY TABLE TO MRDS_LOADER;
GRANT DELETE ANY TABLE TO MRDS_LOADER;
GRANT ALTER ANY TABLE TO MRDS_LOADER;
GRANT EXECUTE ANY PROCEDURE TO MRDS_LOADER;
GRANT SELECT ANY SEQUENCE TO MRDS_LOADER;

