无常笔记ABAP Cheat Sheets中文导读全站搜索

英文原文:SAP-samples · 上游版本 d43eb13c4c7c · 原始 Markdown

Authorization Checks

This cheat sheet ...

Introduction

⬆️ back to top

Terms

The following topic covers authorization-related terms and provides you with the basics: Authorization Basics

⬆️ back to top

Explicit Authorization Checks Using AUTHORITY-CHECK Statements

⬆️ back to top

Implicit Authorization Checks Using CDS Access Control for Read Accesses

⬆️ back to top

Executable Example (SAP Business AI Platform, ABAP environment)

[!NOTE]

Implementation Steps

[!NOTE]

Expand the following collapsible section for the implementation steps required for the executable example.

🟢 Click to expand for the implementation steps

For creating a CDS access control, proceed as follows:

⬆️ back to top

Example Class

Expand the following collapsible section for example code. Provided that the implementation steps have been walked through, the example explores explicit and implicit authorization checks. To try it out, create a demo class named zcl_demo_abap_auth and paste the code into it. After activation, choose F9 in ADT to execute the class. The example is set up to display output in the console.

🟢 Click to expand for example code
CLASS zcl_demo_abap_auth DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS zcl_demo_abap_auth IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.
    "----------------- NOTE -----------------
    "The example explores explicit authorization checks with
    "AUTHORITY-CHECK statements and implicit authorization checks
    "when reading from CDS view entity for which an access control
    "has been specified.
    "The example assumes that you have walked through the
    "implementation steps in the Authorization Checks cheat sheet
    "document, and that you have used the names for artifacts
    "outline there.

    "Populating demo database tables
    zcl_demo_abap_aux=>fill_dbtabs( ).

    "SELECT statement retrieving data from a database table
    "without explicit authorization check
    "This is to emphasize that ABAP SQL statements do not trigger
    "any authorization checks in the database system. Therefore,
    "all data is read from the database table.
    SELECT * FROM zdemo_abap_flsch INTO TABLE @DATA(itab1).

    out->write( data = itab1 name = `itab1` ).
    out->write( |\n| ).

    "----------------- AUTHORITY-CHECK statements -----------------

    "The following examples explore AUTHORITY-CHECK statements by
    "checking out different values for the authorization fields.
    "In the example, CRUD operations are permitted in case of the
    "value 'US'.

    "Using field value 'US'
    "Expected result: Allowed
    AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
            ID 'ZAUTH_CTRY' FIELD 'US'
            ID 'ACTVT'      FIELD '03'.

    IF sy-subrc = 0.
      out->write( `US/03: Allowed` ).
    ELSE.
      out->write( `US/03: Not allowed` ).
    ENDIF.
    out->write( |\n| ).

    "Using a non-specified value for the authorization field
    "Expected result: Not allowed
    AUTHORITY-CHECK
        OBJECT 'ZAUTH_OBJ'
            ID 'ZAUTH_CTRY' FIELD 'DE'
            ID 'ACTVT'      FIELD '03'.

    IF sy-subrc = 0.
      out->write( `DE/03: Allowed` ).
    ELSE.
      out->write( `DE/03: Not allowed` ).
    ENDIF.
    out->write( |\n| ).

    "Using a non-specified value for the authorization field
    "Expected result: Not allowed
    AUTHORITY-CHECK
        OBJECT 'ZAUTH_OBJ'
            ID 'ZAUTH_CTRY' FIELD 'IT'
            ID 'ACTVT'     FIELD '03'.

    IF sy-subrc = 0.
      out->write( `IT/03: Allowed` ).
    ELSE.
      out->write( `IT/03: Not allowed` ).
    ENDIF.
    out->write( |\n| ).
    out->write( |\n| ).

    "The following example checks various values specified for
    "the authorization fields.
    "Expected result: Only the first 4 entries in the table should have "Allowed"
    "The value 10 is not specified for ACTVT in the example.
    TYPES c3 TYPE c LENGTH 3.
    TYPES c3tab TYPE TABLE OF c3 WITH EMPTY KEY.
    DATA auth_check TYPE string_table.
    LOOP AT VALUE c3tab( ( 'US' ) ( 'JP' ) ( 'IT' ) ( 'DE' ) ) INTO DATA(auth_field_z).
      LOOP AT VALUE c3tab( ( '01' ) ( '02' ) ( '03' ) ( '06' ) ( '10' ) ) INTO DATA(auth_field_atvt).
        AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
          ID 'ZAUTH_CTRY' FIELD auth_field_z
          ID 'ACTVT'      FIELD auth_field_atvt.

        IF sy-subrc = 0.
          APPEND |ZAUTH_CTRY '{ auth_field_z }', ACTVT '{ auth_field_atvt }', sy-subrc '{ sy-subrc }': Allowed| TO auth_check.
        ELSE.
          APPEND |ZAUTH_CTRY '{ auth_field_z }', ACTVT '{ auth_field_atvt }', sy-subrc '{ sy-subrc }': Not allowed| TO auth_check.
        ENDIF.
      ENDLOOP.
    ENDLOOP.

    out->write( data = auth_check name = `auth_check`  ).
    out->write( |\n|  ).

    "The following examples explore various specifications for the authorization field values
    "and the DUMMY addition.

    "Activity authorization field value not specified in the example
    "Expected result: sy-subrc = 4, authorization check not successful
    AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
      ID 'ACTVT' FIELD '10'.

    out->write( |1) ACTVT checked and value does not exist, sy-subrc '{ sy-subrc }'| ).

    "DUMMY addition
    "ZAUTH_CTRY not checked, ACTVT checked and value exists
    "Expected result: sy-subrc = 0, authorization check successful or no check was performed
    AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
      ID 'ZAUTH_CTRY' DUMMY
      ID 'ACTVT' FIELD '03'.

    out->write( |2) ZAUTH_CTRY not checked due to DUMMY addition, ACTVT checked and value exists, sy-subrc '{ sy-subrc }'| ).

    "ZAUTH_CTRY not checked, ACTVT checked and value does not exist
    "Expected result: sy-subrc = 4, authorization check not successful, value specified not found
    AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
      ID 'ZAUTH_CTRY' DUMMY
      ID 'ACTVT'      FIELD '10'.

    out->write( |3) ZAUTH_CTRY not checked due to DUMMY addition, ACTVT checked and value does not exist, sy-subrc '{ sy-subrc }'| ).

    "ZAUTH_CTRY checked and value does not exist, ACTVT not checked
    "Expected result: sy-subrc = 4, authorization check not successful, value specified not found
    AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
      ID 'ZAUTH_CTRY' FIELD 'XX'
      ID 'ACTVT'      DUMMY.

    out->write( |4) ZAUTH_CTRY checked and value does not exist, ACTVT not checked due to DUMMY addition, sy-subrc '{ sy-subrc }'| ).

    "The following example includes a non-existent authorization field and ACTVT. None is checked.
    "Expected result: sy-subrc = 0, in this case, no check was performed
    AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
      ID 'NON_EXISTENT' DUMMY
      ID 'ACTVT'        DUMMY.

    out->write( |5) DUMMY addition specified for all authorization fields, sy-subrc '{ sy-subrc }'| ).

    "Non-existent authorization object specified, authorization fields with DUMMY addition
    "Expected result: sy-subrc = 12, no authorization found
    AUTHORITY-CHECK OBJECT 'NON_EXISTENT'
      ID 'NON_EXISTENT' DUMMY
      ID 'ACTVT'        DUMMY.

    out->write( |6) Using a non-existent authorization object, DUMMY addition specified for all authorization fields, sy-subrc '{ sy-subrc }'| ).
    out->write( |\n| ).
    out->write( |\n| ).

    "----------------- Implict authorization checks using CDS access control -----------------
    "The following SELECT statement retrieves data using a CDS view entity as data source.
    "In the example, a CDS access control is included. A WHERE clause is not specified.
    SELECT * FROM zdemo_abap_flsch_ve_auth INTO TABLE @DATA(itab2).

    out->write( data = itab2 name = `itab2` ).
    out->write( |\n| ).

    DATA flag TYPE abap_boolean.
    LOOP AT itab2 INTO DATA(line) WHERE countryfr <> 'US'.
      flag = abap_true.
    ENDLOOP.

    IF flag = abap_true.
      out->write( `Something went wrong with the example procedure. There should only be entries with the condition countryfr = 'US'.` ).
    ELSE.
      out->write( `Expected result. There are only entries with the condition countryfr = 'US' in the internal table.` ).
    ENDIF.
    out->write( |\n| ).

    "Further SELECT statements that specify WHERE clauses
    SELECT * FROM zdemo_abap_flsch_ve_auth WHERE countryfr = 'US' INTO TABLE @DATA(itab3).
    SELECT * FROM zdemo_abap_flsch_ve_auth WHERE countryfr = 'DE' INTO TABLE @DATA(itab4).

    out->write( `SELECT statements that specify WHERE clauses:` ).
    IF itab3 IS NOT INITIAL AND itab4 IS INITIAL.
      out->write( `Expected result. Only entries with the condition countryfr = 'US' should be accessible.` ).
    ELSE.
      out->write( `Something went wrong with the example procedure. There should only be entries with the condition countryfr = 'US'.` ).
    ENDIF.
  ENDMETHOD.

ENDCLASS.

⬆️ back to top

Excursion: Authorization Control in RAP

The following code snippets provide a rudimentary code skeleton to get an idea about global and instance authorization implementations. For more detailed examples, refer to the links provided below.

Global authorization:

"------------ GLOBAL AUTHORIZATION ------------ 

"Example specification in the BDEF
...
authorization master ( global )
...

********************************************

"Example RAP handler method declaration in an ABAP 
"behavior pool to handle global authorization checks

...

METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION
      IMPORTING REQUEST requested_authorizations FOR some_bdef RESULT result.

...

********************************************

"Example RAP handler method implementation 
"You can use the method to check the authorizations against 
"specified authorization objects, fields and activities.
"For example: create, value 01 / update and actions, value 02 /
"delete, value 06
"Note: A check against authorization fields and concrete values such as 
"ZAUTH_FIELD in the example is not relevant here. The authorization check
"is relevant for activities, independent of the state of the instances. 
"Therefore, the authorization field is specified with the DUMMY addition.

... 

METHOD get_global_authorizations.
    
    "Authorization check for create operations
    IF requested_authorizations-%create = if_abap_behv=>mk-on.

      AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
              ID 'ZAUTH_FIELD' DUMMY
              ID 'ACTVT'      FIELD '01'.

      result-%create = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized ).
      "In case there is no authorization granted, you may want to add a message to the reported structure here.

    ENDIF.

    "Authorization check for update operations
    IF requested_authorizations-%update =  if_abap_behv=>mk-on.
      "Note: In draft scenarios, the Edit action is treated like an update operation.
      "So, you can add another condition such as ... OR requested_authorizations-%action-Edit =  if_abap_behv=>mk-on.

      AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
              ID 'ZAUTH_FIELD' DUMMY
              ID 'ACTVT'      FIELD '02'.

      result-%update  = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized ).
      "result-%action-Edit    = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized ).
      "In case there is no authorization granted, you may want to add a message to the reported structure here.
    ENDIF.

    "Authorization check for delete operations
    IF requested_authorizations-%delete = if_abap_behv=>mk-on.

      AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
              ID 'ZAUTH_FIELD' DUMMY
              ID 'ACTVT'      FIELD '06'.

      result-%delete = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized ).
      "In case there is no authorization granted, you may want to add a message to the reported structure here.
    ENDIF.

  ENDMETHOD.

