Hide and disable access to Liferay's 6.1 control panel.

It is often needed to hide control panel dockbar for simple users. This article I'll describe different ways to achieve this.

First we need to change theme little in order to hide dockbar, it can be done in following way.

Create new theme if you don't have theme ready.
In portal_normal.vm file, change following lines of code

#if ($is_signed_in)
#dockbar()
#end

in to following.

#if (($is_signed_in) && $permissionChecker.isCompanyAdmin($company_id))
 #dockbar()
#end


It will remove dockbar for all non admin users.

But user still can be able to access control panel by typing the following url 

http://localhost:8080/group/control_panel.

To avoid this, we can write costume hook to restrict user to doing so.

Let's check how this can be done:

In liferay-hook.xml add following lines of code.

<portal-properties>portal.properties</portal-properties>

Add portal.properties file to hook's src folder and add following line to it.

servlet.service.events.pre=my.event.portal.ControlPanelAccessPreAction

Create ControlPanelAccessPreAction.java in appropriate package and add following code.

package my.event.portal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.RoleServiceUtil;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;

/**
 * The ControlPanelAccessPreAction restricts access to Control panel of simple
 * users.
 */
public class ControlPanelAccessPreAction extends Action {

  /**
   * Instantiates a new control panel access pre action.
   */
  public ControlPanelAccessPreAction() {
 super();
  }

  /*
   * @see com.liferay.portal.kernel.events.Action#run(javax.servlet.http. HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  public void run(HttpServletRequest request,
      HttpServletResponse response) throws ActionException {
 try {

   ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
   if (GroupLocalServiceUtil.getGroup(themeDisplay.getLayout().getGroupId()).isControlPanel()) {

  User currentUser = UserServiceUtil.getUserById(themeDisplay.getUserId());
  if (!RoleServiceUtil.hasUserRole(currentUser.getUserId(),
           currentUser.getCompanyId(),
           "administrator",
           true)) {
    throw new PrincipalException("User " + request.getRemoteUser()
     + " can't access the control panel.");
  }
   
   }
 } catch (Exception ex) {
   throw new ActionException(ex);
 }
  }

}


Deploy the hook.
That's it, now you completely hide and restricted access to control panel.

Comments

Popular posts from this blog

Gradient Border Colors with CSS

10 Useful Libraries and Resources for Responsive Web Design

The Simplest Way To Center Elements Vertically And Horizontally