Search in sources :

Example 21 with WrongAttributeAssignmentException

use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException 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 InternalErrorException, WrongReferenceAttributeValueException {
    // map of reserved vsup mails
    Attribute reservedMailsAttribute;
    Map<String, String> reservedMailsAttributeValue;
    // other vsup mail attributes to get values from
    Attribute mailAliasAttribute;
    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);
        mailAliasAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasUrn);
        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 'vsupMail' 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: '" + 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 (mailAliasAttribute.getValue() != null) {
        actualMailsOfUser.add((String) mailAliasAttribute.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 - if is empty, set vsupMail to vsupPreferredMail
    if (vsupPreferredMailAttribute.getValue() == null && attribute.getValue() != null) {
        vsupPreferredMailAttribute.setValue(attribute.getValue());
        try {
            session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, vsupPreferredMailAttribute);
        } catch (WrongAttributeValueException | WrongAttributeAssignmentException e) {
            throw new InternalErrorException("Unable to store generated vsupMail 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 22 with WrongAttributeAssignmentException

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

the class urn_perun_user_facility_attribute_def_virt_preferredUnixGroupName method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attr = new Attribute(attributeDefinition);
    Attribute preferredGroupNameAttribute = null;
    try {
        Attribute facilityGroupNameNamespaceAttr = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, A_FACILITY_DEF_UNIX_GROUPNAME_NAMESPACE);
        if (facilityGroupNameNamespaceAttr.getValue() != null) {
            String namespace = (String) facilityGroupNameNamespaceAttr.getValue();
            preferredGroupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, A_USER_DEF_PREFERRED_UNIX_GROUPNAME_NAMESPACE + namespace);
            attr = Utils.copyAttributeToVirtualAttributeWithValue(preferredGroupNameAttribute, attr);
        } else {
            attr.setValue(null);
        }
    } catch (AttributeNotExistsException e) {
        throw new ConsistencyErrorException(e);
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    }
    return attr;
}
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)

Example 23 with WrongAttributeAssignmentException

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

the class urn_perun_user_facility_attribute_def_virt_shell method getAttributeValue.

public Attribute getAttributeValue(PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attr = new Attribute(attributeDefinition);
    try {
        Attribute attribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, user, AttributesManager.NS_USER_FACILITY_ATTR_DEF + ":shell");
        if (attribute.getValue() != null) {
            Utils.copyAttributeToVirtualAttributeWithValue(attribute, attr);
            return attr;
        }
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException ex) {
        throw new InternalErrorException(ex);
    }
    try {
        Attribute facilityShells = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":shells");
        Attribute userPrefferedShells = (sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredShells"));
        List<Resource> resources = sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user);
        Set<String> resourcesShells = new HashSet<String>();
        for (Resource resource : resources) {
            List<String> resourcesShellsForTest = (List<String>) sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, AttributesManager.NS_RESOURCE_ATTR_DEF + ":shells").getValue();
            if (resourcesShellsForTest != null)
                resourcesShells.addAll(resourcesShellsForTest);
        }
        if (userPrefferedShells.getValue() != null) {
            for (String pShell : (List<String>) userPrefferedShells.getValue()) {
                if (resourcesShells.contains(pShell)) {
                    Utils.copyAttributeToViAttributeWithoutValue(userPrefferedShells, attr);
                    attr.setValue(pShell);
                    return attr;
                }
            }
        }
        if (facilityShells.getValue() != null) {
            for (String fShell : (List<String>) facilityShells.getValue()) {
                if (resourcesShells.contains(fShell)) {
                    Utils.copyAttributeToViAttributeWithoutValue(facilityShells, attr);
                    attr.setValue(fShell);
                    return attr;
                }
            }
        }
    } catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    return attr;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 24 with WrongAttributeAssignmentException

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

