ABAP权限对象设计与权限检查的实现

2020-11-27 17:18:35 浏览数 (1)

ABAP权限对象设计与权限检查的实现

重难点:①修改标准表MAKT数据 ②权限设置

su20创建权限字段(有可能SPRAS为char1 YY为char2 所以不行)

SU21创建权限对象

创建参数文件

SU20时创建一个YY 对应三种语言即可

!!!创建错误时:SU02—>用户维护—>权限—>选择需要删除的对象类—>选择删除

代码语言:javascript复制
TABLES:makt.
TYPE-POOLS: slis.
INCLUDE <icon>.

DATA: u_command TYPE slis_formname VALUE 'USER_COMMAND',
      ok_code   LIKE sy-ucomm.



*  *ALV 定义声明

DATA: it_fieldcat  TYPE lvc_t_fcat,     "slis_t_fieldcat_alv WITH HEADER LINE,
      wa_fieldcat  TYPE lvc_s_fcat,

      gd_tab_group TYPE slis_t_sp_group_alv,
      gd_layout    TYPE lvc_s_layo,     "slis_layout_alv,
      gd_repid     LIKE sy-repid.
TYPES: BEGIN OF t_display.
        INCLUDE STRUCTURE makt.

TYPES:flag(1),
      save_flag(1),
      maktx_en     LIKE makt-maktx,
      maktx_vi     LIKE makt-maktx,
      celltab      TYPE lvc_t_styl, "全选字段,非全选字段
      field_style  TYPE lvc_t_styl, "FOR DISABLE

      END OF t_display.

DATA: lo_guid TYPE REF TO cl_gui_alv_grid.
DATA: lines TYPE i.
DATA: flag_prn(1),
      gt_event      TYPE slis_t_event WITH HEADER LINE,
      gt_event_exit TYPE slis_t_event_exit WITH HEADER LINE.

DATA: g_bapi_makt   TYPE TABLE OF bapi_makt WITH HEADER LINE, " 物料描述 语言
      g_bapimathead TYPE TABLE OF bapimathead WITH HEADER LINE,   " 带有控制信息的表头段
      g_makt        TYPE TABLE OF t_display WITH HEADER LINE.      "物料数据
*显示内表
DATA: it_display TYPE STANDARD TABLE OF t_display WITH HEADER LINE, "加上这个“WITH HEADER LINE”表示可以对表进行SQL操作-------ZYG
      wa_display TYPE t_display.
DATA:it_display1 TYPE STANDARD TABLE OF t_display WITH HEADER LINE."加上这个“WITH HEADER LINE”表示可以
DATA:it_display_save TYPE STANDARD TABLE OF makt WITH HEADER LINE.     "临时存


SELECTION-SCREEN BEGIN OF BLOCK blk1  WITH FRAME TITLE text-s01. "定义列表显示名称

*PARAMETER: p1 RADIOBUTTON GROUP g1 DEFAULT 'X',
*           p2 RADIOBUTTON GROUP g1.

SELECT-OPTIONS: s_matnr FOR makt-matnr.
*PARAMETERS:  s_spras TYPE makt-spras OBLIGATORY.

SELECTION-SCREEN END OF BLOCK blk1.

INITIALIZATION.

START-OF-SELECTION.



* PERFORM check_authority.
  PERFORM get_data.

  PERFORM build_fieldcatalog CHANGING  it_fieldcat .
  PERFORM frm_alv_output_data.


*
*FORM check_authority .
*  AUTHORITY-CHECK OBJECT 'ZWL_YY'
*     ID 'WLYY' FIELD s_spras
*   ID 'ACTVT'  FIELD '02'.
*  IF sy-subrc <> 0.
*    MESSAGE s001(00) WITH '您没有权限更改此数据' DISPLAY LIKE 'I'.
*    STOP.
*  ENDIF.
*
*ENDFORM.
FORM get_data.

  CLEAR:it_display.
  REFRESH:it_display.


  SELECT matnr
      INTO CORRESPONDING FIELDS OF TABLE it_display FROM makt
  WHERE matnr IN s_matnr.

  LOOP AT it_display.
    SELECT SINGLE maktx INTO it_display-maktx FROM makt
    WHERE matnr = it_display-matnr AND spras = '1'.                  "中文

    SELECT SINGLE maktx INTO it_display-maktx_en FROM makt
    WHERE matnr = it_display-matnr AND spras = 'E'.                  "英文

    SELECT SINGLE maktx INTO it_display-maktx_vi FROM makt
    WHERE matnr = it_display-matnr AND spras = '쁩'.                  "越文

    MODIFY it_display.
  ENDLOOP.
  DELETE ADJACENT DUPLICATES FROM it_display COMPARING ALL FIELDS.

ENDFORM.


* 字段列举 AVL输出字段默认位置和宽度,以及可能属于哪个表中某个字段


FORM build_fieldcatalog CHANGING  it_fieldcat TYPE lvc_t_fcat.

  PERFORM  frm_fieldcat_init TABLES it_fieldcat
                              USING  'FLAG' '选择' '2' '8' '' '' 'X' 'X' 'X' '' '' '' .
  PERFORM  frm_fieldcat_init TABLES it_fieldcat
                              USING  'MATNR' '物料号' '3' '16' '' '' '' '' '' '' '' '' .
