实施实例权限

ABAP RESTful 应用程序编程模型 › 开发 › 开发应用程序 › 使用独占草稿功能开发事务应用 › 使用 UUID 键值构建被管理业务对象 › 开发事务行为 › 开发权限 › 实施实例权限

对于具有草稿的托管场景,授权用户基于权限字段修改差旅实例。

在行为定义中定义实例权限,并在 Travel 的行为实施类中实施该权限。有关概念信息,请参阅权限控制。

备注

在 ABAP 航班参考场景中,权限对象未在后端配置,因此将阻止任何用户进行任何操作。因此,除了通过权限对象 进行的检查外,代码示例还提供模拟的权限检查,而不使用权限对象。不要在生产应用程序中使用模拟代码。

定义

如果尚未执行此操作,请在行为定义中为差旅实体定义 authorization master (instance),为预订和预订补充实体定义 authorization dependent by _Travel。

如果尚未完成,请委派所有操作的权限控制,内部操作和草稿操作除外,以及与更新操作逐个创建的关联。使用语法 authorization: update。

示例代码

...
define behavior for /DMO/R_Travel_D alias Travel
implementation in class /DMO/BP_TRAVEL_D unique
persistent table /DMO/A_TRAVEL_D
...
authorization master ( global, instance )
{...
  association _Booking { create ( features : instance, authorization: update ) ; }

...

  action ( features : instance, authorization: update ) acceptTravel result [1] $self;
  action ( features : instance, authorization: update ) rejectTravel result [1] $self;
  action ( features : instance, authorization: update ) deductDiscount parameter /DMO/A_TRAVEL_DISCOUNT result [1] $self;
  internal action reCalcTotalPrice;

...}

define behavior for /DMO/R_Booking_D alias Booking
implementation in class /DMO/BP_BOOKING_D unique
....
authorization dependent by _Travel
{...
  association _BookingSupplement { create ( authorization: update ); }
...}

define behavior for /DMO/R_BookingSupplement_D alias BookingSupplement
implementation in class /DMO/BP_BOOKINGSUPPLEMENT_D unique
...
authorization dependent by _Travel
{
...
}

通过快速修复,您可以直接在行为定义编辑器中为行为池中的权限控制实施生成方法声明。

实施

实例权限控制必须在方法 FOR INSTANCE AUTHORIZATION 的行为实施中实施。您可以在方法声明的行为定义中使用快速修复。方法导入参数 requested_authorizations,指示请求权限的操作。与其他实例特定的实施一样,该方法导入请求权限的实例的键值。在实施中,必须填充参数 result,该参数必须使用每个请求操作的权限结果填充。该方法还具有更改参数 reported,如果要返回未授权案例的消息,则填充该参数。有关实施详细信息的详细信息,请参阅权限实施。

本指南中的权限概念假设创建、更新和删除实例可能存在不同的权限。考虑与更新操作具有相同权限的差旅业务对象的所有操作。通过此方法,可能允许一个用户创建新的差旅实例,但不允许其使用操作。

由于创建、更新和删除权限对于全局权限和实例权限是相同的(实例权限的相关权限字段除外),因此最佳实践是以单独的方法外包实际权限检查,这些方法可以为业务对象实施中所需的所有权限检查调用。这些方法的实施在权限检查方法中进行了详细说明。由于实例权限检查基于字段值,因此必须调整方法以同时导入参数。

以下步骤将指导您完成实施。

