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