cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Authority Check-For Cost Center Based on User name

janardhan-16
Explorer
0 Kudos
697

Hi all, I hope all are doing well.

I'm new to  SAP ABAP.

We have requirement from our client, where a user should be restricted by the user name to a particular cost center.

For this I have created a Custom Table with fields MANDT, UNAME & KOSTL and i have assigned the cost centers based on User name.

EX:|UNAME ||KOSTL|

    USER1    CC1000

    USER1    CC2000

    USER2    CC2000

    USER3    CC3000

And also I have developed the code in standard functionality using Exits(Enhancements) EXIT_SAPLKBER_001.

But my code is restricting for all users even they are available in table also. 

Can anyone suggest me a proper approach to get a expected output.

Thanks in advance..... 

For Your Reference:

TYPESBEGIN OF ts_z_cs_auth,
         mandt TYPE z_cs_auth-mandt,
         uname TYPE z_cs_auth-uname,
         kostl TYPE z_cs_auth-kostl,
       END OF z_ts_cs_auth.

* Internal Table
DATAlt_user_cs TYPE TABLE OF z_ts_cs_auth.

* Work Area
DATAls_user_cs TYPE ts_z_cs_auth.

* Local Variables
DATAlv_user_name       TYPE sy-uname,
      lv_cs              TYPE ts_z_cs_auth-kostl,
*      lv_message         TYPE string,
      lv_authority_check TYPE LENGTH 1.

lv_user sy-uname.
" Read the authorized cost centers for the user from the custom table
SELECT uname
       kostl
  INTO TABLE lt_user_cs
  FROM z_cs_auth
  WHERE uname lv_user AND kostl lv_cs.
IF lt_user_cs IS INITIAL.
  " No entries found for the user in the custom table
  MESSAGE e001(zuser_auth).
  EXIT.
ENDIF.

CALL FUNCTION 'AUTHORITY_CHECK'
  EXPORTING
*   NEW_BUFFERING       = 3
    user                sy-uname
    object              'z_cs_auth'
    field1              'kostl'
    value1              ls_user_cs-kostl
  EXCEPTIONS
    user_dont_exist     1
    user_is_authorized  2
    user_not_authorized 3
    user_is_locked      4
    OTHERS              5.
IF sy-subrc <> 0.
* Implement suitable error handling here
  MESSAGE e002(zuser_auth).
  EXIT.
ENDIF.

Accepted Solutions (1)

Accepted Solutions (1)

EhsanGhasemi
Participant
0 Kudos

Hello 

First of all, you should check the cost center is reachable at this exit.  If yes, then you must fill lv_cs by cost center  that pass to exit.

SELECT count(*)
  FROM z_cs_auth
  WHERE uname = @SY-uname AND kostl = @iv_kostl."iv_kostl is cost center that reachabel form exit.
IF sy-subrc is not INITIAL.
  " No entries found for the user in the custom table
  MESSAGE e001(zuser_auth).
  EXIT.
ENDIF.

Answers (1)

Answers (1)

r_lindemann
Explorer

So much wrong here, I barely know where to start.

But a few things that will prevent your code from doing what it probably should:

  • you read your custom table with user and cost center number - but the variable lv_cs is not filled anywhere
  • you do an authority check on an authorization object named z_cs_auth - does that actually exist? If it does, you will need to pass it to the function authority_check in upper case. (Or, better: just directly use the ABAP statement AUTHORITY-CHECK, rather than going through the function.)

And please, when you post code, do post it formatted as code. Like this it's really hard to read.