权限检查方法

  1. 在方法 is_update_granted 和 is_delete_granted 中,添加可选导入参数以传递所用代理标识的国家/地区代码。(方法 is_create_granted 不用于实例权限检查,因为创建操作不是实例特定的。)

  2. 在相应方法的方法实施中,通过添加 if-block 区分来自全局和实例权限的调用,其中当提供权限字段 country_code 时会调用权限对象。

  3. 根据 sy-subrc 评估相应操作的权限并填充方法的响应参数。

  4. 由于由权限对象检查的权限未在后端配置,因此我们提供模拟实施以允许所有用户执行所有操作。不要在生产应用程序中使用此代码。

  5. 要针对代理的国家/地区代码模拟权限检查,我们提供模拟实施,仅允许修改具有国家/地区代码为 US 的代理的实例。您可以通过任何其他方式更改此国家/地区代码。

    展开以下代码示例以查看方法 is_update_granted 和 is_delete_granted 的源代码。

    示例代码

    CLASS lhc_travel DEFINITION INHERITING FROM cl_abap_behavior_handler.
    
      PRIVATE SECTION.
    
    ...
        METHODS is_update_granted
          IMPORTING country_code          TYPE land1 OPTIONAL
          RETURNING VALUE(update_granted) TYPE abap_bool.
        METHODS is_delete_granted
          IMPORTING country_code          TYPE land1 OPTIONAL
          RETURNING VALUE(delete_granted) TYPE abap_bool.
    
    ENDCLASS.
    
      METHOD is_update_granted.
        "For instance auth
        IF country_code IS SUPPLIED.
          AUTHORITY-CHECK OBJECT '/DMO/TRVL'
            ID '/DMO/CNTRY' FIELD country_code
            ID 'ACTVT'      FIELD '02'.
          update_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
    
          "Simulation for full authorization
          "(not to be used in productive code)
          update_granted = abap_true.
    
          " simulation of auth check for demo,
          " auth granted for country_code US, else not
    *      CASE country_code.
    *        WHEN 'US'.
    *          create_granted = abap_true.
    *        WHEN OTHERS.
    *          create_granted = abap_false.
    *      ENDCASE.
    
          "For global auth
        ELSE.
          AUTHORITY-CHECK OBJECT '/DMO/TRVL'
            ID '/DMO/CNTRY' DUMMY
            ID 'ACTVT'      FIELD '02'.
          update_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
    
          "Simulation for full authorization
          "(not to be used in productive code)
          update_granted = abap_true.
        ENDIF.
    
      ENDMETHOD.
    
      METHOD is_delete_granted.
        "For instance auth
        IF country_code IS SUPPLIED.
          AUTHORITY-CHECK OBJECT '/DMO/TRVL'
            ID '/DMO/CNTRY' FIELD country_code
            ID 'ACTVT'      FIELD '06'.
          delete_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
    
          "Simulation for full authorization
          "(not to be used in productive code)
          delete_granted = abap_true.
    
          " simulation of auth check for demo,
          " auth granted for country_code US, else not
    *      CASE country_code.
    *        WHEN 'US'.
    *          create_granted = abap_true.
    *        WHEN OTHERS.
    *          create_granted = abap_false.
    *      ENDCASE.
    
          "For global auth
        ELSE.
          AUTHORITY-CHECK OBJECT '/DMO/TRVL'
            ID '/DMO/CNTRY' DUMMY
            ID 'ACTVT'      FIELD '06'.
          delete_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
    
          "Simulation for full authorization
          "(not to be used in productive code)
          delete_granted = abap_true.
        ENDIF.
    
      ENDMETHOD.
    
    ...
    
    ENDCLASS.
    

实例权限实施

在此场景中,实例权限基于差旅实例中使用的旅行社国家/地区代码的值。通过实例权限,您只能根据实例的持久状态检查权限。这意味着,必须提供持久表上的国家/地区代码值(而不是由请求提供的 值)。换句话说,如果更新请求将代理的值更改为不在该用户的权限范围内的值,则不会在事务阶段拒绝该请求(因为持久表上的值是用户获得授权的值)。仅当预检查检查传入值的权限时,才会在保存序列期间拒绝请求。请参阅实施预检查。

对于持久数据库表中尚不存在的实例(因为它们刚刚在当前逻辑工作单元中创建),始终授予权限。这也与编辑草稿的草稿场景相关。

