Sub topic 8
Restricting data with access controls
This chapter shows how to restrict the rows an analytical query returns, using CDS access control. It assumes you can read the cube, dimension and query views from earlier chapters. It also assumes you know basic SQL and the difference between a transactional and an analytical workload.
Why analytics needs its own row-level protection
Embedded analytics reads live transactional data. A sales manager for Germany must not see net amounts for France just because both are stored in the same table. In a separate data warehouse you would filter during extraction or in the reporting tool. Here the query runs directly on the business tables, so the restriction must be applied inside the data model.
SAP solves this with the Data Control Language (DCL). DCL is the part of ABAP CDS that defines access controls. An access control is a separate repository object that attaches a condition to a CDS view entity. When a user runs a query, the database layer adds the condition to the SQL statement. The user never sees rows that fail it.
Three properties make this approach suitable for analytics:
- The rule is attached to the data model, so every front end that uses the view gets the same restriction.
- The condition is added to the SQL statement, so the database filters rows before aggregation. Totals only include rows the user may see.
- The rule is defined once on the view and applies to every query built on it.
The authorization check annotation
Every CDS view entity declares how it is checked, using the @AccessControl.authorizationCheck annotation. The earlier examples used two values.
| Value | Meaning | Typical use |
|---|---|---|
| `#NOT_REQUIRED` | No access control is needed. | Master data such as the customer dimension, when it holds no sensitive rows. |
| `#CHECK` | An access control is expected. If none exists, the view returns no data to users who are subject to the check. | Cubes and queries that hold business transactions. |
| `#NOT_ALLOWED` | The view must not have an access control. | Helper views that are never read directly by users. |
| `#PRIVILEGE_CHECK` | The check is performed through privileges defined in the access control. | Views that use the newer privilege-based model. |
The cube ZI_SalesItemCube uses #CHECK. That is the right choice for transactional facts. With #CHECK, a missing access control is not a silent opening of all data. This makes it a safe default.
The view-level annotation does not define who may see what. It only says that a check applies. The rules live in the DCL source.
Writing a first access control
An access control is written in its own source file, with its own name. The convention is to give it the name of the view it protects. The following example restricts the sales item cube by country, using an authorization object.
@EndUserText.label: 'Access control for sales item cube'
@MappingRole: true
define role ZI_SALESITEMCUBE {
grant select on ZI_SalesItemCube
where ( ) = aspect pfcg_auth( ZSALES_CTY, ZCOUNTRY, ACTVT = '03' );
}The pieces are:
@MappingRole: truemarks this as a role that maps authorization data to view fields. Every classic access control uses this annotation.define rolenames the role. The name matches the view in this example.grant select onstates which view entity the rule protects.whereholds the condition. Here the condition uses the aspectpfcg_auth.ZSALES_CTYis a hypothetical authorization object.ZCOUNTRYis a field of that object.ACTVT = '03'requires the display activity.
For the condition to work, the view needs a field to compare with. The authorization object field ZCOUNTRY must be mapped to a field of the view. Here is the version that does this mapping.
@EndUserText.label: 'Access control for sales item cube'
@MappingRole: true
define role ZI_SALESITEMCUBE {
grant select on ZI_SalesItemCube
where ( Country ) = aspect pfcg_auth( ZSALES_CTY, ZCOUNTRY, ACTVT = '03' );
}The field list in parentheses on the left maps view fields to the authorization object fields on the right, in order. Here the view must expose a Country element. In our earlier cube, the country sits in the customer dimension. This leads to a design decision, covered next.
Where to put the restriction
The sales item cube has no country field of its own. There are three ways to deal with that.
- Add a country element to the cube. You can expose it from the underlying table, or take it through an association path such as
_Customer.Country. The access control then uses that element. - Put the rule on the dimension view and inherit it. The cube reuses the dimension's rule through a path expression in the DCL.
- Create the rule on the query view only. This is not recommended, because other queries on the same cube would stay unprotected.
The general guidance is to protect the lowest view that holds the sensitive data. Every query on top of it then inherits the restriction. A restriction on the consumption layer protects only that one query.
You can write the rule with the inheritance option so a view reuses the access control of another view:
@EndUserText.label: 'Inherited access control'
@MappingRole: true
define role ZC_NETAMOUNTBYCUSTOMER {
grant select on ZC_NetAmountByCustomer
where inherit ZI_SALESITEMCUBE for grant select on ZI_SalesItemCube;
}Use inheritance when the consumption view and the cube belong to the same authorization concept. It avoids copies of the same rule that drift apart over time.
How the check reaches the database
The diagram shows the order of events when a user opens a query.
Because the filter runs before aggregation, the grand total shown to a restricted user is the total of their permitted rows only. It is not the company total with some rows hidden. This matters when a user compares a report against a figure from a colleague with wider access. The two numbers differ for a legitimate reason.
Common rule types
The where clause supports more than one kind of condition. The right choice depends on where the permission data comes from.
| Condition type | Source of the permission | Example use |
|---|---|---|
| `aspect pfcg_auth` | Authorization objects in the user's PFCG roles. | Country, company code, sales organization. |
| Literal conditions | A fixed value in the DCL source. | A view limited to one status value for all users. |
| `aspect` on a user | Information such as the current user, taken from the session. | Rows owned by the user. |
| `inherit` | Another access control. | Consumption views that reuse the cube's rule. |
PFCG is the role maintenance transaction in SAP systems. A security administrator assigns authorization values there, and the CDS access control reads them at run time. You write the structure of the rule. The administrator controls which users have which values.
A literal condition looks like this:
@EndUserText.label: 'Only released items'
@MappingRole: true
define role ZI_RELEASEDITEMS {
grant select on ZI_ReleasedItems
where Status = 'R';
}Literal conditions apply to every user. They are a filter, not a permission, so do not use them to represent different rights for different people.
Edge cases and common errors
An empty result without an error. A view with #CHECK and no access control, or a user with no matching authorization values, returns no rows. The query opens and is simply empty. Check the user's authorizations first. Then check that an access control exists and is active for that exact view.
Wildcards and full access. If a PFCG role gives the user * for the country field, the user sees all countries. This is intended, but review it. A single broad role can override many narrow ones, because authorization values from all of a user's roles are combined.
Several authorization fields. If you list more than one field in the where condition, the user must satisfy the combination in the authorization object. Authorization values are matched per field within one role entry. Be careful with this when you test, because a test user with country DE and company code 1000 in different role entries behaves differently from one with both in a single entry.
Unprotected associations. An association used in a path expression exposes data from the target view. If the target has its own access control, it applies. If the target uses #NOT_REQUIRED, its data is open. Review the dimensions you reach through associations. Sensitive attributes in a dimension need a rule of their own.
Restrictions in the wrong layer. A rule on a consumption query protects only that query. A second query on the same cube bypasses it. Put the rule on the cube.
Missing field in the view projection. An access control can only use fields the view exposes. If you want to restrict on a table field that the view does not select, add it to the view first.
Data aging and transport. An access control is a development object. It is transported with the view. If the view arrives in a quality system without its access control, queries return nothing there. Include both in the same transport request.
Testing your rules
Test with at least three users:
- A user with full access, to confirm the unrestricted total.
- A user with one country, to confirm the filtered total.
- A user with no relevant authorization, to confirm that the result is empty and no error is raised.
Compare the totals. The filtered total for the one-country user should equal the sum for that country as seen by the full-access user. If it does not, the mapping between view fields and authorization fields is wrong.
Summary of working rules
- Set
#CHECKon cubes and queries that hold business transactions. - Write one access control per protected view, and protect the lowest suitable layer.
- Use
inheritfor consumption views that share the cube's authorization concept. - Keep authorization values in PFCG roles. Keep the structure of the rule in DCL.
- Transport views and their access controls together.
Exercise
A query view ZC_NetAmountByCustomer reads the cube ZI_SalesItemCube. The cube has @AccessControl.authorizationCheck: #CHECK but no access control exists. A user opens the query. What does the user see, and what is the correct fix?
Show answer
The user sees an empty result, with no error, because #CHECK without an access control returns no rows to users who are subject to the check. The fix is to create an access control for the cube, for example with define role ZI_SALESITEMCUBE and a where condition that maps a view field such as Country to an authorization object through aspect pfcg_auth. Put the rule on the cube so every query built on it inherits the restriction. Do not switch the cube to #NOT_REQUIRED to make the data appear, because that would open it to all users.
Exercise
A restricted user reports that the grand total in a query is lower than the total a colleague sees. Is this a defect? Explain why.
Show answer
No. The access control condition is added to the SQL statement and the database filters rows before it aggregates. The restricted user's total covers only the permitted rows, while the colleague with wider authorizations has more rows in the total. The difference is expected. It becomes a defect only if the restricted user's total does not match the sum of their permitted rows as seen by a full-access user.