I'm working with liferay server to build a portal application using portlets.
I want to display some user specific data on the website.
How can I get the role of the logged in user in liferay portlet?
my sample java code -
@Override
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
String data = "";
if(userRole = "Admin") { //How to get this user role?
data = "Admin user logged in";
} else if(userRole = "Guest"){
data = "Guest user logged in";
}
request.setAttribute("data", data);
}
And JSP code -
<script>
function WhoLoggedIn() {
document.getElementById("gameForm").submit();
}
</script>
<div onClick="WhoLoggedIn()">Click HERE</div>
<div>${data}</data>
Thanks.
In your portlet's code you can use below method as defined in portlet specification:
portletRequest.isUserInRole("roleName");
Note: Liferay doesn't use the isUserInRole() method in any of the portlets provided by default. Instead it uses Liferay's permission System directly, to achieve more finegrained security.
For more information please refer to below Link
https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/security-and-permissio-4
you can use some method which are there in PermissionChecker.
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
permissionChecker.isCompanyAdmin() // Returns true if the user is an administrator of their company.
permissionChecker.isOmniadmin() // Returns true if the user is a universal administrator.
permissionChecker.isGroupAdmin(long groupId) // Returns true if the user is an administrator of the group.
for more information about PermissionChecker please refer to below link http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/security/permission/PermissionChecker.html
you can also explore RoleLocalServiceUtil class of Liferay
http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/service/RoleLocalServiceUtil.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With