Search in sources :

Example 51 with ConsistencyErrorException

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

the class AttributesManagerEntry method getAttributes.

@Override
public List<Attribute> getAttributes(PerunSession sess, Member member, boolean workWithUserAttributes) throws MemberNotExistsException {
    Utils.checkPerunSession(sess);
    getPerunBl().getMembersManagerBl().checkMemberExists(sess, member);
    List<Attribute> attributes = getAttributesManagerBl().getAttributes(sess, member, workWithUserAttributes);
    Iterator<Attribute> attrIter = attributes.iterator();
    // Choose to which attributes has the principal access
    while (attrIter.hasNext()) {
        Attribute attrNext = attrIter.next();
        if (getAttributesManagerBl().isFromNamespace(sess, attrNext, NS_MEMBER_ATTR)) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrNext, member))
                attrIter.remove();
            else
                attrNext.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrNext, member));
        } else if (getAttributesManagerBl().isFromNamespace(sess, attrNext, NS_USER_ATTR)) {
            User user = getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrNext, user))
                attrIter.remove();
            else
                attrNext.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrNext, user));
        } else {
            throw new ConsistencyErrorException("One of getting attribute is not type of member or user : " + attrNext);
        }
    }
    return attributes;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute)

Example 52 with ConsistencyErrorException

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

the class urn_perun_user_attribute_def_def_uid_namespace method fillAttribute.

/**
 * Fills the new UID for the user at the specified facility. First empty slot
 * in range (minUID, maxUID) is returned.
 */
@Override
public Attribute fillAttribute(PerunSessionImpl sess, User user, AttributeDefinition attribute) throws WrongAttributeAssignmentException {
    String uidNamespace = attribute.getFriendlyNameParameter();
    Attribute atr = new Attribute(attribute);
    Attribute minUidAttribute;
    Attribute maxUidAttribute;
    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 || max == null) {
        // we couldnt determine necessary attributes for getting new uID
        return atr;
    }
    SortedSet<Integer> values = new TreeSet<>();
    // Get all attributes urn:perun:member:attribute-def:def:uid-namespace:[uid-namespace], then we can get the new UID
    List<Attribute> uidAttributes = sess.getPerunBl().getAttributesManagerBl().getAttributesByAttributeDefinition(sess, attribute);
    for (Attribute uidAttribute : uidAttributes) {
        if (uidAttribute.getValue() != null)
            values.add((Integer) uidAttribute.getValue());
    }
    String uidPolicy;
    try {
        uidPolicy = (String) sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_namespace_uid_policy).getValue();
    } catch (AttributeNotExistsException e) {
        throw new InternalErrorException(e);
    }
    if (UID_POLICY_INCREMENT.equals(uidPolicy)) {
        // Only use +1 for max UID
        if (values.isEmpty()) {
            atr.setValue(min);
        } else {
            atr.setValue(Collections.max(values) + 1);
        }
        return atr;
    } else {
        if (values.size() == 0) {
            atr.setValue(min);
            return atr;
        } else {
            for (int i = min; i < max; i++) {
                if (!values.contains(i)) {
                    atr.setValue(i);
                    return atr;
                }
            }
            return atr;
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) TreeSet(java.util.TreeSet) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 53 with ConsistencyErrorException

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

the class urn_perun_user_attribute_def_def_vsupExchangeMail method fillAttribute.

@Override
public Attribute fillAttribute(PerunSessionImpl session, User user, AttributeDefinition attribute) throws WrongAttributeAssignmentException {
    if (user.isSpecificUser()) {
        // Do not fill value for service/sponsored users
        return new Attribute(attribute);
    }
    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 = artFirstName.valueAsString();
        if (artLastName.getValue() != null)
            lastName = artLastName.valueAsString();
    } 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 + "@umprum.cz");
        } else {
            filledAttribute.setValue(mail + "@umprum.cz");
        }
        try {
            checkAttributeSemantics(session, user, filledAttribute);
            return filledAttribute;
        } catch (WrongReferenceAttributeValueException 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) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)

Example 54 with ConsistencyErrorException

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

the class urn_perun_user_attribute_def_def_vsupExchangeMail method checkAttributeSemantics.

@Override
public void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute) throws WrongReferenceAttributeValueException {
    // can be empty
    if (attribute.getValue() == null)
        return;
    try {
        // lock this logic
        Attribute reservedMailsAttribute = sess.getPerunBl().getAttributesManagerBl().getAttributeForUpdate(sess, user, usedMailsUrn);
        List<User> users = sess.getPerunBl().getUsersManagerBl().getUsersByAttributeValue(sess, usedMailsUrn, attribute.valueAsString());
        if (users.size() > 1) {
            String ids = ArrayUtils.toString(users.stream().map(User::getId).collect(Collectors.toList()));
            throw new WrongReferenceAttributeValueException(attribute, null, user, null, null, null, "VŠUP primary mail: '" + attribute.getValue() + "' is shared between users with IDs: " + ids + ".");
        }
        if (users.size() == 1) {
            User ownerUser = users.get(0);
            if (!Objects.equals(ownerUser.getId(), user.getId())) {
                throw new WrongReferenceAttributeValueException(attribute, null, user, null, null, null, "VŠUP primary mail: '" + attribute.getValue() + "' is already in use by User ID: " + ownerUser.getId() + ".");
            }
        }
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new ConsistencyErrorException("Attribute is not of user type.", ex);
    }
}
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) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)

