Search in sources :

Example 71 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_user_facility_attribute_def_def_basicDefaultGID method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl sess, Facility facility, User user, Attribute attribute) throws WrongAttributeValueException, WrongReferenceAttributeValueException, InternalErrorException, WrongAttributeAssignmentException {
    Attribute namespaceAttribute;
    try {
        namespaceAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":unixGID-namespace");
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
    if (namespaceAttribute.getValue() == null) {
        throw new WrongReferenceAttributeValueException(attribute, namespaceAttribute, "Reference attribute is null");
    }
    String namespaceName = (String) namespaceAttribute.getValue();
    Attribute resourceGidAttribute;
    try {
        resourceGidAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGID-namespace:" + namespaceName));
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Namespace from value of " + namespaceAttribute + " doesn't exists. (Resource attribute " + AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGID-namespace:" + namespaceName + " doesn't exists", ex);
    }
    resourceGidAttribute.setValue(attribute.getValue());
    List<Resource> allowedResources = sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user);
    List<Resource> resourcesWithSameGid = sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGidAttribute);
    if (resourcesWithSameGid.isEmpty() && allowedResources.isEmpty() && resourceGidAttribute.getValue() == null)
        return;
    if (resourcesWithSameGid.isEmpty() && resourceGidAttribute.getValue() != null)
        throw new WrongAttributeValueException(attribute, user, facility, "Resource with requiered unix GID doesn't exist.");
    if (allowedResources.isEmpty())
        throw new WrongAttributeValueException(attribute, user, "User has not access to requiered resource");
    resourcesWithSameGid.retainAll(allowedResources);
    if (!resourcesWithSameGid.isEmpty()) {
        //We found at least one allowed resource with same gid as the user have => attribute is OK
        return;
    } else {
        throw new WrongAttributeValueException(attribute, user, "User has not access to resource with required group id");
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 72 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_user_facility_attribute_def_virt_UID method checkAttributeValue.

/**
	 * Checks the new UID of the user at the specified facility. The new UID must
	 * not be lower than the min UID or greater than the max UID. Also no collision between
	 * existing user and the new user is allowed.
	 */
@Override
public void checkAttributeValue(PerunSessionImpl sess, Facility facility, User user, Attribute attribute) throws WrongAttributeValueException, WrongReferenceAttributeValueException, InternalErrorException, WrongAttributeAssignmentException {
    try {
        Attribute uidNamespaceAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":uid-namespace");
        Attribute uidAttribute = null;
        if (uidNamespaceAttribute.getValue() != null) {
            // Get the u:uid-namespace[uidNamespaceAttribute]
            uidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":uid-namespace:" + (String) uidNamespaceAttribute.getValue());
            uidAttribute.setValue(attribute.getValue());
            sess.getPerunBl().getAttributesManagerBl().checkAttributeValue(sess, user, uidAttribute);
        } else {
            throw new WrongReferenceAttributeValueException(attribute, uidAttribute);
        }
    } catch (AttributeNotExistsException e) {
        throw new ConsistencyErrorException(e);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)

