Search in sources :

Example 76 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_vsupMailAlias method fillAttribute.

@Override
public Attribute fillAttribute(PerunSessionImpl session, User user, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
    String firstName = user.getFirstName();
    String lastName = user.getLastName();
    Attribute filledAttribute = new Attribute(attribute);
    try {
        Attribute artFirstName = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:artisticFirstName");
        Attribute artLastName = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:artisticLastName");
        if (artFirstName.getValue() != null)
            firstName = (String) artFirstName.getValue();
        if (artLastName.getValue() != null)
            lastName = (String) artLastName.getValue();
    } catch (AttributeNotExistsException e) {
        throw new ConsistencyErrorException("Definition for artistic names of user doesn't exists.", e);
    }
    if (lastName == null || firstName == null) {
        return filledAttribute;
    }
    // remove all diacritics marks from name
    String mail = ModulesUtilsBlImpl.normalizeStringForLogin(firstName) + "." + ModulesUtilsBlImpl.normalizeStringForLogin(lastName);
    // fill value - start as mail, mail2, mail3, ....
    int iterator = 1;
    while (iterator >= 1) {
        if (iterator > 1) {
            filledAttribute.setValue(mail + iterator + "@vsup.cz");
        } else {
            filledAttribute.setValue(mail + "@vsup.cz");
        }
        try {
            checkAttributeValue(session, user, filledAttribute);
            return filledAttribute;
        } catch (WrongAttributeValueException ex) {
            // continue in a WHILE cycle
            iterator++;
        }
    }
    return filledAttribute;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 77 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_vsupMailAliases method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    // map of reserved vsup mails
    Attribute reservedMailsAttribute;
    Map<String, String> reservedMailsAttributeValue;
    // other vsup mail attributes to get values from
    Attribute vsupMailAttribute;
    Attribute mailAliasAttribute;
    Attribute vsupPreferredMailAttribute;
    // output sets used for comparison
    Set<String> reservedMailsOfUser = new HashSet<>();
    Set<String> actualMailsOfUser = new HashSet<>();
    try {
        reservedMailsAttribute = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, usedMailsKeyVsup, usedMailsUrn);
        vsupMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailUrn);
        mailAliasAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasUrn);
        vsupPreferredMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupPreferredMailUrn);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    }
    if (attribute.getValue() == null && reservedMailsAttribute.getValue() == null) {
        throw new ConsistencyErrorException("Entityless attribute 'urn:perun:entityless:attribute-def:def:usedMails' is empty, but we are removing 'vsupMailAliases' value, so there should have been entry in entityless attribute.");
    }
    if (reservedMailsAttribute.getValue() == null) {
        reservedMailsAttributeValue = new LinkedHashMap<>();
    } else {
        reservedMailsAttributeValue = (Map<String, String>) reservedMailsAttribute.getValue();
    }
    // if SET action and mail is already reserved by other user
    if (attribute.getValue() != null) {
        List<String> mails = (List<String>) attribute.getValue();
        for (String mail : mails) {
            String ownersUserId = reservedMailsAttributeValue.get(mail);
            if (ownersUserId != null && !Objects.equals(ownersUserId, String.valueOf(user.getId()))) {
                // TODO - maybe get actual owners attribute and throw WrongReferenceAttributeException to be nice in a GUI ?
                throw new InternalErrorException("On of VŠUP mail aliases: '" + mail + "' is already in use by User ID: " + ownersUserId + ".");
            }
        }
    }
    for (Map.Entry<String, String> entry : reservedMailsAttributeValue.entrySet()) {
        if (Objects.equals(entry.getValue(), String.valueOf(user.getId()))) {
            // reserved mails of a user
            reservedMailsOfUser.add(entry.getKey());
        }
    }
    if (vsupMailAttribute.getValue() != null) {
        actualMailsOfUser.add((String) vsupMailAttribute.getValue());
    }
    if (vsupPreferredMailAttribute.getValue() != null) {
        actualMailsOfUser.add((String) vsupPreferredMailAttribute.getValue());
    }
    if (mailAliasAttribute.getValue() != null) {
        actualMailsOfUser.add((String) mailAliasAttribute.getValue());
    }
    for (String mail : reservedMailsOfUser) {
        if (!actualMailsOfUser.contains(mail)) {
            // Remove mail, which is not in attributes anymore
            reservedMailsAttributeValue.remove(mail);
        }
    }
    // Put in which is in attribute but not in a map
    if (attribute.getValue() != null) {
        List<String> mails = (List<String>) attribute.getValue();
        for (String mail : mails) {
            reservedMailsAttributeValue.putIfAbsent(mail, String.valueOf(user.getId()));
        }
    }
    // save changes in entityless attribute
    try {
        // always set value to attribute, since we might start with null in attribute and empty map in variable !!
        reservedMailsAttribute.setValue(reservedMailsAttributeValue);
        session.getPerunBl().getAttributesManagerBl().setAttribute(session, usedMailsKeyVsup, reservedMailsAttribute);
    } catch (WrongAttributeValueException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ArrayList(java.util.ArrayList) List(java.util.List) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 78 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_uid_namespace method checkAttributeValue.

@Override
public /**
	 * Checks the new UID of the user. 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.
	 */
void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws WrongAttributeValueException, WrongReferenceAttributeValueException, InternalErrorException, WrongAttributeAssignmentException {
    Integer uid = (Integer) attribute.getValue();
    String uidNamespace = attribute.getFriendlyNameParameter();
    if (uid == null) {
        throw new WrongAttributeValueException(attribute, "Attribute was not filled, therefore there is nothing to be checked.");
    }
    Attribute minUidAttribute = null;
    Attribute maxUidAttribute = null;
    try {
        minUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_minUID);
        maxUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_maxUID);
    } catch (AttributeNotExistsException e) {
        throw new ConsistencyErrorException("minUid and maxUid attributes are required", e);
    }
    Integer min = (Integer) minUidAttribute.getValue();
    Integer max = (Integer) maxUidAttribute.getValue();
    if (min == null) {
        throw new WrongReferenceAttributeValueException(attribute, minUidAttribute);
    }
    if (max == null) {
        throw new WrongReferenceAttributeValueException(attribute, maxUidAttribute);
    }
    //uid is in proper range
    if (uid < min || uid > max) {
        throw new WrongAttributeValueException(attribute, "UID " + uid + " is not proper range (" + min + "," + max + ")");
    }
    // Get all users who have set attribute urn:perun:member:attribute-def:def:uid-namespace:[uid-namespace], with the value.
    List<User> usersWithUid = sess.getPerunBl().getUsersManagerBl().getUsersByAttribute(sess, attribute);
    //remove self
    usersWithUid.remove(user);
    if (!usersWithUid.isEmpty()) {
        if (usersWithUid.size() > 1)
            throw new ConsistencyErrorException("FATAL ERROR: Duplicated UID detected." + attribute + " " + usersWithUid);
        throw new WrongAttributeValueException(attribute, "This UID " + attribute.getValue() + " is already occupied by" + usersWithUid.get(0) + ".");
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) 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)

