My Sample code : (Package)
/*PACKAGE HEADER*/
CREATE OR REPLACE PACKAGE XX_FND_USER_RESP_PKG
IS
PROCEDURE XX_FND_COPY_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER,
I_MODE IN NUMBER DEFAULT 0);
--
PROCEDURE COPY_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER);
--
PROCEDURE DISABLE_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER);
--
PROCEDURE ENABLE_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER);
--
PROCEDURE DISABLE_USER_ALL_RESP(I_USER_ID IN NUMBER);
END XX_FND_USER_RESP_PKG;
/*PACKAGE BODY*/
CREATE OR REPLACE PACKAGE BODY XX_FND_USER_RESP_PKG
IS
PROCEDURE XX_FND_COPY_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER,
I_MODE IN NUMBER DEFAULT 0)
IS
/*
When Who What
===============================
20130313 WenShen Created for 複製某人權限至另一人
說明 :
1. I_MODE : 權限複製規則
0 : 只新增短少的權限,但不將即有卻失效的權限予以復權
1 : 新增短少的權限,且將即有卻失效的權限予以復權(且不影其它即有的權限)
2 : 完整複製,將原有權限全數失效後再執行I_MODE 2
*/
BEGIN
--
IF I_MODE = 0 THEN --只新增短少的權限,但不將即有卻失效的權限予以復權
--新增短少的權限
COPY_USER_RESP(I_FROM_USER_ID,
I_TO_USER_ID);
ELSIF I_MODE = 1 THEN --新增短少的權限,且將即有卻失效的權限予以復權
--Step 1. 新增短少的權限
COPY_USER_RESP(I_FROM_USER_ID,
I_TO_USER_ID);
--Step 2. 依來源USER權限設定將目標USER的權限的予以復權
ENABLE_USER_RESP(I_FROM_USER_ID,
I_TO_USER_ID);
ELSIF I_MODE = 2 THEN --完整複製,將原有權限全數失效後再執行I_MODE 2
--Step 1. 將所有權限予以停權
DISABLE_USER_ALL_RESP(I_TO_USER_ID);
--Step 2. 新增短少的權限
COPY_USER_RESP(I_FROM_USER_ID,
I_TO_USER_ID);
--Step 3. 依來源USER權限設定將目標USER的權限的予以復權
ENABLE_USER_RESP(I_FROM_USER_ID,
I_TO_USER_ID);
ELSE
NULL;
END IF;
END;
--
PROCEDURE COPY_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER)
IS
T_AP_ID NUMBER;
T_RESP_ID NUMBER;
--來源者權限表
CURSOR C_F_RESP
IS
SELECT FR.RESPONSIBILITY_KEY
FROM FND_USER_RESP_GROUPS_DIRECT FUR
JOIN FND_RESPONSIBILITY FR
ON FR.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID
AND (FR.END_DATE IS NULL OR FR.END_DATE >= SYSDATE)
WHERE (FUR.END_DATE IS NULL OR FUR.END_DATE >= SYSDATE)
AND NOT EXISTS (SELECT 'X'
FROM FND_USER_RESP_GROUPS_DIRECT FUR2
WHERE FUR2.USER_ID = I_TO_USER_ID
AND FUR2.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID) --已存在不重覆轉
AND FUR.USER_ID = I_FROM_USER_ID; --來源對象
BEGIN
FOR C1 IN C_F_RESP LOOP
--取得RESP資訊
SELECT FR.RESPONSIBILITY_ID,
FR.APPLICATION_ID
INTO T_RESP_ID,
T_AP_ID
FROM FND_RESPONSIBILITY FR
WHERE FR.RESPONSIBILITY_KEY = C1.RESPONSIBILITY_KEY;
--CALL API
BEGIN
FND_USER_RESP_GROUPS_API.INSERT_ASSIGNMENT(USER_ID => I_TO_USER_ID,
RESPONSIBILITY_ID => T_RESP_ID,
RESPONSIBILITY_APPLICATION_ID => T_AP_ID,
SECURITY_GROUP_ID => 0,
START_DATE => TRUNC(SYSDATE),
END_DATE => NULL,
DESCRIPTION => NULL);
COMMIT;
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
--
PROCEDURE DISABLE_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER)
IS
T_AP_ID NUMBER;
T_RESP_ID NUMBER;
T_END_DATE DATE;
--來源者權限表
CURSOR C_F_RESP
IS
SELECT FR.RESPONSIBILITY_KEY,FUR2.START_DATE
FROM FND_USER_RESP_GROUPS_DIRECT FUR
JOIN FND_RESPONSIBILITY FR
ON FR.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID
JOIN FND_USER_RESP_GROUPS_DIRECT FUR2
ON FUR2.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID
AND FUR2.USER_ID = I_TO_USER_ID
WHERE FUR.END_DATE < SYSDATE --已失效的部分
AND FUR.USER_ID = I_FROM_USER_ID; --來源對象
BEGIN
FOR C1 IN C_F_RESP LOOP
--取得RESP資訊
SELECT FR.RESPONSIBILITY_ID,
FR.APPLICATION_ID
INTO T_RESP_ID,
T_AP_ID
FROM FND_RESPONSIBILITY FR
WHERE FR.RESPONSIBILITY_KEY = C1.RESPONSIBILITY_KEY;
--
IF C1.START_DATE IS NOT NULL THEN
IF C1.START_DATE > SYSDATE THEN
T_END_DATE := C1.START_DATE;
ELSE
T_END_DATE := SYSDATE;
END IF;
END IF;
--
BEGIN
FND_USER_RESP_GROUPS_API.UPDATE_ASSIGNMENT(USER_ID => I_TO_USER_ID,
RESPONSIBILITY_ID => T_RESP_ID,
RESPONSIBILITY_APPLICATION_ID => T_AP_ID,
SECURITY_GROUP_ID => 0,
START_DATE => C1.START_DATE,
END_DATE => T_END_DATE,
DESCRIPTION => NULL);
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
--
PROCEDURE ENABLE_USER_RESP(I_FROM_USER_ID IN NUMBER,
I_TO_USER_ID IN NUMBER)
IS
T_AP_ID NUMBER;
T_RESP_ID NUMBER;
--來源者權限表
CURSOR C_F_RESP
IS
SELECT FR.RESPONSIBILITY_KEY,FUR2.START_DATE
FROM FND_USER_RESP_GROUPS_DIRECT FUR
JOIN FND_RESPONSIBILITY FR
ON FR.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID
JOIN FND_USER_RESP_GROUPS_DIRECT FUR2
ON FUR2.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID
AND FUR2.USER_ID = I_TO_USER_ID
WHERE (FUR.END_DATE IS NULL OR FUR.END_DATE >= SYSDATE)
AND FUR.USER_ID = I_FROM_USER_ID; --來源對象
BEGIN
FOR C1 IN C_F_RESP LOOP
--取得RESP資訊
SELECT FR.RESPONSIBILITY_ID,
FR.APPLICATION_ID
INTO T_RESP_ID,
T_AP_ID
FROM FND_RESPONSIBILITY FR
WHERE FR.RESPONSIBILITY_KEY = C1.RESPONSIBILITY_KEY;
--
BEGIN
FND_USER_RESP_GROUPS_API.UPDATE_ASSIGNMENT(USER_ID => I_TO_USER_ID,
RESPONSIBILITY_ID => T_RESP_ID,
RESPONSIBILITY_APPLICATION_ID => T_AP_ID,
SECURITY_GROUP_ID => 0,
START_DATE => C1.START_DATE,
END_DATE => NULL,
DESCRIPTION => NULL);
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
--
PROCEDURE DISABLE_USER_ALL_RESP(I_USER_ID IN NUMBER)
IS
T_AP_ID NUMBER;
T_RESP_ID NUMBER;
T_END_DATE DATE;
--來源者權限表
CURSOR C_F_RESP
IS
SELECT FR.RESPONSIBILITY_KEY,FUR.START_DATE
FROM FND_USER_RESP_GROUPS_DIRECT FUR
JOIN FND_RESPONSIBILITY FR
ON FR.RESPONSIBILITY_ID = FUR.RESPONSIBILITY_ID
WHERE (FUR.END_DATE IS NULL OR FUR.END_DATE >= SYSDATE)
AND FUR.USER_ID = I_USER_ID; --來源對象
BEGIN
FOR C1 IN C_F_RESP LOOP
--取得RESP資訊
SELECT FR.RESPONSIBILITY_ID,
FR.APPLICATION_ID
INTO T_RESP_ID,
T_AP_ID
FROM FND_RESPONSIBILITY FR
WHERE FR.RESPONSIBILITY_KEY = C1.RESPONSIBILITY_KEY;
--
IF C1.START_DATE IS NOT NULL THEN
IF C1.START_DATE > SYSDATE THEN
T_END_DATE := C1.START_DATE;
ELSE
T_END_DATE := SYSDATE;
END IF;
END IF;
--
BEGIN
FND_USER_RESP_GROUPS_API.UPDATE_ASSIGNMENT(USER_ID => I_USER_ID,
RESPONSIBILITY_ID => T_RESP_ID,
RESPONSIBILITY_APPLICATION_ID => T_AP_ID,
SECURITY_GROUP_ID => 0,
START_DATE => C1.START_DATE,
END_DATE => T_END_DATE,
DESCRIPTION => NULL);
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
END XX_FND_USER_RESP_PKG;
/*使用說明 :*/
begin
XX_fnd_user_resp_pkg.hpx_fnd_copy_user_resp(i_from_user_id
=> :i_from_user_id, ç A使用者USER ID
i_to_user_id
=> :i_to_user_id, ç B使用者USER ID
i_mode
=> :i_mode); ç 複製規則,如下說明
end;
I_MODE
: 權限複製規則
0 : 只新增使用者B短少的權限,但不將即有卻失效的權限予以復權
1 : 新增使用者B短少的權限,且將即有但失效的權限予以復權(且不影使用者B較使用者A多的權限)
2 : 完整複製,將使用者B原有權限全數失效後再執行I_MODE 2
沒有留言:
張貼留言