Example 73 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_resource_attribute_def_def_defaultDataQuota method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrDefaultDataLimit = null;
    String defaultDataQuota = null;
    String defaultDataLimit = null;
    String defaultDataQuotaNumber = null;
    String defaultDataQuotaLetter = null;
    String defaultDataLimitNumber = null;
    String defaultDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    }
    //Get DefaultDataLimit attribute
    try {
        attrDefaultDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataLimit);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with defaultDataLimit from resource " + resource.getId() + " could not obtained.", ex);
    }
    //Get DefaultDataQuota value
    if (attribute.getValue() != null) {
        defaultDataQuota = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(defaultDataQuota);
        Matcher letterMatcher = letterPattern.matcher(defaultDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            defaultDataQuotaNumber = defaultDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataQuotaNumber = null;
        }
        try {
            defaultDataQuotaLetter = defaultDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (defaultDataQuotaNumber != null)
        quotaNumber = new BigDecimal(defaultDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, resource, null, attribute + " can't be less than 0.");
    }
    //Get DefaultDataLimit value
    if (attrDefaultDataLimit != null && attrDefaultDataLimit.getValue() != null) {
        defaultDataLimit = (String) attrDefaultDataLimit.getValue();
        Matcher numberMatcher = numberPattern.matcher(defaultDataLimit);
        Matcher letterMatcher = letterPattern.matcher(defaultDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            defaultDataLimitNumber = defaultDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataLimitNumber = null;
        }
        try {
            defaultDataLimitLetter = defaultDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (defaultDataLimitNumber != null)
        limitNumber = new BigDecimal(defaultDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataLimit, resource, null, resource, null, attrDefaultDataLimit + " cant be less than 0.");
    }
    //Compare DefaultDataQuota with DefaultDataLimit
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataLimit, resource, null, resource, null, "Try to set unlimited quota, but limit is still " + defaultDataLimitNumber + defaultDataLimitLetter);
        }
    } else if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
        if (defaultDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (defaultDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (defaultDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (defaultDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (defaultDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (defaultDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (defaultDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (defaultDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (defaultDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (defaultDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataLimit, resource, null, resource, null, attribute + " must be less than or equals to " + defaultDataLimit);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Example 74 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_resource_attribute_def_def_homeMountPoints method checkAttributeValue.

/**
	 * Allows only homeMountPoints which are contained in underlying facility
	 * @param perunSession
	 * @param resource
	 * @param attribute
	 * @throws InternalErrorException
	 * @throws WrongAttributeValueException
	 * @throws WrongReferenceAttributeValueException
	 * @throws WrongAttributeAssignmentException
	 */
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    if (attribute.getValue() == null) {
        throw new WrongAttributeValueException(attribute);
    }
    Facility facility = perunSession.getPerunBl().getResourcesManagerBl().getFacility(perunSession, resource);
    Attribute facilityAttr = null;
    try {
        facilityAttr = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, facility, A_F_homeMountPoints);
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException(ex);
    }
    if (facilityAttr.getValue() == null)
        throw new WrongReferenceAttributeValueException(attribute, facilityAttr, "Reference attribute have null value.");
    if (!((List<String>) facilityAttr.getValue()).containsAll((List<String>) attribute.getValue())) {
        throw new WrongAttributeValueException(attribute);
    }
    List<String> homeMountPoints = (List<String>) attribute.getValue();
    if (!homeMountPoints.isEmpty()) {
        Pattern pattern = Pattern.compile("^/[-a-zA-Z.0-9_/]*$");
        for (String st : homeMountPoints) {
            Matcher match = pattern.matcher(st);
            if (!match.matches()) {
                throw new WrongAttributeValueException(attribute, "Bad homeMountPoints attribute format " + st);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) List(java.util.List) Facility(cz.metacentrum.perun.core.api.Facility) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 75 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_member_resource_attribute_def_def_filesLimit method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Member member, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrFilesQuota = null;
    Integer filesQuota = null;
    Integer filesLimit = null;
    //Get FilesQuotaAttribute
    try {
        attrFilesQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, member, A_MR_filesQuota);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with filesQuota from member " + member.getId() + " and resource " + resource.getId() + " could not obtained.", ex);
    }
    //Get FilesLimit value
    if (attribute.getValue() != null) {
        filesLimit = (Integer) attribute.getValue();
    } else {
        try {
            attribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesLimit);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException("Attribute with defaultFilesLimit from resource " + resource.getId() + " could not obtained.", ex);
        }
        if (attribute != null && attribute.getValue() != null) {
            filesLimit = (Integer) attribute.getValue();
        }
    }
    if (filesLimit != null && filesLimit < 0)
        throw new WrongAttributeValueException(attribute, resource, member, attribute + " cannot be less than 0.");
    //Get FilesQuota value
    if (attrFilesQuota != null && attrFilesQuota.getValue() != null) {
        filesQuota = (Integer) attrFilesQuota.getValue();
    } else if (attrFilesQuota == null || attrFilesQuota.getValue() == null) {
        try {
            attrFilesQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesQuota);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException("Attribute with defaultFilesQuota from resource " + resource.getId() + " could not obtained.", ex);
        }
        if (attrFilesQuota != null || attrFilesQuota.getValue() != null) {
            filesQuota = (Integer) attrFilesQuota.getValue();
        }
    }
    if (filesQuota != null && filesQuota < 0)
        throw new ConsistencyErrorException(attrFilesQuota + " cannot be less than 0.");
    //Compare FilesLimit with FilesQuota
    if (filesQuota == null || filesQuota == 0) {
        if (filesLimit != null && filesLimit != 0)
            throw new WrongReferenceAttributeValueException(attribute, attrFilesQuota, resource, member, resource, null, "Try to set limited limit, but there is still set unlimited Quota.");
    } else if ((filesQuota != null && filesQuota != 0) && (filesLimit != null && filesLimit != 0)) {
        if (filesLimit < filesQuota)
            throw new WrongReferenceAttributeValueException(attribute, attrFilesQuota, resource, member, resource, null, attribute + " must be more than or equals to " + attrFilesQuota);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Aggregations

WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)109 Attribute (cz.metacentrum.perun.core.api.Attribute)101 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)83 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)70 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)64 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)62 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)51 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)27 Resource (cz.metacentrum.perun.core.api.Resource)13 ArrayList (java.util.ArrayList)13 Facility (cz.metacentrum.perun.core.api.Facility)12 Matcher (java.util.regex.Matcher)12 User (cz.metacentrum.perun.core.api.User)11 Group (cz.metacentrum.perun.core.api.Group)9 BigDecimal (java.math.BigDecimal)6 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)5 List (java.util.List)5 Pattern (java.util.regex.Pattern)5 Pair (cz.metacentrum.perun.core.api.Pair)3 LinkedHashMap (java.util.LinkedHashMap)3