@Ignore
 @Test(expected = WrongReferenceAttributeValueException.class)
 public void notAbleToSetIfNotHaveGroupName() throws Exception {
   System.out.println("IsUnixGroup.notAbleToSetIfNotHaveGroupName");
   groupGID.setValue(null);
   groupGroupName.setValue(null);
   perun.getAttributesManagerBl().setAttribute(sess, resource, group, groupGID);
   perun.getAttributesManagerBl().setAttribute(sess, resource, group, groupGroupName);
   groupIsUnixGroup.setValue(1);
   perun.getAttributesManagerBl().setAttribute(sess, resource, group, groupIsUnixGroup);
 }
 @Ignore
 @Test
 public void automaticGenerationOfGID() throws Exception {
   System.out.println("IsUnixGroup.automaticGenerationOfGID");
   groupGID.setValue(null);
   groupGroupName.setValue("Test");
   perun.getAttributesManagerBl().setAttribute(sess, resource, group, groupGID);
   perun.getAttributesManagerBl().setAttribute(sess, resource, group, groupGroupName);
   groupIsUnixGroup.setValue(1);
   perun.getAttributesManagerBl().setAttribute(sess, resource, group, groupIsUnixGroup);
   assertNotNull(
       perun
           .getAttributesManagerBl()
           .getAttribute(sess, resource, group, A_GR_unixGID)
           .getValue());
 }
  private Attribute setUpUserAttributeWithStringValue() throws Exception {

    Attribute attr = new Attribute();
    attr.setNamespace("urn:perun:user:attribute-def:opt");
    attr.setFriendlyName("user_string_test_attribute");
    attr.setType(String.class.getName());
    attr.setValue("UserStringAttribute test value");
    assertNotNull(
        "unable to create user attribute",
        perun.getAttributesManagerBl().createAttribute(sess, attr));
    // create new resource member attribute
    return attr;
  }
