Skip to Content
Author's profile photo EDGAR MARTINEZ

Set and access current user roles – Fiori Launchpad on SAP Cloud Platform – Portal

Hello there,

In this blog we will have a quick review on how to create and assign custom roles in SAP Cloud Platform Portal and Fiori Launchpad Portal. We will also cover how to access this roles from a SAPUI5 / Fiori app.

Creating and assigning a user role

First, we should go to the service Portal overview page inside SAP Cloud Platform. In this page, we should make click on the Configure Portal option.

From the Roles section, we can go and create a new Custom role. In this case, it will be called Test_Role.

Once that we have created the role, we will assign a user to this role. We can do so by making click on the Assign option after selecting the new created role.

Now, we will have both the role and the user ready for the next step.

Next, we will need to enter our SAP Fiori Launchpad site, inside the portal service.

In the configuration cockpit, we should go to Roles, under the Content Management section. There, we should create the same role we created on the Portal configuration.

We must be careful of setting the Role ID with the same name as our previously created role.

We can now assign Catalogs and groups as required for this new role.

 

Accessing roles from a SAPUI5 app

To access the roles for a user we can include the following code inside our Component.js init method.

try{
			
  jQuery.sap.require("sap.ushell.cpv2.services.cloudServices.SiteService");

}catch(oException){
		
}

This will help us ensure we have the SiteService component loaded.

Next, we can create a SiteService object where we need to access the roles. In this example, the object is created inside the CreateContent method of the view.

        try {
            if (sap.ushell && sap.ushell.cpv2 && sap.ushell.cpv2.services && sap.ushell.cpv2.services.cloudServices && sap.ushell.cpv2.services.cloudServices.SiteService) {

                var oLocalSiteService = sap.ushell.cpv2.services.cloudServices.SiteService();
                var oRoles = oLocalSiteService.siteModel.getProperty("/roles");
                var oProperty;


                for (oProperty in oRoles) {

                    if (oRoles.hasOwnProperty(oProperty)) {
                        if (oProperty.toString() === "Test_Role"{
				/// Some action based on the Test_Role
                        }
                    }
                }


            }
        } catch (oException) {

        }

As you can see, we are able to access the user configured roles from this object. Next we could specify some defined behavior based on the user role.

 

I hope this blog might be helpful.

 

Thank you!

Assigned Tags

      14 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Raghavendra Kulkarni
      Raghavendra Kulkarni

      Hello,

      Thanks, I am not able to find the portal service in my factory account.

      Could you please help me here, what I need to do to get the service.

      Author's profile photo EDGAR MARTINEZ
      EDGAR MARTINEZ
      Blog Post Author

      Hi Raghavendra,

      You would normally find the portal service inside the Services tab of your Account (for trial accounts) or Subaccount (on productive cloud instances).

      If it is not enabled, you could make click on the service, then click the Enable button on the next screen.

      Please let me know if this procedure is useful, in case it is not, please share a screenshot and more details on your account type (no passwords or usernames are necessary).

      Thank you!

      Best Regards!

      Edgar.

      Author's profile photo Former Member
      Former Member

      Hello Edgar,

       

      Thanks for the very nice blog!!

       

      We have requirement wherein we need to change a role of a user i.e from role A to role B from our custom SAPUI5 app.

      Wanted to understand if this update operation is possible to achieve or can we only do a read of roles from SAPUI5 app.

       

      Thanks is advance!!

       

      Regards,

      Jigar Salecha

      Author's profile photo Ankit Verma
      Ankit Verma
      Hello,
      I got the same requirement to fetch the custom roles and I am using below code to load the site services
      try{

      jQuery.sap.require("sap.ushell.cpv2.services.cloudServices.SiteService");

      }catch(oException){

      } but I am getting an exception: "failed to load 'sap/ushell/cpv2/services/cloudServices/SiteService.js' from ../../resources/sap/ushell/cpv2/services/cloudServices/SiteService.js"

      I have also added the bootstrap code:

      <script src="../resources/sap/ushell_abap/bootstrap/abap.js"></script>

      but still cpv2 service is not availbable.

      Is this CPV2 service depends upon the Launchpad version or HANA cloud version? if not then how can I fix this error or is there any other way to fetch the roles?

      Author's profile photo EDGAR MARTINEZ
      EDGAR MARTINEZ
      Blog Post Author

      Hello Ankit,

       

      Something I saw is that you need to be running the application in the context of the Fiori Launchpad (that means, no directly from the WEBIDE) so you can have access to the site service instance.

       

      Please let me know if this helps!

       

      Thanks!

       

      Best Regards!

      Edgar.

      Author's profile photo Daniel Kullmann
      Daniel Kullmann

      Yes, this helped! The app really has to run in the FLP; running from WebIDE using FLP Sandbox doesn't work!

      Author's profile photo Inderpreet Singh
      Inderpreet Singh

      Is there any limitation on the  number of roles ?

      I am not getting all the roles assigned to my user id.

      Author's profile photo EDGAR MARTINEZ
      EDGAR MARTINEZ
      Blog Post Author

      Hello Inderpreet,

       

      Unfortunately, I'm not sure if there is a limitation regarding the number of roles you can retrieve from the ones that you have assigned to your user. Perhaps we could explore a bit more, which roles, in particular, are you unable to get?

       

      Thanks!

      Best Regards!

       

      Edgar.

      Author's profile photo Juan López
      Juan López
      Hello good afternoon, a question.
      
      
      
      
      I have 500 users and I must assign roles, there is an API to perform this process or it must be done one by one
      Author's profile photo EDGAR MARTINEZ
      EDGAR MARTINEZ
      Blog Post Author

      Hi Juan,

       

      I think it could possible using the SAP Cloud Platform Identity Authentication Service and to work at group rather than role level. Then making the mapping between these groups and the groups in SCP Cockpit.

      Here is a link to the API documentation that I hope might be helpful.

      https://help.sap.com/viewer/6d6d63354d1242d185ab4830fc04feb1/Cloud/en-US/9e36479e92ca4b0db75f085d4ab3aa23.html

      Thank you!

      Best Regards!

      Author's profile photo Arjun Singh Thakur
      Arjun Singh Thakur

      Hi Edgar,

      Does this approach work for Portal site in Cloud Foundry as well?

      Regards,

      Arjun

       

      Author's profile photo EDGAR MARTINEZ
      EDGAR MARTINEZ
      Blog Post Author

      Hi Arjun,

       

      I guess the API might be a bit different, since the authentication mechanism is not the same in CF as in Neo. Still, I think a good idea would be to explore the behavior at runtime to see if you can get access to the information the same way from the Launchpad side.

       

      Best Regards!

       

      Edgar

      Author's profile photo Miqueias Maia
      Miqueias Maia

      Dears, did u found something about that on CF?

      This approach doesn't work on FLP CF.

      Author's profile photo Juan Alcantara Castro
      Juan Alcantara Castro

      Hi,

      I'm having the same issue, were you able to find a solution?