留言板

Permissions not refreshed after programatically adding a UserGroup

Marcin Dziób,修改在7 年前。

Permissions not refreshed after programatically adding a UserGroup

New Member 帖子: 2 加入日期: 17-2-8 最近的帖子
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,修改在7 年前。

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend 帖子: 2047 加入日期: 08-10-7 最近的帖子
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,修改在7 年前。

RE: Permissions not refreshed after programatically adding a UserGroup

New Member 帖子: 2 加入日期: 17-2-8 最近的帖子
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,修改在7 年前。

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend 帖子: 2047 加入日期: 08-10-7 最近的帖子
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,修改在7 年前。

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
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?