/**
   * Determines whether the specified principalId is in the recipient graph of this action request
   *
   * @param principalId the principal id to check
   * @return whether the specified principalId is in the recipient graph of this action request
   */
  public boolean isRecipientRoutedRequest(String principalId) {
    // before altering this method it is used in checkRouteLogAuthentication
    // don't break that method
    if (principalId == null || "".equals(principalId)) {
      return false;
    }

    boolean isRecipientInGraph = false;
    if (isReviewerUser()) {
      isRecipientInGraph = getPrincipalId().equals(principalId);
    } else if (isGroupRequest()) {
      Group group = getGroup();
      if (group == null) {
        LOG.error("Was unable to retrieve workgroup " + getGroupId());
      }
      isRecipientInGraph =
          KimApiServiceLocator.getGroupService().isMemberOfGroup(principalId, group.getId());
    }

    for (ActionRequestValue childRequest : getChildrenRequests()) {
      isRecipientInGraph = isRecipientInGraph || childRequest.isRecipientRoutedRequest(principalId);
    }

    return isRecipientInGraph;
  }
 public String getExceptionWorkgroupName() {
   Group exceptionGroup = getExceptionWorkgroup();
   if (exceptionWorkgroupName == null || exceptionWorkgroupName.equals("")) {
     if (exceptionGroup != null) {
       return exceptionGroup.getName();
     }
   }
   return exceptionWorkgroupName;
 }
  public ActionForward addDelegationMember(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
    RoleDocumentDelegationMember newDelegationMember = roleDocumentForm.getDelegationMember();

    // See if possible to add with just Group Details filled in (not returned from lookup)
    if (StringUtils.isEmpty(newDelegationMember.getMemberId())
        && StringUtils.isNotEmpty(newDelegationMember.getMemberName())
        && StringUtils.isNotEmpty(newDelegationMember.getMemberNamespaceCode())
        && StringUtils.equals(
            newDelegationMember.getMemberTypeCode(),
            KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE.getCode())) {
      Group tempGroup =
          KimApiServiceLocator.getGroupService()
              .getGroupByNamespaceCodeAndName(
                  newDelegationMember.getMemberNamespaceCode(),
                  newDelegationMember.getMemberName());
      if (tempGroup != null) {
        newDelegationMember.setMemberId(tempGroup.getId());
      }
    }

    // See if possible to grab details for Principal
    if (StringUtils.isEmpty(newDelegationMember.getMemberId())
        && StringUtils.isNotEmpty(newDelegationMember.getMemberName())
        && StringUtils.equals(
            newDelegationMember.getMemberTypeCode(),
            KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode())) {
      Principal principal =
          KimApiServiceLocator.getIdentityService()
              .getPrincipalByPrincipalName(newDelegationMember.getMemberName());
      if (principal != null) {
        newDelegationMember.setMemberId(principal.getPrincipalId());
      }
    }

    if (checkDelegationMember(newDelegationMember)
        && KRADServiceLocatorWeb.getKualiRuleService()
            .applyRules(
                new AddDelegationMemberEvent(
                    "", roleDocumentForm.getRoleDocument(), newDelegationMember))) {
      newDelegationMember.setDocumentNumber(roleDocumentForm.getDocument().getDocumentNumber());
      if (StringUtils.isEmpty(newDelegationMember.getDelegationTypeCode())) {
        newDelegationMember.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
      }
      roleDocumentForm.getRoleDocument().addDelegationMember(newDelegationMember);
      roleDocumentForm.setDelegationMember(
          roleDocumentForm.getRoleDocument().getBlankDelegationMember());
    }
    return mapping.findForward(RiceConstants.MAPPING_BASIC);
  }
 protected boolean checkDelegationMember(RoleDocumentDelegationMember newMember) {
   if (StringUtils.isBlank(newMember.getMemberTypeCode())
       || StringUtils.isBlank(newMember.getMemberId())) {
     GlobalVariables.getMessageMap()
         .putError(
             "document.delegationMember.memberId",
             RiceKeyConstants.ERROR_EMPTY_ENTRY,
             new String[] {"Member Type Code and Member ID"});
     return false;
   }
   if (MemberType.PRINCIPAL.getCode().equals(newMember.getMemberTypeCode())) {
     Principal principalInfo = getIdentityService().getPrincipal(newMember.getMemberId());
     if (principalInfo == null) {
       GlobalVariables.getMessageMap()
           .putError(
               "document.delegationMember.memberId",
               RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
               new String[] {newMember.getMemberId()});
       return false;
     } else {
       newMember.setMemberName(principalInfo.getPrincipalName());
     }
   } else if (MemberType.GROUP.getCode().equals(newMember.getMemberTypeCode())) {
     Group groupInfo = null;
     groupInfo = getGroupService().getGroup(newMember.getMemberId());
     if (groupInfo == null) {
       GlobalVariables.getMessageMap()
           .putError(
               "document.delegationMember.memberId",
               RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
               new String[] {newMember.getMemberId()});
       return false;
     } else {
       newMember.setMemberName(groupInfo.getName());
       newMember.setMemberNamespaceCode(groupInfo.getNamespaceCode());
     }
   } else if (MemberType.ROLE.getCode().equals(newMember.getMemberTypeCode())) {
     Role roleInfo = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
     if (roleInfo == null) {
       GlobalVariables.getMessageMap()
           .putError(
               "document.delegationMember.memberId",
               RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
               new String[] {newMember.getMemberId()});
       return false;
     } else {
       newMember.setMemberName(roleInfo.getName());
       newMember.setMemberNamespaceCode(roleInfo.getNamespaceCode());
     }
   }
   return true;
 }
 public String getIdForGroupName(String namespace, String groupName) {
   Group group =
       KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(namespace, groupName);
   if (group == null) {
     throw new RiceIllegalArgumentException(
         "Given namespace of '"
             + namespace
             + "' and name of '"
             + groupName
             + "' was invalid.  Failed to lookup a corresponding group ID.");
   }
   return group.getId();
 }
  protected boolean checkKimDocumentRoleMember(KimDocumentRoleMember newMember) {
    boolean memberExists = false;
    String memberName = null;
    String memberNamespace = null;

    if (StringUtils.isBlank(newMember.getMemberId())) {
      GlobalVariables.getMessageMap()
          .putError(
              "document.member.memberId",
              RiceKeyConstants.ERROR_EMPTY_ENTRY,
              new String[] {"Member ID"});
      return false;
    }

    if (MemberType.PRINCIPAL.getCode().equals(newMember.getMemberTypeCode())) {
      Principal pi = this.getIdentityService().getPrincipal(newMember.getMemberId());
      if (pi != null) {
        memberExists = true;
        memberName = pi.getPrincipalName();
        memberNamespace = "";
      }
    } else if (MemberType.GROUP.getCode().equals(newMember.getMemberTypeCode())) {
      Group gi = KimApiServiceLocator.getGroupService().getGroup(newMember.getMemberId());
      if (gi != null) {
        memberExists = true;
        memberName = gi.getName();
        memberNamespace = gi.getNamespaceCode();
      }
    } else if (MemberType.ROLE.getCode().equals(newMember.getMemberTypeCode())) {
      Role ri = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
      if (!validateRole(newMember.getMemberId(), ri, "document.member.memberId", "Role")) {
        return false;
      } else {
        memberExists = true;
        memberName = ri.getName();
        memberNamespace = ri.getNamespaceCode();
      }
    }

    if (!memberExists) {
      GlobalVariables.getMessageMap()
          .putError(
              "document.member.memberId",
              RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
              new String[] {newMember.getMemberId()});
      return false;
    }
    newMember.setMemberName(memberName);
    newMember.setMemberNamespaceCode(memberNamespace);
    return true;
  }
 public String getDisplayName() {
   if (isUserRequest()) {
     Person person = getPerson();
     if (person != null) {
       return person.getName();
     }
   } else if (isGroupRequest()) {
     Group group = getGroup();
     if (group != null) {
       return group.getName();
     } else {
       return getGroupId();
     }
   } else if (isRoleRequest()) {
     return getRoleName();
   }
   return "";
 }
  public boolean isRecipientRoutedRequest(Recipient recipient) {
    // before altering this method it is used in checkRouteLogAuthentication
    // don't break that method
    if (recipient == null) {
      return false;
    }

    boolean isRecipientInGraph = false;
    if (isReviewerUser()) {
      if (recipient instanceof KimPrincipalRecipient) {
        isRecipientInGraph =
            getPrincipalId().equals(((KimPrincipalRecipient) recipient).getPrincipalId());
      } else if (recipient instanceof KimGroupRecipient) {
        isRecipientInGraph =
            KimApiServiceLocator.getGroupService()
                .isMemberOfGroup(
                    getPrincipalId(), ((KimGroupRecipient) recipient).getGroup().getId());
      }

    } else if (isGroupRequest()) {
      Group group = getGroup();
      if (group == null) {
        LOG.error("Was unable to retrieve workgroup " + getGroupId());
      }
      if (recipient instanceof KimPrincipalRecipient) {
        KimPrincipalRecipient principalRecipient = (KimPrincipalRecipient) recipient;
        isRecipientInGraph =
            KimApiServiceLocator.getGroupService()
                .isMemberOfGroup(principalRecipient.getPrincipalId(), group.getId());
      } else if (recipient instanceof KimGroupRecipient) {
        isRecipientInGraph =
            ((KimGroupRecipient) recipient).getGroup().getId().equals(group.getId());
      }
    }

    for (ActionRequestValue childRequest : getChildrenRequests()) {
      isRecipientInGraph = isRecipientInGraph || childRequest.isRecipientRoutedRequest(recipient);
    }

    return isRecipientInGraph;
  }
 protected boolean validDuplicateGroupName(IdentityManagementGroupDocument groupDoc) {
   Group group = null;
   if (null != groupDoc.getGroupNamespace() && null != groupDoc.getGroupName()) {
     group =
         KimApiServiceLocator.getGroupService()
             .getGroupByNamespaceCodeAndName(
                 groupDoc.getGroupNamespace(), groupDoc.getGroupName());
   }
   boolean rulePassed = true;
   if (group != null) {
     if (group.getId().equals(groupDoc.getGroupId())) {
       rulePassed = true;
     } else {
       GlobalVariables.getMessageMap()
           .putError(
               "document.groupName",
               RiceKeyConstants.ERROR_DUPLICATE_ENTRY,
               new String[] {"Group Name"});
       rulePassed = false;
     }
   }
   return rulePassed;
 }
  @Test
  public void testCreateGroupWithCustomKimTypeAttributes() {
    // This test needs to be run in separate test class
    // with @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
    KimAttribute.Builder kimAttribute =
        KimAttribute.Builder.create(KIM_ATTRIBUTE_COMPONENT_NAME, KIM_ATTRIBUTE_NAME, NAMESPACE);
    kimAttribute.setAttributeLabel(KIM_ATTRIBUTE_LABEL);
    kimAttribute.setId(KIM_ATTRIBUTE_ID);
    kimAttribute.setActive(true);
    KimAttributeBo kimAddrBo =
        KRADServiceLocator.getDataObjectService().save(KimAttributeBo.from(kimAttribute.build()));

    KimType.Builder kimTypeBuilder = KimType.Builder.create();
    kimTypeBuilder.setId(KIM_TYPE_ID);
    kimTypeBuilder.setName(KIM_TYPE_NAME);
    kimTypeBuilder.setNamespaceCode(NAMESPACE);
    kimTypeBuilder.setServiceName(KIM_TYPE_SERVICE_NAME);
    kimTypeBuilder.setActive(true);
    KimTypeBo kimTypeBo =
        KRADServiceLocator.getDataObjectService().save(KimTypeBo.from(kimTypeBuilder.build()));

    KimTypeAttribute.Builder kimTypeAttributeBuilder = KimTypeAttribute.Builder.create();
    kimTypeAttributeBuilder.setId(KIM_TYPE_ATTRIBUTE_ID);
    kimTypeAttributeBuilder.setActive(true);
    kimTypeAttributeBuilder.setSortCode("a");
    kimTypeAttributeBuilder.setKimTypeId(KIM_TYPE_ID);
    kimTypeAttributeBuilder.setKimAttribute(kimAttribute);
    KimTypeAttributeBo kimTypeAttrBo =
        KRADServiceLocator.getDataObjectService()
            .save(KimTypeAttributeBo.from(kimTypeAttributeBuilder.build()));

    kimTypeBuilder =
        KimType.Builder.create(
            KimApiServiceLocator.getKimTypeInfoService().getKimType(KIM_TYPE_ID));
    kimTypeAttributeBuilder = KimTypeAttribute.Builder.create(kimTypeAttrBo);
    List<KimTypeAttribute.Builder> attrList = Collections.singletonList(kimTypeAttributeBuilder);
    kimTypeBuilder.setAttributeDefinitions(attrList);

    kimTypeBo =
        KRADServiceLocator.getDataObjectService().save(KimTypeBo.from(kimTypeBuilder.build()));
    KRADServiceLocator.getDataObjectService().flush(KimTypeBo.class);

    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put(KIM_ATTRIBUTE_NAME, KIM_ATTRIBUTE_VALUE);

    Group.Builder groupInfo = Group.Builder.create(NAMESPACE, GROUP_NAME, KIM_TYPE_ID);
    groupInfo.setAttributes(attributes);
    groupInfo.setActive(true);
    groupInfo.setDescription(KIM_ATTRIBUTE_LABEL + " " + KIM_ATTRIBUTE_VALUE);
    Group group = KimApiServiceLocator.getGroupService().createGroup(groupInfo.build());

    Group result =
        KimApiServiceLocator.getGroupService()
            .getGroupByNamespaceCodeAndName(NAMESPACE, GROUP_NAME);
    assertEquals(groupInfo.isActive(), result.isActive());
    assertTrue(groupInfo.getNamespaceCode().equals(result.getNamespaceCode()));
    assertTrue(groupInfo.getName().equals(result.getName()));
    assertTrue(groupInfo.getKimTypeId().equals(result.getKimTypeId()));
    assertEquals(1, result.getAttributes().size());
    assertTrue(result.getAttributes().get(KIM_ATTRIBUTE_NAME).contains(KIM_ATTRIBUTE_VALUE));
  }
 @Override
 public boolean isMemberOfGroup(String principalId, String namespaceCode, String groupName) {
   Group group = getGroupByName(namespaceCode, groupName);
   return group == null ? false : isMemberOfGroup(principalId, group.getId());
 }