Message Boards
Liferay 7/DXP Organization Role assignment issue
Liferay 7/DXP Organization Role assignment issue
New Member Posts: 20 Join Date: 8/22/16 Recent PostsHello folks,
I'm working on Liferay 7/dxp(Liferay Digital Experience Platform
7.0.10 GA1 Patch: de-71-7010, hotfix-8192-7010).
In my project I
have to assign organization role programatically.
I Used UserGroupRoleLocalServiceUtil for adding the organization
role.
Here is the code snip:
try{
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = themeDisplay.getUser();
//Getting organization
Organization organization = OrganizationLocalServiceUtil.fetchOrganization(themeDisplay.getCompanyId(), "ABC_ORG");
System.out.println(organization);
if(Validator.isNotNull(organization)) {
boolean isOrgAssigned = UserLocalServiceUtil.hasOrganizationUser(organization.getOrganizationId(), user.getUserId());
System.out.println("isOrgAssigned :: "+isOrgAssigned);
if(!isOrgAssigned) {
UserLocalServiceUtil.addOrganizationUser(organization.getOrganizationId(), user.getUserId());
}
Role orgRole = RoleLocalServiceUtil.fetchRole(themeDisplay.getCompanyId(), "ORG_USR");
System.out.println("orgRole :: "+orgRole);
if(Validator.isNotNull(orgRole)) {
boolean hasUserGroupRole = UserGroupRoleLocalServiceUtil.hasUserGroupRole(user.getUserId(), organization.getGroupId(), orgRole.getRoleId());
System.out.println("hasUserGroupRole :: "+hasUserGroupRole);
if(!hasUserGroupRole) {
//Getting orgGroup
Group orgGroup = GroupLocalServiceUtil.getOrganizationGroup(organization.getCompanyId(), organization.getOrganizationId());
long [] userIds = new long[]{user.getUserId()};
System.out.println("========>>>>>>>>>>>> Before call.....");
UserGroupRoleLocalServiceUtil.addUserGroupRoles(userIds, orgGroup.getGroupId(), orgRole.getRoleId());
System.out.println("========>>>>>>>>>>>> AFter call......");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
Here in above code I'm getting user from themeDisplay
I have
one Organization called "ABC_ORG".
I
have one organization role called "ORG_USR".
I want to assign "ORG_USR" Organization role to User for "ABC_ORG" organization.
While executing the above script till below line I'm getting logs.
========>>>>>>>>>>>>
Before call.....
But when
"UserGroupRoleLocalServiceUtil.addUserGroupRoles(userIds,
orgGroup.getGroupId(), orgRole.getRoleId());" line gets executed
remaining code will not executed. I dont get line
"========>>>>>>>>>>>>
AFter call......" in logs. There is no exception nothing.
After executing above code the site can't reach exception message appeared in browser.
After executing above code if I do clear dbcache from server administration. The reset of the code gets executed and I got
========>>>>>>>>>>>> AFter call.......
I tried all 3 apis available in UserGroupRoleLocalServiceUtil class for assigning organization role.
- addUserGroupRole(UserGroupRole userGroupRole)
- addUserGroupRoles(long userId, long groupId, long[] roleIds)
- addUserGroupRoles(long[] userIds, long groupId, long roleId)
For all of the above getting same issue.
I checked liferay source code for execution logic in
Service (line no 285) :UserGroupRoleLocalServiceImpl.java
Persistence (line no 2869):UserGroupRolePersistenceImpl.java
There we have cache cleaning logic that might causing issue.
Please someone help me out in this.
Thanks
Jiten Vaghela