Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
3,691

Recently I had an issue with locking users. I have generated a report about users who have not been logged for a couple of months. This file contained only usernames, since that is all you need to know when it comes to locking someone. The expiration date in this task is the date of lock.

Since locking hundreds of users and setting their expiration date in a couple of systems is insanity, I had to find a different way to do so. Here is a program serving that task:

Mass user lock
REPORT  Z_MASS_USER_LOCK.

"variables for uploading and reading file with usernames
DATA: paa_file  TYPE SAPB-SAPPFAD,
       pafile    TYPE SAPB-SAPPFAD.
       "paa_file2 TYPE string.

"parameter for local file path
PARAMETERS: p_file(70) TYPE c.
"set filenames
pafile = p_file.
paa_file = 'Users_For_Lock.csv'.

"path to local file in wich users to be locked is stored
OPEN DATASET  paa_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
CLOSE DATASET paa_file.

CALL FUNCTION 'ARCHIVFILE_CLIENT_TO_SERVER'
   EXPORTING
     PATH             = pafile
     TARGETPATH       = paa_file
* EXCEPTIONS
*   ERROR_FILE       = 1
*   OTHERS           = 2
           .
IF SY-SUBRC <> 0.
* Implement suitable error handling here
   MESSAGE 'No such file' TYPE 'E'.
ENDIF.

DATA: text    TYPE TABLE OF string,
       csvstr  TYPE string,
       pa_file TYPE string,
       curpos  TYPE i,
       endpos  TYPE i.

pa_file = paa_file.

"data of the user we want to lock
TYPES: BEGIN OF user,
         uname  TYPE BAPIBNAME-BAPIBNAME, "username
         logond TYPE BAPILOGOND,         "logon date, here we set valid through date
         logonx TYPE BAPILOGONX,
        END   OF user.

DATA: wa_user type user.

"

TRY .
     OPEN DATASET pa_file FOR INPUt in TEXT MODE ENCODING UTF-8.
     SET DATASET  pa_file POSITION END OF FILE .
     GET DATASET  pa_file POSITION endpos.
     SET DATASET  pa_file POSITION 0.

     WHILE curpos <> endpos.

       READ DATASET pa_file INTO csvstr.
       APPEND csvstr TO text.
       GET DATASET pa_file POSITION curpos.

       wa_user-uname = csvstr.           "set uname of the user to be locked
       wa_user-logond-GLTGB = sy-datum"set current date as the validity expiration date
       wa_user-logonx-GLTGB = 'X'.       "mark that you want to change the validity expiration date

       DATA RETURN_DUMMY LIKE BAPIRET2 OCCURS 0.


       "set users expiration date
       CALL FUNCTION 'BAPI_USER_CHANGE'
         EXPORTING
           USERNAME          = wa_user-uname
           LOGONDATA         = wa_user-logond
           LOGONDATAX        = wa_user-logonx
         TABLES
           RETURN            = RETURN_DUMMY
         .

       "lock the user
       CALL FUNCTION 'BAPI_USER_LOCK'
         EXPORTING
           USERNAME = wa_user-uname
         TABLES
           RETURN   = RETURN_DUMMY.



     ENDWHILE.

     CLOSE DATASET pa_file.
   CATCH cx_sy_file_open_mode.
     MESSAGE 'Upload a proper file' TYPE 'E'.
ENDTRY.

After starting the program, this is what you should get:

In p_file parameter you add path to local .csv file containing a list of users. An example file should look like this:

Users_to_be_locked.csv

Username1

Username2

Username3

Username4

Username5

...

Username n

Afterwards you press F8 and the program should run perfectly.

The program uploads your local file into server and works on it. It sets expiration date as current date.

To lock users on other system, all you need is an RFC connection to them and to modify lines:

       "set users expiration date
       CALL FUNCTION 'BAPI_USER_CHANGE'
         EXPORTING
           USERNAME          = wa_user-uname
           LOGONDATA         = wa_user-logond
           LOGONDATAX        = wa_user-logonx
         TABLES
           RETURN            = RETURN_DUMMY
         .

       "lock the user
       CALL FUNCTION 'BAPI_USER_LOCK'
         EXPORTING
           USERNAME = wa_user-uname
         TABLES
           RETURN   = RETURN_DUMMY.

to

       "set users expiration date
       CALL FUNCTION 'BAPI_USER_CHANGE' DESTINATION <rfc_name>
         EXPORTING
           USERNAME          = wa_user-uname
           LOGONDATA         = wa_user-logond
           LOGONDATAX        = wa_user-logonx
         TABLES
           RETURN            = RETURN_DUMMY
         .

       "lock the user
       CALL FUNCTION 'BAPI_USER_LOCK' DESTINATION <rfc_name>
         EXPORTING
           USERNAME = wa_user-uname
         TABLES
           RETURN   = RETURN_DUMMY.

Hope this proves usefull

8 Comments
Labels in this area