*  PERFORM  frm_fieldcat_init TABLES it_fieldcat
*                              USING  'spras' '语言' '3' '16' '' '' '' '' '' '' '' '' .

*********权限控制**************

  AUTHORITY-CHECK OBJECT 'ZWLYY'     " 英文               
  ID 'E' FIELD 'E'.
  IF sy-subrc = 0.
    PERFORM  frm_fieldcat_init TABLES it_fieldcat
    USING  'MAKTX_EN' '英文物料描述' '5' '50' '' '' '' 'X' 'X' '' '' '' .
  ELSE.
    PERFORM  frm_fieldcat_init TABLES it_fieldcat
    USING  'MAKTX_EN' '英文物料描述' '5' '50' '' '' '' '' 'X' '' '' '' .
  ENDIF.

  AUTHORITY-CHECK OBJECT 'ZWLYY'   " 越文
  ID 'V' FIELD '쁩'.
  IF sy-subrc = 0.
    PERFORM  frm_fieldcat_init TABLES it_fieldcat
    USING  'MAKTX_VI' '越文物料描述' '6' '100' '' '' '' 'X' 'X' '' '' '' .
    ELSE.
    PERFORM  frm_fieldcat_init TABLES it_fieldcat
    USING  'MAKTX_VI' '越文物料描述' '6' '100' '' '' '' '' 'X' '' '' '' .
  ENDIF.

  AUTHORITY-CHECK OBJECT 'ZWLYY'      " 中文
  ID 'Z' FIELD '1'.
  IF sy-subrc = 0.
    PERFORM  frm_fieldcat_init TABLES it_fieldcat
    USING  'MAKTX' '中文物料描述' '4' '50' '' '' '' 'X' 'X' '' '' '' .
  ELSE.
    PERFORM  frm_fieldcat_init TABLES it_fieldcat
    USING  'MAKTX' '中文物料描述' '4' '50' '' '' '' '' 'X' '' '' '' .
  ENDIF.

ENDFORM.                    "build_fieldcatalog

* 字段属性设置

FORM frm_fieldcat_init TABLES t_fieldcat TYPE lvc_t_fcat
USING fieldname TYPE c           " 字段名
      scrtext_m TYPE c           " 标题
      col_pos   TYPE i           " 所在列
      outputlen TYPE i           " 输出长度
      emphasize TYPE c            " 高亮显示
      key       TYPE c            " 主键
      checkbox  TYPE c
      edit      TYPE c
      fix_column TYPE c           " 固定列
      ref_tabname TYPE c         "参考表
      ref_fieldname TYPE c       "参考字段
      do_sum   TYPE c
      .
  DATA: ls_fieldcat TYPE lvc_s_fcat.
  ls_fieldcat-fieldname   = fieldname.
  ls_fieldcat-scrtext_m   = scrtext_m.
  ls_fieldcat-col_pos     = col_pos.
  ls_fieldcat-outputlen   = outputlen.
  ls_fieldcat-emphasize   = emphasize."如注释此行序列号行项目绿色背景消失-------------zyg
  ls_fieldcat-key         = key.       "表示是带主键操作
  ls_fieldcat-checkbox    = checkbox.  "如果注释此行,查询、编辑时候则FLAG标识行项目不显示为方框-------------zyg
  ls_fieldcat-edit        = edit.      "如果注释次行项目单据在查询状态就不能编辑--------------zyg
  ls_fieldcat-fix_column  = fix_column.
  IF ref_tabname NE ''."意思是如果参考表<>空------------zyg
    ls_fieldcat-ref_table    = ref_tabname.
    ls_fieldcat-ref_field        = ref_fieldname.

  ENDIF.
  ls_fieldcat-do_sum     =  do_sum.
  APPEND ls_fieldcat TO t_fieldcat.
  CLEAR  ls_fieldcat.
ENDFORM.                    "frm_fieldcat_init


* ALV输出


FORM frm_alv_output_data.

  gt_event-name = 'CALLER_EXIT'.   "slis_ev_caller_exit_at_start事件
  gt_event-form = 'FM_BUTTON'.
  APPEND gt_event .

  gd_layout-zebra      = 'X'.
  gd_layout-cwidth_opt = 'X'.  "optimize columwidth 适应窗

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
*"------------------ 不注释的显示自定义工具状态
      i_callback_pf_status_set = 'FRM_SET_STATUS'        "自定义工具状态
      i_callback_user_command  = 'USER_COMMAND'           "触发相应事件
      it_fieldcat_lvc          = it_fieldcat[]
      is_layout_lvc            = gd_layout
      i_save                   = 'A'
*     i_structure_name         = 'ZTB_CAIGOU'   " 如果允许,则列举超过字段
    TABLES
      t_outtab                 = it_display "相应enter。
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.


ENDFORM.                    "frm_alv_output_data


*激活自定义状态栏
FORM frm_set_status USING rt_extab TYPE slis_t_extab.
  CLEAR: rt_extab.
  REFRESH: rt_extab.
  SET PF-STATUS 'ZMM050' EXCLUDING rt_extab.

