ERPClimb logoERPClimb

Code examples · CDS views · Intermediate

CDS access control (DCL): show each user only the leave requests they may see

The problem

Managers may see leave requests only of employees in their area.

How we solve it

  1. 1Authorization objectZ_LEAVE (SU21)
  2. 2Switch on the checkview annotation
  3. 3DCL rolegrant select
  4. 4Projection inheritssecond role

The result

A user reads only requests of employees for whom their roles contain the authorization object value.

SAP objects in play

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

Managers may see leave requests only of employees in their area. The restriction must be enforced in the data layer, not only in one screen, for every view that consumers read.

2. How we design it

Enforce the rule at the data layer. We switch on the access check in the view, then add a DCL role that ties the view to an authorization object. The database read is automatically restricted to rows the user's authorizations allow, whatever the consumer.

Why we decided it this way

  1. 1Authorization in the view, not in each reportThe rule sits next to the data, so it does not depend on each screen. Every CDS view that consumers read needs its own role; the projection can inherit it in one line (last step).
  2. 2Authorization object with an employee fieldAdministrators grant access with ordinary roles (PFCG), the same way as for other SAP authorizations.
  3. 3Display activity 03The role grants read access only. Changes would need their own checks in the behavior.

Before you start

  • The view ZI_LeaveRequest from the managed RAP example.
  • An authorization object Z_LEAVE with the fields Z_EMPID and ACTVT, created in transaction SU21 (on-premise system).
  • Rights to create a role in PFCG for testing.

3. The code, step by step

Step 1 of 4: Create the authorization object

Define what an administrator can grant.

Shell · Transaction SU21 (steps)
Object class : ZHR
Object       : Z_LEAVE
Fields       : ACTVT   (03 = display)
               Z_EMPID (employee id, 8 characters)
Part by part (plain text)
  1. Object definition. Open SU21, create an object class (or use a Z class), then the object Z_LEAVE. Add the field ACTVT and a field Z_EMPID (you create it in SU20 with a data element of 8 characters). Save and activate. Then create a role in PFCG, add the object manually with ACTVT = 03 and Z_EMPID = the employee numbers you want, and assign the role to your test user.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

Transaction codes

4. The final outcome

A user reads only requests of employees for whom their roles contain the authorization object value.

What you should see

User with Z_EMPID = 00000042 sees only requests of employee 00000042. User with Z_EMPID = * sees all requests. User without the authorization sees no rows.

Try it and check the result

  • Activate both roles and the view. Log in with a test user that has the PFCG role with Z_EMPID = 00000042.
  • Open the view in Data Preview (F8): only rows of employee 00000042 appear.
  • Change the role to Z_EMPID = * and generate the profile; after relogin all rows appear.
  • Remove the PFCG role from the user but keep the DCL role: the preview returns no rows.

Common mistakes

  • Setting #CHECK without writing the role protects nothing: you only get a warning and all rows stay visible.
  • Testing as a developer with SAP_ALL hides the restriction. Test with a normal user.
  • The authorization check applies to reads through the view. A write path that bypasses it must be protected in the behavior too.
  • Do not rely on the UI to hide rows. The role is the real protection.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP CDS access control with PFCG authorizations (S/4HANA on-premise and private cloud). The DCL is the same in the BTP ABAP Environment; there the authorization object is created in ADT and assigned through IAM apps, business catalogs and business roles instead of SU21 and PFCG.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.