Authorization Checks

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Not every employee of a company may do everything with every expense claim. A clerk may display a claim, a manager may approve it, and only the claims department may delete one. SAP systems enforce such rules with authorizations: the administrator gives each user a set of permissions, and the program asks the system before it does something sensitive whether the current user has the matching permission. This page explains the building blocks (authorization fields, authorization objects, the user master record), the statement AUTHORITY-CHECK with which a program asks the question, how to read its answer, how the permissions get from a developer’s design to a user (roles and default values), and what changes in ABAP Cloud, where CDS access control and the RAP authorization methods do much of the asking for you. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The statement works in classic Standard ABAP, on premise and in the Public Edition, and in ABAP Cloud, with the differences listed in the section "What differs in ABAP Cloud". The administration steps that hand out permissions differ between the classic system (role maintenance in the SAP GUI) and ABAP Cloud (IAM apps, business catalogs and business roles); this page only introduces both. Every listing needs the authorization concept of an SAP system, so each one is labelled as requiring an SAP system and was not run here, except one small lab listing whose result proves nothing about permissions.

Before you read

Key terms

  • Authorization field: the smallest unit of an authorization object; it stands either for data, such as a key field of a table, or for an activity such as display or change.

  • Authorization object: a repository object that groups up to ten authorization fields; checks and permissions are always about one object.

  • Authorization: an entry in a user’s permissions that gives complete or generic values for the fields of one authorization object.

  • User master record: the record that exists for every user and holds, among other things, the authorizations the user has.

  • Authorization check: the comparison of the values a program needs with the values in the user’s authorizations; it is explicit (the statement AUTHORITY-CHECK) or implicit.

  • Check indicator: a setting that decides whether a check on an authorization object really takes place in the current context.

The building blocks

The Keyword Documentation introduces the authorization concept with a few terms, and the rest of the page uses them in this order.

  • An authorization field is the smallest unit. It stands for data, for example a key field of a database table, or for activities such as read or change. An activity is written as a two-character code; the codes are kept in a dictionary table (TACT, with TACTZ for customer-specific codes) and are maintained in transaction SU20. The standard field with the fixed name ACTVT carries the activity.

  • An authorization object is the repository object on which authorizations are based. It consists of up to ten authorization fields, and checks against it use combinations of those fields. Objects are grouped in authorization object classes and maintained in transaction SU21.

  • An authorization is an entry in the user master record, part of an authorization profile. It gives complete or generic values for the fields of one authorization object, and the combination says which activities the user may perform on which data.

  • An authorization profile combines several authorizations; assigning profiles to a user in the user master record is the authorization assignment. Profiles are generated from role maintenance, transaction PFCG.

  • The user master record must exist before anybody can log on, and it holds the profiles. It is maintained in transaction SU01.

The ABAP Cloud variant of the same introduction, written for people who have never worked with an ABAP system, adds a plain example: an authorization field "Product Type" with the values Material and Service, and an authorization object that combines such fields with the field Activity. Its important rule is that the fields of one object are related by AND: a user needs a permission that fits every field named in the check.

flowchart LR F1["Authorization field ZEXP_OP: CREATE, CHANGE, DELETE"] --> O["Authorization object ZEXP_CLM"] F2["More fields, up to 10 in total"] --> O O --> A["Authorization: values for the fields of the object"] A --> P["Authorization profile, generated from a role"] P --> U["User master record of one user"] PRG["Program: AUTHORITY-CHECK OBJECT ZEXP_CLM"] -- "asks" --> U U -- "sy-subrc 0, 4 or 12" --> PRG

The running example and the invented object

The examples protect the claims of the running example, the table ZEXP_CLAIM of Database Tables and Structures, with the fields client, claim_id, employee_id, status, currency_code, total_amount and the included audit structure zexp_s_audit. The page does not extend that table.

