LRP-614 practice question 17 of 25

ThemeDisplay.getUserId() returns a value of -1 for the Guest user.

Full set: 144 questions
Answer options
A: True
B: False
Correct Answer:
FALSE
Explanation

The ThemeDisplay.getUserId() method returns the primary key of the User object associated with the current request. In Liferay, an unauthenticated (guest) session is associated with a specific, pre-configured user account known as the "default user" for that portal instance. This default user is a standard entity in the User database table and, consequently, has a unique, positive long value as its primary key. Therefore, for a guest user, ThemeDisplay.getUserId() returns this positive ID, not a sentinel value like -1.

Why Incorrect

The statement "True" is incorrect. While some programming contexts use negative numbers or zero to represent a special state like an unauthenticated user, this is not the convention for Liferay's ThemeDisplay.getUserId() API call. The method is designed to consistently return the primary key of the user context for the request, which for a guest is the company's default user—a real account with a positive ID.

References

1. Liferay Portal 6.2 Developer's Guide: In the "Users" section of Chapter 2, "Portal Basics," the guide explains the concept of the default user. It states, "Every portal instance has a default user. This is the user account that is used for guests (users who have not signed in)." This establishes that a guest session is mapped to a full-fledged user account, which by definition has a standard, positive primary key.

Source: Liferay, Inc. (2013). Liferay Portal 6.2 Developer's Guide. Page 50.

2. Liferay Portal 6.2 API Javadoc for ThemeDisplay: The documentation for the getUserId() method specifies its return type and purpose.

public long getUserId()

Returns the user ID of the user.

This indicates that the method returns the actual primary key (long) of the User object. The User object for a guest is the default user, which has a positive ID. The Javadoc does not specify any special return value like -1 for the guest case.

Source: Liferay Portal 6.2 CE Javadoc, com.liferay.portal.theme.ThemeDisplay interface documentation.

3. Liferay Portal 6.2 API Javadoc for Company: The Company object, which represents a portal instance, has a method to retrieve the default user's ID.

public long getDefaultUserId()

This method returns the primary key of the default user for the company. The ThemeDisplay object for a guest user is populated with this user's information, confirming that a positive long ID is used.

Source: Liferay Portal 6.2 CE Javadoc, com.liferay.portal.model.Company interface documentation.

Scroll to Top