Sub topic 10
Restricting roles by organizational values
This chapter assumes you know what a business role and a business catalog are, and that you can read an authorization object with its fields and activities. If a role gives a user the right to open an app, a restriction decides which company codes, plants or sales organizations that user may work with inside the app.
Why restrictions exist
A business role such as Accounts Payable Accountant is written once and used by many people. A user in Germany should post invoices only for company code 1010. A user in France should post them only for 2010. Writing two roles with different catalogs would be wasteful and hard to keep consistent. Instead, you keep one role and restrict it by organizational values.
An organizational value is a value of a field that describes a unit of the enterprise structure. Typical fields are:
| Field | Meaning | Example authorization object |
|---|---|---|
| BUKRS | Company code | F_BKPF_BUK |
| VKORG | Sales organization | V_VBAK_VKO |
| WERKS | Plant | M_BEST_WRK |
| EKORG | Purchasing organization | M_BEST_EKO |
Restrictions control the data scope. They do not change which apps appear on the launchpad. That is decided by the catalogs in the role.
How a restriction reaches the authorization check
The restriction is stored with the role. When the user opens an app, the app or its OData service runs an authority check, and that check compares the value in the data with the values the role grants.
The same field can appear in many authorization objects. A restriction on company code therefore usually reaches every object in the role that has the field BUKRS.
Two ways to maintain restrictions
How you maintain restrictions depends on the product edition.
PFCG with organizational levels
In an on-premise or private cloud system, the role is a PFCG role. Organizational fields such as BUKRS, VKORG and WERKS are defined as organizational level fields. In PFCG you open the Authorizations tab and choose the organizational levels function. You enter the values once, and PFCG fills them into every authorization that uses the field. In the authorization tree, such a field shows a variable name that begins with a dollar sign, for example $BUKRS.
This design has an important consequence. If you maintain the value once at the organizational level, you cannot accidentally leave one object with a different value. If you overwrite a single authorization manually, that field is no longer filled from the organizational level. Doing this is a common source of inconsistent roles.
Restriction types on business roles
In SAP S/4HANA Cloud, and in systems that use the Maintain Business Roles app, the role editor offers restrictions on the assigned catalogs and apps. Each restriction belongs to a restriction type. The three types you normally meet are:
- Read: the user may display data for the listed values.
- Write: the user may create, change or delete data for the listed values.
- Value help: the user sees only the listed values in input help.
For each type, the restriction applies to a restriction field, such as Company Code. You choose whether the field is unrestricted or limited to specific values. The values you enter, such as 1010 and 1020, are what the generated authorizations contain.
Read and Write are separate on purpose. A user may read data of all company codes for reporting, but post only in their own.
A worked example
Suppose your company has three company codes: 1010, 1020 and 2010. You need two groups of users.
- Group A posts and displays in 1010 and 1020.
- Group B displays everything, but posts only in 2010.
You can build this from one business role template by copying it twice:
- Copy the template to
Z_AP_ACCOUNTANT_DEand set the Write restriction for Company Code to1010and1020. Set the Read restriction to the same values. - Copy it to
Z_AP_ACCOUNTANT_FR. Set the Write restriction to2010and leave the Read restriction unrestricted. - Assign each role to the matching users.
Both roles contain the same catalogs, so the launchpad looks the same for both groups. Only the data scope differs.
Testing a restriction in code
A restriction is only a stored value until some code checks it. This ABAP report lists which company codes the current user may display. It uses the same standard object as the chapter on authority checks and is a quick way to see the effect of a restriction.
REPORT zdemo_bukrs_check.
SELECT bukrs FROM t001 INTO TABLE @DATA(lt_bukrs).
LOOP AT lt_bukrs INTO DATA(ls_bukrs).
AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'
ID 'BUKRS' FIELD ls_bukrs-bukrs
ID 'ACTVT' FIELD '03'.
IF sy-subrc = 0.
WRITE: / ls_bukrs-bukrs, 'display allowed'.
ELSE.
WRITE: / ls_bukrs-bukrs, 'display not allowed'.
ENDIF.
ENDLOOP.Run it as a test user who has the Group A role. You should see 1010 and 1020 as allowed and 2010 as not allowed. If you see all company codes allowed, look for another role of the user that grants * for BUKRS. Authorizations from all of a user's roles add up, so one unrestricted role cancels every restriction elsewhere.
Wildcards, ranges and empty values
The values you can enter follow the rules of authorization fields.
| Entry | Effect |
|---|---|
| `1010` | Exactly this value |
| `1010` to `1999` (interval) | All values in the range, compared as text |
| `1*` (pattern) | All values that start with `1` |
| `*` | All values, including future ones |
| Empty field (no value) | Only objects that check for an empty value, which is rarely what you want |
Two details deserve attention. First, intervals compare characters, not numbers, so 900 to 1000 does not behave as you might expect for numeric strings of different length. Second, * is the fastest way to give access to company codes you create next year. That makes it convenient and also risky.
Common errors
- Unrestricted values from another role. The user has two roles, and one has
*for the field. The user can then reach all values. Compare the roles in the user's authorization profile. - Field not filled by the organizational level. Someone edited one authorization manually in PFCG. The field shows a literal value instead of the variable, so a later change of the organizational level does not update it. Reset the field to take its value from the organizational level.
- Restriction on the wrong type. You restricted Write but left Read unrestricted and then wondered why users can still display other company codes. Check each type.
- Missing value in a secondary field. An app checks both company code and, for example, accounting document type. A restriction on company code alone does not fix a failure on the second field. SU53 shows the field that failed.
- Role changed but user unchanged. After you change a restriction, regenerate the profile if your system requires it and run the user comparison. Users may keep the old values until then.
Restrictions versus other limits
Restrictions are not the only way to limit data. CDS access controls can also grant rows by organizational value, using the same authorization objects. A role restriction on VKORG therefore affects both the classic authority check and the CDS access control that refers to V_VBAK_VKO. A restriction in the role is the right place to maintain values, because it is one place and it works for both techniques. Hard-coded values in an access control are harder to maintain and should be avoided.
Exercise
A user holds two business roles. Role A restricts Company Code to 1010. Role B grants Company Code * for the same read authorization. Which company codes can the user display, and how would you fix this if the user should see only 1010?
Show answer
The user can display all company codes, because authorizations from all roles add up and * includes every value. To fix it, remove or restrict the * value in Role B, or remove Role B from the user if it is not needed. Then check the result with SU53 or a test such as the authority check report in this chapter.
Exercise
Why is it better to maintain a company code in PFCG through the organizational level than by editing the authorization field in each object?
Show answer
The organizational level fills the same value into every authorization that has the field, shown as a variable such as $BUKRS. One change updates them all. If you edit one object by hand, that object keeps its own value and no longer follows the organizational level, so the role becomes inconsistent.