示例#4
0
 private static Attribute createAttribute(Map<String, String> beanAttr)
     throws InternalErrorException {
   if (beanAttr == null) return null;
   Attribute attribute = new Attribute();
   attribute.setId(Integer.valueOf(beanAttr.get("id")).intValue());
   attribute.setFriendlyName(BeansUtils.eraseEscaping(beanAttr.get("friendlyName")));
   attribute.setNamespace(BeansUtils.eraseEscaping(beanAttr.get("namespace")));
   attribute.setType(BeansUtils.eraseEscaping(beanAttr.get("type")));
   attribute.setValue(
       BeansUtils.stringToAttributeValue(
           BeansUtils.eraseEscaping(beanAttr.get("value")), attribute.getType()));
   return attribute;
 }
  private Attribute setUpUserLargeAttributeWithMapValue() throws Exception {

    Attribute attr = new Attribute();
    attr.setNamespace("urn:perun:user:attribute-def:opt");
    attr.setFriendlyName("user_map_test_large_attribute");
    attr.setType(LinkedHashMap.class.getName());
    Map<String, String> value = new LinkedHashMap<String, String>();
    value.put("UserLargeAttribute", "test value");
    attr.setValue(value);
    assertNotNull(
        "unable to create user attribute",
        perun.getAttributesManagerBl().createAttribute(sess, attr));
    return attr;
  }
  private Attribute setUpUserAttributeWithListValue() throws Exception {

    Attribute attr = new Attribute();
    attr.setNamespace("urn:perun:user:attribute-def:opt");
    attr.setFriendlyName("user_list_test_attribute");
    attr.setType(ArrayList.class.getName());
    List<String> value = new ArrayList<String>();
    value.add("UserStringAttribute test value");
    value.add("UserStringAttribute2 test2 value2");
    attr.setValue(value);
    assertNotNull(
        "unable to create user attribute",
        perun.getAttributesManagerBl().createAttribute(sess, attr));
    // create new resource member attribute
    return attr;
  }
  @Override
  public Attribute getAttributeValue(
      PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition)
      throws InternalErrorException {
    Attribute attribute = new Attribute(attributeDefinition);
    HashMap<String, String> organizationsWithLoa = new LinkedHashMap<String, String>();

    List<UserExtSource> extSources =
        sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
    if (extSources == null || extSources.isEmpty())
      return attribute; // If no userExtSources, so no Loa for any of them.

    String version = attributeDefinition.getFriendlyNameParameter();
    if (version == null)
      throw new InternalErrorException(
          "There is no parameter (cs or en) in attribute " + attributeDefinition);

    UserExtSource userExtSourceForCreating = null;
    UserExtSource userExtSourceForModifiing = null;

    // Initialize MapOfExtSource
    initializeMapOfExtSourceName();

    for (UserExtSource uES : extSources) {
      String uEName = uES.getExtSource().getName();
      String uELoa = String.valueOf(uES.getLoa());

      if (uES.getCreatedAt() != null) {
        Date testingDate = null;
        Date lastUsedDate = null;
        boolean parsed = true;
        try {
          testingDate = BeansUtils.DATE_FORMATTER.parse(uES.getCreatedAt());
        } catch (Exception ex) {
          // Not Parsed correctly
          parsed = false;
        }
        if (parsed) {
          if (userExtSourceForCreating == null || userExtSourceForCreating.getCreatedAt() == null)
            userExtSourceForCreating = uES;
          else {
            try {
              lastUsedDate =
                  BeansUtils.DATE_FORMATTER.parse(userExtSourceForCreating.getCreatedAt());
              if (testingDate != null && testingDate.compareTo(lastUsedDate) < 0) {
                userExtSourceForCreating = uES;
              }
            } catch (Exception ex) {
              // Not Parsed correctly
              userExtSourceForCreating = uES;
            }
          }
        }
      }

      if (uES.getModifiedAt() != null) {
        Date testingDate = null;
        Date lastUsedDate = null;
        boolean parsed = true;
        try {
          testingDate = BeansUtils.DATE_FORMATTER.parse(uES.getModifiedAt());
        } catch (Exception ex) {
          // Not Parsed correctly
          parsed = false;
        }
        if (parsed) {
          if (userExtSourceForModifiing == null
              || userExtSourceForModifiing.getModifiedAt() == null) userExtSourceForModifiing = uES;
          else {
            try {
              lastUsedDate =
                  BeansUtils.DATE_FORMATTER.parse(userExtSourceForModifiing.getModifiedAt());
              if (testingDate != null && testingDate.compareTo(lastUsedDate) < 0) {
                userExtSourceForModifiing = uES;
              }
            } catch (Exception ex) {
              // Not Parsed correctly
              userExtSourceForModifiing = uES;
            }
          }
        }
      }

      String uESimpleName = getSimpleNameOfExtSource(uEName, version.equals("cs"));
      organizationsWithLoa.put(uESimpleName, uELoa);
    }

    // Set created,modified by userExtSources
    if (userExtSourceForCreating != null) {
      attribute.setValueCreatedAt(userExtSourceForCreating.getCreatedAt());
      attribute.setValueCreatedBy(userExtSourceForCreating.getCreatedBy());
    }
    if (userExtSourceForModifiing != null) {
      attribute.setValueModifiedAt(userExtSourceForModifiing.getModifiedAt());
      attribute.setValueModifiedBy(userExtSourceForModifiing.getModifiedBy());
    }
    attribute.setValue(organizationsWithLoa);
    return attribute;
  }
  public Attribute getAttributeValue(
      PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition)
      throws InternalErrorException {
    Attribute attr = new Attribute(attributeDefinition);
    try {
      Attribute attribute =
          sess.getPerunBl()
              .getAttributesManagerBl()
              .getAttribute(
                  sess, facility, user, AttributesManager.NS_USER_FACILITY_ATTR_DEF + ":shell");
      if (attribute.getValue() != null) {
        Utils.copyAttributeToVirtualAttributeWithValue(attribute, attr);
        return attr;
      }
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException ex) {
      throw new InternalErrorException(ex);
    }

    try {
      Attribute facilityShells =
          sess.getPerunBl()
              .getAttributesManagerBl()
              .getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":shells");
      Attribute userPrefferedShells =
          (sess.getPerunBl()
              .getAttributesManagerBl()
              .getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredShells"));
      List<Resource> resources =
          sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user);
      Set<String> resourcesShells = new HashSet<String>();

      for (Resource resource : resources) {
        List<String> resourcesShellsForTest =
            (List<String>)
                sess.getPerunBl()
                    .getAttributesManagerBl()
                    .getAttribute(
                        sess, resource, AttributesManager.NS_RESOURCE_ATTR_DEF + ":shells")
                    .getValue();
        if (resourcesShellsForTest != null) resourcesShells.addAll(resourcesShellsForTest);
      }
      if (userPrefferedShells.getValue() != null) {
        for (String pShell : (List<String>) userPrefferedShells.getValue()) {
          if (resourcesShells.contains(pShell)) {
            Utils.copyAttributeToViAttributeWithoutValue(userPrefferedShells, attr);
            attr.setValue(pShell);
            return attr;
          }
        }
      }
      if (facilityShells.getValue() != null) {
        for (String fShell : (List<String>) facilityShells.getValue()) {
          if (resourcesShells.contains(fShell)) {
            Utils.copyAttributeToViAttributeWithoutValue(facilityShells, attr);
            attr.setValue(fShell);
            return attr;
          }
        }
      }

    } catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
      throw new InternalErrorException(ex);
    }
    return attr;
  }