以下步骤将指导您完成实例权限的实施步骤。

  1. 读取导入 keys 到内部表 travels 的所有实例。由于 AgencyID 与权限相关,因此必须将其包含在读取结果中。

  2. 要获取代理的已导入实例键值持久版本的国家/地区代码,请从持久表中选择代理表 _D 中具有内连接的差旅 /DMO/A_TRAVEL/DMO/AGENCY。

  3. 循环 travels 表以单独为每个实例分配权限。需要考虑两种情况:

    • 对于数据库表 /DMO/A_TRAVEL_D 中具有持久状态的实例,将权限检查委托给相关方法进行更新或删除,并传递代理相应国家/地区代码的检索值。要检查是否存在持久实例,请检查选择结果中是否存在具有当前差旅 UUID 的实例。如果 sy-subrc 返回 0,则表示读取成功。

      如果相应的权限检查方法拒绝相应操作的权限,则使用适当的消息填充 reported 参数。

    • 对于数据库表 /DMO/A_TRAVEL_D 上没有持久状态的实例,检查创建的权限,因为只要未提交更改,就会将新创建实例的更新或删除操作视为创建操作的一部分。

  4. 使用为每个操作确定的权限填充结果参数 result。

    展开以下代码示例以查看方法 get_instance_authorizations 的源代码。

    示例代码

    CLASS lhc_travel DEFINITION INHERITING FROM cl_abap_behavior_handler.
    
      PRIVATE SECTION.
    
    ...
        METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
          IMPORTING keys REQUEST requested_authorizations FOR travel RESULT result.
    
    ENDCLASS.
    
    CLASS lhc_travel IMPLEMENTATION.
      METHOD get_instance_authorizations.
    
        DATA: update_requested TYPE abap_bool,
              delete_requested TYPE abap_bool,
              update_granted   TYPE abap_bool,
              delete_granted   TYPE abap_bool.
    
        READ ENTITIES OF /DMO/R_Travel_D IN LOCAL MODE
          ENTITY Travel
            FIELDS ( AgencyID )
            WITH CORRESPONDING #( keys )
        RESULT DATA(travels)
        FAILED failed.
    
        CHECK travels IS NOT INITIAL.
    
        "Select country_code and agency of corresponding persistent travel instance
        "authorization  only checked against instance that have active persistence
        SELECT  FROM /DMO/A_TRAVEL_D AS travel  INNER JOIN /DMO/AGENCY AS agency
              ON travel~agency_id = agency~agency_id
              FIELDS travel~travel_uuid , travel~agency_id, agency~country_code
              FOR ALL ENTRIES IN @travels WHERE travel_uuid EQ @travels-TravelUUID
              INTO  TABLE @DATA(travel_agency_country).
    
        "edit is treated like update
        update_requested = COND #( WHEN requested_authorizations-%update                = if_abap_behv=>mk-on OR
                                        requested_authorizations-%action-Edit           = if_abap_behv=>mk-on
                                   THEN abap_true ELSE abap_false ).
    
        delete_requested = COND #( WHEN requested_authorizations-%delete                = if_abap_behv=>mk-on
                                   THEN abap_true ELSE abap_false ).
    
        LOOP AT travels INTO DATA(travel).
          "get country_code of agency in corresponding instance on persistent table
          READ TABLE travel_agency_country WITH KEY travel_uuid = travel-TravelUUID
            ASSIGNING FIELD-SYMBOL(<travel_agency_country_code>).
    
          "Auth check for active instances that have before image on persistent table
          IF sy-subrc = 0.
    
            "check auth for update
            IF update_requested = abap_true.
              update_granted = is_update_granted( <travel_agency_country_code>-country_code  ).
              IF update_granted = abap_false.
                APPEND VALUE #( %tky = travel-%tky
                                %msg = NEW /dmo/cm_flight_messages(
                                                         textid    = /dmo/cm_flight_messages=>not_authorized_for_agencyid
                                                         agency_id = travel-AgencyID
                                                         severity  = if_abap_behv_message=>severity-error )
                                %element-AgencyID = if_abap_behv=>mk-on
                               ) TO reported-travel.
              ENDIF.
            ENDIF.
    
            "check auth for delete
            IF delete_requested = abap_true.
              delete_granted = is_delete_granted( <travel_agency_country_code>-country_code ).
              IF delete_granted = abap_false.
                APPEND VALUE #( %tky = travel-%tky
                                %msg = NEW /dmo/cm_flight_messages(
                                         textid   = /dmo/cm_flight_messages=>not_authorized_for_agencyid
                                         agency_id = travel-AgencyID
                                         severity = if_abap_behv_message=>severity-error )
                                %element-AgencyID = if_abap_behv=>mk-on
                               ) TO reported-travel.
              ENDIF.
            ENDIF.
    
            " operations on draft instances and on active instances that have no persistent before image (eg Update on newly created instance)
            " create authorization is checked, for newly created instances
          ELSE.
            update_granted = delete_granted = is_create_granted( ).
            IF update_granted = abap_false.
              APPEND VALUE #( %tky = travel-%tky
                              %msg = NEW /dmo/cm_flight_messages(
                                       textid   = /dmo/cm_flight_messages=>not_authorized
                                       severity = if_abap_behv_message=>severity-error )
                              %element-AgencyID = if_abap_behv=>mk-on
                            ) TO reported-travel.
            ENDIF.
          ENDIF.
    
          APPEND VALUE #( LET upd_auth = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed
                                                 ELSE if_abap_behv=>auth-unauthorized )
                              del_auth = COND #( WHEN delete_granted = abap_true THEN if_abap_behv=>auth-allowed
                                                 ELSE if_abap_behv=>auth-unauthorized )
                          IN
                           %tky = travel-%tky
                           %update                = upd_auth
                           %action-Edit           = upd_auth
    
                           %delete                = del_auth
                        ) TO result.
        ENDLOOP.
    
      ENDMETHOD.
    
    ENDCLASS.
    

来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译

← 实施全局权限 · 实施预检查 →