Sub topic 8
Restricting Data with Access Controls
This chapter assumes you can read the cube, dimension and query views from the earlier chapters. You should also know basic SQL WHERE logic and what an SAP authorization object is. Here you add row-level restrictions to analytical views with CDS access control, written in the Data Control Language (DCL).
Why analytical views need access control
A query view reads live transactional data. Without a restriction, every user who can run the query sees every row. A sales manager for Germany should not see revenue for Brazil.
CDS access control solves this inside the data model. The database applies the restriction while it reads the data, so every front end gets the same result. This covers Fiori apps, the key user tools and external consumers.
How the check works
The cube carries the annotation @AccessControl.authorizationCheck: #CHECK. This tells the system to look for an access control for that view. An access control is a separate DCL object that defines a role. The role maps view fields to the authorization values of the current user.
At runtime the system adds a filter to the query, as if you had written a WHERE clause. The user's own authorizations decide which values appear in that filter.
Writing an access control
The cube from the earlier chapter has no country field. To filter by country, you can use the customer dimension. A common choice is to put the restriction on the cube field that the authorization object covers. This example assumes the cube also exposes a Country field, added from the same table, and that your system has an authorization object ZSD_COUNTRY with the field COUNTRY and the activity 03 (display).
@EndUserText.label: 'Access control for sales item cube'
@MappingRole: true
define role ZI_SalesItemCube {
grant select on ZI_SalesItemCube
where ( Country ) = aspect pfcg_auth( ZSD_COUNTRY, COUNTRY, ACTVT = '03' );
}The parts work as follows:
@MappingRole: truemarks the object as a role that maps authorizations to data.grant select onnames the view that the role protects.where ( Country ) = aspect pfcg_auth(...)compares the view field with the values that the user holds for the authorization object field.ACTVT = '03'limits the check to users who have display authorization.
A user whose profile grants COUNTRY = 'DE' sees only German rows. A user with no matching authorization sees no rows.
Where to place the restriction
You can protect views at several layers. The usual guidance is to put the role on the cube, because the cube is the shared source for all queries built on it. Queries inherit the protection through the view stack.
| Layer | Typical practice | Reason |
|---|---|---|
| Interface or cube view | Define the access control here | One definition protects every query on top |
| Consumption query view | Usually no separate role | The cube role already applies |
| Dimension view | Only if the master data itself is sensitive | Dimensions often hold non-sensitive data |
Common errors
- The annotation says
#NOT_REQUIREDon a sensitive view. The system then skips the check entirely. Use#CHECKon every cube that holds business data. Use#NOT_REQUIREDonly for non-sensitive data, such as the customer dimension in this tutorial's earlier example. #CHECKis set but no role exists. Depending on the system settings, users then get no data or an error. Always create the role together with the view.- The field in the
whereclause is not in the view. The role must use fields that the protected view exposes. Add the field to the view or choose another one. - The user has no authorization values. An empty result is correct behavior, not a defect. Check the user's roles in transaction
PFCGand test withSU53to see failed checks. - Testing only with a developer user. Developers often have broad authorizations. Test with a user who has a narrow role.
Exercise
Exercise
A query view ZC_NetAmountByCustomer reads the cube ZI_SalesItemCube. You write a DCL role for the cube only. Does the query view also respect the restriction, and which annotation on the cube makes the system look for the role?
Show answer
Yes. The query reads through the cube, so the filter from the role applies to the query as well. The annotation @AccessControl.authorizationCheck: #CHECK on the cube makes the system look for and apply the access control.