*&---此程序用于增加指定权限(PROFILES例: SAP_ALL , S_DEVELOP),或从user1 COPY权限给user2 .
*&---"author: lijq21@qq.com
REPORT ztest_lijq LINE-SIZE 255.
TYPES: ty_profile_tab TYPE STANDARD TABLE OF tprhilf.
DATA:it_ust04 TYPE suid_tt_ust04,
is_ust04 TYPE ust04,
lt_new_profiles TYPE ty_profile_tab,
ct_usrbf2_new TYPE suid_tt_usrbf2,
ct_usrbf2_old TYPE suid_tt_usrbf2,
ct_ust10c_cache TYPE suid_tt_ust10c,
ct_ust10s_cache TYPE suid_tt_ust10s,
lt_insert TYPE suid_tt_usrbf2,
lt_delete TYPE suid_tt_usrbf2,
lo_exc TYPE REF TO cx_sy_open_sql_db.
DATA: l_usr04 LIKE usr04,
l_ust04 LIKE ust04,
lt_ust04 LIKE ust04 OCCURS 0,
l_usrbf2 LIKE usrbf2 OCCURS 0 WITH HEADER LINE,
lv_profn TYPE xuprofname.
DATA:lv_cx_root TYPE REF TO cx_root,
l_message(200) TYPE c.
FIELD-SYMBOLS:<fs_ust04> LIKE ust04,
<fs_usrbf2> LIKE usrbf2.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(30) text1.
PARAMETERS: p_user LIKE usr01-bname OBLIGATORY DEFAULT sy-uname. "将被修改权限的用户名
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(30) text7.
PARAMETERS: p_prof TYPE xuprofile DEFAULT 'S_DEVELOP' MATCHCODE OBJECT prof_single_composite_active.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(30) text2.
PARAMETERS: p_cuser LIKE usr01-bname MATCHCODE OBJECT user_comp. "参照已有权限的用户名
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text3.
SELECTION-SCREEN: BEGIN OF LINE.
PARAMETERS: rad_1 TYPE c RADIOBUTTON GROUP g1 DEFAULT 'X' USER-COMMAND a. "add
SELECTION-SCREEN COMMENT 5(30) text4.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
PARAMETERS: rad_2 TYPE c RADIOBUTTON GROUP g1. "copy
SELECTION-SCREEN COMMENT 5(30) text5.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE.
PARAMETERS: p_chk TYPE c AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN COMMENT 5(30) text6.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN END OF BLOCK b3.
*---------------------------------------------------------------------------------*
*AT SELECTION-SCREEN.
*---------------------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
IF rad_1 EQ 'X'.
LOOP AT SCREEN.
IF screen-name CP 'P_CUSER'.
screen-input = 0.
* screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ELSE.
LOOP AT SCREEN.
IF screen-name CP 'P_PROF'.
screen-input = 0.
* screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.
*---------------------------------------------------------------------------------*
*INITIALIZATION.
*---------------------------------------------------------------------------------*
INITIALIZATION.
text1 = 'Add Auth User Name'. text2 = 'Copyed User Name'. text3 = 'Option'.
text4 = 'Add Profile'. text5 = 'Copy the user''s Auth'.
text6 = 'Test Run'. text7 = 'Select Profile name'.
START-OF-SELECTION.
TRY .
IF rad_1 = 'X'.
SELECT SINGLE profn FROM usr10 INTO lv_profn WHERE profn = p_prof AND aktps = 'A'.
IF sy-subrc NE 0.
MESSAGE i000(oo) WITH 'Not found The Profile name'. RETURN.
ENDIF.
SELECT SINGLE * FROM ust04 INTO l_ust04 WHERE bname EQ p_user AND profile = p_prof.
IF sy-subrc EQ 0.
MESSAGE i000(oo) WITH 'The current USER have the Profile auth'. RETURN.
ENDIF.
SELECT SINGLE * FROM usr04 INTO l_usr04 WHERE bname EQ p_user.
IF sy-subrc NE 0.
MESSAGE i000(oo) WITH 'Not found The USER'. RETURN.
ENDIF.
is_ust04-bname = p_user.
is_ust04-profile = p_prof.
APPEND is_ust04 TO it_ust04.
APPEND p_prof TO lt_new_profiles.
*.....processing for inserted and deleted profiles
PERFORM get_opt_target_authority IN PROGRAM saplsuid_identity
USING it_ust04
lt_new_profiles
CHANGING ct_usrbf2_new
ct_ust10s_cache
ct_ust10c_cache .
*.....get current state of authorities
SELECT * FROM usrbf2 INTO TABLE ct_usrbf2_old WHERE bname = p_user ORDER BY PRIMARY KEY.
cl_suid_tools=>compare_tables( EXPORTING it_new = ct_usrbf2_new
it_old = ct_usrbf2_old
CHANGING it_del = lt_delete
it_ins = lt_insert ).
TRY.
WRITE p_prof TO l_usr04-profs+l_usr04-nrpro(12).
l_usr04-nrpro = l_usr04-nrpro + 12.
MODIFY usr04 FROM l_usr04 .
CLEAR:l_ust04.
l_ust04-bname = p_user.
l_ust04-profile = p_prof.
INSERT ust04 FROM l_ust04 .
****---测试中,会把当前prof之外的所有对象都删除.
**** IF lt_delete IS NOT INITIAL.
**** DELETE usrbf2 FROM TABLE lt_delete.
**** ENDIF.
IF lt_insert IS NOT INITIAL.
INSERT usrbf2 FROM TABLE lt_insert.
ENDIF.
CATCH cx_sy_open_sql_db INTO lo_exc.
ROLLBACK WORK.
CLEAR:l_message.
l_message = lo_exc->get_text( ).
MESSAGE l_message TYPE 'E'.
ENDTRY.
ELSEIF rad_2 = 'X'.
*--Change Table usr04 ---get refer to UNAME
SELECT SINGLE * FROM usr04 INTO l_usr04 WHERE bname EQ p_cuser.
IF sy-subrc NE 0.
EXIT.
ENDIF.
l_usr04-bname = p_user.
*---Copy Table ust04
SELECT * FROM ust04 INTO TABLE lt_ust04 WHERE bname EQ p_cuser.
LOOP AT lt_ust04 ASSIGNING <fs_ust04> CASTING.
<fs_ust04>-bname = p_user.
ENDLOOP.
*---Copy Table usrbf2
SELECT * FROM usrbf2 INTO TABLE l_usrbf2 WHERE bname = p_cuser.
LOOP AT l_usrbf2 ASSIGNING <fs_usrbf2> CASTING.
<fs_usrbf2>-bname = p_user.
ENDLOOP.
IF lt_ust04 IS NOT INITIAL AND l_usrbf2[] IS NOT INITIAL.
MODIFY usr04 FROM l_usr04 .
DELETE FROM ust04 WHERE bname = p_user.
INSERT ust04 FROM TABLE lt_ust04 .
DELETE FROM usrbf2 WHERE bname = p_user.
INSERT usrbf2 FROM TABLE l_usrbf2 ACCEPTING DUPLICATE KEYS.
ELSE.
MESSAGE 'Copy failed of the User''s Authorities ' TYPE 'E'.
EXIT.
ENDIF.
ENDIF.
IF p_chk = 'X'.
ROLLBACK WORK.
MESSAGE 'Test run success !' TYPE 'S'.
EXIT.
ENDIF.
MESSAGE 'Change Auth success !' TYPE 'S'.
CATCH cx_root INTO lv_cx_root.
ROLLBACK WORK.
CLEAR:l_message.
l_message = lv_cx_root->get_text( ).
MESSAGE l_message TYPE 'E'.
ENDTRY.