CDS Access Control (DCL)
|
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 user may see every row of a business table. In the Expense claims example an employee should see the claims of
the cost centres they work for, and the claims they filed themselves, but not the claims of the whole company. ABAP CDS
lets you state such a rule once, next to the data model, instead of repeating a check in every program that reads the
data. The rule is written in a separate source called an access control, in a small language named DCL (Data Control
Language), and the system adds it to every read of the protected view automatically. After this page you can read and
write a CDS role, choose between the kinds of condition, say what the four values of @AccessControl.authorizationCheck
do, and know the few ways in which the check is switched off. The page is written against Standard ABAP release 8.16 and
ABAP for Cloud Development release 9.20 (2608). CDS access control applies to ABAP Cloud and to classic Standard ABAP,
on premise and in the Public Edition alike; the section on ABAP Cloud lists the restrictions that apply only when you
develop in the cloud.
Before you read
-
Tables and Structures in the ABAP Dictionary: the
ZEXP_CLAIMtable that the protected view reads. -
ABAP SQL Basics: access control is applied to every ABAP SQL read of a protected view.
-
ADT (Eclipse) Setup: access controls are edited only in ADT.
-
The CDS view entity that this page protects is described on CDS View Entities; this page only shows the few lines of the view that matter here.
Key terms
-
CDS access control: the rule that restricts which rows of a CDS entity a user gets when ABAP SQL reads it.
-
DCL (Data Control Language): the language in which access controls are written; each CDS role has its own DCL source.
-
CDS role: the object created with
DEFINE ROLE; it holds one or more access rules and applies to every user. -
Access rule: one
GRANT SELECT ONstatement inside a CDS role; it names one CDS entity and either its access condition or full access. -
Authorization object: a classic SAP object with up to ten authorization fields, maintained in transaction SU21; user authorizations are values of its fields.
-
PFCG condition: an access condition that compares fields of the CDS entity with the authorizations that the role maintenance (transaction PFCG) gave the current user.
Two ways to check authorizations
SAP has long had one way to check what a user may do: the classic authorization concept. A user master record holds
authorizations, each of which gives values for the fields of an authorization object. A program asks the question
itself with the statement AUTHORITY-CHECK OBJECT, or the system asks it for you, for example when a transaction starts.
The check answers yes or no for one request.
CDS access control is a second way, built for data. It is declared on the data model and checked implicitly each time ABAP SQL reads a protected CDS entity, so the result of the read already contains only the rows that the user may see. The two ways work together and you can use either alone. The documentation gives a clear division of labour: keep the classic checks for start authorizations (may this user start the application at all) and use CDS access control inside the application for instance-based checks (may this user see this particular claim).
The parts of CDS access control
Three pieces fit inside each other.
-
A CDS role is created with
DEFINE ROLE. It is not assigned to individual users: the annotation@MappingRole: truemakes it apply to every user, in every client. What differs per user is the data the conditions look at, such as the user name or the classic authorizations. -
An access rule is one
GRANT SELECT ONstatement inside a role. It names exactly one CDS entity and either states an access condition afterWHEREor, withoutWHERE, grants full access. -
An access condition is a test on the elements (the columns) of the entity. It can compare an element with a literal value, with the current user, with the user’s classic authorizations (PFCG condition), or with data of a user-defined aspect, and it can reuse the conditions of another entity (inheritance condition).
When ABAP SQL reads a protected entity, the runtime adds the access condition to the selection as an extra filter, so the database only returns matching rows.
The entity to protect
The running example protects the claim view of the Expense claims application. Only the first lines matter here: the
annotation that asks for access control, and the elements that the conditions will use. CostCenter says which cost
centre the claim belongs to, and CreatedBy holds the user name of whoever filed it. The table ZEXP_CLAIM of the
page on tables and structures has no cost centre column, so this page extends it with one column, cost_center, which
the listing needs; created_by already comes from the included audit structure ZEXP_S_AUDIT, and its type must be able
to hold any user name. It requires an SAP system; not run here.
@AccessControl.authorizationCheck: #MANDATORY
define view entity ZI_ExpClaim
as select from zexp_claim
{
key claim_id as ClaimId,
employee_id as EmployeeId,
cost_center as CostCenter,
status as Status,
created_by as CreatedBy
}
The annotation controls how the check is evaluated; its four values are described in a section below. #MANDATORY is
chosen here because the claims are sensitive: if someone deletes the access control by mistake, reading the view fails
loudly instead of silently showing everything.
Defining a role
An access control is its own development object, with its own DCL source. The documentation recommends giving the role
the same name as the CDS entity it protects. The shape is always the same: the annotation, DEFINE ROLE, a name, and
curly brackets that hold the rules.
@MappingRole: true
define role ZI_ExpClaim {
grant select on ZI_ExpClaim
where ( CostCenter ) = aspect pfcg_auth( ZEXP_CC, ZCOSTCTR, ACTVT = '03' )
or CreatedBy = $session.user;
}
Requires an SAP system; not run here. This is the access control of the running example: restrict claims to the
employee’s cost centre. The rule reads: a row is visible when its CostCenter matches a value that the user’s
authorizations give for the field ZCOSTCTR of the authorization object ZEXP_CC (with display activity 03), or when
the claim was filed by the user. Both objects are examples for this page: you create the authorization object in
transaction SU21 and an administrator puts its values into a PFCG role.
Source: ABAP Keyword Documentation: CDS DCL - DEFINE ROLE and CDS DCL - DEFINE ROLE, conditional_rule
Reading it from the top.
-
@MappingRole: trueis mandatory. It is what assigns the role to every user. -
grant select on ZI_ExpClaimopens the access rule for one entity.WHEREfollowed by an access condition makes it a conditional rule; a rule withoutWHEREis a full access rule (see below). -
The conditions are built with
AND,ORandNOT.NOTbinds stronger thanAND, andANDstronger thanOR; parentheses change the order. -
The rule ends with a semicolon.
The documentation recommends one access rule per role, and the role may hold rules for several entities. Several rules for one entity are joined with a logical or, which is why the example puts both tests in a single rule.
The kinds of access condition
Literal conditions
A literal condition compares an element with a fixed value, with a pattern, or with a session variable. Examples are
Status = 'A', Status BETWEEN ... AND ..., LIKE, IS NULL, and a comparison with $session.user,
$session.user_date, $session.user_timezone, $session.system_date or $session.system_language. The syntax check
refuses a comparison whose element cannot hold every possible value of the variable. A character literal is written in
single quotation marks.
@MappingRole: true
define role ZI_ExpClaimOwn {
grant select on ZI_ExpClaim
where CreatedBy = $session.user;
}
Requires an SAP system; not run here. A user sees only the claims they filed. Because two roles for one entity are joined with a logical or, this role together with the one above would not narrow anything; the example only shows the syntax of a literal condition.
PFCG conditions
A PFCG condition links elements of the entity to the fields of an authorization object. At runtime the system takes the
authorizations of the current user for that object and turns them into filter conditions. The left side is a list of
elements in parentheses; the right side starts with ASPECT pfcg_auth and names the authorization object, the
authorization fields that the elements map to (in the same order and the same number), and optionally further
authorization fields with fixed values.
How the user’s authorizations are combined follows fixed rules.
-
Several authorizations of the user are joined with a logical or.
-
Inside one authorization, the values of different fields are joined with a logical and.
-
Several values of one field are joined with a logical or.
So a user whose PFCG role allows cost centres CC1000 and CC2000 with display activity sees claims of both. The
condition is true when at least one authorization matches the values of the row. A user who has no authorization for
the object sees no row through this condition. Four details are worth remembering.
-
If the user has a full authorization for a field, no filter is built for that element, which accepts every value, including the null value. If that is unwanted, add
AND CostCenter IS NOT NULL. -
BYPASS WHENcan switch off the test for one element when it is initial or null, and the operator?=also accepts rows whose elements are all null or initial. -
A PFCG condition with elements on the left side cannot be negated with
NOT. -
Authorizations are evaluated as if the user had full authorization in update task processing, as with the classic
AUTHORITY-CHECK.
The size of the resulting database statement depends on the role data of the user. A tool that fills a role with hundreds of single cost centres makes the statement long and slower, so prefer generic values where the business allows.
Source: CDS DCL - DEFINE ROLE, pfcg_condition and CDS DCL aspect
User conditions
A user condition compares an element with the user name by writing ASPECT user on the right side, for example
uname = aspect user. The documentation marks the older base syntax ASPECT_USER as deprecated and recommends
$session.user, because the new form checks at syntax-check time that the element is compatible with all user names. Two
variants compare the user alias or the business partner number of the user instead.
User-defined aspects
When the rule depends on data of your own model, for example a table that says which cost centres each employee
manages, you define an aspect with DEFINE ASPECT. An aspect reads from an entity, takes the user name from one element
of it, and delivers output elements that are matched with the left side of a condition. Rows exist in the protected
entity for which a row exists for the current user in the aspect entity. The aspect entity must carry auditing
annotations (@AccessControl.auditing.type and a component with @AccessControl.auditing.specification) because
the content of the aspect decides who may see data, so the system must be able to reconstruct it later. A user-defined
aspect cannot be negated with NOT.
Source: CDS DCL - DEFINE ASPECT
Inheritance conditions
A CDS role restricts only the entity it names. If a view such as ZI_ExpClaimList reads ZI_ExpClaim, it does not inherit the role of
ZI_ExpClaim at all, and an ABAP SQL query on the upper view sees all rows unless that view has its own access control.
When you want to reuse the rules, write an inheritance condition.
@MappingRole: true
define role ZI_ExpClaimList {
grant select on ZI_ExpClaimList
where inheriting conditions from entity ZI_ExpClaim;
}
Requires an SAP system; not run here. The view ZI_ExpClaimList, which selects from ZI_ExpClaim, is protected by the conditions that apply to
ZI_ExpClaim. The inherited conditions must fit the elements of the entity that inherits them, so ZI_ExpClaimList must
expose the elements CostCenter and CreatedBy under the same names, or the conditions are mapped with a replacing
clause. If the source entity has no role yet, a syntax warning appears and the condition becomes true; the addition
DEFAULT TRUE or DEFAULT FALSE chooses the result on purpose. The annotation @AccessControl.authorizationCheck of
the source entity is ignored when conditions are inherited. The older form INHERIT parent_role FOR GRANT SELECT ON
is not available in the current ABAP language version, so tutorials from older releases that show it need rewriting.
Combining rules and granting full access
Several access rules for one entity, in one role or in several, are joined with a logical or. The addition
COMBINATION MODE OR says so explicitly and COMBINATION MODE AND makes a rule an additional restriction instead. The
full condition is ( cond_or_1 OR cond_or_2 ... ) AND cond_and_1 AND cond_and_2 ....
A rule without WHERE is a full access rule: grant select on ZI_ExpClaim;. It gives unrestricted access and overrides
the construction above, even when other rules use COMBINATION MODE AND. SAP does not deliver roles with full access;
partners and customers use them to override roles that SAP supplies. REDEFINITION is the tool for replacing a rule that
already exists for an entity, and WITH OPTIONAL ELEMENTS bridges a short gap when the source of an inheritance changes;
the documentation advises against keeping it as a permanent design.
The annotation AccessControl.authorizationCheck
The annotation on the CDS entity decides what the runtime does with the roles. Its default, when you write nothing, is
#NOT_REQUIRED.
| Value | Meaning |
|---|---|
|
Without an access control every user has full access; with one, it is evaluated. |
|
Runs like |
|
An access control must exist. Without one, reading the entity in ABAP SQL ends in a runtime error. Use it where access control is critical. |
|
No access control should exist; a role that exists is ignored at runtime and a warning is shown. For technical entities. |
|
Evaluated only by dedicated frameworks such as SADL. For ABAP SQL it behaves like |
The annotation applies to views, hierarchies, table functions, table entities and external entities. #NOT_ALLOWED is
also what the documentation recommends for entities read client-independently, because CDS access control does not work
for client-independent access.
Source: AccessControl.authorizationCheck
Switching access control off
There are a few intended ways to read all rows of a protected entity, and every one of them is a decision to review.
-
@AccessControl.authorizationCheck: #NOT_ALLOWEDon the entity switches the check off for every reader. -
The addition
WITH PRIVILEGED ACCESSin theFROMclause of ABAP SQL switches it off for one entity of one query. -
The addition
PRIVILEGED ACCESSafterOPTIONSswitches it off for the whole statement, including entities reached through a path expression. -
A full access rule in a customer role overrides the rules of another role.
-
The user
SAP*in emergency mode is not checked at all, neither PFCG conditions nor literal ones. -
In an update session the check must be switched off with the annotation or with
WITH PRIVILEGED ACCESS; otherwise a runtime error occurs.
SELECT FROM zi_expclaim
FIELDS claimid, costcenter
INTO TABLE @DATA(all_claims)
OPTIONS PRIVILEGED ACCESS.
Requires an SAP system; not run here. The query ignores every access condition of ZI_ExpClaim. Keep it to code that
needs all rows for a technical reason, such as a posting step, and never to display data to a user. The documentation
adds that PRIVILEGED ACCESS should not be written without an OPTIONS clause, and that it cannot be combined with
WITH PRIVILEGED ACCESS of the FROM clause in the same statement.
What a program can and cannot tell
Access control changes the selection, not the program. A program that reads claims cannot tell whether a missing row does not exist or was removed by an access condition, so it must not report "claim not found" as a statement about the data. The extra filter also costs time, depending on how complex the condition and the protected entity are, how many values the user’s authorizations hold, and where the protected entity sits in the statement. Access controls apply to the entity that ABAP SQL reads directly; they do not travel up or down a stack of views by themselves.
Access control in RAP
In the ABAP RESTful Application Programming Model, the read access to a business object is protected with exactly this
DCL, on the CDS data model the business object is based on. The rules are checked per authorization master group, which
is the authorization master entity together with its authorization dependent entities, and authorizations may only get
stricter down the tree. For the annotation this means that #CHECK should not be used at all and #MANDATORY is used
instead; children of an entity with #NOT_REQUIRED must be #NOT_REQUIRED or #MANDATORY. Authorization control for
changes (create, update, delete, actions) is declared in the behavior definition and is a topic of the RAP pages.
Restrictions in ABAP Cloud
When customers and partners develop access controls in the SAP BTP ABAP environment or the SAP S/4HANA Cloud ABAP
environment, an access control can only use other objects that are in the same software component or released APIs with
the C1 contract. This concerns entities used as a source for PFCG mappings and user-defined aspects, association targets
in path expressions, inheritance sources and authorization objects. The protected entity itself must be in the same
software component as the access control. For entity-based inheritance from an entity of another component, the
replacement step ROOT WITH is mandatory and only a few other steps are allowed; the role-based form without a selector
(GRANT SELECT ON ... INHERIT ...) is not allowed.
Creating an access control in ADT
Access controls can only be edited in ABAP Development Tools for Eclipse (ADT), in an editor of their own that differs from the DDL editor. The workflow in the documentation is: define the CDS entity in its data definition, use the Access Control Wizard to generate a DCL template for it, then write the rules in the Access Control Editor, check the syntax and activate. The prerequisite is the standard developer authorization profile. If no access control exists and is activated for an entity, a user who can reach the entity sees all its data. The user guide also recommends protecting the application itself with classic start authorizations.
For releases before ABAP Cloud, the same concept is described in the on-premise administration documentation, which also
explains why a role can be defined and still ignored: the annotation is #NOT_ALLOWED, the CDS entity is used through
another entity, or the query uses WITH PRIVILEGED ACCESS. Source:
ABAP Authorization Concept for CDS Entities
Summary
-
CDS access control restricts the rows that ABAP SQL returns from a CDS entity; it is declared in DCL, next to the data model, and applied implicitly to every read.
-
A CDS role (
DEFINE ROLE, marked@MappingRole: true) applies to all users and holds access rules; each rule (GRANT SELECT ON) names one entity and either aWHEREcondition or full access. -
Conditions compare elements with literals and session variables, with the classic authorizations of the user (PFCG conditions with
ASPECT pfcg_auth), with the user, or with a user-defined aspect, and can be combined withAND,ORandNOT. -
There is no automatic inheritance between entities: each entity needs its own access control, or an inheritance condition that reuses another entity’s conditions.
-
@AccessControl.authorizationCheckchooses the behaviour:#MANDATORYprotects against a missing role,#NOT_ALLOWEDswitches the check off, andPRIVILEGED ACCESSswitches it off in a single query. -
Keep classic authorizations for start authorizations and use CDS access control for the instance-based check.