Tuesday, May 31, 2016

Evaluating user password in NetWeaver ABAP systems

ABAP programmers sometime need to authenticate a user during the runtime of some ABAP application. As an example of this need; one can say that if user is supposed to confirm some data in ABAP application a system shall ask for user’s password and once it is correct it really does the confirmation. Assumption here is that once user is entering the password he or she must be really aware that a particular activity (e.g. confirming a batch in manufacturing process) is untended to be done.


So being as the ABAP programmer how would I validate the user password? Luckily SAP is providing a very handy function module to do that. The name of the FM is SUSR_LOGIN_CHECK_RFC. It has very simple interface of importing parameters and by evaluating exceptions I can suite my application with regards either validation passed, user is locked, password is wrong etc.



REPORT ZMM_PWD_CHECK.

PARAMETERS: p_usr TYPE sy-uname,
            p_pwd TYPE rsyst-bcode.

CALL FUNCTION 'SUSR_LOGIN_CHECK_RFC'
 EXPORTING
   bname                  = sy-uname
   password               = p_pwd
 EXCEPTIONS
   wait                   = 1
   user_locked            = 2
   user_not_active        = 3
   password_expired       = 4
   wrong_password         = 5
   no_check_for_this_user = 6
   internal_error         = 7.

WRITE: sy-subrc.
CASE sy-subrc.
  WHEN 0. WRITE: 'everything OK'.
  WHEN 1. WRITE: 'wait'.
  WHEN 2. WRITE: 'user_locked '.
  WHEN 3. WRITE: 'user_not_active'.
  WHEN 4. WRITE: 'password_expired'.
  WHEN 5. WRITE: 'wrong_password '.
  WHEN 6. WRITE: 'no_check_for_this_user'.
  WHEN 7. WRITE: 'internal_error'.
  WHEN OTHERS.
ENDCASE.

Source code available at: github.com/softy12/ABAP-PWD-CHECK

No comments: