Daniel Berlin on Security IT Audit, SAP security, developmentā€¦ and all the rest

12May 12

Source – ZS_JOBS_FIND_STEPUSER

*&---------------------------------------------------------------------*
*& Report ZS_JOBS_FIND_STEPUSER
*&---------------------------------------------------------------------*
*& Purpose: Find step users in jobs
*& Author : Daniel Berlin
*& Version: 1.0.0
*& License: CC BY 3.0 (http://creativecommons.org/licenses/by/3.0/)
*&---------------------------------------------------------------------*
 
REPORT zs_jobs_find_stepuser.
 
INCLUDE: lbtchdef.
 
TABLES:     tbtco, tbtcp, usr02.
TYPE-POOLS: slis.
 
DATA: BEGIN OF gt_joblist OCCURS 0,
        jobname   LIKE tbtco-jobname,   " Job name
        jobcount  LIKE tbtco-jobcount,  " Job ID
        status    TYPE string,          " Job status
        sdluname  LIKE tbtco-sdluname,  " Scheduler
        stepcount LIKE tbtcp-stepcount, " Step ID
        progname  LIKE tbtcp-progname,  " Report name
        authcknam LIKE tbtcp-authcknam, " Step user
        alvbox    TYPE c,               " ALV checkbox status
        cellcolor TYPE lvc_t_scol,      " ALV cell color
      END OF gt_joblist,
      gs_cellcolor TYPE lvc_s_scol,
      BEGIN OF r_status OCCURS 0,
         sign(1)   TYPE c,
         option(2) TYPE c,
         low       LIKE tbtco-status,
         high      LIKE tbtco-status,
      END OF r_status.
 
SELECTION-SCREEN: BEGIN OF BLOCK a WITH FRAME TITLE n_job.
SELECT-OPTIONS: r_jobnam FOR tbtco-jobname  MATCHCODE OBJECT sh_tbtcp,
                r_sdlusr FOR tbtco-sdluname MATCHCODE OBJECT user_addr.
SELECTION-SCREEN: COMMENT /1(10) n_stat,
                  BEGIN OF LINE,
                  COMMENT 5(10) n_stat_p.
PARAMETERS: p_stat_p AS CHECKBOX DEFAULT 'X'. " scheduled = geplant
SELECTION-SCREEN: END OF LINE,
                  BEGIN OF LINE,
                  COMMENT 5(10) n_stat_s.
PARAMETERS: p_stat_s AS CHECKBOX DEFAULT 'X'. " released  = freigegeben
SELECTION-SCREEN: END OF LINE,
                  BEGIN OF LINE,
                  COMMENT 5(10) n_stat_y.
PARAMETERS: p_stat_y AS CHECKBOX.             " ready     = bereit
SELECTION-SCREEN: END OF LINE,
                  BEGIN OF LINE,
                  COMMENT 5(10) n_stat_r.
PARAMETERS: p_stat_r AS CHECKBOX.             " active    = aktiv
SELECTION-SCREEN: END OF LINE,
                  BEGIN OF LINE,
                  COMMENT 5(10) n_stat_f.
PARAMETERS: p_stat_f AS CHECKBOX.             " finished  = fertig
SELECTION-SCREEN: END OF LINE,
                  BEGIN OF LINE,
                  COMMENT 5(10) n_stat_a.
PARAMETERS: p_stat_a AS CHECKBOX.             " canceled  = abgebrochen
SELECTION-SCREEN: END OF LINE,
                  END OF BLOCK a.
 
SELECTION-SCREEN: BEGIN OF BLOCK b WITH FRAME TITLE n_step.
SELECT-OPTIONS: r_usrnam FOR usr02-bname MATCHCODE OBJECT user_addr DEFAULT sy-uname.
SELECTION-SCREEN: END OF BLOCK b.
 
INITIALIZATION.
 
  AUTHORITY-CHECK OBJECT 'S_BTCH_ADM'
                  ID 'BTCADMIN' FIELD 'Y'.
 
  IF sy-subrc <> 0.
    MESSAGE 'Missing authorisation.' TYPE 'E'.              "#EC NOTEXT
    LEAVE PROGRAM.
  ENDIF.
 
  " --- Titles & texts
  n_job    = 'Job'.
  n_stat   = 'Job status'.                                  "#EC NOTEXT
  n_stat_p = 'scheduled'.
  n_stat_s = 'released'.
  n_stat_y = 'ready'.
  n_stat_r = 'active'.
  n_stat_f = 'finished'.
  n_stat_a = 'canceled'.
  n_step   = 'Step'.                                        "#EC NOTEXT
 
START-OF-SELECTION.
 
  PERFORM jobstatus_range_prepare.
 
  " --- Cell color defaults
  gs_cellcolor-fname     = 'AUTHCKNAM'.
  gs_cellcolor-color-int = 0.  " 1 = intensified
  gs_cellcolor-color-inv = 0.  " 0 = background, 1 = text
 
  " --- Find all relevant jobs
  SELECT *
    FROM tbtco
   WHERE jobname  IN r_jobnam
     AND sdluname IN r_sdlusr
     AND status   IN r_status.
 
    " --- Find all associated job steps
    SELECT *
      FROM tbtcp
     WHERE jobname   =  tbtco-jobname
       AND jobcount  =  tbtco-jobcount
       AND authcknam IN r_usrnam.
 
      gt_joblist-jobname  = tbtco-jobname.
      gt_joblist-jobcount = tbtco-jobcount.
      CASE tbtco-status.
        WHEN btc_scheduled. gt_joblist-status = n_stat_p.
        WHEN btc_released.  gt_joblist-status = n_stat_s.
        WHEN btc_ready.     gt_joblist-status = n_stat_y.
        WHEN btc_running.   gt_joblist-status = n_stat_r.
        WHEN btc_finished.  gt_joblist-status = n_stat_f.
        WHEN btc_aborted.   gt_joblist-status = n_stat_a.
      ENDCASE.
      gt_joblist-sdluname  = tbtco-sdluname.
      gt_joblist-stepcount = tbtcp-stepcount.
      gt_joblist-progname  = tbtcp-progname.
      gt_joblist-authcknam = tbtcp-authcknam.
 
      " --- Set cell color depending on step user existence
      SELECT SINGLE COUNT(*) FROM usr02 WHERE bname = gt_joblist-authcknam.
      IF sy-dbcnt = 1.
        gs_cellcolor-color-col = 5.  " green
      ELSE.
        gs_cellcolor-color-col = 6.  " red
      ENDIF.
      APPEND gs_cellcolor TO gt_joblist-cellcolor.
 
      APPEND gt_joblist.
      CLEAR  gt_joblist.
 
    ENDSELECT.
 
  ENDSELECT.
 
  PERFORM alv_result_show.
 
*&---------------------------------------------------------------------*
*&      Form  jobstatus_range_prepare
*&---------------------------------------------------------------------*
FORM jobstatus_range_prepare.
 
  r_status-sign   = 'I'.
  r_status-option = 'EQ'.
 
  IF p_stat_p = 'X'. r_status-low = btc_scheduled. APPEND r_status. ENDIF.
  IF p_stat_s = 'X'. r_status-low = btc_released.  APPEND r_status. ENDIF.
  IF p_stat_y = 'X'. r_status-low = btc_ready.     APPEND r_status. ENDIF.
  IF p_stat_r = 'X'. r_status-low = btc_running.   APPEND r_status. ENDIF.
  IF p_stat_f = 'X'. r_status-low = btc_finished.  APPEND r_status. ENDIF.
  IF p_stat_a = 'X'. r_status-low = btc_aborted.   APPEND r_status. ENDIF.
 
  CLEAR r_status.
 
ENDFORM.                    "jobstatus_range_prepare
 
*&---------------------------------------------------------------------*
*&      Form  alv_result_show
*&---------------------------------------------------------------------*
FORM alv_result_show.
 
  DATA: lt_fieldcat TYPE         slis_t_fieldcat_alv WITH HEADER LINE,
        ls_layout   TYPE         slis_layout_alv,
        lt_sort     TYPE         slis_t_sortinfo_alv,
        ls_sort     LIKE LINE OF lt_sort.
 
  " --- Field catalog
  lt_fieldcat-fieldname   = 'JOBNAME'.
  lt_fieldcat-ref_tabname = 'TBTCO'.
  lt_fieldcat-key         = 'X'.
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  lt_fieldcat-fieldname   = 'JOBCOUNT'.
  lt_fieldcat-ref_tabname = 'TBTCO'.
  lt_fieldcat-key         = 'X'.
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  lt_fieldcat-fieldname   = 'SDLUNAME'.
  lt_fieldcat-ref_tabname = 'TBTCO'.
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  lt_fieldcat-fieldname   = 'STATUS'.
  lt_fieldcat-seltext_m   = 'Job status'.                   "#EC NOTEXT
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  lt_fieldcat-fieldname   = 'STEPCOUNT'.
  lt_fieldcat-ref_tabname = 'TBTCP'.
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  lt_fieldcat-fieldname   = 'PROGNAME'.
  lt_fieldcat-ref_tabname = 'TBTCP'.
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  lt_fieldcat-fieldname   = 'AUTHCKNAM'.
  lt_fieldcat-ref_tabname = 'TBTCP'.
  APPEND lt_fieldcat. CLEAR lt_fieldcat.
 
  " --- Layout
  ls_layout-box_fieldname     = 'ALVBOX'.
  ls_layout-coltab_fieldname  = 'CELLCOLOR'.
  ls_layout-colwidth_optimize = 'X'.
  ls_layout-no_input          = 'X'.
* ls_layout-zebra             = 'X'.
 
  " --- Sort field
  ls_sort-fieldname = 'JOBNAME'.  APPEND ls_sort TO lt_sort.
  ls_sort-fieldname = 'JOBCOUNT'. APPEND ls_sort TO lt_sort.
 
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-repid
      i_callback_user_command = 'ALV_CALLBACK_USER_COMMAND'
      is_layout               = ls_layout
      it_fieldcat             = lt_fieldcat[]
      it_sort                 = lt_sort
      i_save                  = 'X'
    TABLES
      t_outtab                = gt_joblist
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.
 
ENDFORM.                    "alv_result_show
 
*&---------------------------------------------------------------------*
*&      Form  alv_callback_user_command
*&---------------------------------------------------------------------*
FORM alv_callback_user_command                              "#EC CALLED
                         USING pv_ucomm LIKE sy-ucomm
                               ps_field TYPE slis_selfield.
 
  IF pv_ucomm <> '&IC1'.                   " Double click
    EXIT.
  ENDIF.
 
  IF ps_field-fieldname = 'JOBNAME' OR ps_field-fieldname = 'JOBCOUNT'.
    DATA: ls_jobselect LIKE btcselect,
          lt_joblist   LIKE tbtcjob OCCURS 0 WITH HEADER LINE.
 
    READ TABLE gt_joblist INDEX ps_field-tabindex.
 
    ls_jobselect-jobname = gt_joblist-jobname.
    CASE ps_field-fieldname.
      WHEN 'JOBNAME'.
        ls_jobselect-prelim   = p_stat_p.  " scheduled
        ls_jobselect-schedul  = p_stat_s.  " released
        ls_jobselect-ready    = p_stat_y.  " ready
        ls_jobselect-running  = p_stat_r.  " active
        ls_jobselect-finished = p_stat_f.  " finished
        ls_jobselect-aborted  = p_stat_a.  " canceled
      WHEN 'JOBCOUNT'.
        ls_jobselect-jobcount = gt_joblist-jobcount.
        ls_jobselect-username = gt_joblist-sdluname.
    ENDCASE.
 
    " --- Select job
    CALL FUNCTION 'BP_JOB_SELECT'
      EXPORTING
        jobselect_dialog  = btc_no
        jobsel_param_in   = ls_jobselect
      TABLES
        jobselect_joblist = lt_joblist
      EXCEPTIONS
        OTHERS            = 1.
 
    " --- Show SM37 job list
    CALL FUNCTION 'BP_JOBLIST_PROCESSOR_SM37B'
      EXPORTING
        joblist_opcode = btc_joblist_edit
      TABLES
        joblist        = lt_joblist
      EXCEPTIONS
        OTHERS         = 1.
 
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
 
  ELSEIF ps_field-fieldname = 'SDLUNAME' OR ps_field-fieldname = 'AUTHCKNAM'.
    DATA: lv_user TYPE xubname.
 
    lv_user = ps_field-value.
    TRANSLATE lv_user TO UPPER CASE.                      "#EC SYNTCHAR
 
    CALL FUNCTION 'SUSR_USER_MAINT_WITH_DIALOG'
      EXPORTING
        user_to_display = lv_user
      EXCEPTIONS
        OTHERS          = 1.
 
  ELSEIF ps_field-fieldname = 'STEPCOUNT'.
    DATA: lt_steplist LIKE tbtcstep OCCURS 0.
 
    READ TABLE gt_joblist INDEX ps_field-tabindex.
 
    " --- Read job data
    CALL FUNCTION 'BP_JOB_READ'
      EXPORTING
        job_read_jobname  = gt_joblist-jobname
        job_read_jobcount = gt_joblist-jobcount
*       job_step_number   = gt_joblist-stepcount
        job_read_opcode   = btc_read_all_jobdata
      TABLES
        job_read_steplist = lt_steplist
      EXCEPTIONS
        OTHERS            = 1.
 
    IF sy-subrc <> 0.
      MESSAGE e658(bt).  " Job does not exist
    ENDIF.
 
    " --- Show step list (display only)
    CALL FUNCTION 'BP_STEPLIST_EDITOR'
      EXPORTING
        steplist_dialog = btc_yes
        steplist_opcode = btc_show_steplist
      TABLES
        steplist        = lt_steplist
      EXCEPTIONS
        OTHERS          = 1.
 
  ELSEIF ps_field-fieldname = 'PROGNAME'.
    SET PARAMETER ID 'RID' FIELD ps_field-value.
 
    CALL FUNCTION 'ABAP4_CALL_TRANSACTION'
      EXPORTING
        tcode  = 'SE38'
      EXCEPTIONS
        OTHERS = 1.
 
  ENDIF.
 
ENDFORM.                    "alv_callback_user_command
Comments (0) Trackbacks (0)

No comments yet... you could be the first!




Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.


No trackbacks yet.