Friday, February 1, 2008

Mass Locking/Unlocking Users in SAP

Create the following report and execute it with the a list of users you do not want locked in the sapmnt\IGNORE_USERS_.txt and it will generate a list of locked users in sapmnt\LOCKED_USERS_.txt:


REPORT ZLOCKUSR.

TABLESUSR02.

PARAMETERSFUNCT_(1TYPE C DEFAULT SPACE.

PARAMETERSIGNOR_(65TYPE C DEFAULT
'\\SAPSID\sapmnt\ignore_users_CLI.txt',
LOCKD_
(65TYPE C DEFAULT
'\\SAPSID\sapmnt\locked_users_CLI.txt'.

DATAYULOCK TYPE X VALUE '80'"Gesperrt durch Falschanmeld.
      YUSLOC 
TYPE X VALUE '40'"Gesperrt durch Administrator

DATAMESSAGE_TITLE(40),
      MESSAGE_TEXT
(60),
      NUM_LOCKED 
TYPE I.

DATAUSER LIKE USR02-BNAME.

DATABEGIN OF IGNORE_TAB OCCURS 0,
BNAME 
LIKE USR02-BNAME,
END OF IGNORE_TAB.

DATALOCKED LIKE USR02-BNAME,
UNLOCKED 
LIKE USR02-BNAME,
ANSWER 
TYPE C.

INITIALIZATION.
REPLACE 'SAPSID' WITH SY-HOST(5INTO IGNOR_.
REPLACE 'SAPSID' WITH SY-HOST(5INTO LOCKD_.
REPLACE 'CLI' WITH SY-MANDT INTO IGNOR_.
REPLACE 'CLI' WITH SY-MANDT INTO LOCKD_.

START
-OF-SELECTION.
PERFORM MAIN.


USER 
'ZUSER'.


*&---------------------------------------------------------------------*
*& Form MAIN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM MAIN.
PERFORM CHECK_FUNCTION.
* perform status_message.
ENDFORM" MAIN
*&---------------------------------------------------------------------*
*& Form CHECK_FUNCTION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CHECK_FUNCTION.
CASE FUNCT_.
WHEN 'L'.
PERFORM LOCK_USERS.
WHEN 'U'.
PERFORM UNLOCK_USERS.
WHEN 'l'.
PERFORM LOCK_USERS.
WHEN 'u'.
PERFORM UNLOCK_USERS.
WHEN OTHERS.
PERFORM STATUS_MESSAGE.
ENDCASE.
ENDFORM" CHECK_FUNCTION
*&---------------------------------------------------------------------*
*& Form LOCK_USERS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM LOCK_USERS.
MESSAGE_TITLE 
'Warning!'.
MESSAGE_TEXT 
'Users not in file will be locked.'.

CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
DEFAULTOPTION 
'Y'
TEXTLINE1 
MESSAGE_TEXT
TEXTLINE2 
'OK to proceed?'
TITEL 
MESSAGE_TITLE
IMPORTING
ANSWER 
ANSWER
EXCEPTIONS
OTHERS 1.
IF ANSWER 'J'.
NUM_LOCKED 
0.
CLEAR IGNORE_TAB.
REFRESH IGNORE_TAB.
OPEN DATASET LOCKD_ FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
OPEN DATASET IGNOR_ FOR INPUT IN TEXT MODE ENCODING DEFAULT.
READ DATASET IGNOR_ INTO IGNORE_TAB.
IF SY-SUBRC 0.
DO.
APPEND IGNORE_TAB.
READ DATASET IGNOR_ INTO IGNORE_TAB.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
ENDDO.
ENDIF.
SORT IGNORE_TAB.
SELECT FROM USR02 WHERE BNAME NE 'SAP*'
AND BNAME NE 'DDIC'
AND BNAME NE 'SAPCPIC'.
READ TABLE IGNORE_TAB WITH KEY USR02-BNAME BINARY SEARCH.
IF SY-SUBRC NE 0.
IF USR02-UFLAG YULOCK AND USR02-UFLAG YUSLOC.
PERFORM LOCK_USER(SAPMS01JUSING USR02-BNAME.
NUM_LOCKED 
NUM_LOCKED + 1.
LOCKED 
USR02-BNAME.
TRANSFER LOCKED TO LOCKD_.
ENDIF.
ENDIF.
ENDSELECT.
CLOSE DATASET LOCKD_.
MESSAGE_TITLE 
'Information'.
MESSAGE_TEXT 
' Users Locked'.
MOVE NUM_LOCKED TO MESSAGE_TEXT(5).
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL 
MESSAGE_TITLE
TXT1 
MESSAGE_TEXT
TXT2 
SPACE
EXCEPTIONS
OTHERS 1.
ELSE.
EXIT.
ENDIF.
ENDFORM" LOCK_USERS

*&---------------------------------------------------------------------*
*& Form UNLOCK_USERS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM UNLOCK_USERS.
REFRESH IGNORE_TAB.
CLEAR IGNORE_TAB.
MESSAGE_TITLE 
'Warning!'.
MESSAGE_TEXT 
'Users in file will be unlocked.'.

CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
DEFAULTOPTION 
'Y'
TEXTLINE1 
MESSAGE_TEXT
TEXTLINE2 
'OK to proceed?'
TITEL 
MESSAGE_TITLE
IMPORTING
ANSWER 
ANSWER
EXCEPTIONS
OTHERS 1.
IF ANSWER 'J'.
OPEN DATASET LOCKD_ FOR INPUT IN TEXT MODE ENCODING DEFAULT.
READ DATASET LOCKD_ INTO IGNORE_TAB.
IF SY-SUBRC 0.
DO.
APPEND IGNORE_TAB.
READ DATASET LOCKD_ INTO IGNORE_TAB.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
ENDDO.
ENDIF.
SORT IGNORE_TAB.
LOOP AT IGNORE_TAB.
SELECT SINGLE FROM USR02 WHERE BNAME IGNORE_TAB-BNAME.
IF SY-SUBRC EQ 0.
* if usr02-uflag o yusloc.
PERFORM UNLOCK_USER(SAPMS01JUSING USR02-BNAME.
NUM_LOCKED 
NUM_LOCKED + 1.
* endif.
ENDIF.
CLOSE DATASET LOCKD_.
ENDLOOP.
MESSAGE_TITLE 
'Information'.
MESSAGE_TEXT 
' Users UnLocked'.
MOVE NUM_LOCKED TO MESSAGE_TEXT(5).
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL 
MESSAGE_TITLE
TXT1 
MESSAGE_TEXT
TXT2 
SPACE
EXCEPTIONS
OTHERS 1.
ENDIF.
ENDFORM" UNLOCK_USERS

*&---------------------------------------------------------------------*
*& Form EXIT_MESSAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM EXIT_MESSAGE.

ENDFORM" EXIT_MESSAGE
*&---------------------------------------------------------------------*
*& Form STATUS_MESSAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM STATUS_MESSAGE.
CLEAR MESSAGE_TEXT.
MESSAGE_TITLE 
'Information'.
MESSAGE_TEXT 
'Please enter U (unlock) or L (lock)'.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL 
MESSAGE_TITLE
TXT1 
MESSAGE_TEXT
TXT2 
SPACE
EXCEPTIONS
OTHERS 1.

ENDFORM" STATUS_MESSAGE