Fórum

Assigning usergroups to a role in liferay 6.1.x

Koen De Jaeger, modificado 8 Anos atrás.

Assigning usergroups to a role in liferay 6.1.x

New Member Postagens: 13 Data de Entrada: 14/09/10 Postagens Recentes
The use case is simple. Imaging thousands of usergroups which names end with a hyphen and a rolename. For example usergroup1-ro. With 'ro' being the name of an existing role. I wanted to write a piece of code that simply goes through all those usergroups and assigns them to the corresponding role, based on that rolename suffix found in the usergroupname.

Little usergroups are coupled to a role at this point and what's weird is that UserGroupGroupRoleLocalServiceUtil.getUserGroupGroupRoles(userGroup.getUserGroupId()) always returns a result with the role that needs to be coupled. Then when I look up the usergroups for that role (control panel), the usergroup is NOT in the list. So how can this return a usergroupgrouprole for that exact role. 'alreadyCoupled' is never 'false'. There always comes back a usergroupgrouprole.

Another thing I don't get is the second parameter of addUserGroupGroupRoles. Seems redundant.

      
List<company> allCompanies = CompanyLocalServiceUtil.getCompanies(ALL_POS, ALL_POS);
for (Company company : allCompanies) {
    List<role> allRoles = RoleLocalServiceUtil.getRoles(company.getCompanyId());
    List<usergroup> userGroups = UserGroupLocalServiceUtil.getUserGroups(company.getCompanyId());
    for (UserGroup userGroup : userGroups) {
        int iBeforeRole = userGroup.getName().lastIndexOf("-");
        if (iBeforeRole == -1) {
            continue;
        }
        String roleNameFromUserGroupName = userGroup.getName().substring(iBeforeRole + 1);
        Role roleFromUserGroupName = getRoleByName(allRoles, roleNameFromUserGroupName);
        if (roleFromUserGroupName == null) {
            continue;
        }
        boolean alreadyCoupled = false;
        List<usergroupgrouprole> userGroupGroupRoles = UserGroupGroupRoleLocalServiceUtil.getUserGroupGroupRoles(userGroup.getUserGroupId());
        for (UserGroupGroupRole userGroupGroupRole : userGroupGroupRoles) {
            long roleId = userGroupGroupRole.getRoleId();
            Role role = getRoleById(allRoles, roleId);
            if (role.getName().equalsIgnoreCase(roleNameFromUserGroupName)) {
               alreadyCoupled = true;
               break;
            }
        }
        if (!alreadyCoupled) {
            UserGroupGroupRoleLocalServiceUtil.addUserGroupGroupRoles(
                userGroup.getUserGroupId(), 
                userGroup.getGroup().getGroupId(), 
                new long[]{roleFromUserGroupName.getRoleId()}
            );
        }
    }
}
</usergroupgrouprole></usergroup></role></company>
thumbnail
Amos Fong, modificado 8 Anos atrás.

RE: Assigning usergroups to a role in liferay 6.1.x (Resposta)

Liferay Legend Postagens: 2047 Data de Entrada: 07/10/08 Postagens Recentes
Hey Koen,

These names get confusing cause of the common "group" part in the name...but this method is actually "User Group" Group roles (or in the frontend, "Site Roles"). This service is pretty confusing and doesn't seem to validate everything either...

So for this method: UserGroupGroupRoleLocalServiceUtil.getUserGroupGroupRoles(userGroup.getUserGroupId());
When it returns something, that means that User Group is assigned somewhere to a Site Role (any site).

Another thing I don't get is the second parameter of addUserGroupGroupRoles. Seems redundant.

Some background, the "Group" table is actually a backing table for many entities like User, Organizations, Sites (same thing as group), and User Groups. The groupId is what is used to relate those entities to other entities like Layouts and Roles. What you're actually doing here is assigning your User Group to the groupId's site role of roleId.

I'm guessing your roles are probably "Regular" roles, in which case you should use:
GroupLocalServiceUtil.addRoleGroups(long roleId, long[] groupIds)
GroupLocalServiceUtil.hasRoleGroup(long roleId, long groupId)

Where groupId = userGroup.getGroup().getGroupId()

Hope that clears things up
Koen De Jaeger, modificado 8 Anos atrás.

RE: Assigning usergroups to a role in liferay 6.1.x

New Member Postagens: 13 Data de Entrada: 14/09/10 Postagens Recentes
Thanks for helping out. It works now.