cancel
Showing results for 
Search instead for 
Did you mean: 

SAP MDK: How to take user roles?

emorales
Explorer
252

Good afternoon community. I have an MDK application in which I need to access the roles of the logged in user to know which buttons or screens to show. I am making a basic example trying to show or hide a particular button. This role is linked to a rule.

Now I'm just printing a string in the app that returns me useful information. But in roles I only see an array with a single string called "openid".

emorales_0-1725994947424.png

I was expecting to see the "Admin" role which I added to my user, but I don't see it. Here's how I have it configured. This is the logic of what you are seeing:

emorales_3-1725995382391.png

emorales_1-1725995068128.png

emorales_2-1725995186237.png

I know that the role works because I am getting information from my CAP service that the Admin role needs. My problem now is how to access the list of roles that the user who logged in has so that I can use that information to my advantage and be able to show certain information to the users within the application. I appreciate any information you can provide me.

robinkuck
Product and Topic Expert
Product and Topic Expert

Hi @emorales,

an alternative approach is to enable feature flags via the Mobile Settings Exchange feature in SAP Mobile Services based on a rule-based activation. A rule can be the assignment of a specific UserGroup in the Corporate IDP, not role collection. Enabled feature flags can be retrieved and stored in for example AppClientData after the client launched using following code snippet:

/**
* @param {IClientAPI} clientAPI
*/
export default function LoadUserGroups(clientAPI) {
  let appId = clientAPI.evaluateTargetPath('#Application/#AppData/MobileServiceAppId');
  let featurePoliciesUrl = `/mobileservices/Storage/v1/runtime/application/${appId}/global/mobileservices/settingsExchange/featureVectorPolicies`;
  let params = {
      'method': 'GET'
  };
  return clientAPI.sendRequest(featurePoliciesUrl, params).then(r => {
      if (r && r.statusCode === 200 && r.content) {
          const featureFlags = JSON.parse(r.content.toString());
          if (featureFlags.allowedPolicies) {
              let userGroups = featureFlags.allowedPolicies.map(featurePolicy => featurePolicy.id);
              clientAPI.getAppClientData().UserGroups = userGroups;
          }
      }
  }).catch(oError => {
      return [];
  });
}

 

View Entire Topic
panushjain
Product and Topic Expert
Product and Topic Expert

Hi @emorales 

I'm sharing one way of doing it if the backend role plays a significant part in your application’s logic.

  1. Define the Backend Entity (UserInfoSet):
    • Create a backend entity, "UserInfoSet" that will store user role information. This entity will include at least the user identifier and the role name.
  2. Structure of UserInfoSet:
    • Example fields might include:
      • userId (unique identifier for the user)
      • roleName (the role assigned to the user)
  3. Fetching Role Information:

    • Implement a method that reads the role information from "UserInfoSet" when needed, such as during user initialization or session start i.e. in the OnSuccess of your InitializeOnline.action.
  4. Using Role Information:

    • Utilize the fetched role information to manage access or permissions within your application.

Thank you.