The authorization object is not a standard SAP object. The authorization object ZEXP_CLM with the one authorization field ZEXP_OP (values CREATE, CHANGE, DELETE) is invented for this section, and it is the same object that RAP Authorization and Concurrency uses. It exists in no system until somebody creates it, in transaction SU21 on a classic system or in ADT in ABAP Cloud. Real objects usually put the activity into the standard field ACTVT instead of a custom operation field, as the next section’s SAP example does with the code 03, display.

The statement AUTHORITY-CHECK

The statement AUTHORITY-CHECK OBJECT asks whether the user has an authorization for the object and whether that authorization is sufficient for the request in the statement. After the object name, each ID names one authorization field, and FIELD gives the value the program needs. The documentation fixes these rules.

  • You must name at least one and at most ten fields of the object. Field names are written in uppercase letters.

  • A value to be checked has at most 40 characters.

  • Instead of FIELD, a field can take the addition DUMMY, which means that field is not checked. Fields of the object that are not listed are not checked either.

  • The check succeeds when the user has at least one authorization for the object in which every listed field has a value set that contains the value to be checked.

  • If you name a field that does not belong to the object, no check is possible and sy-subrc is 4.

  • Do not name the same field twice. The check then treats the two entries as different fields, so all the values must fit into one authorization.

The program stays in control: the statement only sets sy-subrc, and your code must test it and stop. A check whose result nobody reads protects nothing.

AUTHORITY-CHECK OBJECT 'ZEXP_CLM'
  ID 'ZEXP_OP' FIELD 'CHANGE'.
IF sy-subrc <> 0.
  MESSAGE 'You may not change claims.' TYPE 'E'.
ENDIF.

Source: Keyword Documentation: AUTHORITY-CHECK OBJECT. The object ZEXP_CLM is invented for this page. This listing requires an SAP system; not run here.

Reading sy-subrc

The result is in sy-subrc. The documentation lists these values.

sy-subrc Meaning

0

The check succeeded, or no check was performed because the check indicator says "no check". An authorization was found whose value sets include the values you passed.

4

The check failed. Authorizations for the object exist, but none that includes the values you passed, or you passed a field the object does not have, or too many fields.

12

The user has no authorization for the object at all.

24

No longer set.

40

An invalid user name was given with FOR USER (classic Standard ABAP only).

Two details change how you write the test. First, 0 does not always mean the user has the permission: when the check indicator for the object is "no check" in the current context, the statement also returns 0. The documentation therefore advises that the indicator should always be set to check, and notes that for objects of the areas AS ABAP and human resources it cannot be turned off at all. Second, most code only needs "allowed or not", and sy-subrc <> 0 covers 4 and 12 together; separate 12 from 4 only when the message to the user should differ ("you have no access to claims at all" against "you may not change this one").

A small lab listing runs the same shape of code. The local lab (open-abap) has no authorization concept, so the statement compiles and always leaves sy-subrc at 0. It shows only that the branches around the statement are valid ABAP; it says nothing about what a real system answers.

REPORT zauth.

START-OF-SELECTION.
  AUTHORITY-CHECK OBJECT 'ZEXP_CLM'
    ID 'ZEXP_OP' FIELD 'CREATE'.

  CASE sy-subrc.
    WHEN 0.
      WRITE / 'Check passed, or no check was performed'.
    WHEN 4.
      WRITE / 'Authorizations exist, but not for this value'.
    WHEN 12.
      WRITE / 'No authorization for this object at all'.
    WHEN OTHERS.
      WRITE / 'Another code, see the documentation'.
  ENDCASE.

Output of the lab run (open-abap transpiler 2.14.30; the lab always answers 0):

Check passed, or no check was performed

Source: Keyword Documentation: AUTHORITY-CHECK OBJECT. Run in a scratch copy of the abap-lab of this site; the object ZEXP_CLM is invented for this page.

Checking only that some authorization exists

If every field is given as DUMMY, no value is compared, and the documentation says the statement returns 0 when at least one authorization for the object exists in the user master record, whatever its value sets, and 12 when none exists. That is a cheap way to ask "is this user in the claims business at all", for example before building a menu.