ENDFORM.                    "FRM_SET_STATUS



FORM user_command USING ucomm    TYPE sy-ucomm
      selfield TYPE slis_selfield.


*  DATA: lr_grid type ref to cl_gui_alv_grid.
  DATA:lt_index_rows TYPE lvc_t_row.
  DATA: flag_save  TYPE i.
  DATA: lv_index TYPE i.

*  取得当前alv的grid
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_guid.
* 更新数据到内表 (checkbox打钩)
  CALL METHOD lo_guid->check_changed_data.
*5此处代码阻止'REUSE_ALV_GRID_DISPLAY创建新的屏幕,造成屏幕多层
  selfield-refresh = 'X'.
*"//-------------------------------退出
  CASE ucomm.
    WHEN 'F03' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.

**//--------------------------------------------全选
    WHEN  'YES_ALL'." 全选

      PERFORM yes_select CHANGING it_display[].

**//-------------------------------------非全选
    WHEN  'NO_ALL'." 全选
      PERFORM not_select CHANGING it_display[].
    WHEN 'SAVE'. "保存数据
      CLEAR:g_makt.
      LOOP AT it_display WHERE flag = 'X'.

        MOVE-CORRESPONDING it_display TO g_makt.
        APPEND g_makt.

*        it_display-save_flag = 'X'.
        MODIFY it_display.
      ENDLOOP.


      LOOP AT  g_makt.               

        "head
        g_bapimathead-material  = g_makt-matnr.    " 物料号
        g_bapimathead-basic_view = 'X'.            " 基础视图
        APPEND g_bapimathead.
        "desc.
          g_bapi_makt-matl_desc = g_makt-maktx.    " 物料描述
          g_bapi_makt-langu_iso = 'ZH'."EN,VI        '语言
        APPEND g_bapi_makt.

          g_bapi_makt-matl_desc = g_makt-maktx_en.    " 物料描述
          g_bapi_makt-langu_iso = 'EN'."EN,VI        '语言
        APPEND g_bapi_makt.

          g_bapi_makt-matl_desc = g_makt-maktx_vi.    " 物料描述
          g_bapi_makt-langu_iso = 'VI'."EN,VI        '语言
        APPEND g_bapi_makt.


        CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'      " 批量创建/修改物料函数     
          EXPORTING
            headdata            = g_bapimathead
          TABLES
            materialdescription = g_bapi_makt.
        CLEAR:g_bapimathead,g_bapi_makt.              " 清表头
        REFRESH:g_bapimathead,g_bapi_makt.            " 清内存

      ENDLOOP.

  ENDCASE.

ENDFORM.


FORM not_select CHANGING pt_outtab TYPE STANDARD TABLE.
  DATA: ls_outtab TYPE t_display.
  DATA: l_valid  TYPE c,
        l_locked TYPE c.


*§A4ad. Before you (a)set, reset or (d)evaluate checkboxes,
*       you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.

  CALL METHOD lo_guid->check_changed_data
    IMPORTING
      e_valid = l_valid.

  IF l_valid EQ 'X'.

    LOOP AT pt_outtab INTO ls_outtab.
      PERFORM check_lock USING    ls_outtab
      CHANGING l_locked.
      IF l_locked IS INITIAL
      AND NOT ls_outtab-flag EQ '-'.
        ls_outtab-flag = ' '.
      ENDIF.

      MODIFY pt_outtab FROM ls_outtab.
    ENDLOOP.

    CALL METHOD lo_guid->refresh_table_display.

  ENDIF.



ENDFORM.                    "deselect_all_entries


FORM yes_select CHANGING pt_outtab TYPE STANDARD TABLE.
  DATA: ls_outtab TYPE t_display.
  DATA: l_valid  TYPE c,
        l_locked TYPE c.


*§A4ad. Before you (a)set, reset or (d)evaluate checkboxes,
*       you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.

  CALL METHOD lo_guid->check_changed_data
    IMPORTING
      e_valid = l_valid.

  IF l_valid EQ 'X'.

    LOOP AT pt_outtab INTO ls_outtab.
      PERFORM check_lock USING    ls_outtab
      CHANGING l_locked.
      IF l_locked IS INITIAL
      AND NOT ls_outtab-flag EQ '-'.
        ls_outtab-flag = 'X'.
      ENDIF.

      MODIFY pt_outtab FROM ls_outtab.
    ENDLOOP.

    CALL METHOD lo_guid->refresh_table_display.

  ENDIF.



ENDFORM.                    "deselect_all_entries


FORM check_lock USING    ps_outtab TYPE t_display
CHANGING p_locked.
  DATA ls_celltab TYPE lvc_s_styl.

  LOOP AT ps_outtab-celltab INTO ls_celltab.
    IF ls_celltab-fieldname = 'FLAG'.
      IF ls_celltab-style EQ cl_gui_alv_grid=>mc_style_disabled.
        p_locked = 'X'.
      ELSE.
        p_locked = space.
      ENDIF.
    ENDIF.
  ENDLOOP.

ENDFORM.

0 人点赞