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.
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
hi piyas,
since the DC com.sap.security.api.sda is deprecated (~2007) you should use tc/je/usermanagement/api instead.
regards,
nice document
Anu
Hi Piyas,
Nice document but this process is deprecated can you also update or new process?
Regards
Kumar
Hi All,
currently used API used for NWDI 7.3 is com.sap.security.api.jar and also ume related jar as well.
the ume related jar is tc/je/usermanagement/api in nwds 7.3
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
Hi Piyas & Nimish,
Thanks a lot
Regards
Kumar