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

29 comments:

Unknown said...

Hi Kamalkant,
I am trying to do something similar. I am developing a mutual friends portlet. When i am on my own profile page, this portlet should be hidden but when i visit my friends profile page, I should see this portlet displaying mutual friends.
I tried doing the way you suggested but not able to get through.
I created a role "MutualConnectionsRole" and added View permissions for mutual-connections portlet, through the control panel.
When user A logs-in and navigates to his own profile page, i still see the portlet. I assigned MutualConnectionsRole to user A and also tried removing View permissions for the role. Also there are no other view permsissions assigned for power-user and user roles.
Can you pls. share some more info?

Unknown said...

check the permission on a portlet by clicking cofiguration see whether it has guest permission , power user , user view permission.
If yes remove them and test.
Thanks
KamalKant

rynam0 said...

Thank You!! It would seem to me that this should be the default behavior. Thanks again for the post.

Anonymous said...

I am looking for such type of informative news and i get through this blog so i am very much thankful to you for sharing such a great information.
- liferay clustering

Vinh Doan said...

Is there any way to hide a portlet at runtime? For example, we need a portlet show only if it matches a specific condition.
Thanks,
Shinobivn

Vinh Doan said...

Is there anyway to hide a portlet at runtime? I would like to hide a portlet if it matches a specific condition, is it possible?

Thanks.

Unknown said...

@Vinh-yes u can do it u need to put condition in render_portlet.jsp which is under html/portal. Search for line portletDisplay.setAccess(access);
and change as per your condition and in portal-ext.property keep layout.show.portlet.access.denied=false or u have to do is remove view permission for that portlet using ResourcePermissionLocalServiceUtil.removeResourcePermission(long companyId, java.lang.String name, int scope, java.lang.String primKey,long roleId, java.lang.String actionId)
before rendering portlet and in portal-ext.property keep layout.show.portlet.access.denied=false

Unknown said...

Hello kamalkant,
i am trying to hide some option of a portlet in community for community user,like remove,preference etc.i tried this article,but i was not able to see some option associated with portlet.can you help me in this.


http://content.liferay.com/4.0.0/docs/developers/ch07s03.html

Unknown said...

@puneet: Tell me what exactly you need then surely I will help you.

Unknown said...

thanks kamalkant,this is the post in liferay for my problem,please help me in this,i was not able to solve in last 20 days
http://www.liferay.com/community/forums/-/message_boards/message/8874713

sushruthp said...

Hi,

I followed the same thing you have mentioned above but the portlet still shows up for a user who does not have view permission. I user liferay 6.0.6.

Unknown said...

@puneet : You want for a particular portlet or for whole portal.

Viju said...

Hi,
Using Liferay 6.06
Even i need a help to hide a perticular portlet based on user role. ex: if admin then view the respective page and portlet else portlet should not be visible

Unknown said...

@Viju,
Follow the below steps:
1.set the above mentioned property in portal-ext.properties
i.e layout.show.portlet.access.denied=false

Step:2 You can write the code in render method . Code I have already mentioned above.
example:
ResourcePermissionLocalServiceUtil.removeResourcePermission(
themeDisplay.getCompanyId(), portlet.getPortletName(),
ResourceConstants.SCOPE_INDIVIDUAL, primKey, 10138,
ActionKeys.VIEW);

ResourcePermissionLocalServiceUtil.removeResourcePermission(
themeDisplay.getCompanyId(), portlet.getPortletName(),
ResourceConstants.SCOPE_INDIVIDUAL, primKey, 10701,
ActionKeys.VIEW);

here 10138 -- roleId of guest
10701 --- roleId of custom role

hope this helps you

Unknown said...

@Viju:
To portlet object and primKey use below code.
Portlet portlet = (Portlet) renderRequest
.getAttribute(WebKeys.RENDER_PORTLET);
String primKey = PortletPermissionUtil.getPrimaryKey(themeDisplay
.getPlid(), portlet.getPortletId());

Anonymous said...

Hi kamalkant rajput..

Regaridng Step 2 above - Can you tell me where these code changes are to be made?

Also, do you know when Liferay plan to fix this issue so that the permission functionality works out of the box?

Charlie.

Unknown said...

@charlie:Step 2 you have to write in render method of your portlet. In ext environment if we are making a portlet then in portlet-ext.xml , we mention portlet-class. In that class we have two action processAction for actionURL and render method for renderURL. In that render method we have to write this code.

Anonymous said...

Hi Kamalkant

I am trying to hide some portlets from the application menu based on my own permissions ( outside liferay). As such I want to be able to dynamically hide the portlets from the applications menu. Is this possible ? If the permissions are then changed - the user should dynamically see the new portlets in the "Add applications" menu.
I was thinking I can control this through the javascript layer ?
Would love to hear your insights.

thanks !
-karyn

Unknown said...

@karyn: which version of liferay you are using. In liferay 6 we have one layout_configuration under portlet in side that we have view.jsp which contains _getRelevantPortletCategory method you customize that method. E.g you have portlet A which supports action key "ADD" then you add one more condition if user don't have permission then empty block should execute.
That's it I think it should do.
Above thing you should do only when you are not using liferay permission otherwise in liferay "ADD TO PAGE" permission and "MANAGE PAGE" permission is there.

Anonymous said...

Hi Kamalkant
Thanks for the response - I am currently on liferay 5.2.3 - I will try out what you suggested.

tx !

-karyn

Sanjoy said...

Hi Kamalkant,

You mentioned above:

here 10138 -- roleId of guest
10701 --- roleId of custom role

Could you please tell me how and where can I find the role id of a particular role?

Thanks very much!

Unknown said...

@sanjoy: u will find that in role_ table

Sanjoy said...

Thanks kamalkant!
But I am using in memory HSQL. I am not sure how to view the tables. I have tried to create connection using eclipse Data Source Explorer. Couldn't configure the connection. Can any help me?

Regards,

Sanjoy

Unknown said...

@sanjoy:I mainly use mysql , regarding hsql u can see http://www.razorsql.com/features/hsqldb_features.html or

http://hsqldb.org/

Anonymous said...

Hi Kamalkant,

I am trying to add a hook modifying the render_portlet.jsp in order prevent a list of porlets on a page from rendering based on a condition.

How can I do this using render_portlet.jsp?

Thank you.!

Anonymous said...

Hi Kamalkant,
could you please help me on this query,i have published in liferay forum.
http://www.liferay.com/community/forums/-/message_boards/message/16524077

Bookshare@forall said...

Hi Kamalkant,
Could you please suggest/help me for this question i have raised in liferay forum

http://www.liferay.com/community/forums/-/message_boards/message/16524077

Unknown said...

Hi Kamalkant,

I need to hide menu(Home,sign out) for some users,
Is it possible ??

Unknown said...

@Shobit: sure based on role or group membership you should do it