Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting user role in Java Portlets

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.

like image 891
webExplorer Avatar asked Jul 01 '26 18:07

webExplorer


1 Answers

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

like image 110
Gautam Avatar answered Jul 03 '26 08:07

Gautam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!