Ejemplo n.º 1
0
 public List<String> getLocationsForPrincipalInRole(
     String principalId,
     String namespaceCode,
     String roleName,
     DateTime asOfDate,
     boolean isActiveOnly) {
   List<String> locations = new ArrayList<String>();
   Role role = getRoleService().getRoleByNamespaceCodeAndName(namespaceCode, roleName);
   if (role != null) {
     locations =
         getLocationsForPrincipalInRoles(
             principalId, Collections.singletonList(role.getId()), asOfDate, isActiveOnly);
   }
   return locations;
 }
Ejemplo n.º 2
0
  public List<String> getDepartmentsForPrincipalInRole(
      String principalId,
      String namespaceCode,
      String roleName,
      DateTime asOfDate,
      boolean isActiveOnly) {
    List<String> departments = new ArrayList<String>();
    Role role = getRoleService().getRoleByNamespaceCodeAndName(namespaceCode, roleName);
    if (role != null) {
      departments =
          getDepartmentsForPrincipalInRoles(
              principalId, Collections.singletonList(role.getId()), asOfDate, isActiveOnly);
    }

    // TODO:
    // Do we want to pass groupKeyCode instead of location to speed up the performance?
    List<String> locations =
        getLocationsForPrincipalInRole(
            principalId, namespaceCode, roleName, asOfDate, isActiveOnly);
    departments.addAll(
        getDepartmentService().getDepartmentValuesWithLocations(locations, asOfDate.toLocalDate()));

    return new ArrayList<String>(departments);
  }
Ejemplo n.º 3
0
  public boolean principalHasRole(
      String principalId,
      String namespaceCode,
      String roleName,
      Map<String, String> qualification,
      DateTime asOfDate) {
    boolean principalHasRole = false;

    String roleId = getRoleService().getRoleIdByNamespaceCodeAndName(namespaceCode, roleName);

    if (roleId == null) {
      return false;
    }
    if (asOfDate.compareTo(LocalDate.now().toDateTimeAtStartOfDay()) == 0) {
      principalHasRole =
          getRoleService()
              .principalHasRole(principalId, Collections.singletonList(roleId), qualification);
    } else {
      List<RoleMember> roleMembers =
          getRoleMembers(namespaceCode, roleName, qualification, asOfDate, true);

      for (RoleMember roleMember : roleMembers) {
        if (MemberType.PRINCIPAL.equals(roleMember.getType())) {
          if (StringUtils.equals(roleMember.getMemberId(), principalId)) {
            principalHasRole = true;
            break;
          }
        } else if (MemberType.GROUP.equals(roleMember.getType())) {
          if (HrServiceLocator.getKPMEGroupService()
              .isMemberOfGroupWithId(principalId, roleMember.getMemberId(), asOfDate)) {
            // if (getGroupService().isMemberOfGroup(principalId, roleMember.getMemberId())) {
            principalHasRole = true;
            break;
          }
        } else if (MemberType.ROLE.equals(roleMember.getType())) {
          Role derivedRole =
              getRoleService()
                  .getRoleByNamespaceCodeAndName(
                      KPMENamespace.KPME_HR.getNamespaceCode(),
                      KPMERole.DERIVED_ROLE_POSITION.getRoleName());
          // check if the member represents the (nested) derived role 'position'
          if (derivedRole != null && roleMember.getMemberId().equals(derivedRole.getId())) {
            // add custom attributes
            Map<String, String> qual = new HashMap<String, String>();
            qual.putAll(roleMember.getAttributes());
            qual.put("asOfDate", asOfDate.toString());
            // return true if the principal id is a member of the (nested) derived role 'position'
            RoleTypeService roleTypeService = getRoleTypeService(derivedRole);
            if (roleTypeService.hasDerivedRole(
                principalId,
                new ArrayList<String>(),
                derivedRole.getNamespaceCode(),
                derivedRole.getName(),
                qual)) {
              principalHasRole = true;
              break;
            }
          }
        }
      }
    }

    return principalHasRole;
  }
Ejemplo n.º 4
0
  /**
   * Helper method to recursively search for role members.
   *
   * @param role The role
   * @param qualification The map of role qualifiers
   * @param asOfDate The effective date of the role
   * @param activeOnly or not to get only active role members
   * @return the list of role members in {@code role}.
   */
  private List<RoleMember> getRoleMembers(
      Role role, Map<String, String> qualification, DateTime asOfDate, boolean activeOnly) {
    List<RoleMember> roleMembers = new ArrayList<RoleMember>();

    if (asOfDate == null) {
      asOfDate = LocalDate.now().toDateTimeAtStartOfDay();
    }
    if (role != null) {
      RoleTypeService roleTypeService = getRoleTypeService(role);

      if (roleTypeService == null || !roleTypeService.isDerivedRoleType()) {
        List<RoleMember> primaryRoleMembers =
            getPrimaryRoleMembers(role, qualification, asOfDate, activeOnly);

        if (CollectionUtils.isNotEmpty(primaryRoleMembers)) {
          // flatten into constituent group and principal role members
          for (RoleMember primaryRoleMember : primaryRoleMembers) {
            if (MemberType.PRINCIPAL.equals(primaryRoleMember.getType())) {
              roleMembers.add(primaryRoleMember);
            } else if (MemberType.GROUP.equals(primaryRoleMember.getType())) {
              roleMembers.add(primaryRoleMember);
            } else if (MemberType.ROLE.equals(primaryRoleMember.getType())) {
              // recursive call to get role members
              Map<String, String> copiedQualification =
                  addCustomDerivedQualifications(
                      primaryRoleMember.getAttributes(), asOfDate, activeOnly);
              List<RoleMembership> memberships =
                  getRoleService()
                      .getRoleMembers(
                          Collections.singletonList(primaryRoleMember.getMemberId()),
                          copiedQualification);
              for (RoleMembership membership : memberships) {
                RoleMember roleMember =
                    RoleMember.Builder.create(
                            membership.getRoleId(),
                            membership.getId(),
                            membership.getMemberId(),
                            membership.getType(),
                            null,
                            null,
                            membership.getQualifier(),
                            "",
                            "")
                        .build();

                roleMembers.add(roleMember);
              }
            }
          }
        }
      } else {
        Map<String, String> qual =
            addCustomDerivedQualifications(qualification, asOfDate, activeOnly);
        List<RoleMembership> derivedRoleMembers =
            roleTypeService.getRoleMembersFromDerivedRole(
                role.getNamespaceCode(), role.getName(), qual);

        if (CollectionUtils.isNotEmpty(derivedRoleMembers)) {
          for (RoleMembership derivedRoleMember : derivedRoleMembers) {
            RoleMember roleMember =
                RoleMember.Builder.create(
                        derivedRoleMember.getRoleId(),
                        derivedRoleMember.getId(),
                        derivedRoleMember.getMemberId(),
                        derivedRoleMember.getType(),
                        null,
                        null,
                        derivedRoleMember.getQualifier(),
                        role.getName(),
                        role.getNamespaceCode())
                    .build();

            roleMembers.add(roleMember);
          }
        }
      }
    }

    return roleMembers;
  }