Monday, March 8, 2010

Hiding portlet in liferay

Requirement: Hide the portlet for which user is not having permission to view.

Simple Approach:
It is very difficult to hide or show portlet at user level. To get rid of these type of problem liferay provide us Roles. So we can create roles and on those roles we can easily assign permission on each portlet.
Creating role in liferay is very simple.
Just login as Admin and using dock navigate to control panel , click on Role from left panel and then click Add...for more details refer portal administration guide from liferay.
Once this is done click on define permission and select portlet permission.
e.g. Suppose you want to remove view permission from portlet A then uncheck view.

When you will login as user with that role then you will get message something like this.
"you don't have required permission to access this portlet"
Finally to make this portlet invisible for that role user who is not having required permission to view the portlet.In portal-ext.properties add this line
layout.show.portlet.access.denied=false

IPC-using public render parameter

In last post we have learned how to do coordination between 2 portlet commonly known as Inter Portlet Communication. In post I am going to describe IPC using public render parameter.

This is the simplest method for IPC given by JSR-286.

Description:

In JSR 168 (Portlet 1.0), the render parameters set in the processAction() method are available only in the render phase of the same portlet.

By using the public render parameters feature, the render parameters set in the processAction() method of one portlet are available in render parameters of the other portlets. Using public render parameters instead of events avoids the additional process event call. The public render parameters can also be set on the render URL.

To enable coordination of render parameters with other portlets within the same portlet application or across portlet applications, the portlet can declare public render parameters in its deployment descriptor using the public-render-parameter element in the portlet application section. Public render parameters can be viewed and changed by other portlets or components.

In the portlet section, each portlet can specify the public render parameters to be shared through the supported-public-render-parameter element. The supported-public-render-parameter element must reference the identifier of a public render parameter defined in the portlet application section in a public-render-parameter element.

Example: If we carry same old portlets which we have build in last example i.e for Event model IPC namely test1-portlet and test2-portlet.

Step 1:

Declare the render parameters to be shared in the portlet.xml file by setting the public render parameters at the portlet application level.

e.g:

<portlet-app ...>

<portlet> ... </portlet>

<public-render-parameter>

<identifier>user-id</identifier> <qname xmlns:x="http://abc.com/userId">x:userId</qname>

</public-render-parameter>

</portlet-app>

Note:

1.A developer can declare a list of public paramters for a portlet application in portlet.xml.

2.Parameter names are namespaced to avoid naming conflict.

Step 2:

Portlet must declare which public param they want to use.

e.g.

After adding declaration portlet.xml will look something like this.

<portlet-app ......>

<portlet>

<portlet-name>test1</portlet-name> <display-name>test1</display-name>

........ ........

<supported-public-render-parameter>

user-id

</supported-public-render-parameter>

</portlet>

<public-render-parameter>

<identifier>user-id</identifier> <qname xmlns:x="http://abc.com/userId">x:userId</qname>

</public-render-parameter>

</portlet-app>

Note: Public params are available in all lifecycle method like processAction , processEvent, render and serveResource.

Step 3: We can set render parameter in the processAction() method by using the defined public render parameter identifier as the key.

e.g.

public void processAction(ActionRequest request, ActionResponse response)

throws IOException, PortletException { ........ response.setRenderParameter("user-id", userId); ........

}

Step 4: A portlet can read public render parameter using follwing method

request.getPublicParameterMap()

Note: Public render parameters are merged with regular parameters so can also be read using

request.getParameter(name) or request.getParameterMap()

Step 5: A portlet can remove a public render parameter by invoking following methods.

response.removePublicRenderParameter(name)

or

portletURL.removePublicRenderParameter(name)