Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Matthew Hanlon
API consistency in UserLocalServiceUtil and GroupLocalServiceUtil
December 9, 2013 11:57 AM
Answer

Matthew Hanlon

Rank: New Member

Posts: 2

Join Date: July 8, 2010

Recent Posts

I was directed here from LESA. I am using Liferay EE 6.1.20. I am getting what appears to be inconsistent results between UserLocalServiceUtil#hasGroupUser(long,long) and GroupLocalServiceUtil#hasUserGroup(long,long).

My assumptions:

* Group in this case refers to Site (Group is an overloaded term in the API).
* The User is a member of the Site, e.g. the user is listed in the User list for the site under Control Panel > Site Memberships > View Members.

Given these assumptions:

GroupLocalServiceUtil#hasUserGroup(long, long):
 1    /**
 2     * Returns <code>true</code> if the user is immediately associated with the
 3     * group, or associated with the group via the user's organizations,
 4     * inherited organizations, or user groups.
 5     *
 6     * @param  userId the primary key of the user
 7     * @param  groupId the primary key of the group
 8     * @return <code>true</code> if the user is associated with the group;
 9     *         <code>false</code> otherwise
10     * @throws SystemException if a system exception occurred
11     */
12    public boolean hasUserGroup(long userId, long groupId)


If the User is a member of the Site or associated via an Organization or inherited Organization or Group, this should return true. In my test case, the user is a member of the Site and it returns true.

UserLocalServiceUtil#hasGroupUser(long, long):
 1    /**
 2     * Returns <code>true</code> if the user is a member of the group.
 3     *
 4     * @param  groupId the primary key of the group
 5     * @param  userId the primary key of the user
 6     * @return <code>true</code> if the user is a member of the group;
 7     *         <code>false</code> otherwise
 8     * @throws SystemException if a system exception occurred
 9     */
10    public boolean hasGroupUser(long groupId, long userId)


If the User is a member of the Site, this should return true. In my test case, the user is a member of the Site, but this returns false.

The difference between these, to me, is the inheritance. Unless I am misunderstanding, then UserLocalServiceUtil#hasGroupUser(long, long) should be equivalent to GroupLocalServiceUtil#hasUserGroup(long, long, boolean) when the third argument in the latter is false. However:

Control Panel Script:
 1
 2userId = <user id>; // replace with real value
 3groupId = <group id>; // replace with real value
 4
 5test1 = Packages.com.liferay.portal.service.UserLocalServiceUtil.hasGroupUser(userId, groupId);
 6test2 = Packages.com.liferay.portal.service.GroupLocalServiceUtil.hasUserGroup(userId, groupId);
 7test3 = Packages.com.liferay.portal.service.GroupLocalServiceUtil.hasUserGroup(userId, groupId, false);
 8
 9out.println("UserLocalServiceUtil#hasGroupUser(long, long): " + test1);
10out.println("GroupLocalServiceUtil#hasUserGroup(long, long): " + test2);
11out.println("GroupLocalServiceUtil#hasUserGroup(long, long, boolean): " + test3);


Output:

1
2UserLocalServiceUtil#hasGroupUser(long, long): false
3GroupLocalServiceUtil#hasUserGroup(long, long): true
4GroupLocalServiceUtil#hasUserGroup(long, long, boolean): true


What am I misunderstanding? Is it simply that the "Group" referred to by each method is not the same object? In that case, can someone tell me what "Group" is referred to by UserLocalServiceUtil?

Regards,
Matt.
Amos Fong
RE: API consistency in UserLocalServiceUtil and GroupLocalServiceUtil
December 9, 2013 6:11 PM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1844

Join Date: October 7, 2008

Recent Posts

Hey Matt,

It think you're correct in thinking that the difference is the "inheritance". The API is a little confusing here because the checks should probably be all be in one place (in GroupLocalServiceImpl IMO).

From the code, UserLocalServiceUtil.hasGroupUser() should return true if the user is directly associated with the group.

So essentially
UserLocalServiceUtil.hasGroupUser() should be the same as GroupLocalServiceUtil.hasUserGroup(userId, groupId, false).

I think what may be your issue is that the UserLocalService call has the parameters in a different order...(more API confusion)

1    public boolean hasGroupUser(long groupId, long userId)
2        throws SystemException {
3
4        return groupPersistence.containsUser(groupId, userId);
5    }
Matthew Hanlon
RE: API consistency in UserLocalServiceUtil and GroupLocalServiceUtil
December 9, 2013 8:01 PM
Answer

Matthew Hanlon

Rank: New Member

Posts: 2

Join Date: July 8, 2010

Recent Posts

Amos,

Amos Fong:
I think what may be your issue is that the UserLocalService call has the parameters in a different order...(more API confusion)


Oh, man, I feel dumb. I really stared at that, too, because I thought that might have been it! Ok, I was just calling the UserLocalServiceUtil method with the args swapped. Thanks for the extra set of eyes on this.

Regards,
Matt.