Foren

Permissions not refreshed after programatically adding a UserGroup

Marcin Dziób, geändert vor 7 Jahren.

Permissions not refreshed after programatically adding a UserGroup

New Member Beiträge: 2 Beitrittsdatum: 08.02.17 Neueste Beiträge
Hello,

I've got such situation:

1. I programatically add User to UserGroup when he logs in - using a hook (custom authenticator).

UserGroupLocalServiceUtil.addUserUserGroup(newUser.getUserId(), group.getUserGroupId())


The UserGroup is already defined in the system, I'm not creating it programatically. This UserGroup is assigned to Role "A", which has View permission to my custom portlet and public page in site.

2. I check in Control Panel and this user has UserGroups assigned.

3. Later in portlet I'm checking if this user has permission to specific portlet and page:

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
        PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
PortletPermissionUtil.contains(portletData.getPlid(), portletData.getPortletId(), ActionKeys.VIEW) &&
LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.VIEW))


But both checks (for Portlet permission and Layout permission) returns false. Strange thing is, that it starts to work (both checks return true) after user changes language - even if he later changes language back to default. Logging out and logging in again doesn't work.

When I assign UserGroups manually from Control Panel everything is working just after login, without changing language.

What am I doing wrong? Should I somehow tell Liferay to refresh cached permissions or something?

I'm using Liferay 6.2 GA6.
thumbnail
Amos Fong, geändert vor 7 Jahren.

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend Beiträge: 2047 Beitrittsdatum: 07.10.08 Neueste Beiträge
Hi Marcin,

Yea it looks like a permission cache issue. addUserUserGroup() looks to be an autogenerated method from service build, so it only writes to persistence and nothing else.

You can either try this method instead which clears cache and reindexes:
setUserUserGroups(long userId, long[] userGroupIds)


Or you can manually clear cache yourself:
PermissionCacheUtil.clearCache(userId);


Take a look at the UserGroupLocalServiceImpl.java class for more info.
Marcin Dziób, geändert vor 7 Jahren.

RE: Permissions not refreshed after programatically adding a UserGroup

New Member Beiträge: 2 Beitrittsdatum: 08.02.17 Neueste Beiträge
Thank you very much for your help. I'm not working on cluster, so that wasn't the case, but refreshing permissions cache worked. Here is the code I'm using:

MethodKey methodKey = new MethodKey(ClassResolverUtil.resolveByPortalClassLoader("com.liferay.portal.security.permission.PermissionCacheUtil"),
                "clearCache", long[].class);
PortalClassInvoker.invoke(false, methodKey, new long[]{user.getUserId()});
thumbnail
Amos Fong, geändert vor 7 Jahren.

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend Beiträge: 2047 Beitrittsdatum: 07.10.08 Neueste Beiträge
Oh I didn't realize PermissionCacheUtil was in portal-impl.jar. I'd recommend to just call setUserUserGroups(long userId, long[] userGroupIds) cause it'd be easier to maintain and cleaner.
thumbnail
Juan Gonzalez, geändert vor 7 Jahren.

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend Beiträge: 3089 Beitrittsdatum: 28.10.08 Neueste Beiträge
Hi Marcin.

There is a known bug that can be reproduced in a similar use case than yours, but it only affects cluster nodes. Are you using a cluster, or permissions aren't refreshed even without having any cluster in the same node?