Daniel Berlin on Security Insight on SAP security, development stuff… and all the rest

1Oct 12

Source – ZS_EXPORT_FAVS_4_ROLE_MENU

*&---------------------------------------------------------------------*
*& Report ZS_EXPORT_FAVS_4_ROLE_MENU
*&---------------------------------------------------------------------*
*& Purpose: Export a user's favorites for import into a role menu
*&          as per SAP note 389675.
*& Author : Daniel Berlin
*& Version: 1.0.0
*& License: CC BY 3.0 (http://creativecommons.org/licenses/by/3.0/)
*&---------------------------------------------------------------------*
 
REPORT zs_export_favs_4_role_menu.
 
TABLES: smen_buffc.
 
TYPES: rec_type(20)   TYPE c,           " Name of record type
       version(20)    TYPE c,           " Version of file format
       object_id(5)   TYPE c,           " Node object ID (right-aligned)
       parent_id(5)   TYPE c,           " Parent node ID (right-aligned)
       sort_order(5)  TYPE c,           " Sort sequence  (right-aligned)
       node_type(20)  TYPE c,           " Node type – e.g. FOLDER, ...
       node_data(255) TYPE c,           " Node information
       text_lang(2)   TYPE c,           " Language for the node text
       node_text(80)  TYPE c.           " Node text
 
DATA: BEGIN OF s_format,                " Record type 'FORMAT'
        rec_type      TYPE rec_type,
        version       TYPE version,
      END   OF s_format,
      BEGIN OF t_nodes OCCURS 0,        " Record type 'NODE'
        rec_type      TYPE rec_type,
        object_id     TYPE object_id,
        parent_id     TYPE parent_id,
        sort_order    TYPE sort_order,
        node_type     TYPE node_type,
        node_data     TYPE node_data,
      END   OF t_nodes,
      BEGIN OF t_texts OCCURS 0,        " Record type 'TEXT'
        rec_type      TYPE rec_type,
        object_id     TYPE object_id,
        text_lang     TYPE text_lang,
        node_text     TYPE node_text,
      END   OF t_texts,
      BEGIN OF t_export OCCURS 0,       " Data to export
        line          TYPE string,
      END   OF t_export,
      v_user          TYPE symsgv,      " User name
      v_file          TYPE string,      " Local file name
      v_lang          LIKE t002-laiso.  " ISO language key
 
PARAMETERS: p_user LIKE usr02-bname MATCHCODE OBJECT user_addr
                                    DEFAULT          sy-uname
                                    OBLIGATORY,
            p_file TYPE rlgrap-filename.
 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
 
  " -- F4 help for local file name
  CALL FUNCTION 'NAVIGATION_FILENAME_HELP'
    EXPORTING
      default_path      = ''
      mode              = 'S'
    IMPORTING
      selected_filename = p_file.
 
START-OF-SELECTION.
 
  " -- Verify that favorites exist for the selected user
  SELECT COUNT(*) FROM smen_buffc WHERE uname = p_user.
 
  IF sy-dbcnt = 0.
    v_user = p_user.
 
    CALL FUNCTION 'POPUP_DISPLAY_MESSAGE'
      EXPORTING
        titel = 'Error reading favorites'                   "#EC NOTEXT
        msgty = 'W'
        msgid = '1X'
        msgno = 060
        msgv1 = v_user.
 
    EXIT.
  ENDIF.
 
  " -- Set FORMAT record
  s_format-rec_type = 'FORMAT'.
  s_format-version  = '1.2B'.
 
  " -- Read the ISO language key for the current login language
  SELECT SINGLE laiso INTO v_lang FROM t002 WHERE spras = sy-langu.
 
  " -- Read the user's favorites and transform them
  SELECT * FROM smen_buffc WHERE uname = p_user.
    t_nodes-rec_type   = 'NODE'.
    t_nodes-object_id  = smen_buffc-object_id.
    t_nodes-parent_id  = smen_buffc-parent_id.
    t_nodes-sort_order = smen_buffc-sort_order.
 
    CASE smen_buffc-reporttype.
      WHEN ''.                                  " Folder
        t_nodes-node_type = 'FOLDER'.
 
      WHEN 'TR'.                                " Transaction
        t_nodes-node_type = 'TRANSACTION'.
        t_nodes-node_data = smen_buffc-report.
 
      WHEN 'OT'.                                " ... others:
        CASE smen_buffc-report.
          WHEN 'KW'.                            " Knowledge Warehouse
            t_nodes-node_type = 'KW'.
            SELECT SINGLE url
                     INTO t_nodes-node_data
                     FROM smen_buffi
                    WHERE uname     = p_user
                      AND object_id = smen_buffc-object_id.
 
          WHEN 'URL'.                           " URL
            t_nodes-node_type = 'URL'.
            SELECT SINGLE url
                     INTO t_nodes-node_data
                     FROM smen_buffi
                    WHERE uname     = p_user
                      AND object_id = smen_buffc-object_id.
 
        ENDCASE.
    ENDCASE.
 
    " -- If we found a valid entry ...
    IF t_nodes-node_type IS NOT INITIAL.
      " -- Generate a TEXT record for the above NODE record
      t_texts-rec_type  = 'TEXT'.
      t_texts-object_id = smen_buffc-object_id.
      t_texts-text_lang = v_lang.
      t_texts-node_text = smen_buffc-text.
 
      APPEND t_nodes.
      APPEND t_texts.
    ENDIF.
 
    CLEAR t_nodes.
    CLEAR t_texts.
 
  ENDSELECT.
 
  " -- Add FORMAT record to export table
  t_export-line = s_format.
  APPEND t_export.
 
  SORT t_nodes BY object_id.
 
  LOOP AT t_nodes.
    " -- Add NODE record to export table
    t_export-line = t_nodes.
    APPEND t_export.
 
    " -- Add corresponding TEXT record to export table
    READ TABLE t_texts WITH KEY object_id = t_nodes-object_id.
 
    t_export-line = t_texts.
    APPEND t_export.
  ENDLOOP.
 
  " -- Download to local file
  IF p_file IS NOT INITIAL.
    v_file = p_file.
 
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filetype                  = 'ASC'
        filename                  = v_file
        trunc_trailing_blanks     = 'X'
        trunc_trailing_blanks_eol = 'X'
      TABLES
        data_tab                  = t_export
      EXCEPTIONS
        OTHERS                    = 1.
 
    IF sy-subrc <> 0.
      CALL FUNCTION 'POPUP_DISPLAY_MESSAGE'
        EXPORTING
          titel = 'Error writing file'                      "#EC NOTEXT
          msgty = sy-msgty
          msgid = sy-msgid
          msgno = sy-msgno
          msgv1 = sy-msgv1
          msgv2 = sy-msgv2
          msgv3 = sy-msgv3
          msgv4 = sy-msgv4.
 
      EXIT.
    ENDIF.
  ENDIF.
 
  " -- Print exported data to screen
  LOOP AT t_export.
    WRITE: / t_export-line.
  ENDLOOP.