the class urn_perun_user_facility_attribute_def_virt_dataQuotas method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attribute = new Attribute(attributeDefinition);
    Map<String, String> countedQuotas = new HashMap<>();
    //merge attribute settings for every allowed resource
    List<Map<String, Pair<BigDecimal, BigDecimal>>> mergedMemberResourceQuotas = new ArrayList<>();
    List<Resource> allowedResources = sess.getPerunBl().getResourcesManagerBl().getAllowedResources(sess, facility, user);
    for (Resource resource : allowedResources) {
        //get allowed member of this user on this resource (using his VO)
        Vo membersVo;
        try {
            membersVo = sess.getPerunBl().getVosManagerBl().getVoById(sess, resource.getVoId());
        } catch (VoNotExistsException ex) {
            throw new ConsistencyErrorException("Vo should exists, because resource with this id exists " + resource);
        }
        Member memberOnResource;
        try {
            memberOnResource = sess.getPerunBl().getMembersManagerBl().getMemberByUser(sess, membersVo, user);
        } catch (MemberNotExistsException ex) {
            throw new ConsistencyErrorException("User should have member in this VO, because he was listed in allowed assigned resources " + user + ", " + membersVo + " , " + resource);
        }
        //get resource quotas
        Map<String, Pair<BigDecimal, BigDecimal>> resourceTransferedQuotas;
        Attribute resourceQuotas;
        try {
            resourceQuotas = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, A_R_defaultDataQuotas);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        }
        if (resourceQuotas == null || resourceQuotas.getValue() == null)
            resourceTransferedQuotas = new HashMap<>();
        else {
            try {
                resourceTransferedQuotas = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(resourceQuotas, resource, null, true);
            } catch (WrongAttributeValueException ex) {
                throw new ConsistencyErrorException("Quotas on resource " + resource + " are in bad format.", ex);
            }
        }
        //get members quotas
        Map<String, Pair<BigDecimal, BigDecimal>> memberTransferedQuotas;
        Attribute memberQuotas;
        try {
            memberQuotas = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, memberOnResource, A_MR_dataQuotas);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        }
        if (memberQuotas == null || memberQuotas.getValue() == null)
            memberTransferedQuotas = new HashMap<>();
        else {
            try {
                memberTransferedQuotas = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(memberQuotas, resource, memberOnResource, true);
            } catch (WrongAttributeValueException ex) {
                throw new ConsistencyErrorException("Quotas on resource " + resource + " for member " + memberOnResource + " are in bad format.", ex);
            }
        }
        //merge quotas and add them to the big map by resources
        mergedMemberResourceQuotas.add(sess.getPerunBl().getModulesUtilsBl().mergeMemberAndResourceTransferedQuotas(memberTransferedQuotas, resourceTransferedQuotas));
    }
    //now we have all resource and member merged quotas, so we need to create 1 transfered map with sum of values
    Map<String, Pair<BigDecimal, BigDecimal>> finalTransferredQuotas = sess.getPerunBl().getModulesUtilsBl().countUserFacilityQuotas(mergedMemberResourceQuotas);
    //transfer map back to attribute value
    attribute.setValue(sess.getPerunBl().getModulesUtilsBl().transferQuotasBackToAttributeValue(finalTransferredQuotas, true));
    //return attribute
    return attribute;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) BigDecimal(java.math.BigDecimal) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with WrongAttributeAssignmentException

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

the class urn_perun_user_facility_attribute_def_virt_defaultUnixGID method setAttributeValue.

public boolean setAttributeValue(PerunSessionImpl sess, Facility facility, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    try {
        Attribute attributeToSet = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, user, AttributesManager.NS_USER_FACILITY_ATTR_DEF + ":defaultUnixGID");
        attributeToSet.setValue(attribute.getValue());
        return sess.getPerunBl().getAttributesManagerBl().setAttributeWithoutCheck(sess, facility, user, attributeToSet);
    } catch (WrongAttributeAssignmentException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeValueException ex) {
        throw new InternalErrorException(ex);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(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) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Aggregations

WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)127 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)97 Attribute (cz.metacentrum.perun.core.api.Attribute)95 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)61 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)59 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)55 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)52 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)42 User (cz.metacentrum.perun.core.api.User)31 ArrayList (java.util.ArrayList)31 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)14 Facility (cz.metacentrum.perun.core.api.Facility)14 LinkedHashMap (java.util.LinkedHashMap)11 Member (cz.metacentrum.perun.core.api.Member)10 Map (java.util.Map)9 Group (cz.metacentrum.perun.core.api.Group)8 List (java.util.List)8 Resource (cz.metacentrum.perun.core.api.Resource)7 Vo (cz.metacentrum.perun.core.api.Vo)6 HashMap (java.util.HashMap)6