Instance authorization:

"------------ INSTANCE AUTHORIZATION ------------ 

"Example specification in the BDEF
"Both global and instance authorization can be specified.
...
authorization master ( global, instance )
...

********************************************

"Example RAP handler method declaration in an ABAP 
"behavior pool to handle instance authorization checks

...

METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
     IMPORTING keys REQUEST requested_authorizations FOR some_bdef RESULT result.

...

********************************************

"Example RAP handler method implementation 

... 

METHOD get_instance_authorizations.
    
   "Reading all the instances with the imported keys into an internal table
    READ ENTITIES OF some_bdef IN LOCAL MODE
      ENTITY some_entity
      ALL FIELDS WITH CORRESPONDING #( keys )
      RESULT DATA(res)
      ...

    IF res IS INITIAL.
      RETURN.
    ENDIF.

    LOOP AT res INTO DATA(r).
      
      ...

      "Covering only delete operations in the code snippet
      IF requested_authorizations-%delete = if_abap_behv=>mk-on.

        AUTHORITY-CHECK OBJECT 'ZAUTH_OBJ'
           ID 'ZAUTH_FIELD' FIELD r-some_field
           ID 'ACTVT'       FIELD '06'.

        DATA(is_delete_allowed) = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized ).
      ENDIF.

      ...

      APPEND VALUE #( ...
                      %delete = is_delete_allowed
                      ...
                    ) TO result.
    ENDLOOP.

ENDMETHOD.

More information and implementation examples:

⬆️ back to top

More Information