Foren

Show different portlets if user is logged in or not

thumbnail
Manuel M., geändert vor 7 Jahren.

Show different portlets if user is logged in or not

New Member Beiträge: 9 Beitrittsdatum: 28.06.13 Neueste Beiträge
Requirement:
A customer has the requirement to show some Portlets on his Home-Page only when a user is logged in, some only when the user is logged out, ans some in both cases.

Solution-Attempt:
Since every user automatically gets the Guest Role, i thought about creating 2 Sections in my homepage layout and showing one only if no user is signed in or the user is Omniadmin (to configure the portlets ;-) ) and one showing only if there is a user logged in.

I appended a (simplified) layout template so you can reconstruct my problem.

Problem:
This mechansim works perfectly except for one unexpected behaviour:

When I drag a Portlet into the user section, all is fine, and when I drag it out or delete it from there, all seems fine, until i reload the page.
If i do, i see my portlet is either duplicated (drag into another dropzone) or it is only half deleted and I see "You dont have the permission to view ..."

So I have 2 questions:
1. Is this a bug or is this really a expected behaviour? (Liferay behaves like I described above on vanilla Liferay 6.2 EE & CE)
2. Is there another (better) way to achieve that some portlets are visible only for guests? (Setting the ext-property permissions.check.guest.enabled=false is a path I would like avoid as it seems to have to many drawbacks)
thumbnail
Andrew Jardine, geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Are these custom portlets? If the are then perhaps you can manage whether or not they render as part of the portlet configuration. You could create a config mode that has a drop down with the three options and then in your JSP use the preference that is set to determine whether or not to render the content.
thumbnail
Manuel M., geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

New Member Beiträge: 9 Beitrittsdatum: 28.06.13 Neueste Beiträge
Only some of them are custom portlets, i too have some asset publishers, webcontent display portlet so far.

I thought of what you suggested too, but I don't like it too much as it is not a very generic approach. And if the customer wants to use a media display portlet tomorrow I would have to adapt this as well...
thumbnail
Andrew Jardine, geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Agreed -- it only works for your custom portlets. Here is something else I just tried (out of curiosity).

1. Created a new site called sample (http://localhost:8080/web/sample)
2. Added a single public page called home (http://localhost:8080/web/sample/home)
3. On this page added the Hello World portlet
4. Create a new Regular Role called "Visible"
5. Created a new user (andrew) with no role assignments
6. Configured the Hello World portlet on the home page of the sample site removing Guest VIEW right and adding it VIEW for "Visible" role
7. Logged out
8. Accessed the page as a Guest user http://localhost:8080/web/sample/home -- and saw error message about no permissions to view portlet (see below for addressing this)
9. Logged in as andrew .. same thing, no permissions to view portlet
10. Logged in as the admin and assigned andrew the "Visible" role
11. Logged in again as andrew and was able to see the hello world portlet.

The issue that remained was seeing the nasty "you don't have permissions!" error. I set this property in my portal-ext

layout.show.portlet.access.denied=true


So it looks like maybe you can solve this problem just using Liferays permissioning system -- provided you have the right Roles setup and the right portlet permissions enabled.
thumbnail
Manuel M., geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

New Member Beiträge: 9 Beitrittsdatum: 28.06.13 Neueste Beiträge
Thanks for your 2nd answer. Not sure if I just misunderstood, but my problem isn't (case 1) to hide portlets from non logged in users (aka guests), but (case 2) to show a portlet for guests but hide it for logged in users.

Case 1 is very easily configurable with the Liferay permission system (and the setting to hide non accessible portlets), but case 2 seems impossible to me as every user has the guest permission. Since this is used throughout pages / portlets it is therefor not recommended to deactivate (in my first post the permissions.check.guest.enabled=false setting).
thumbnail
Andrew Jardine, geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Hi Manuel,

I see, apologies, I misunderstood your requirements. One of the problems often faced trying to help people in the forums is that we get very "generic" messages because people don't want to (or can't legally) discuss their current project work. What is written makes sense to them because they have the full context (including the parts not being shared) but to the rest of us they can read confusingly.

I think the reason you aren't finding a simple solution to your problem is because its a little out of the ordinary. Public pages are meant to store information that is public. A you said, hiding a portlet that is not public is easy enough but I have never come across something that an anonymous user could see that a signed in user cannot. In fact, that doesn't even make sense -- you trust the person you don't know less than the person you do?

If is possible for you to give us a concrete use case so that it's clearer? For example, if you show a generic banner to guest users, but you want a piece of content related to a users profile when they are signed in? Something like the Audience Targeting portlet would provide a possible solution for this kind of thing as it allows you to evaluate your user and show content accordingly.

So basically, what you are doing is out of the norm (as I am sure you guessed). The layout solution you are proposing I guess might work, but perhaps we could recommend an alternative if we understood more specifically what you were trying to do.
thumbnail
Manuel M., geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

New Member Beiträge: 9 Beitrittsdatum: 28.06.13 Neueste Beiträge
Yes, it is exactly like you described.

Examples are:
The guest sees some (ad/informational) banner in the place where the user sees his dashboard with new messages, as well the guest sees some content regarding to register himself (makes no sense to show the logged in user) etc.

And since there are many portlets already on the home page, the idea was to have some portlets showed to both, and when a portlet is not considered relevant for the guest or the user, simply to hide it.
thumbnail
Andrew Jardine, geändert vor 7 Jahren.

RE: Show different portlets if user is logged in or not

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Hi Manuel,

I would try having a look at the Audience Targeting portlet(s) from the marketplace (not sure if you have worked with it before). (https://web.liferay.com/marketplace/-/mp/application/43707761) .. there is an EE equivalent too if you are on that license.

Out of the box you get portlets that allow you to manage web content and dynamically set the item based on a user segment. One of the rules you can set for a use segment is whether or not they are logged in. if you want to hide something then perhaps you can just use an "empty web content" which should take no space on the page.

This won't immediately help you for custom portlets or Document Library instances etc, but I imagine that you coul duse the Audience Targeting API to add logic to your custom portlets to response based on a user segment. I've never actually tried that though (in all honesty). The Audience Targeting stuff is pretty powerful though so I would definitely check it out. It might allow you to solve you immediate needs via configuration rather than code.