Example 79 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_userCertificates method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException {
    try {
        HashMap<String, String> certs = (HashMap<String, String>) attribute.getValue();
        if (certs != null) {
            for (String certDN : certs.keySet()) {
                String cert = certs.get(certDN);
                // Remove --- BEGIN --- and --- END ----
                String certWithoutBegin = cert.replaceFirst("-----BEGIN CERTIFICATE-----", "");
                String rawCert = certWithoutBegin.replaceFirst("-----END CERTIFICATE-----", "");
                X509Certificate.getInstance(Base64.decodeBase64(rawCert.getBytes()));
            }
        }
    } catch (CertificateException e) {
        throw new WrongAttributeValueException(attribute, user, "Wrong format, certificate must be in PEM format prepended by -----BEGIN CERTIFICATE----- and appended by -----END CERTIFICATE-----.", e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CertificateException(javax.security.cert.CertificateException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 80 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_userPreferredCertDN method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException {
    Attribute userCertDNs = null;
    try {
        userCertDNs = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
    Map<String, String> certDNsValue = null;
    if (userCertDNs.getValue() != null) {
        certDNsValue = (Map<String, String>) userCertDNs.getValue();
    } else {
        if (attribute.getValue() != null)
            throw new WrongReferenceAttributeValueException(attribute, userCertDNs, "There is no certificates for this user so preferred certificate can't be choose.");
        else
            return;
    }
    if (attribute.getValue() == null) {
        if (certDNsValue != null || !certDNsValue.isEmpty())
            throw new WrongAttributeValueException(attribute, user, "This attribute value can't be null because of notNull attribute userCertDNs");
    } else {
        String preferredCertDNValue = (String) attribute.getValue();
        if (!certDNsValue.containsKey(preferredCertDNValue))
            throw new WrongAttributeValueException(attribute, "This attribute value must be one of exsiting keys in userCertDNs.");
    }
}
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

WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)135 Attribute (cz.metacentrum.perun.core.api.Attribute)100 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)83 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)82 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)75 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)66 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)54 Matcher (java.util.regex.Matcher)31 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)27 ArrayList (java.util.ArrayList)17 LinkedHashMap (java.util.LinkedHashMap)16 Facility (cz.metacentrum.perun.core.api.Facility)14 Map (java.util.Map)14 Resource (cz.metacentrum.perun.core.api.Resource)12 User (cz.metacentrum.perun.core.api.User)11 Group (cz.metacentrum.perun.core.api.Group)9 Pattern (java.util.regex.Pattern)8 BigDecimal (java.math.BigDecimal)7 List (java.util.List)7 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)5