How to Visualize a SDK Liferay portlet in panel control with other roles than Administrator
Version Liferay-5.2.3
For a SDK portlet to be visualizer in panelControl we just need add two lines in lifeay-portlet.xml :
The first line indicates that this portlet will be available in the Control Panel under that category, and the second indicate the position of the portlet in that category.
But this portlet only will be visualized by administrators users, to define other roles wee need to create a class that extends BaseControlPanelEntry and there control the roles, wee must indicate this class in liferay-portlet.xml:
Then create the roles in liferay-portlet.xml:
The class that control the roles:
This class just gets the roles defined in portlet and verify if the user have some role defined in the portlet, this class search for organization roles in the user.
And now all the users with role "ROL_EDITOR" have the permission to see the portlet.
For a SDK portlet to be visualizer in panelControl we just need add two lines in lifeay-portlet.xml :
<control-panel-entry-category>content</control-panel-entry-category> <control-panel-entry-weight>12.0</control-panel-entry-weight>
The first line indicates that this portlet will be available in the Control Panel under that category, and the second indicate the position of the portlet in that category.
But this portlet only will be visualized by administrators users, to define other roles wee need to create a class that extends BaseControlPanelEntry and there control the roles, wee must indicate this class in liferay-portlet.xml:
<control-panel-entry-class> com.portlet.struts.TagsControlPanelEntry </control-panel-entry-class>
Then create the roles in liferay-portlet.xml:
<role-mapper> <role-name>ROL_EDITOR</role-name> <role-link>ROL_EDITOR</role-link> </role-mapper>
The class that control the roles:
package com.portlet.struts; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.liferay.portal.model.Organization; import com.liferay.portal.model.Portlet; import com.liferay.portal.model.Role; import com.liferay.portal.security.permission.PermissionChecker; import com.liferay.portal.service.OrganizationLocalServiceUtil; import com.liferay.portal.service.RoleLocalServiceUtil; import com.liferay.portlet.BaseControlPanelEntry; public class TagsControlPanelEntry extends BaseControlPanelEntry { private static Log log = LogFactory.getLog(TagsControlPanelEntry.class); public boolean isVisible(PermissionChecker permissionChecker, Portlet portlet) throws Exception { long groupId ; try { Listorg = OrganizationLocalServiceUtil.getUserOrganizations(permissionChecker.getUserId()); groupId = org.get(0).getGroup().getGroupId(); } catch (Exception e1) { log.error("Caught:" + e1); } String roles[] = new String[2]; try { roles = portlet.getRolesArray(); } catch (Exception e) { log.error("Caught:" + e); } List userRoles = null; try { userRoles = RoleLocalServiceUtil.getUserGroupRoles( permissionChecker.getUserId(), groupId); } catch (Exception e) { log.error("Caught:" + e); } try { for (int i = 0; i < roles.length; i++) { for (Role userRole : userRoles) { if (userRole.getName().equals(roles[i])) { return true; } } } } catch (Exception e) { log.error("Caught:" + e); } return false; } }
This class just gets the roles defined in portlet and verify if the user have some role defined in the portlet, this class search for organization roles in the user.
And now all the users with role "ROL_EDITOR" have the permission to see the portlet.