AUTHORITY-CHECK OBJECT 'ZEXP_CLM'
  ID 'ZEXP_OP' DUMMY.
IF sy-subrc = 0.
  " The user has some authorization for claims.
ENDIF.

Source: Keyword Documentation: AUTHORITY-CHECK OBJECT. This listing requires an SAP system; not run here.

Where to put the check

ABAP SQL does not check anything for you. The ABAP Cloud cheat sheet says it plainly: ABAP SQL statements do not trigger authorization checks in the database system, so database tables may be accessed without restriction, and it is up to the programmer to make sure every user who can call the program may see the data it handles. The Keyword Documentation’s chapter on insufficient authorization checks agrees and lists the places where nothing is checked unless you do it: SELECT and other SQL access to database tables, SUBMIT for programs without an authorization group, and CALL TRANSACTION without WITH AUTHORITY-CHECK. Every such place a user can reach without sufficient authorization must be secured explicitly with AUTHORITY-CHECK, and its result analysed carefully.

The pattern for the running example is: check first, then touch the data. The method below changes the status of one claim of ZEXP_CLAIM, and the check comes before the read and the write.

METHOD set_status.
  " iv_claim_id: key of the claim, iv_status: new status, a one-character code
  AUTHORITY-CHECK OBJECT 'ZEXP_CLM'
    ID 'ZEXP_OP' FIELD 'CHANGE'.
  IF sy-subrc <> 0.
    RAISE EXCEPTION TYPE cx_sy_authorization_error.
  ENDIF.

  UPDATE zexp_claim SET status = @iv_status
    WHERE claim_id = @iv_claim_id.
ENDMETHOD.

Source: Keyword Documentation: Insufficient Authorization Checks and ABAP Cheat Sheet: Authorization Checks. The class CX_SY_AUTHORIZATION_ERROR is the exception that CALL TRANSACTION ... WITH AUTHORITY-CHECK raises (see the Keyword Documentation page cited in the next section); raising it from your own method is this page’s choice, not an SAP rule, and a real design would define its own exception class. This listing requires an SAP system; not run here.

Reading it: the check is the first thing in the method, so no data is read or changed for a user without the permission. A user who can reach set_status through any other program that skips the check is not stopped, which is why the check belongs in the code that changes the data and not only in the screen that calls it.

For reads, the documentation mentions a second tool: the documented class CL_AUTH_OBJECTS_TO_SQL generates a dynamic WHERE condition for ABAP SQL from authorization objects, so a SELECT returns only the data the user may see. It may replace a check inside a SELECT loop. The page does not show its methods, because it has not been checked against the class documentation. For a protected CDS view, CDS access control does the same job declaratively, see below.

Implicit checks: transactions and CDS

An explicit check is the programmer’s own question. The system also asks some questions itself. The authorization concept page lists them: starting a transaction by entering its code in the SAP GUI command field, and statements such as LEAVE TO TRANSACTION and SUBMIT make implicit checks. With CALL TRANSACTION, the addition WITH AUTHORITY-CHECK checks the authorization object S_TCODE and any authorization object entered for the transaction code in its definition (transaction SE93) before the transaction starts, and a missing authorization raises the catchable exception CX_SY_AUTHORIZATION_ERROR. The documentation calls this the recommended method. Calling Programs and Memory shows the statement in use.

For data, ABAP CDS has its own implicit check. When a CDS view entity has an access control with a CDS role, every ABAP SQL read of that entity returns only the rows the role allows, and the role can be based on authorization objects through the pfcg_auth aspect. The annotation @AccessControl.authorizationCheck says whether an access control is required for the entity. CDS Access Control (DCL) describes the roles, the four values of the annotation and how to switch the check off. The documentation draws a clear line between the two ways: keep the classic explicit check for start authorizations (may this user start the application) and use CDS access control inside the application for instance-based questions (may this user see this claim).

The statement variants, FOR USER and the update task

