掲示板

Permissions not refreshed after programatically adding a UserGroup

7年前 に Marcin Dziób によって更新されました。

Permissions not refreshed after programatically adding a UserGroup

New Member 投稿: 2 参加年月日: 17/02/08 最新の投稿
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
7年前 に Amos Fong によって更新されました。

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend 投稿: 2047 参加年月日: 08/10/07 最新の投稿
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.
7年前 に Marcin Dziób によって更新されました。

RE: Permissions not refreshed after programatically adding a UserGroup

New Member 投稿: 2 参加年月日: 17/02/08 最新の投稿
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
7年前 に Amos Fong によって更新されました。

RE: Permissions not refreshed after programatically adding a UserGroup

Liferay Legend 投稿: 2047 参加年月日: 08/10/07 最新の投稿
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
7年前 に Juan Gonzalez によって更新されました。

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?