Sub topic 14
Authorization checks in ABAP and CDS access controls
This chapter assumes you know what an authorization object, its fields and its activities are, and that a PFCG role generates the authorization values a user receives. Here you will see how code enforces those values, first with an ABAP statement and then with a CDS access control.
Why code needs its own checks
A business role grants access to an app, and the role's authorization values say what the user may do inside it. Something still has to enforce those values when data is read or changed. The launchpad does not do this. The code behind the app does, in one of two ways:
- An explicit
AUTHORITY-CHECKstatement in ABAP. - A CDS access control that filters the rows a query returns.
Both read the same authorization data that PFCG produced for the user. If you understand one, the other follows.
The ABAP authority check
AUTHORITY-CHECK tests whether the user's buffered authorizations contain a matching combination of field values. The example checks the standard object F_BKPF_BUK for display access (activity 03) to one company code.
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.Each ID line names one field of the object and the value to test. The check passes only if one authorization of the user covers every field at once. The result is in sy-subrc:
| sy-subrc | Meaning |
|---|---|
| 0 | The user has a matching authorization. |
| 4 | The user has authorizations for the object, but none match. |
| 12 | The user has no authorization for the object at all. |
Always test sy-subrc <> 0 rather than a single code. Run the check before you read or change the data, not afterwards.
The CDS access control
An authority check tests one value at a time. A CDS view can return thousands of rows, so checking row by row in code is slow and easy to forget. A CDS access control moves the test into the data read. The system adds the user's allowed values as a filter.
@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 it from the right:
pfcg_auth( V_VBAK_VKO, VKORG, ACTVT = '03' )names the authorization object, the field that carries the organizational value, and a fixed activity.SalesOrganizationis the view element compared with the user'sVKORGvalues.grant select on ZI_SalesOrderapplies the rule to every read of that view.
A user whose role grants VKORG values 1010 and 1020 for display sees only sales orders in those two sales organizations.
Choosing between them
Use an access control when the check is about which records a user may see. Use AUTHORITY-CHECK when the check guards an action, such as posting a document, or when the value comes from program logic. An app can use both.
Common errors
- Checking only
ACTVTand forgetting the organizational field, which lets users act in any organization. - Using an activity that does not match the operation, such as
03for a change. - Writing an access control but leaving the view's authorization check unset, so the control is never applied.
- Testing
sy-subrcafter another statement has already overwritten it.
Exercise
A user's role grants VVBAKVKO with VKORG = 1010 and ACTVT = 03. A query on ZI_SalesOrder, protected by the access control above, runs for that user. Which rows come back, and which check would you add in ABAP before letting the user delete a sales order?
Show answer
Only rows whose SalesOrganization is 1010 come back, because the access control filters on the user's VKORG values for display. For deletion, add an AUTHORITY-CHECK on VVBAKVKO with the relevant VKORG field value and ACTVT = '06' (delete), then stop with a message if sy-subrc is not 0.
Exercise
An AUTHORITY-CHECK returns sy-subrc = 12. What does that tell you about the user's roles, compared with a result of 4?
Show answer
12 means the user has no authorization for the object at all, so the role is probably missing it. 4 means the user has the object but no authorization matches the tested field values, so the values in the role need adjusting.