The classic statement has one addition, FOR USER user, which checks the authorization of another user whose name is held in a field of the type of sy-uname. If the name is invalid, sy-subrc is 40. The documentation says this addition replaces the older function module AUTHORITY_CHECK. It also warns that it is a security risk: it can be misused to bypass a check by naming a user with wide authorizations, so a user name passed in from outside must never be used, or must be checked very carefully. The current user should not be given as sy-uname either, because that is redundant and the field can be overwritten, for example in the debugger. Without the addition, the check always uses the actual user name, not the content of sy-uname.

AUTHORITY-CHECK OBJECT 'ZEXP_CLM' FOR USER lv_user
  ID 'ZEXP_OP' FIELD 'CHANGE'.

Source: Keyword Documentation: AUTHORITY-CHECK OBJECT. Classic Standard ABAP only. lv_user is a variable of the type of sy-uname filled by trusted code. This listing requires an SAP system; not run here.

One more rule matters for programs that write data: no authorization check is possible during an update. In an update function module triggered by COMMIT WORK, the statement AUTHORITY-CHECK always sets sy-subrc to 0 and checks nothing. So the check must happen in the dialog part of the program, before the update is registered, as in set_status above. The same page adds that CDS access control is not allowed during an update. See SAP LUW, Update Tasks and Commit for the update task itself.

From the check to a role: SU22, SU24 and PFCG

A check in code is useless until some user has the matching authorization. In a classic system the path is the following, as the (older) role-administration documentation describes it.

  1. The developer implements the checks, tests the application and records in transaction SU22 which authorization objects the application needs, with a default status: "No" (the object is not needed for the core function), "Yes" (the Profile Generator adds an authorization for it, with proposed values) or "Yes, without values" (an empty authorization is added, because only the customer knows the values).

  2. In the customer system, transaction SU25 copies SAP’s default data to the customer tables, which transaction SU24 maintains. Only the customer default values apply when the Profile Generator is used.

  3. An administrator creates a role in transaction PFCG: a menu of the applications the work needs, plus the authorizations generated from the default values. Users get the role, and the generated profile enters their user master record.

The mechanism lets a developer say, once, that this application needs the authorization object ZEXP_CLM, and lets every customer fill in the values. The same documentation says that after an upgrade the administrator uses SU25 to compare the SAP default values with the customer’s and to adjust the roles.

When a user says "I get No authorization", transaction SU53 shows the last failed authorization check of that user: the checked object, the fields and the values tested, compared with what the user master record holds. The administrator can open it from any of the user’s sessions. If you do not know which authorizations an application needs, the system trace of transaction ST01 records the checks that happen. Both descriptions come from SAP ERP documentation of release 6.05, which is old, but no newer page for them was found; the transactions themselves are still the ones the current ABAP Platform documentation names for default values (SU22 and SU24) and for objects (SU21).

sequenceDiagram participant D as Developer participant S as SU22 and SU24 default data participant A as Administrator in PFCG participant U as User master record participant P as Program with AUTHORITY-CHECK D->>P: writes AUTHORITY-CHECK OBJECT ZEXP_CLM D->>S: states that the application needs ZEXP_CLM A->>S: reads the default values A->>U: assigns a role, which gives an authorization P->>U: asks whether the user has ZEXP_CLM with ZEXP_OP CHANGE U-->>P: sy-subrc 0, 4 or 12 Note over U,P: SU53 shows the last failed check

What differs in ABAP Cloud

ABAP for Cloud Development uses the same statement with fewer options, and gives users their permissions in a different way.

  • The authorization object name must be a literal or a constant. The variable form of the classic statement is "not available in the current ABAP language version".

  • The addition FOR USER is not available. You can only check the current user.

  • Authorization fields, authorization objects and default authorization values are created as development objects in ADT, not in the SAP GUI transactions.

  • Business users get permissions through IAM apps (Identity and Access Management apps) that list the authorization objects and the field values, grouped into business catalogs; an administrator then assigns catalogs to business roles and roles to users. Communication users, which are technical users for integration, get theirs through communication scenarios and arrangements. Default authorization values for an object and a service document which objects may be checked at run time, and are assigned automatically when the service is added to an IAM app or a communication scenario; the IAM app can overwrite them.

  • Most APIs offer a privileged mode that suppresses authorization checks where they are not relevant.