Example 55 with ConsistencyErrorException

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

the class urn_perun_user_attribute_def_def_vsupMail method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws WrongReferenceAttributeValueException {
    // list of reserved mails for user
    Attribute reservedMailsAttribute;
    ArrayList<String> reservedMailsAttributeValue;
    // other vsup mail attributes to get values from
    Attribute vsupPreferredMailAttribute;
    Attribute vsupExchangeMailAttribute;
    Attribute vsupExchangeMailAliasesAttribute;
    // output sets used for comparison
    Set<String> reservedMailsOfUser = new HashSet<>();
    Set<String> actualMailsOfUser = new HashSet<>();
    // get related attributes
    try {
        reservedMailsAttribute = session.getPerunBl().getAttributesManagerBl().getAttributeForUpdate(session, user, usedMailsUrn);
        vsupPreferredMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupPreferredMailUrn);
        vsupExchangeMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupExchangeMailUrn);
        vsupExchangeMailAliasesAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupExchangeMailAliasesUrn);
    } 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("User attribute 'urn:perun:user:attribute-def:def:usedMails' is empty, but we are removing 'vsupMail' value, so there should have been entry in usedMails attribute.");
    }
    if (reservedMailsAttribute.getValue() == null) {
        reservedMailsAttributeValue = new ArrayList<>();
    } else {
        reservedMailsAttributeValue = reservedMailsAttribute.valueAsList();
    }
    // fill set for comparison
    reservedMailsOfUser.addAll(reservedMailsAttributeValue);
    if (vsupPreferredMailAttribute.getValue() != null) {
        actualMailsOfUser.add(vsupPreferredMailAttribute.valueAsString());
    }
    if (vsupExchangeMailAttribute.getValue() != null) {
        actualMailsOfUser.add(vsupExchangeMailAttribute.valueAsString());
    }
    if (vsupExchangeMailAliasesAttribute.getValue() != null) {
        actualMailsOfUser.addAll(vsupExchangeMailAliasesAttribute.valueAsList());
    }
    // Remove values, which are no longer set to any of user mail attributes
    for (String mail : reservedMailsOfUser) {
        if (!actualMailsOfUser.contains(mail)) {
            // Remove mail, which is not in attributes anymore
            reservedMailsAttributeValue.remove(mail);
            // since this attribute holds single value, we can break the cycle here
            break;
        }
    }
    // if SET action and new mail is not present (prevent duplicates within the value)
    if (attribute.getValue() != null && !reservedMailsAttributeValue.contains(attribute.valueAsString())) {
        reservedMailsAttributeValue.add(attribute.valueAsString());
    }
    // save changes in reserved mails attribute
    try {
        // always set value to attribute, since we might start with null in attribute and empty list in variable !!
        reservedMailsAttribute.setValue(reservedMailsAttributeValue);
        session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, reservedMailsAttribute);
    } catch (WrongAttributeValueException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    // if setting non-empty value, process u:d:vsupExchangeMailAliases and u:d:vsupPreferredMail
    if (attribute.getValue() != null) {
        ArrayList<String> vals = vsupExchangeMailAliasesAttribute.valueAsList();
        if (vals == null)
            vals = new ArrayList<>();
        if (!vals.contains(attribute.valueAsString())) {
            // store value between u:d:vsupExchangeMailAliases if not yet present
            try {
                vals.add(attribute.valueAsString());
                vsupExchangeMailAliasesAttribute.setValue(vals);
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, vsupExchangeMailAliasesAttribute);
            } catch (WrongAttributeValueException | WrongAttributeAssignmentException e) {
                throw new InternalErrorException("Unable to store generated vsupMail between vsupExchangeMailAliases.", e);
            }
        }
        // store value with changed domain to @umprum.cz into u:d:vsupPreferredMail if it's still empty
        if (vsupPreferredMailAttribute.getValue() == null) {
            vsupPreferredMailAttribute.setValue(attribute.valueAsString().replace("@vsup.cz", "@umprum.cz"));
            try {
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, vsupPreferredMailAttribute);
            } catch (WrongAttributeValueException | WrongAttributeAssignmentException e) {
                throw new InternalErrorException("Unable to store generated vsupMail (with modified domain to @umprum.cz) to vsupPreferredMail.", e);
            }
        }
    }
}
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) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) HashSet(java.util.HashSet)

Aggregations

ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)281 Attribute (cz.metacentrum.perun.core.api.Attribute)212 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)162 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)120 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)111 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)102 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)84 User (cz.metacentrum.perun.core.api.User)60 ArrayList (java.util.ArrayList)51 Group (cz.metacentrum.perun.core.api.Group)44 Facility (cz.metacentrum.perun.core.api.Facility)41 Resource (cz.metacentrum.perun.core.api.Resource)37 Member (cz.metacentrum.perun.core.api.Member)30 LinkedHashMap (java.util.LinkedHashMap)23 Vo (cz.metacentrum.perun.core.api.Vo)22 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)21 GroupResourceMismatchException (cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException)20 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)19 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)17 VoNotExistsException (cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)17