需求:SAP期初上线的时候,业务顾问经常会遇到批量创建角色和分配角色权限的情况
岗位需求:一般是业务顾问定义权限,BASIS进行后期运维,今天讲两个批导功能,方便期初上线
主要函数:PRGN_READ_ROLE_MENU和PRGN_SAVE_ROLE_MENU
批量给某个角色到事务码权限
第一步
建立一个底表,用于存储哪个角色需要哪些事务码权限(当然你也可以进行模板导入,相当于不存储于数据库中,只做临时界面展示,一般我建议批导功能除了批导字段外,最好加入批导日志,批导时间和批导人员)
第二步
SE38创建程序,程序代码如下
REPORT ZRIMPORT_TCODE.
DATA: role TYPE agr_name,
it_rt TYPE TABLE OF agr_tcodes,
wa_rt TYPE agr_tcodes,
i_anz TYPE p,confirm_flag(1),
it_gw TYPE TABLE OF ztrole_tcode WITH HEADER LINE,
itab_gw TYPE TABLE OF ztrole_tcode WITH HEADER LINE.
DATA: it_hier TYPE TABLE OF agr_hier,
wa_hier TYPE agr_hier,
it_dtl_flags TYPE TABLE OF agr_dtl_flags,
it_dtl_bor_methods TYPE TABLE OF agr_dtl_bor_methods,
it_dtl_bor_params TYPE TABLE OF agr_dtl_bor_params,
it_hiert TYPE TABLE OF agr_hiert,
wa_hiert TYPE agr_hiert,
i_zh LIKE sy-langu,i_anz1 TYPE p.
SELECT * FROM ztrole_tcode INTO TABLE it_gw.
APPEND LINES OF it_gw TO itab_gw.
i_zh = '1'."语言设为中文
DESCRIBE TABLE it_gw LINES i_anz.
IF i_anz > 0.
CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
textline1 = '该功能用于角色导入事务代码使用,请确保先清除自定义表ZTROLE_TCODE中的旧数据后上载新数据,'
textline2 = '确认上载的数据没问题后再执行导入操作,是否确认要执行导入操作?'
titel = '警告'
IMPORTING
answer = confirm_flag.
CASE confirm_flag.
WHEN 'N'. EXIT. "NO
WHEN 'A'. EXIT. "Cancel
WHEN 'J'. "YES
SORT itab_gw BY role.
DELETE ADJACENT DUPLICATES FROM itab_gw COMPARING role.
LOOP AT itab_gw.
CALL FUNCTION 'PRGN_READ_ROLE_MENU'
EXPORTING
role = itab_gw-role
language = i_zh
TABLES
t_nodes = it_hier
t_dtl_flags = it_dtl_flags
t_dtl_bor_methods = it_dtl_bor_methods
t_dtl_bor_params = it_dtl_bor_params
t_tcodes = it_rt
t_texts = it_hiert
EXCEPTIONS
not_authorized = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
LOOP AT it_gw WHERE role = itab_gw-role.
role = it_gw-role.
wa_rt-agr_name = role.
wa_rt-type = 'TR'.
wa_rt-tcode = it_gw-tcode.
wa_rt-direct = 'X'.
APPEND wa_rt TO it_rt.
DESCRIBE TABLE it_hier LINES i_anz1.
wa_hier-agr_name = it_gw-role.
wa_hier-object_id = i_anz1 + 2.
wa_hier-parent_id = 1.
wa_hier-sort_order = ( wa_hier-object_id - 1 ) * 10.
wa_hier-reporttype = 'TR'.
wa_hier-report = it_gw-tcode.
wa_hier-menu_level = '01'.
wa_hier-attributes = 'X'.
APPEND wa_hier TO it_hier.
wa_hiert-agr_name = it_gw-role.
wa_hiert-spras = i_zh.
wa_hiert-object_id = i_anz1 + 2.
SELECT SINGLE ttext FROM tstct INTO wa_hiert-text WHERE sprsl = i_zh AND tcode = it_gw-tcode .
APPEND wa_hiert TO it_hiert.
ENDLOOP.
CALL FUNCTION 'PRGN_SAVE_ROLE_MENU'
EXPORTING
activity_group = role " agr_name
TABLES
nodes = it_hier
texts = it_hiert
tcodes = it_rt " agr_tcodes
EXCEPTIONS
not_authorized = 1
activity_group_enqueued = 2
no_auth_for_tcodes = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CLEAR: wa_rt,it_rt[],it_hier[],it_hiert[],wa_hier,wa_hiert.
ENDLOOP.
ENDCASE.
ELSE.
MESSAGE '自定义表ZTROLE_TCODE没有要导入的数据,请按模板先上载数据!' TYPE 'I'.
ENDIF.
第三步
PFCG当中需要增加对应的角色,只需要创建角色,无需给角色分配事务码权限(给角色分配事务码权限由程序完成)
第四步
在自定义的底表中,维护好对应的角色和事务码权限,截图内容为参考截图(两个角色,多个事务码为案例)
第五步
执行程序,即可完成导入。该程序适用于大批量角色权限分配的时候(例如期初上线,或者由新增新权限,批量角色添加权限的时候)