Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Many a times in the SAP environment we face issues if some object is locked by another user. Moreover sometimes we require to stop users from logging on to SAP system until some particular job(say Payroll Processing) is finshed.

I recently faced the same issue and my requirement was to Lock all the user IDs based on the selection criteria and kick them out of the system if they are active and then finally unlock them once my job is done. 

I decided to write down a code for the same so that we can handle such situations with ease in future.

Note:-  Only Authorized users should be given access to this program. 

Below is the Code, hope it can be of some help to you people -

*&---------------------------------------------------------------------*
*& Report  ZAMIT_LOCK                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZAMIT_LOCK  message-id rp                                     .

*eject
*&---------------------------------------------------------------------
*& Type-Pool *
*& *
*&---------------------------------------------------------------------
TYPE-POOLS: SSCR.

*eject
*&---------------------------------------------------------------------
*& Tables and Infotypes *
*& *
*&---------------------------------------------------------------------
tables: usr02.

*eject
*&---------------------------------------------------------------------
*& Selection-Screen *
*& *
*&---------------------------------------------------------------------
SELECTION-SCREEN BEGIN OF BLOCK FRM1 WITH FRAME TITLE TEXT-001.
select-options: s_usrgp for usr02-CLASS no intervals modif Id XYZ,
                      s_bname for usr02-BNAME no intervals modif Id XYZ.
SELECTION-SCREEN END OF BLOCK FRM1.

SELECTION-SCREEN BEGIN OF BLOCK FRM2 WITH FRAME TITLE TEXT-002.
Parameters: p_lock   radiobutton group ABC USER-COMMAND CH,
                  p_unlock radiobutton group ABC                .
SELECTION-SCREEN END OF BLOCK FRM2.

*eject
*&---------------------------------------------------------------------
*& Variables *
*& *
*&---------------------------------------------------------------------
* To hide select-option tabs
DATA:   w_restrict TYPE sscr_restrict,
        w_opt_list TYPE sscr_opt_list,
        w_ass      TYPE sscr_ass.

DATA: w_tabix like sy-tabix value '0',
      indxkey like indx-SRTFD        ,
      indxkey1 like indx-SRTFD       ,
      w_mandt   like sy-mandt        ,
      w_flag(1) type c               ,
      w_lines   type i               ,
      w_count   type i               ,
      w_active  like SM04DIC-COUNTER .

*eject
*&---------------------------------------------------------------------
*& Internal tables *
*& *
*&---------------------------------------------------------------------
DATA: return like BAPIRET2 occurs 0 with  header line.

DATA: begin of t_usr occurs 0,
       bname like usr02-bname,
       uflag like usr02-uflag,
      end of t_usr.

DATA: begin of t_memory occurs 0,
       bname like usr02-bname,
      end of t_memory.

DATA: begin of t_error occurs 0,
       bname       like usr02-bname,
       message(40) type c,
      end of t_error.

*eject
*&---------------------------------------------------------------------
*& Constants *
*& *
*&---------------------------------------------------------------------
CONSTANTS:  c_X(1)      TYPE c              VALUE 'X'              ,
            c_1         TYPE i              VALUE '1'              ,
            c_20        TYPE i              VALUE '20'             ,
            c_0        TYPE i              VALUE '0'              ,
            c_eq        LIKE w_ass-op_main  VALUE 'EQ'             ,
            c_s_bname   LIKE w_ass-name     VALUE 'S_BNAME'        ,
            c_star      LIKE w_ass-sg_main  VALUE '*'              ,
            c_space     LIKE w_ass-sg_addy  VALUE ''               ,
            c_s         LIKE w_ass-kind     VALUE 'S'              ,
            c_xyz(3)    TYPE C              VALUE 'XYZ'            ,
            c_under(1)  TYPE C              VALUE '_'              ,
            c_flag(8)   TYPE C              VALUE 'FLAG_RUN'       ,
            c_lock(4)   TYPE C              VALUE 'LOCK'           ,
            c_A(1)      TYPE c              VALUE 'A'              .

*eject
*&---------------------------------------------------------------------
*& Initialization *
*& *
*&---------------------------------------------------------------------
Initialization.
* Hide the ranges
  PERFORM f0001_hide_select_option.

*eject
*&---------------------------------------------------------------------
*& AT Selection-screen *
*& *
*&---------------------------------------------------------------------
at selection-screen output.

  if p_unlock eq C_X.
    loop at screen.
      if screen-group1 = c_XYZ.
        SCREEN-INPUT = c_0.
        MODIFY SCREEN.
      endif.
    endloop.
  endif.

*eject
*&---------------------------------------------------------------------
*& Start-of Selection *
*& *
*&---------------------------------------------------------------------
start-of-selection.
*
  w_mandt = sy-mandt.

  Concatenate  sy-uname
               c_FLAG
       into    indxkey1
  separated by c_under   .

  IMPORT w_flag from DATABASE INDX(ST)
         client w_mandt
         ID     indxkey1.

  if  w_flag eq c_x
  and p_lock eq c_x.
    message e016 with 'Unlock the IDs from previous run'(003).
  endif.

  Concatenate  sy-uname
               c_lock
       into    indxkey
  separated by c_under   .

  if p_lock eq C_X.

    select bname uflag into table t_usr
           from  usr02
           where ustyp eq c_A
           and   bname in s_bname
           and   class in s_usrgp.

    if sy-subrc ne c_0.
      stop.
    endif.

    describe table t_usr lines w_lines.

    if w_lines eq c_1.
      clear t_usr.
      read table t_usr index c_1.
      if t_usr-bname eq sy-uname.
        stop.
      endif.
      clear t_usr.
    endif.

    move c_x to w_flag.
    EXPORT w_flag to DATABASE INDX(ST)
           client w_mandt
           ID     indxkey1.

    sort t_usr by bname.

    delete adjacent duplicates from t_usr comparing bname.

    clear w_count.
    loop at t_usr.
      check t_usr-bname ne sy-uname and
            t_usr-uflag eq c_0.
      clear w_active.
      CALL FUNCTION 'BAPI_USER_LOCK'
        EXPORTING
          USERNAME = t_usr-bname
        TABLES
          RETURN   = return.
*
      if sy-subrc eq c_0.
        CALL FUNCTION 'TH_USER_INFO'
          EXPORTING
            CLIENT       = sy-mandt
            USER         = t_usr-bname
          IMPORTING
            ACT_SESSIONS = w_active.

        if not w_active is initial.

          CALL FUNCTION 'TH_DELETE_USER'
            EXPORTING
              USER            = t_usr-bname
              CLIENT          = sy-mandt
            EXCEPTIONS
              AUTHORITY_ERROR = 1
              OTHERS          = 2.
          if sy-subrc ne c_0.
            clear t_error.
            t_error-bname   = t_usr-bname.
            t_error-message = 'Unable to End Active Session for User'(004).
            append t_error.
          endif.
        endif.
        add c_1 to w_count.
        clear t_memory.
        t_memory-bname = t_usr-bname.
        append t_memory.
      else.
        clear t_error.
        t_error-bname   = t_usr-bname.
        t_error-message = 'User not locked'.
        append t_error.
      endif.
    endloop.
  elseif p_unlock eq C_X.
    clear: t_memory.
    refresh t_memory.
    IMPORT t_memory from DATABASE INDX(ST)
           client w_mandt
           ID     indxkey.
    if sy-subrc ne c_0.
      stop.
    else.
      move SPACE to w_flag.

      EXPORT w_flag to DATABASE INDX(ST)
             client w_mandt
             ID     indxkey1.
      delete from indx where SRTFD eq indxkey.
      clear w_count.
      clear: return,t_error.
      refresh t_error.

      loop at t_memory.

        CALL FUNCTION 'BAPI_USER_UNLOCK'
          EXPORTING
            USERNAME = t_memory-bname
          TABLES
            RETURN   = return.

        if sy-subrc eq c_0.
          add 1 to w_count.
        else.
          clear t_error.
          t_error-bname   = t_usr-bname.
          t_error-message = 'User not Unlocked'.
          append t_error.
        endif.
      endloop.
    endif.
  endif.

*eject
*&---------------------------------------------------------------------
*& End-of Selection *
*& *
*&---------------------------------------------------------------------
end-of-selection.

  if p_lock eq C_X.
    skip 1.
    write:'No of Users Locked:',Space,w_count.

    delete from indx where SRTFD eq indxkey.

    EXPORT t_memory to DATABASE INDX(ST)
           client w_mandt
           ID     indxkey.

*   Write erroneous users
    if not t_error is initial.
      skip 1.
      write:'Erroneous users:' color 6.
      skip 1.
      uline 1(57).

      perform f0002_output.

    endif.

  elseif p_unlock eq C_X.
    skip 1.
    write:'No of Users Unlocked:',Space,w_count.

*   Write erroneous users
    if not t_error is initial.
      skip 1.
      write:'Unable to Unlock following users:' color 6.
      skip 1.
      uline 1(57).

      perform f0002_output.

    endif.
  endif.
*eject
*&---------------------------------------------------------------------*
*&      Form  F0001_HIDE_SELECT_OPTION
*&---------------------------------------------------------------------*
FORM F0001_HIDE_SELECT_OPTION .

  MOVE c_eq TO w_opt_list-name       .
  MOVE c_X  TO w_opt_list-options-eq .

  APPEND w_opt_list TO w_restrict-opt_list_tab.

  MOVE: c_s        TO  w_ass-kind    ,
        c_s_bname  TO  w_ass-name    ,
        c_star     TO  w_ass-sg_main ,
        c_space    TO  w_ass-sg_addy ,
        c_eq       TO  w_ass-op_main .

  APPEND w_ass TO w_restrict-ass_tab.

* Function module to restrict the range
  CALL FUNCTION 'SELECT_OPTIONS_RESTRICT'
    EXPORTING
      restriction = w_restrict.

ENDFORM.                    " F0001_HIDE_SELECT_OPTION
*eject
*&---------------------------------------------------------------------*
*&      Form  F0002_OUTPUT
*&---------------------------------------------------------------------*
FORM F0002_OUTPUT .

  loop at t_error.
    if w_tabix eq c_20.
      clear w_tabix.
      NEW-PAGE.
    endif.

    w_tabix = w_tabix + c_1.
    write:   /1  sy-vline,
             15  sy-vline,
             57  sy-vline.
    write:  2    t_error-bname,
            17   t_error-message.

    uline /1(57).
  endloop.

ENDFORM.                    " F0001_OUTPUT

  • No labels