Google Search - Blog...........

SAP ABAP - Update Roles For The Users.

REPORT zrole_update.

TABLES: agr_define.

TYPES: BEGIN OF ty_upload,
user TYPE char30,
role TYPE char30,
END OF ty_upload.

DATA: username LIKE bapibname-bapibname,
activitygroups TYPE TABLE OF bapiagr WITH HEADER LINE,
return TYPE TABLE OF bapiret2 WITH HEADER LINE,
uname LIKE bapibname-bapibname,
roles TYPE TABLE OF bapiagr WITH HEADER LINE,
ret TYPE TABLE OF bapiret2 WITH HEADER LINE,
ret2 LIKE bapireturn1,
itab TYPE STANDARD TABLE OF alsmex_tabline WITH HEADER LINE,
it_upload TYPE TABLE OF ty_upload WITH HEADER LINE.

SELECT-OPTIONS: s_name FOR agr_define-agr_name.

PARAMETERS: p_file TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = 'P_FILE'
IMPORTING
file_name = p_file.


START-OF-SELECTION.

PERFORM get_file_data.

END-OF-SELECTION.

PERFORM get_user_roles.

PERFORM create_roles.
*&---------------------------------------------------------------------*
*& Form GET_FILE_DATA
*----------------------------------------------------------------------*
FORM get_file_data .


CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = 1
i_begin_row = 1
i_end_col = 10000
i_end_row = 65535
TABLES
intern = itab.

DELETE itab WHERE row = 1.


LOOP AT itab .

CASE: itab-col.

WHEN '0001'.
it_upload-user = itab-value.

WHEN '0002'.
it_upload-role = itab-value.

ENDCASE.

AT END OF row.
APPEND it_upload.
ENDAT.

ENDLOOP.

ENDFORM. " GET_FILE_DATA
*&---------------------------------------------------------------------*
*& Form GET_USER_ROLES
*----------------------------------------------------------------------*
FORM get_user_roles .

LOOP AT it_upload.

username = uname = it_upload-user .

CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
username = uname
TABLES
activitygroups = roles
return = ret.

LOOP AT roles.
CLEAR : roles-to_dat.
roles-to_dat = '99991231'.
MOVE roles TO activitygroups .
APPEND activitygroups.
ENDLOOP.

CALL FUNCTION 'BAPI_USER_ACTGROUPS_ASSIGN'
EXPORTING
username = username
TABLES
activitygroups = activitygroups
return = return.

IF return IS INITIAL.
WRITE:/ 'Roles for the user is successful with end dates', username.
ELSE.
WRITE:/ 'Roles for the user is unsuccessful with end dates', username.
ENDIF.

ENDLOOP.

ENDFORM. " GET_USER_ROLES
*&---------------------------------------------------------------------*
*& Form CREATE_ROLES
*----------------------------------------------------------------------*
FORM create_roles .

DATA : v_user TYPE xubname,
it_roles TYPE TABLE OF bapiagr WITH HEADER LINE,
wa_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
TYPES: BEGIN OF ty_texts,
agr_name TYPE agr_texts-agr_name,
text TYPE agr_texts-text,
END OF ty_texts.

DATA: it_text TYPE TABLE OF ty_texts WITH HEADER LINE.

SELECT agr_name
text
FROM agr_texts
INTO TABLE it_text
FOR ALL ENTRIES IN it_upload
WHERE agr_name = it_upload-role.


LOOP AT it_upload.


it_roles-agr_name = it_upload-role.
it_roles-from_dat = sy-datum.
it_roles-from_dat = '99991231'.

READ TABLE it_text WITH KEY agr_name = it_upload-role.
IF sy-subrc = 0.
it_roles-agr_text = it_text-text.
ENDIF.

APPEND it_roles.
CLEAR it_roles.

AT END OF user.

v_user = it_upload-user.

CALL FUNCTION '/SDF/UPDATE_AUTH_ROLES'
EXPORTING
username = v_user
TABLES
roles = it_roles
return = wa_return.

ENDAT.

ENDLOOP.


ENDFORM. " CREATE_ROLES

No comments:

Post a Comment