Pages

Monday 23 April 2012

Writing a utility class for finding Role of a User


                Writing a utility class for finding Role of a User


Hi everyone,
                This article is for developers quick reference  to create a Custom Util class which will enable the developer to use a single method and find user’s ROLE .
It internally uses Liferay provided API i.e. com.liferay.portal.service.RoleServiceUtil


Just copy below code into a class and use it anywhere you want.



import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;


/**
 * @Author Srikanth S
 * @Description Portlet provides All Roles dependencies for User.
 */
public class RoleUtil {
     
      private static Log _log = LogFactoryUtil.getLog(RoleUtil.class);
        /**
         * Checks whether the user is power user or not.
         *
         * @param user
         *            instance of user.
         * @return Boolean value true or false.
         */

        public static boolean isPowerUser(User user)
        {
                return isUserInRole(user, "Power User");
        }

        /**
         * Checks whether the user is administrator or not.
         *
         * @param user
         *            instance of user.
         * @return Boolean value true or false.
         */

        public static boolean isAdmin(User user)
        {
                return isUserInRole(user, "Administrator");
        }

        /**
         * Checks whether the user is guest or not.
         *
         * @param user
         *            instance of user.
         * @return Boolean value true or false.
         */

        public static boolean isUser(User user)
        {
                return isUserInRole(user, "User");
        }

        /**
         * Checks whether the user belongs to corresponding role.
         *
         * @param user
         *            instance of user.
         * @param role
         *            role of user.
         * @return Boolean value true or false.
         */

        private static boolean isUserInRole(User user, String role)
        {
                boolean flag = false;

                if (user == null) return false;

                try {
                      for (Role role1 : RoleServiceUtil.getUserRoles(user.getUserId())) {
                              if (role1.getName().equalsIgnoreCase(role)) {
                                      flag = true;
                                      break;
                              }
                      }
                }catch (SystemException se) {
                  if(_log.isDebugEnabled()){
                                    _log.debug("SystemException : "+ se.getMessage());
                               }
               }
                return flag;
        }
}






Now you can use this in any class or even in JSP’s directly as shown below.
Example :<init.jsp>

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%@page import="com.cisco.nci.mobileapps.common.util.RoleUtil"%>

<liferay-theme:defineObjects />
<portlet:defineObjects />

<%
      boolean isGuest = !themeDisplay.isSignedIn();
      boolean isAdmin = !isGuest && themeDisplay.getPermissionChecker().isCompanyAdmin();
      boolean isPowerUser = !isAdmin && RoleUtil.isPowerUser(user);
      boolean isUser = !isPowerUser && RoleUtil.isUser(user);
     
      String currentCommunityName = themeDisplay.getLayout().getGroup().getName();
      String pageName = themeDisplay.getLayout().getName(themeDisplay.getLocale()).toLowerCase();
     
      String ctxPath = request.getContextPath();
%>


Hope this was helpful

Regards
Srikanth S

1 comment: