SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_GET_IBM_I_USER_INFO ( 
	NAME_OR_ID VARCHAR(60) DEFAULT  NULL  ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.ESRTVIUSRI 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
BEGIN 
DECLARE V_SQLSTATE CHAR ( 5 ) ; 
DECLARE V_DB2_RETURN_CODE INT ; 
DECLARE V_DB2_MESSAGE_ID CHAR ( 7 ) ; 
DECLARE V_ERROR_MESSAGE CLOB ( 2048 ) ; 
-- Capture diagnostic info 
GET DIAGNOSTICS CONDITION 1 
V_DB2_RETURN_CODE = DB2_RETURNED_SQLCODE , 
V_SQLSTATE = RETURNED_SQLSTATE , 
V_ERROR_MESSAGE = MESSAGE_TEXT , 
V_DB2_MESSAGE_ID = DB2_MESSAGE_ID ; 
RETURN 
( 
VALUES JSON_OBJECT ( 
'success' VALUE 'false' , 'SQLInfo' VALUE JSON_OBJECT ( 
'SQLState' VALUE V_SQLSTATE , 
'SQLCode' VALUE V_DB2_RETURN_CODE , 
'messageID' VALUE V_DB2_MESSAGE_ID , 
'message' VALUE V_ERROR_MESSAGE , 
'currentTimestamp' VALUE CURRENT_TIMESTAMP 
) 
) ) ; 
END ; 
RETURN 
COALESCE ( 
JSON_OBJECT ( 
'status' : 'success' , 'data' VALUE ( 
SELECT JSON_ARRAYAGG ( 
JSON_OBJECT ( 
'userId' VALUE AUTHORIZATION_NAME , 
'textDescription' VALUE TEXT_DESCRIPTION , 
'status' VALUE STATUS , 
'previousSignOn' VALUE PREVIOUS_SIGNON , 
'signonInvalidAttempts' VALUE 
SIGN_ON_ATTEMPTS_NOT_VALID , 
'userClass' VALUE USER_CLASS_NAME , 
'specialAuths' VALUE JSON_ARRAY ( SELECT TRIM ( 
FRAGMENT ) 
FROM TABLE ( 
CISTOOLS . SPLIT_STRING ( 
SPECIAL_AUTHORITIES , ' ' ) 
) 
WHERE TRIM ( FRAGMENT ) IS NOT NULL 
AND TRIM ( FRAGMENT ) <> '' ) , 
'groupProfile' VALUE GROUP_PROFILE_NAME , 
'supplementalGroups' VALUE 
SUPPLEMENTAL_GROUP_LIST , 
'passwordChanged' VALUE PASSWORD_CHANGE_DATE , 
'pwExpires' VALUE DATE_PASSWORD_EXPIRES , 
'daysUntilPwExpires' VALUE 
DAYS_UNTIL_PASSWORD_EXPIRES , 
'pwExpireFlag' VALUE SET_PASSWORD_TO_EXPIRE , 
'userEmpNo' VALUE JSON_QUERY ( SAILPOINT . 
ES_GET_EMPLOYEE_NUMBER ( AUTHORIZATION_NAME ) , 
'$.data' ) FORMAT JSON , 
'jobDesc' VALUE JOB_DESCRIPTION_NAME , 
'msgQueue' VALUE MESSAGE_QUEUE_NAME , 
'outQueue' VALUE OUTPUT_QUEUE_NAME , 
'homeDir' VALUE HOME_DIRECTORY , 
'locale' VALUE LOCALE_PATH_NAME , 
'uid' VALUE USER_ID_NUMBER , 
'gid' VALUE GROUP_ID_NUMBER , 
'digitalCert' VALUE 
DIGITAL_CERTIFICATE_INDICATOR , 
'pwLevel01' VALUE PASSWORD_LEVEL_0_1 , 
'pwLevel23' VALUE PASSWORD_LEVEL_2_3 , 
'localPwMgmt' VALUE LOCAL_PASSWORD_MANAGEMENT 
) FORMAT JSON 
) 
FROM QSYS2 . USER_INFO_BASIC 
WHERE ( LOCATE ( 
UPPER ( 
TRIM ( 
IFNULL ( NAME_OR_ID , AUTHORIZATION_NAME ) ) 
) , UPPER ( AUTHORIZATION_NAME ) ) > 0 
OR LOCATE ( 
UPPER ( 
TRIM ( 
IFNULL ( NAME_OR_ID , AUTHORIZATION_NAME ) ) 
) , UPPER ( TEXT_DESCRIPTION ) ) > 0 ) ) FORMAT JSON 
) , JSON_OBJECT ( 
'success' VALUE 'false' , 'SQLInfo' VALUE JSON_OBJECT ( 
'messageID' VALUE 'SQL02000' , 
'message' VALUE 'Requested user not found ' , 
'currentTimestamp' : CURRENT_TIMESTAMP 
) 
) ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.ESRTVIUSRI 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.ESRTVIUSRI 
TO PUBLIC ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.ESRTVIUSRI 
TO QSECOFR WITH GRANT OPTION ; 

