留言板

Guest and User object - possible a bug?

thumbnail
Sebastian Konkol,修改在14 年前。

Guest and User object - possible a bug?

Junior Member 帖子: 34 加入日期: 09-3-20 最近的帖子
Hi,
I came today to an interesting case. I'm analyzing roles in my portlet. I'm taking user object from portlet request and then taking roles from this object (as in snippet below). The interesting part is that there is no user object when no one is authenticated (but as I understand even then there is user - Guest).

Map userInfo = (Map) getPortletRequest().getAttribute(
				PortletRequest.USER_INFO);

		String givenName = (userInfo != null) ? (String) userInfo
				.get("liferay.user.id") : "";
		if (StringUtils.isNotBlank(givenName)) {
			this.user = UserLocalServiceUtil.getUser(Long.parseLong(givenName));
		}  

Maybe I dont understand something, but... shouldn't user object be ready with Guest role inside even if user is not logged?
Placing the null value in portlet request for guest user seems to be incostistent behavior.
Right now my workaround looks like this:

Map userInfo = (Map) getPortletRequest().getAttribute(
				PortletRequest.USER_INFO);

		String givenName = (userInfo != null) ? (String) userInfo
				.get("liferay.user.id") : "";
		if (StringUtils.isNotBlank(givenName)) {
			this.user = UserLocalServiceUtil.getUser(Long.parseLong(givenName));
		} else { 
			this.user = UserLocalServiceUtil.getDefaultUser(getCompanyId());

		}

and the companyId value is the placed within configuration of my portlet.

Nevertheless, can someone tell me, why Guests have null user object? I belive that this has some logical explanation emoticon but I just dont get it emoticon

I almost forgot- I'm using Liferay 5.2.3

Best regards
Sebastian Konkol