Easy Mass Maintain of display, locking and deleting users

REPORT ZSU01 NO STANDARD PAGE HEADING.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (20) COMMENT0.
SELECTION-SCREEN POSITION 56.

PARAMETERS: DISP RADIOBUTTON GROUP R1.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (20) COMMENT1.
SELECTION-SCREEN POSITION 56.

PARAMETERS: LOCK RADIOBUTTON GROUP R1.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (20) COMMENT2.
SELECTION-SCREEN POSITION 56.

PARAMETERS: DELETE RADIOBUTTON GROUP R1.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B1.

* User type input
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (35) COMMENT3.
SELECTION-SCREEN POSITION 56.

PARAMETERS: INAC RADIOBUTTON GROUP R2.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (35) COMMENT4.
SELECTION-SCREEN POSITION 56.

PARAMETERS: NOLOG RADIOBUTTON GROUP R2.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B2.

* Choose SAP Version
SELECTION-SCREEN BEGIN OF BLOCK B2A WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (35) COMMENT7.
SELECTION-SCREEN POSITION 56.

PARAMETERS: IVER1 RADIOBUTTON GROUP R2A.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (35) COMMENT8.
SELECTION-SCREEN POSITION 56.

PARAMETERS: IVER2 RADIOBUTTON GROUP R2A.

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B2A.

*--- Period input
SELECTION-SCREEN BEGIN OF BLOCK B3 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (12) COMMENT5.
SELECTION-SCREEN POSITION 16.

PARAMETERS: DAYS(3) TYPE N OBLIGATORY DEFAULT '60'.

SELECTION-SCREEN COMMENT 21(20) COMMENT6.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B3.

*--- Initialize the selection screen
INITIALIZATION.
  COMMENT0 = 'DISPLAY USERS WHO'.
  COMMENT1 = 'LOCK USERS WHO'.
  COMMENT2 = 'DELETE USERS WHO'.
  COMMENT3 = 'LAST LOGGED IN'.
  COMMENT4 = 'NEVER LOGGED IN AND WERE CREATED'.
  COMMENT5 = 'AT LEAST'.
  COMMENT6 = 'DAYS AGO'.
  COMMENT7 = '4.6x (Tested)'.
  COMMENT8 = '3.x (not tested)'.

START-OF-SELECTION.
*--- Data declaration
  TABLES: USR02.

  DATA: LAST_DATE TYPE D.

  DATA: BEGIN OF USERS OCCURS 50,
          BNAME LIKE USR02-BNAME,
          TRDAT LIKE USR02-TRDAT,
          ERDAT LIKE USR02-ERDAT,
          UFLAG LIKE USR02-UFLAG,
  END OF USERS.

  data: begin of bdc_tab occurs 100.
          include structure bdcdata.
  data: end of bdc_tab.

*--- Add Selection Option for User Name!
SELECT-OPTIONS USERNAME FOR USR02-BNAME OBLIGATORY DEFAULT 'xxName?xx'.

*--- Calculate the date
  LAST_DATE = SY-DATUM.
  LAST_DATE = LAST_DATE - DAYS.

*--- Find the users that fulfill the criterias
  SELECT * FROM USR02 WHERE USTYP = 'A' AND BNAME IN USERNAME.

    IF USR02-TRDAT <= LAST_DATE.
      IF USR02-TRDAT = '00000000' AND NOLOG = 'X'.
        IF USR02-ERDAT <= LAST_DATE.
          MOVE-CORRESPONDING USR02 TO USERS.
          APPEND USERS.
        ENDIF.
      ELSEIF USR02-TRDAT <> '00000000' AND INAC = 'X'.
        MOVE-CORRESPONDING USR02 TO USERS.
        APPEND USERS.
      ENDIF.
    ENDIF.

  ENDSELECT.

*--- Depending on the action: display, lock or delete
  IF DISP = 'X'.
    WRITE: / '  USER       LAST LOGIN   CREATED   UFLAG (128=LOCKED)'.
    SKIP.
    LOOP AT USERS.
       WRITE: / USERS-BNAME, USERS-TRDAT, USERS-ERDAT, USERS-UFLAG.
    ENDLOOP.
  ELSEIF LOCK = 'X'.
    WRITE: / 'LOCKED:'.
    WRITE: / '  USER       LAST LOGIN   CREATED'.
    SKIP.
    LOOP AT USERS.
      WRITE: / USERS-BNAME, USERS-TRDAT, USERS-ERDAT.
      SELECT SINGLE * FROM USR02 WHERE BNAME = USERS-BNAME.
      USR02-UFLAG = '128'.
      MODIFY USR02.
    ENDLOOP.
  ELSE.
    WRITE: / 'DELETED:'.
    WRITE: / '  USER       LAST LOGIN   CREATED'.
    SKIP.
    LOOP AT USERS.
      WRITE: / USERS-BNAME, USERS-TRDAT, USERS-ERDAT.
      PERFORM USER_DELETE.
    ENDLOOP.
  ENDIF.

*---------------------------------------------------------------------*
*       FORM USER_DELETE                                              *
*---------------------------------------------------------------------*
FORM USER_DELETE.

PERFORM BDC_FILL USING 'X'
     'SAPMS01J'
     '0200'.

PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'DELU'.

PERFORM BDC_FILL USING ' '
     'BDC_CURSOR'
     'XU200-XUSER'.

PERFORM BDC_FILL USING ' '
     'XU200-XUSER'
     USERS-BNAME.

PERFORM BDC_FILL USING 'X'
     'SAPLSPO1'
     '0400'.

PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'YES'.

PERFORM BDC_FILL USING 'X'
     'SAPLSPO1'
     '0100'.

PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'YES'.

PERFORM BDC_FILL USING 'X'
     'SAPMS01J'
     '0200'.

PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'BACK'.

PERFORM BDC_FILL USING ' '
     'BDC_CURSOR'
     'XU200-XUSER'.

PERFORM BDC_FILL USING ' '
     'XU200-XUSER'
     USERS-BNAME.

IF IVER1 = 'X'.
   call transaction 'OPF0' using bdc_tab mode 'N'.   "4.6x
ELSE.
   call transaction 'SU01' using bdc_tab mode 'N'.   "3.x
ENDIF.

ENDFORM.

*---------------------------------------------------------------------*
*       FORM BDC_FILL                                                 *
*---------------------------------------------------------------------*
FORM BDC_FILL USING P1 P2 P3.

  clear bdc_tab.

  if p1 = 'X'.
    bdc_tab-dynbegin = p1.
    bdc_tab-program = p2.
    bdc_tab-dynpro =  p3.
  else.
    bdc_tab-dynbegin = p1.
    bdc_tab-fnam = p2.
    bdc_tab-fval = p3.
  endif.

  append bdc_tab.

ENDFORM.

*--- End of ABAP Program

More Function Module
Functions / SAP Script / ALV

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.