Business Example
Before deploying to our productive SAP BTP environment, you want to ensure that only permitted users can access your app to view and edit data. Therefore, you will first add authorizations to your CAP service and then add two mock users to further test your app locally.
Exercise Options
You can perform this exercise in two ways:
- Live Environment – using the instructions provided below, you can perform the tasks in the SAP BTP Free Tier account
- Platform Simulation – follow the step-by-step instructions within the simulation
Prerequisite
Make sure that you have successfully completed the previous exercises. Alternatively, you can switch to the solution branch of the last exercise and continue here.
Steps
Add CAP Role Restrictions to Entities.
In this step, you will add authorizations to the
Risks
service. You will add two different rolesRiskManager
andRiskViewer
with different access scopes.Open the following file:
srv/risk-service.cds
.Replace the file content with the following code snippet to add the restrictions.
Code snippetExpandusing {riskmanagement as rm} from '../db/schema'; @path: 'service/risk' service RiskService @(requires: 'authenticated-user') { entity Risks @(restrict: [ { grant: 'READ', to : 'RiskViewer' }, { grant: [ 'READ', 'WRITE', 'UPDATE', 'UPSERT', 'DELETE' ], // Allowing CDS events by explicitly mentioning them to : 'RiskManager' } ]) as projection on rm.Risks; annotate Risks with @odata.draft.enabled; entity Mitigations @(restrict: [ { grant: 'READ', to : 'RiskViewer' }, { grant: '*', // Allow everything using wildcard to : 'RiskManager' } ]) as projection on rm.Mitigations; annotate Mitigations with @odata.draft.enabled; // BusinessPartner @readonly entity BusinessPartners as projection on rm.BusinessPartners; }
Save the file.
With this change, users who are assigned the role
RiskViewer
can view ("READ") risks and mitigations. Users who are assigned the roleRiskManager
can view and change risks and mitigations ("*").Add mock users for Local Testing.
Note
Since the authorization checks have been added to the CAP model, they apply not only when deployed to the cloud but also for local testing. Therefore, you will need a way to log in to the application locally.CAP allows you to add local users for testing as part of the
cds
configuration. For this, you can use the.cdsrc.json
file to add the mock users.Note
The
.cdsrc.json
file can be used to store project configurations, like in thepackage.json
file. Learn more here1.In the project, go to the file .cdsrc.json and open it for editing.
In the editor, replace its content with the following lines:
Code snippetExpand{ "[development]": { "auth": { "passport": { "strategy": "mock", "users": { "risk.viewer@tester.sap.com": { "password": "initial", "ID": "riskviewer", "userAttributes": { "email": "risk.viewer@tester.sap.com" }, "roles": ["RiskViewer"] }, "risk.manager@tester.sap.com": { "password": "initial", "ID": "riskmanager", "userAttributes": { "email": "risk.manager@tester.sap.com" }, "roles": ["RiskManager"] } } } } } }
Save the file.
The file defines two users,
riskviewer
andriskmanager
. Let's take a look at theriskmanager
example.The user is defined by an
ID
, which can be any identifier for a user. The user has anemail
, apassword
parameter, and aroles
parameter.
Try to access the application with the mock user.
Note
When accessing the
Risks
or theMitigations
service in the browser, you get a basic authorization pop-up window, asking for your user and password. Note, that you can not log out easily. For this you would have to restart your browser. You can use both users that you defined in the previous step to log in and see how this works.In the tab with the running application, navigate back to the
launchpad.html
page.Choose the Risks tile and in the app and choose Go.
In the pop-up, enter the Usernameriskmanager.
Enter the Passwordinitial.
You can now access the Risks application.
Platform Simulation
Choose the Start button (shown in the following figure) to open a simulation of the platform. Then follow the step-by-step instructions to add authorizations and mock users.
ExerciseStart Exercise
Result
You enabled authentication by adding annotations to your service definition. You also added mock users for local testing.