Skip to Content

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./wp-content/uploads/2014/03/1_413739.jpg

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

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

        1. Piyas Kumar Das Post author

          Hi Nimish,

          Thanks for the update and also mentioning the jar.

          Hi Kumar,

          I hope you got you new version or non deprecated option.

          Cheers

          Piyas

          (0) 

Leave a Reply