Sub topic 14
Authorization checks in ABAP and CDS access controls
Prerequisites and scope
This chapter assumes you know three things from earlier chapters. An authorization object has fields, and one of them is usually ACTVT (activity). A PFCG role generates a profile that holds authorization values. A business role gives a business user access to apps through business catalogs.
Catalogs and roles decide which apps a user can open. They do not stop code inside an app from reading data the user should not see. That job belongs to authorization checks in the code. This chapter covers the two places where such checks live: ABAP statements and CDS access controls. Both read the same authorization values that PFCG puts into the role, and both can use the same authorization object.
Why the checks live in code
A launchpad tile only starts an app. Once the app runs, it reads tables and returns rows. If the code does not test authorizations, every user who can open the app sees every row.
The role supplies the values. The code asks, for each action or each row, whether the user holds a matching value. This is why SU24 defaults matter. SU24 tells PFCG which authorization objects an app checks, so the generated role contains the values that the code will test. If the code checks an object that the role does not contain, the check fails for every user.
The ABAP AUTHORITY-CHECK statement
The AUTHORITY-CHECK statement tests one authorization object against the values in the current user's buffer. You name the object, then list each field with the value you need. The system sets sy-subrc to show the result.
AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'
ID 'BUKRS' FIELD lv_company_code
ID 'ACTVT' FIELD '03'.
IF sy-subrc <> 0.
" The user has no display authorization for this company code.
MESSAGE e000(00) WITH 'No authorization for company code' lv_company_code.
ENDIF.Here F_BKPF_BUK is the standard object for accounting documents by company code. The check asks whether the user has activity 03 (display) for the company code in lv_company_code. If one of the user's authorizations for this object contains both values, sy-subrc is 0.
What sy-subrc tells you
| sy-subrc | Meaning |
|---|---|
| 0 | The user has an authorization that covers all the values you asked for. |
| 4 | The user has no matching authorization. |
| 8 | The check named a field that the object does not have, or a field was not supplied correctly. |
| 12 | The user has no authorization at all for this object. |
| 40 | A field is missing from the call, or a name does not match the object. |
Treat any value other than 0 as a refusal. Do not write code that depends on telling 4 from 12 in production. The distinction is useful when you debug.
Rules that matter in practice
- The system matches all the listed fields inside a single authorization. It does not combine values from two different authorizations of the same object. If the user holds company code 1000 with activity 01 in one authorization and company code 2000 with activity 03 in another, a check for 1000 with activity 03 fails.
- A field you leave out of the call is a program error, not a wildcard. If you do not want to test a field, write
ID 'FIELDNAME' DUMMY. - A role value of
*passes any value. This is why broad roles are risky, as covered in the chapter on restrictions. - Run the check before the action, and check the specific data you are about to touch. Checking the object with a fixed company code, when the user can pick any code, protects nothing.
The next example shows a check that skips one field on purpose.
AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'
ID 'BUKRS' DUMMY
ID 'ACTVT' FIELD '03'.
IF sy-subrc = 0.
" The user may display documents in at least one company code.
ENDIF.This pattern answers a different question: can the user display anything at all? Use it to decide whether to show a menu entry or a button. Do not use it as the gate for reading data.
The limit of row-by-row checks
An ABAP check suits one action on one object, such as posting a document. It is a poor fit for reading a large result set. If a report selects 100,000 sales orders and then checks each row in a loop, three problems appear.
- The loop is slow, because every row triggers a check after the database has already sent it.
- Paging and counting break. The database returns 50 rows, the check removes 30, and the user sees a page of 20.
- Every program that reads the same data must repeat the check. One forgotten copy leaks data.
Moving the check into the data model solves all three. That is what CDS access controls do.
CDS access controls
A CDS view defines a data model that Fiori apps and APIs read. A CDS access control is a separate object that adds a filter to a view. The database applies the filter while it runs the query, so the app receives only the rows the user may see.
@EndUserText.label: 'Access control for sales orders'
@MappingRole: true
define role ZI_SalesOrderAC {
grant select on ZI_SalesOrder
where ( SalesOrganization ) = aspect pfcg_auth( V_VBAK_VKO, VKORG, ACTVT = '03' );
}Read this definition piece by piece.
@MappingRole: truemarks the object as an access control that maps authorizations to a filter.define role ZI_SalesOrderACnames the access control. The name is usually close to the view name.grant select on ZI_SalesOrdersays which view the rule protects.where ( SalesOrganization ) = aspect pfcg_auth( ... )compares the view fieldSalesOrganizationwith the values of the PFCG authorization.pfcg_auth( V_VBAK_VKO, VKORG, ACTVT = '03' )names the authorization objectV_VBAK_VKO, the object fieldVKORGthat supplies the sales organization values, and a fixed activity of03.
At runtime the system reads the sales organizations in the user's authorizations for V_VBAK_VKO with activity 03. It then adds them to the query as a filter. A user with sales organizations 1010 and 1020 receives rows for those two and no others.
The view needs an annotation that switches the check on. In the view source, @AccessControl.authorizationCheck: #CHECK makes the system apply the access control. With #NOT_REQUIRED or #NOT_ALLOWED the setting differs, so confirm it in each view you protect.
@AccessControl.authorizationCheck: #CHECK
define view entity ZI_SalesOrder
as select from vbak
{
key vbeln as SalesOrder,
vkorg as SalesOrganization,
netwr as NetAmount
}Behavior to expect
- A user with no authorization for the object gets no rows. The query does not fail. It returns an empty result.
- The same PFCG values drive the filter. When an administrator adds a sales organization to the role, the user sees new rows after the next logon or buffer refresh, with no code change.
- Fields in the
whereclause must exist in the view. A mismatch gives an activation error in the access control. - If a view has no access control and is set to
#CHECK, the result depends on the system version. Treat it as a defect to fix, not as a feature.
Comparing the two techniques
| Aspect | ABAP AUTHORITY-CHECK | CDS access control |
|---|---|---|
| Typical use | Allow or refuse one action | Filter the rows of a read |
| Where it runs | In application code, after the data is chosen | In the database query |
| Result on failure | `sy-subrc` not 0, the program decides what happens | Rows are missing from the result |
| Maintenance | One check per program | One definition per view |
| Authorization source | PFCG role values | PFCG role values |
Many applications use both. The access control filters what the user can read, and an ABAP check guards the actions on a chosen record, such as releasing or deleting it.
Connecting back to roles and troubleshooting
Because both techniques read PFCG values, the earlier tools still work. When a user sees too few rows, open SU53 right after the failed check, or run the system trace (ST01) to see which object and values the code requested. Then compare them with the role in PFCG. For CDS, the missing piece is usually a value such as a sales organization that the role does not list. For ABAP, it is often a missing activity or a company code.
When you design a custom role, check which objects the app's CDS views and ABAP code test. SU24 should list them. If it does not, the role you generate from the catalog will miss them, and users will open the app but see nothing.
Common errors
- Omitting a field from
AUTHORITY-CHECKinstead of usingDUMMY, which yields a nonzerosy-subrcfor every user. - Checking only after the data has been read and shown.
- Assuming that a business catalog restricts data. It only controls which apps appear.
- Granting
*for the organizational field in a test role and moving it to production. - Creating an access control for a view but leaving the view without the
#CHECKannotation.
Exercise
A report reads sales orders from a CDS view and runs an ABAP loop that calls AUTHORITY-CHECK on VVBAKVKO for each row. Users complain that pages show fewer rows than requested. Name the cause and the fix.
Show answer
The rows are filtered after the database has already returned a page, so removing rows leaves short pages. Move the check into a CDS access control on the view, using aspect pfcgauth with VVBAK_VKO, VKORG and ACTVT = '03', and set the view to @AccessControl.authorizationCheck: #CHECK. The database then returns only permitted rows, so paging and counts are correct. Keep an ABAP check only for actions on a single record.
Exercise
A user holds an authorization for FBKPFBUK with company code 1000 and activity 01, and another with company code 2000 and activity 03. What does a check for company code 1000 with activity 03 return, and why?
Show answer
It returns a nonzero sy-subrc. The system tests all fields within one authorization, and it does not combine values across authorizations. Neither authorization contains both company code 1000 and activity 03.