Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member182534
Active Participant

Hi Reader,

This blog gives us the steps you need to perform to get the current logged in user and the roles assigned to the user.

This is a very common requirement we use in Web Dynpro Java.

Say for example depending on the back-end role assigned to the user should be able to view some specific details and perform specific operation.

Procedure:

Step 1 : Add "com.sap.security.api.sda" in to the dependencies of the WebDynpro Java DC.

Step 2 : Use the following code:


try {
   String roleId, uniqueName, displayName = null;
   IUser currentUser = WDClientUser.getCurrentUser().getSAPUser(); //Code to get current user
   Iterator<String> rolesAssignedToUser = currentUser.getRoles(true);//Code to retrieve all the assigned roles
  
   while (rolesAssignedToUser.hasNext()) {
  
   roleId = rolesAssignedToUser.next();//Code to read the role id
  
   displayName = UMFactory.getRoleFactory().getRole(roleId).getDisplayName();//Code to get the display name
  
   } 
   } catch (WDUMException e1) {
   e1.printStackTrace();
   } catch (UMException e) {
   e.printStackTrace();
   }

Step 3 : Use the following imports for the above code.


import com.sap.security.api.IUser;
import com.sap.security.api.UMException;
import com.sap.security.api.UMFactory;

I just hope this small code helps all. this definitely helped me in my project.

Happy to help

Cheers

Piyas

7 Comments
Labels in this area