The parts of one ABAP Cloud solution, from design to run time, are then the following. The row "RAP" is covered by the RAP page; the others are named here so a newcomer can place them.

Part What it does Where to read more

Authorization field and object

Describe what can be checked, for example ZEXP_OP and ZEXP_CLM.

This page.

AUTHORITY-CHECK OBJECT

Explicit question from ABAP code.

This page.

CDS access control

Limits the rows of a protected CDS view, based on authorization objects.

CDS Access Control (DCL)

RAP authorization control

Global and instance authorization of a business object, implemented in handler methods that use AUTHORITY-CHECK OBJECT.

RAP Authorization and Concurrency

IAM app, business catalog, business role

Give business users the permissions.

Conceptual Background: Access Management

The RAP row deserves one sentence of its own. The Keyword Documentation says the authorization handler methods of a behavior definition should be implemented with AUTHORITY-CHECK OBJECT, and they return a result per operation. A RAP service is therefore protected by the same objects and the same statement, only the place the question is asked from is a handler method. Global authorization depends on the user only (may this user create claims); instance authorization also depends on the claim itself (may this user change this submitted claim).

Common mistakes

  • Writing the check and ignoring sy-subrc. The statement never stops the program by itself.

  • Relying on the check in the UI only. Another program or an API that changes the same table is not stopped by it.

  • Treating 0 as proof of permission. It also means "no check was performed".

  • Checking inside the update task. It is not possible there; check before registering the update.

  • Using FOR USER with a name that comes from input. It is a way around the check.

  • Checking a field twice in one statement, which turns into a check on two separate fields.

  • Hard-coding values the administrator should decide. Put the permitted values in the authorization, not in the program.

What older books show, and what has changed

A book written for AS ABAP 7.5 or older, such as the first edition (2016) of Complete ABAP, comes before ABAP Cloud, RAP and CDS access control, and it describes the world of role maintenance in the SAP GUI. Read it for the meaning of authorization objects, fields, profiles and sy-subrc, which has not changed. Four things to correct. First, the function module AUTHORITY_CHECK that older code and books use to check another user is replaced by AUTHORITY-CHECK ... FOR USER, and neither exists in ABAP Cloud. Second, a book that says the object name may come from any variable describes the classic statement; ABAP Cloud requires a literal or a constant. Third, the old rule of thumb that objects starting with S_ are system objects and P_ personnel objects has been replaced by an explicit criticality flag (critical or not critical) set when the object is created. Fourth, in ABAP Cloud and the Public Edition users do not get permissions from PFCG roles you maintain yourself; the IAM app, business catalog and business role described above do that, and the book’s chapter on role maintenance applies to on premise systems.

Summary

  • An authorization object groups up to ten authorization fields; the administrator gives users authorizations with values for those fields, and the program asks with AUTHORITY-CHECK OBJECT ... ID ... FIELD ....

  • The answer is in sy-subrc: 0 for success or "no check", 4 for a failed check, 12 for no authorization for the object at all. Test it and stop the program yourself.

  • ABAP SQL and many statements do not check anything on their own, so the check must come before the access, in the code that changes the data. During an update task no check is possible.

  • DUMMY skips a field, and with all fields on DUMMY the statement only tells you whether any authorization exists. FOR USER is classic only and is a security risk with outside input.

  • In a classic system the path from check to user goes through the default data of SU22 and SU24 and the roles of PFCG, and SU53 explains a failed check. In ABAP Cloud, IAM apps, business catalogs and business roles do the same job.

  • CDS access control and RAP authorization control ask the question for you, based on the same objects.