0
votes

I'm struggling to find any way to retrieve roles inherited through groups.

I've created a group, mygroup, and a role, mygrouprole, assigned users to mygroup, and assigned mygroup to mygrouprole. When I click on a user, I can see they have inherited mygrouprole.

From my jsp page, I call com.myco.UserUtil.hasRole(Long userId, String role).

This works fine when the user is assigned directly to the role, but not transitively through a group.

 public static boolean hasRole(Long userID, String role) {
        boolean blnFound = false;
        try {

            List<Role> lstRoles = RoleServiceUtil.getUserRoles(userID);
            m_logger.info("RoleServiceUtil.getUserRoles");
            for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
                Role rlRole = iterator.next();
                m_logger.info(rlRole.getName());
                if (rlRole.getName().equals(role)) {
                    blnFound = true;
                    m_logger.debug("User " + Long.toString(userID) + " has role " + role);
                    break;
                }
            }
        } catch (Exception e) {
            ...
        }
}

So far I've tried a number of alternate functions, but none of them seem to be able to find the role.

public static boolean hasRole(Long userID, String role) {
        boolean blnFound = false;
        try {

            List<Role> lstRoles = RoleServiceUtil.getUserRoles(userID);
            m_logger.info("RoleServiceUtil.getUserRoles");
            for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
                Role rlRole = iterator.next();
                m_logger.info(rlRole.getName());
                if (rlRole.getName().equals(role)) {
                    blnFound = true;
                    m_logger.debug("User " + Long.toString(userID) + " has role " + role);
                    break;
                }
            }

            lstRoles = RoleLocalServiceUtil.getUserRoles(userID);
            m_logger.info("RoleLocalServiceUtil.getUserRoles");
            for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
                Role rlRole = iterator.next();
                m_logger.info(rlRole.getName());
                if (rlRole.getName().equals(role)) {
                    blnFound = true;
                    m_logger.debug("User " + Long.toString(userID) + " has role " + role);
                    break;
                }
            }

            lstRoles = RoleLocalServiceUtil.getUserRelatedRoles(userID,CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId());
            m_logger.info("RoleLocalServiceUtil.getUserRelatedRoles");
            for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
                Role rlRole = iterator.next();
                m_logger.info(rlRole.getName());
                if (rlRole.getName().equals(role)) {
                    blnFound = true;
                    m_logger.debug("User " + Long.toString(userID) + " has role " + role);
                    break;
                }
            }

            List<UserGroupRole> lstUGRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(userID);
            m_logger.info("UserGroupRoleLocalServiceUtil.getUserGroupRoles");
            for (Iterator<UserGroupRole> iterator = lstUGRoles.iterator(); iterator.hasNext();) {
                UserGroupRole rlRole = iterator.next();
                m_logger.info(rlRole.getRole().getName());
                if (rlRole.getRole().getName().equals(role)) {
                    blnFound = true;
                    m_logger.debug("User " + Long.toString(userID) + " has role " + role);
                    break;
                }
            }

            blnFound = UserGroupRoleLocalServiceUtil.hasUserGroupRole(
                userID,
                // TODO: this only works when there is one company
                CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
                role
            );
            m_logger.info("UserGroupRoleLocalServiceUtil.hasUserGroupRole:"+Boolean.toString(blnFound));

            blnFound = UserGroupRoleLocalServiceUtil.hasUserGroupRole(
                userID,
                // TODO: this only works when there is one company
                CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
                role,
                true
            );
            m_logger.info("UserGroupRoleLocalServiceUtil.hasUserGroupRole:"+Boolean.toString(blnFound));

            blnFound = RoleLocalServiceUtil.hasUserRole(
                userID,
                // TODO: this only works when there is one company
                CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
                role,
                true
            );
            m_logger.info("RoleLocalServiceUtil.hasUserRole:"+Boolean.toString(blnFound));

            blnFound = RoleLocalServiceUtil.hasUserRoles(
                userID,
                // TODO: this only works when there is one company
                CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
                new String[] {role},
                true
            );
            m_logger.info("RoleLocalServiceUtil.hasUserRoles:"+Boolean.toString(blnFound));
} catch (Exception e) {
            ...
        }
}

The UserGroupRoleLocalServiceUtil.getUserGroupRoles doesn't find any roles

1

1 Answers

0
votes

I would have expected a simpler/cleaner way to do this, maybe that exists, but I've found a workaround.

public static boolean hasRole(Long userID, String role) {
    boolean blnFound = false;
    try {

        // Get a list of all the roles directly assigned to a user
        List<Role> userRoles = RoleServiceUtil.getUserRoles(userID);

        // Get a list of all the user's groups and add any assigned roles to the role list
        List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(userID);
        for (Iterator<UserGroup> iterator = userGroupList.iterator(); iterator.hasNext();) {
            UserGroup group = iterator.next();
            m_logger.trace(group.getName());
            userRoles.addAll(RoleLocalServiceUtil.getGroupRoles(group.getGroupId()));
        }

        // Check to see if the role is in the list
        for (Iterator<Role> iterator = userRoles.iterator(); iterator.hasNext();) {
            Role rlRole = iterator.next();
            m_logger.debug(rlRole.getName());
            if (rlRole.getName().equals(role)) {
                blnFound = true;
                m_logger.debug("User " + Long.toString(userID) + " has role " + role);
                break;
            }
        }
    } catch (Exception e) {
        ...
    }
}

Essentially I combine the list of roles assigned directly to the user and the roles assigned to any of the user's groups, and then search that list for the specified role.