0
votes

I need to get the list of Organization roles and Site roles that are present in my Liferay.

I have used the following below code to get Site Roles and Organization Roles

List<UserGroupRole> list =UserGroupRoleLocalServiceUtil.getUserGroupRoles(0,UserGroupRoleLocalServiceUtil.getUserGroupRolesCount());
                                  System.out.println("UserGroupRole list size  "+list.size());
                                  System.out.println("getUserGroupRolesCount  "+UserGroupRoleLocalServiceUtil.getUserGroupRolesCount());                        

                                  for (UserGroupRole roleObj : list) {

                                         if(roleObj != null && roleObj.getRole() != null ){
                                                if (RoleConstants.TYPE_ORGANIZATION==roleObj.getRole().getType() && RoleConstants.ORGANIZATION_ADMINISTRATOR.equals(roleObj.getRole().getName())) {
                                                       System.out.println("Role Name " + roleObj.getRole().getName() + " Role Id "+ roleObj.getRole().getRoleId() + " Role type " + roleObj.getRole().getType());

                                                       session.setAttribute(LoginConstants.ORGANIZATION_ADMINISTRATOR_ROLE_ID, roleObj.getRole().getRoleId(),PortletSession.APPLICATION_SCOPE);

                                                       } else 
                                                if (RoleConstants.TYPE_ORGANIZATION==roleObj.getRole().getType() && RoleConstants.ORGANIZATION_OWNER.equals(roleObj.getRole().getName())) {
                                                       System.out.println("Role Name " + roleObj.getRole().getName() + " Role Id "+ roleObj.getRole().getRoleId() + " Role type " + roleObj.getRole().getType());

                                                       session.setAttribute(LoginConstants.ORGANIZATION_OWNER_ROLE_ID, roleObj.getRole().getRoleId(),PortletSession.APPLICATION_SCOPE);

                                                       } else
                                                if (RoleConstants.TYPE_SITE==roleObj.getRole().getType() && RoleConstants.SITE_ADMINISTRATOR.equals(roleObj.getRole().getName())) {
                                                       System.out.println("Role Name " + roleObj.getRole().getName() + " Role Id "+ roleObj.getRole().getRoleId() + " Role type " + roleObj.getRole().getType());

                                                       session.setAttribute(LoginConstants.SITE_ADMINISTRATOR_ROLE_ID, roleObj.getRole().getRoleId(),PortletSession.APPLICATION_SCOPE);

                                                       } else 
                                                if (RoleConstants.TYPE_SITE==roleObj.getRole().getType() && RoleConstants.SITE_OWNER.equals(roleObj.getRole().getName())) {
                                                       System.out.println("Role Name " + roleObj.getRole().getName() + " Role Id "+ roleObj.getRole().getRoleId() + " Role type " + roleObj.getRole().getType());

                                                       session.setAttribute(LoginConstants.SITE_OWNER_ROLE_ID, roleObj.getRole().getRoleId(),PortletSession.APPLICATION_SCOPE);

                                                }
                                         }
                                  }

I was expecting the following 4 roles details (Role Id) - Organization Administrator - Organization Owner - Site Administrator - Site Owner

But I did not got "Site Administrator" role details ,the reason I did not got was this role was not assigned to any user , hence there was no "UserGroupRole" mapping present for the same. The UserGroupRole list size was 2776.

When I assigned a user the "Site Administrator" role , I was able to get it from the above code and the size of the UserGroupRole list was 2777.

Is there any other way so that I can get the List of all Organization roles and Site roles. Please help me and thanks for the same.

1
But why are you using UserGroupRole query only?Daniele Baggio
Hi @DanieleBaggio..Please can you advise an alternative solution for the same to get the list of all Organization roles and Site roles present in LiferayAnkur Mehrotra
I think RoleLocalServiceUtil.getRoles(companyId) will be the right way...Daniele Baggio
indentation by 56 characters, with only 1/3 of the code visible without scrolling in the desktop view? I bet you can do better... please edit your question to make the code readable.Olaf Kock

1 Answers

0
votes

Thanks @Daniele Baggio

I am able to get the 4 roles as below

  • Organization Administrator
  • Organization Owner
  • Site Administrator
  • Site Owner

Role orgAdmin=RoleLocalServiceUtil.getRole(companyId,RoleConstants.ORGANIZATION_ADMINISTRATOR);

Role orgOwner=RoleLocalServiceUtil.getRole(companyId,RoleConstants.ORGANIZATION_OWNER); Role siteAdmin=RoleLocalServiceUtil.getRole(companyId,RoleConstants.SITE_ADMINISTRATOR); Role siteOwner=RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_OWNER);