Wednesday, November 20, 2013

How to check authorization for a pernr (Employee) SAP ABAP

Hi Folks,
in the standard abap there is a nice authorization object for "pernr" ( Employess) it looks like this more or less

AUTHORITY-CHECK OBJECT 'P_PERNR'
ID 'AUTHC' FIELD LEVEL
ID 'PSIGN' FIELD '...'
ID 'INFTY' FIELD INFTY
ID 'SUBTY' FIELD SUBTY.

However given the different auth concepts in Hr and the possibility that the customer has its own custom abap coding in authorizations it is higly recommendend to use the standard function module

FUNCTION hr_check_authority_pernr.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(TCLAS) LIKE  PSPAR-TCLAS DEFAULT 'A'
*"     VALUE(PERNR) LIKE  PRELP-PERNR
*"     VALUE(BEGDA) LIKE  PRELP-BEGDA DEFAULT SY-DATUM
*"     VALUE(ENDDA) LIKE  PRELP-ENDDA DEFAULT SY-DATUM
*"     VALUE(UNAME) TYPE  SYUNAME DEFAULT SY-UNAME
*"  TABLES
*"      I0001 STRUCTURE  P0001 OPTIONAL
*"  EXCEPTIONS
*"      NO_AUTHORIZATION_FOR_PERNR
*"----------------------------------------------------------------------

You can call it like this
  CALL FUNCTION 'HR_CHECK_AUTHORITY_PERNR'
    EXPORTING
      pernr                      = pernr-pernr
    EXCEPTIONS
      no_authorization_for_pernr = 1
      OTHERS                     = 2.

  IF sy-subrc NE 0.
    MESSAGE E016(rp) WITH 'MISSING AUTH FOR PERNR' pernr-pernr.
  ENDIF.


This function calls the authorization BADI handling both standard and custom authorization checks and should keep you whithin the standard.

HTH
Cheers