*&---------------------------------------------------------------------*
*& Program to kick out users out of the system immediately.            *
*&   Use it for example for maintenance tasks or in Go-Lives.          *
*&   To avoid that users might login again you should lock them        *
*&   before.                                                           *
*& In releases >= SAP_BASIS 740 SP8 there is an SAP program            *
*& RLFW_SD_LOGOFF_USERS available that also can be used.               *
*&---------------------------------------------------------------------*
REPORT %Sc4SAPProgramName%.   " <- will be replaced by Sc4SAP
* F4 for select-options client(s) and user(s)
data: f4Client like t000-mandt,
      f4User like usr02-bname.
* Client(s) where the users are to be kicked out.
selection-screen begin of line.
  selection-screen comment (25) txt_1 for field sClient.
  select-options: sClient for f4Client default sy-mandt.
selection-screen end of line.
* Users to be kicked out. Can be restricted if necessary. The user
*   that is running this task (SY-UNAME) will not be kicked out.
selection-screen begin of line.
  selection-screen comment (25) txt_2 for field sUname.
  select-options: sUname FOR f4User.
selection-screen end of line.
* Some data definition.
data: clients type standard table of mandt,
      servers type standard table of msxxlist,
      uInfo type standard table of uinfo,
      usrList type standard table of usrinfo.
initialization.
  " The authorization for locking users has to be given.
  authority-check object 'S_USER_GRP'
      ID 'ACTVT' field '05'
      ID 'CLASS' field 'DUMMY'.
  if sy-subrc <> 0.
    message e494(01) with '(any)'.
  endif.
  " Some texts for the selection screen
  txt_1 = 'Clients:'.
  txt_2 = 'Users to be kicked out:'.
start-of-selection.
  " 1st: get the targeted clients.
  select mandt from T000 into table clients
      where mandt in sClient.
  " 2nd: get all servers of the system.
  call function 'TH_SERVER_LIST'
    tables list = servers
    exceptions no_server_list = 1
               others = 2.
  if sy-subrc <> 0.
    write:/ 'Error determining servers of this system. SY-SUBRC:',
        sy-subrc.
    message E517(CNV).
  endif.
  " 3rd: get the users currently logged in.
  call function 'TH_USER_LIST'
    tables list = uInfo
           usrlist = usrList
    exceptions auth_misssing = 1
               others = 2.
  if sy-subrc <> 0.
    write:/ 'Error determining users currently logged in. SY-SUBRC:',
        sy-subrc.
    message E584(01).
  endif.
  " 4th: iterate the clients and servers and log off all users
  "        except the one used for this task in this client.
  loop at clients assigning field-symbol(<client>).
    write: / 'Now processing client:', <client>.
    loop at servers assigning field-symbol(<server>).
      write: /2 'Server:', <server>-name.
      loop at usrList assigning field-symbol(<user>)
          where ( bname in sUname and mandt = <client> ).
        " Don't kill the own session!
        if ( sy-mandt = <client> and <user>-bname = sy-uname ).
          continue.
        endif.
        call 'ThSndDelUser'
            id 'MANDT'  field <client>
            id 'BNAME'  field <user>-bname
            id 'SERVER' field <server>-name
            id 'TID'    field <user>-tid.
      endloop.
    endloop.
    write:/ 'Finished kicking users out of the system in client', <client>.
  endloop.