Skip to Content
Author's profile photo Piyas Kumar Das

How to get Current logged in user and the roles assigned to the user.

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

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Christian Santej
      Christian Santej

      hi piyas,

      since the DC com.sap.security.api.sda is deprecated (~2007) you should use tc/je/usermanagement/api instead.

      regards,

      Author's profile photo Former Member
      Former Member

      nice document

      Anu

      Author's profile photo Former Member
      Former Member

      Hi Piyas,

      Nice document but this process is deprecated can you also update or new process?

      Regards

      Kumar

      Author's profile photo Nimisha Girijan
      Nimisha Girijan

      Hi All,

      currently used API used for NWDI 7.3 is com.sap.security.api.jar and also ume related jar as well.

      Author's profile photo Nimisha Girijan
      Nimisha Girijan

      the ume related jar is tc/je/usermanagement/api in nwds 7.3

      Author's profile photo Piyas Kumar Das
      Piyas Kumar Das
      Blog 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

      Author's profile photo Former Member
      Former Member

      Hi Piyas & Nimish,

      Thanks a lot

      Regards

      Kumar