Thursday, December 5, 2013

Liferay admin detection, community admin, Organization admin



If you see any portlet, it used to have four button 1. Preference 2. Maximise 3. Minimise 4. Close.
Only those user who has admin privilege, can see those button or will have able to change preference.

If user has community admin/owner, organization admin/owner, they can also see those button on their respective page. So, there are many combination like
user will not have admin privilege but have community admin then which kind of button they can see.

Now, if you have to developer one button and condition is that only admin(super admin, comminity admin, organization admin) can see that button, then before showing
that button, you have to whether logged in user has admin facility or not.

Liferay has given one API by which you can easily find out, use below code:

public boolean isUserLiferayAdmin(){
      PortletContextImpl portletContext = (PortletContextImpl)context;
      ThemeDisplay themeDisplay= (ThemeDisplay)portletContext.getRequest().getAttribute(WebKeys.THEME_DISPLAY);
      PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
      Long groupId = themeDisplay.getLayout().getGroupId();
      boolean isAnyLiferayAdmin = false;
       try {
   isAnyLiferayAdmin = GroupPermissionUtil.contains(permissionChecker,groupId, ActionKeys.MANAGE_LAYOUTS);
    }catch (Exception e1) {
     isAnyLiferayAdmin = false;
    }
      return isAnyLiferayAdmin;
   }


you can write this method in BaseComponent.java, so all the user defined portlet can user this method.

Other way is also there to determine admin previlages:

try{
    List roles = user.getRoles();
    String isAdmin  = "false";
    for(Role role:roles){
    if(role.getName().equals("Administrator")) {isAdmin = "true";break;}
    }
   session.setAttribute("USER_GROUP",isAdmin);
  }catch(Exception e){
   log.error("Not able to fetch Liferay user group info");
  }