Search in sources :

Example 81 with AttributeNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException 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 82 with AttributeNotExistsException

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

the class urn_perun_user_attribute_def_def_userPreferredCertDN method fillAttribute.

public Attribute fillAttribute(PerunSessionImpl sess, User user, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
    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();
        Set<String> keys = certDNsValue.keySet();
        for (String key : keys) {
            if (key != null && !key.isEmpty()) {
                Attribute attr = new Attribute(attribute);
                attr.setValue(key);
                return attr;
            }
        }
    }
    return new Attribute(attribute);
}
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)

Example 83 with AttributeNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException 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)

Example 84 with AttributeNotExistsException

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

the class UsersManagerEntryIntegrationTest method getUsersByAttribute.

@Test
public void getUsersByAttribute() throws Exception {
    System.out.println(CLASS_NAME + "getUsersByAttribute");
    // Check if the attribute already exists
    Attribute attr;
    AttributeDefinition attrDef;
    try {
        attrDef = perun.getAttributesManagerBl().getAttributeDefinition(sess, "urn:perun:user:attribute-def:opt:user_test_attribute");
    } catch (AttributeNotExistsException e) {
        // Attribute doesn't exist, so create it
        attrDef = new AttributeDefinition();
        attrDef.setNamespace("urn:perun:user:attribute-def:opt");
        attrDef.setFriendlyName("user-test-attribute");
        attrDef.setType(String.class.getName());
        attrDef = perun.getAttributesManagerBl().createAttribute(sess, attrDef);
    }
    attr = new Attribute(attrDef);
    attr.setValue("UserAttribute");
    // Set the attribute to the user
    perun.getAttributesManagerBl().setAttribute(sess, user, attr);
    assertTrue("results must contain user", usersManager.getUsersByAttribute(sess, attr).contains(user));
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 85 with AttributeNotExistsException

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

the class MembersManagerBlImpl method sendPasswordResetLinkEmail.

public void sendPasswordResetLinkEmail(PerunSession sess, Member member, String namespace, String url) throws InternalErrorException {
    User user = perunBl.getUsersManagerBl().getUserByMember(sess, member);
    List<Attribute> logins = perunBl.getAttributesManagerBl().getLogins(sess, user);
    boolean found = false;
    for (Attribute a : logins) {
        if (a.getFriendlyNameParameter().equals(namespace))
            found = true;
    }
    if (!found)
        throw new InternalErrorException(user.toString() + " doesn't have login in namespace: " + namespace);
    String email = "";
    try {
        Attribute a = perunBl.getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredMail");
        if (a != null && a.getValue() != null) {
            email = (String) a.getValue();
        }
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException(ex);
    }
    int id = getMembersManagerImpl().storePasswordResetRequest(sess, user, namespace);
    Utils.sendPasswordResetEmail(user, email, namespace, url, id);
}
Also used : User(cz.metacentrum.perun.core.api.User) 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)

Aggregations

AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)138 Attribute (cz.metacentrum.perun.core.api.Attribute)120 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)94 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)81 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)75 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)64 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)59 ArrayList (java.util.ArrayList)30 Resource (cz.metacentrum.perun.core.api.Resource)25 Matcher (java.util.regex.Matcher)19 Facility (cz.metacentrum.perun.core.api.Facility)17 User (cz.metacentrum.perun.core.api.User)17 LinkedHashMap (java.util.LinkedHashMap)15 Group (cz.metacentrum.perun.core.api.Group)14 Map (java.util.Map)13 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)12 List (java.util.List)11 HashSet (java.util.HashSet)8 BigDecimal (java.math.BigDecimal)7 Pattern (java.util.regex.Pattern)6