Search in sources :

Example 36 with AttributeNotExistsException

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

the class urn_perun_user_attribute_def_virt_kerberosLogins method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attribute = new Attribute(attributeDefinition);
    List<String> krbPrincipalName = new ArrayList<String>();
    Attribute krbLogins;
    try {
        krbLogins = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":kerberosLogins");
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException("kerberosLogins attribute for " + user + " not exist.", ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException("kerberos Logins attribute bad assignment.", ex);
    }
    if (krbLogins.getValue() != null) {
        krbPrincipalName.addAll((List<String>) krbLogins.getValue());
    }
    List<UserExtSource> userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
    for (UserExtSource uES : userExtSources) {
        if (uES.getExtSource() != null) {
            String login = uES.getLogin();
            String type = uES.getExtSource().getType();
            if (type != null && login != null) {
                if (type.equals(ExtSourcesManager.EXTSOURCE_KERBEROS)) {
                    if (!krbPrincipalName.contains(login))
                        krbPrincipalName.add(login);
                }
            }
        }
    }
    attribute = Utils.copyAttributeToViAttributeWithoutValue(krbLogins, attribute);
    attribute.setValue(krbPrincipalName);
    return attribute;
}
Also used : 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)

Example 37 with AttributeNotExistsException

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

the class urn_perun_user_attribute_def_virt_login_namespace_bbmri_persistent method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute bbmriPersistent = new Attribute(attributeDefinition);
    try {
        Attribute bbmriPersistentShadow = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, SHADOW);
        if (bbmriPersistentShadow.getValue() == null) {
            bbmriPersistentShadow = sess.getPerunBl().getAttributesManagerBl().fillAttribute(sess, user, bbmriPersistentShadow);
            if (bbmriPersistentShadow.getValue() == null) {
                throw new InternalErrorException("BBMRI id couldn't be set automatically");
            }
            sess.getPerunBl().getAttributesManagerBl().setAttribute(sess, user, bbmriPersistentShadow);
        }
        bbmriPersistent.setValue(bbmriPersistentShadow.getValue());
        return bbmriPersistent;
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    } catch (AttributeNotExistsException e) {
        throw new InternalErrorException(e);
    } catch (WrongReferenceAttributeValueException e) {
        throw new InternalErrorException(e);
    } catch (WrongAttributeValueException e) {
        throw new InternalErrorException(e);
    }
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 38 with AttributeNotExistsException

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

the class urn_perun_user_attribute_def_def_vsupMailAlias 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 mailAliasesAttribute;
    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);
        mailAliasesAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasesUrn);
        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 'vsupMailAlias' 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) {
        String ownersUserId = reservedMailsAttributeValue.get((String) attribute.getValue());
        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("VŠUP mail alias: '" + attribute.getValue() + "' 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 (mailAliasesAttribute.getValue() != null) {
        actualMailsOfUser.addAll((ArrayList<String>) mailAliasesAttribute.getValue());
    }
    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;
        }
    }
    // Put in which is in attribute but not in a map
    if (attribute.getValue() != null) {
        reservedMailsAttributeValue.putIfAbsent((String) attribute.getValue(), 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);
    }
    // if set, check vsupPreferredMail and set it's value if is currently empty or equals vsupMail
    if (attribute.getValue() != null) {
        String preferredMail = (String) vsupPreferredMailAttribute.getValue();
        if (preferredMail == null || Objects.equals(preferredMail, vsupMailAttribute.getValue())) {
            vsupPreferredMailAttribute.setValue(attribute.getValue());
            try {
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, vsupPreferredMailAttribute);
            } catch (WrongAttributeValueException | WrongAttributeAssignmentException e) {
                throw new InternalErrorException("Unable to store generated vsupMailAlias 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) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 39 with AttributeNotExistsException

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

the class urn_perun_user_attribute_def_def_vsupMailAlias method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongAttributeAssignmentException {
    // can be empty
    if (attribute.getValue() == null)
        return;
    // if set, must match generic format
    Matcher emailMatcher = emailAliasPattern.matcher((String) attribute.getValue());
    if (!emailMatcher.find())
        throw new WrongAttributeValueException(attribute, user, "School mail alias is not in a correct form: \"firstName.lastName[counter]@vsup.cz\".");
    try {
        Attribute reservedMailsAttribute = sess.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(sess, usedMailsKeyVsup, usedMailsUrn);
        if (reservedMailsAttribute.getValue() != null) {
            Map<String, String> reservedMailsAttributeValue = (Map<String, String>) reservedMailsAttribute.getValue();
            String ownersUserId = reservedMailsAttributeValue.get((String) attribute.getValue());
            if (ownersUserId != null && !Objects.equals(ownersUserId, String.valueOf(user.getId()))) {
                throw new WrongAttributeValueException("VŠUP mail alias: '" + attribute.getValue() + "' is already in use by User ID: " + ownersUserId + ".");
            }
        }
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Matcher(java.util.regex.Matcher) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 40 with AttributeNotExistsException

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

the class urn_perun_user_facility_attribute_def_def_shell method allShellsAtSpecifiedFacility.

/**
	 * Internal method for getting all allowed shells at specified facility
	 */
private List<String> allShellsAtSpecifiedFacility(PerunSessionImpl session, Facility facility, User user) throws InternalErrorException, WrongAttributeAssignmentException {
    List<Resource> availableResources;
    availableResources = session.getPerunBl().getUsersManagerBl().getAllowedResources(session, facility, user);
    List<String> allowedShells = new ArrayList<String>();
    for (Resource r : availableResources) {
        Attribute resourceAttr;
        try {
            resourceAttr = session.getPerunBl().getAttributesManagerBl().getAttribute(session, r, AttributesManager.NS_RESOURCE_ATTR_DEF + ":shells");
        } catch (AttributeNotExistsException ex) {
            throw new InternalErrorException("Attribute with all shells of facility " + facility.getId() + " could not be obtained", ex);
        }
        if (resourceAttr.getValue() != null) {
            allowedShells.addAll(((List<String>) resourceAttr.getValue()));
        }
    }
    return allowedShells;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) 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