Skip to content

Instantly share code, notes, and snippets.

@oarthursilva
Created June 21, 2022 22:19
Show Gist options
  • Save oarthursilva/ec4c3396116ed75b6a9e5589c1480f0d to your computer and use it in GitHub Desktop.
Save oarthursilva/ec4c3396116ed75b6a9e5589c1480f0d to your computer and use it in GitHub Desktop.
SAP ABAP Authority Check
CLASS zcl_core_authority_check DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES zif_core_authority_check .
CONSTANTS:
BEGIN OF sc_activity_field,
actvt TYPE zcore_auth_field VALUE 'ACTVT',
END OF sc_activity_field .
CONSTANTS:
BEGIN OF sc_activity_value,
display TYPE zcore_auth_value VALUE '03',
END OF sc_activity_value .
CONSTANTS sc_authorized TYPE abap_bool VALUE abap_true ##NO_TEXT.
CONSTANTS sc_unauthorized TYPE abap_bool VALUE abap_false ##NO_TEXT.
METHODS constructor
IMPORTING
!io_auth_conditions TYPE REF TO if_auth_objects_to_conditions OPTIONAL .
PROTECTED SECTION.
PRIVATE SECTION.
DATA mo_auth_conditions TYPE REF TO if_auth_objects_to_conditions .
METHODS is_authorized
IMPORTING
!iv_authorization_object TYPE zcore_auth_object
RETURNING
VALUE(result) TYPE abap_bool
RAISING
zcx_core_authority_check .
ENDCLASS.
CLASS ZCL_CORE_AUTHORITY_CHECK IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_CORE_AUTHORITY_CHECK->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] IO_AUTH_CONDITIONS TYPE REF TO IF_AUTH_OBJECTS_TO_CONDITIONS(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
IF mo_auth_conditions IS BOUND.
mo_auth_conditions = io_auth_conditions.
ELSE.
mo_auth_conditions = cl_auth_objects_to_conditions=>create( NEW cl_auth_object_values_provider( ) ).
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_CORE_AUTHORITY_CHECK->IS_AUTHORIZED
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_AUTHORIZATION_OBJECT TYPE ZCORE_AUTH_OBJECT
* | [<-()] RESULT TYPE ABAP_BOOL
* | [!CX!] ZCX_CORE_AUTHORITY_CHECK
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD is_authorized.
mo_auth_conditions->get_conditions(
EXPORTING
iv_authorization_object = iv_authorization_object
IMPORTING
et_conditions = DATA(lt_conditions)
ev_authorized = result
).
IF result = abap_false.
RAISE EXCEPTION TYPE zcx_core_authority_check
EXPORTING
textid = zcx_core_authority_check=>not_authorized
mv_authorization_object = iv_authorization_object.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_CORE_AUTHORITY_CHECK->ZIF_CORE_AUTHORITY_CHECK~CHECK_DISPLAY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_AUTHORIZATION_OBJECT TYPE ZCORE_AUTH_OBJECT
* | [--->] IT_FIELD_MAPPING TYPE TT_FIELD_MAPPING(optional)
* | [<-()] RESULT TYPE ABAP_BOOL
* | [!CX!] ZCX_CORE_AUTHORITY_CHECK
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD zif_core_authority_check~check_display.
mo_auth_conditions->add_authorization_object(
iv_authorization_object = iv_authorization_object
it_activities = VALUE #( ( auth_field = sc_activity_field-actvt value = sc_activity_value-display ) )
it_field_mapping = it_field_mapping
).
result = is_authorized( iv_authorization_object ).
ENDMETHOD.
ENDCLASS.
@oarthursilva
Copy link
Author

oarthursilva commented Jun 21, 2022

how to use:

*&---------------------------------------------------------------------*
*& Report ZTEST_AUTHORITY_CHECK
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_authority_check.

DATA lo_authority_check TYPE REF TO zif_core_authority_check.
CREATE OBJECT lo_authority_check TYPE zcl_core_authority_check.

TRY.
    DATA(lv_is_authorized) = lo_authority_check->check_display( 'ZMM_PR_USR' ).
  CATCH zcx_core_authority_check INTO DATA(lx_core_authority_check).
    lx_root->raise_message( ).
ENDTRY.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment