Code examples · Modern ABAP · Beginner
Who may do this? AUTHORITY-CHECK with S_TCODE and a clear message
The problem
A custom report shows sales order data.
How we solve it
- 1AskAUTHORITY-CHECK
- 2Sales areaV_VBAK_VKO
- 3Useallow or refuse
The result
Allowed users see the data line, other users see a message that names the missing right.
1. The requirement
A custom report shows sales order data. Only users who are allowed to display sales orders (transaction VA03) may see it. A user without the right must get a clear message, not an empty screen and not the data.
2. How we design it
SAP does not hide data by itself: the program has to ask. AUTHORITY-CHECK compares the authorizations of the current user with what the program asks for. An authorization object is a lock with named fields. S_TCODE has one field, TCD, the transaction code. We ask: may this user start VA03? SY-SUBRC says the answer: 0 allowed, 4 or 12 not allowed (12 means the user has no authorization for the object at all), anything else is a mistake in the program. The check sits in its own small method that returns true or false, so it can be reused and tested.
Why we decided it this way
- 1Check before reading dataA check after the SELECT is too late: the data is already in memory. The question comes first.
- 2A small method that answers true or falseEvery report that needs this rule calls the same method. The rule changes in one place.
- 3Allowed, not allowed, and a mistakeSY-SUBRC 4 and 12 both mean 'not allowed': 12 is the normal answer for a user who has no authorization for the object at all. Any other value is a mistake that a developer must see, so ASSERT stops the program instead of showing 'no authorization'.
Before you start
- A test user with the authorization object S_TCODE for VA03, and one without it. Roles are maintained in transaction PFCG.
- ADT and a package such as $TMP.
3. The code, step by step
Step 1 of 3: A method that asks
One reusable place for the question 'may this user display sales orders?'
" Class with a public check method
CLASS zcl_order_access DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
CLASS-METHODS check_display_allowed RETURNING VALUE(allowed) TYPE abap_bool.
CLASS-METHODS check_sales_area_allowed
IMPORTING sales_org TYPE vkorg
channel TYPE vtweg
division TYPE spart
RETURNING VALUE(allowed) TYPE abap_bool.
ENDCLASS.
Part by part (plain text)
- Class with a public check method. CHECK_DISPLAY_ALLOWED returns true or false. It is a class method, so it is called without creating an object. The class is also runnable, so we can try it with F9.
- AUTHORITY-CHECK OBJECT 'S_TCODE'. OBJECT names the authorization object and ID names its field, TCD. FIELD is the value we ask for, here the transaction code VA03. The check is done for the user who runs the program, SY-UNAME. A result of 0 means allowed. Values 4 and 12 mean not allowed (12: the user has no authorization for the object at all). Any other value means that something is wrong with the check itself, so ASSERT stops the program with a dump that the developer sees, instead of silently answering 'not allowed'.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
Transaction codes
Authorization objects
If it does not work
4. The final outcome
Allowed users see the data line, other users see a message that names the missing right.
What you should see
User with both rights: Authorization for VA03 and sales area 1000 / 10 / 00 is present, the order data may be shown User without the transaction right: No authorization to display sales orders (transaction VA03) User with VA03 but not for that sales area: No authorization for sales area 1000 / 10 / 00
Try it and check the result
- Run the class with F9 as a user who has VA03 in his role: the second line appears.
- Run it as a user without the right (a second test user, or a role without S_TCODE for VA03): the refusal message appears. Then give the user VA03 but a V_VBAK_VKO authorization for a different sales organization: the second message appears.
- After a refusal, call transaction SU53 as that user: it shows the authorization object, the field and the value that was missing. This is what the user sends to the security team.
Common mistakes
- Do not check only in the menu or the transaction code. A user can reach the program in other ways, so the program itself must ask.
- Never answer 'not allowed' for every non-zero SY-SUBRC without looking at it. A wrong object name or an invalid user is a bug and must stay visible to the developer.
- S_TCODE says only that the transaction may be started. Business restrictions such as the sales area need their own check, as in the second method. Use the sales area of the order you read, not a fixed one: the values 1000 / 10 / 00 are placeholders.