Search in sources :

Example 41 with InternalErrorException

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

the class ResourcesManagerImpl method getAdmins.

public List<User> getAdmins(PerunSession sess, Resource resource) throws InternalErrorException {
    try {
        Set<User> setOfAdmins = new HashSet<>();
        // Direct admins
        setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from authz join users on authz.user_id=users.id" + "  where authz.resource_id=? and authz.role_id=(select id from roles where name=?)", UsersManagerImpl.USER_MAPPER, resource.getId(), Role.RESOURCEADMIN.getRoleName()));
        // Admins through a group
        List<Group> listOfGroupAdmins = getAdminGroups(sess, resource);
        for (Group authorizedGroup : listOfGroupAdmins) {
            setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from users join members on users.id=members.user_id " + "join groups_members on groups_members.member_id=members.id where groups_members.group_id=?", UsersManagerImpl.USER_MAPPER, authorizedGroup.getId()));
        }
        return new ArrayList(setOfAdmins);
    } catch (EmptyResultDataAccessException e) {
        return new ArrayList<>();
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 42 with InternalErrorException

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

the class urn_perun_group_attribute_def_def_unixGID_namespace method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    String gidNamespace = attribute.getFriendlyNameParameter();
    //get attribute with usedGids for update
    //IMPORTANT: for update lock row in table of attr values, be careful when using
    Attribute usedGids;
    try {
        usedGids = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, gidNamespace, A_E_usedGids);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
    //Get Map of gids (if there is no value, use empty map
    Map<String, String> usedGidsValue = new LinkedHashMap<>();
    if (usedGids.getValue() != null)
        usedGidsValue = (Map<String, String>) usedGids.getValue();
    //initial settings
    String key = "G" + group.getId();
    String oldGid = usedGidsValue.get(key);
    //for removing gid
    if (attribute.getValue() == null) {
        //remove record from map
        if (oldGid != null) {
            usedGidsValue.remove(key);
            //looking for another oldGid value, if not exists, add depleted record
            if (!usedGidsValue.containsValue(oldGid)) {
                usedGidsValue.put("D" + oldGid, oldGid);
            }
        }
    //for setting gid
    } else {
        String newUnixGid = ((Integer) attribute.getValue()).toString();
        //add new record to map
        usedGidsValue.put(key, newUnixGid);
        //looking for another oldGid value, if not exists, add depleted record
        if (oldGid != null && !usedGidsValue.containsValue(oldGid)) {
            usedGidsValue.put("D" + oldGid, oldGid);
        }
    }
    //set new attribute value for usedGids
    usedGids.setValue(usedGidsValue);
    try {
        session.getPerunBl().getAttributesManagerBl().setAttribute(session, gidNamespace, usedGids);
    } catch (WrongAttributeValueException ex) {
        throw new WrongReferenceAttributeValueException(attribute, usedGids, ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
}
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) 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) LinkedHashMap(java.util.LinkedHashMap)

Example 43 with InternalErrorException

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

the class urn_perun_group_attribute_def_def_unixGroupName_namespace method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    //Need to know if this is remove or set, if value is null, its remove, otherway it is set
    String groupNameNamespace = attribute.getFriendlyNameParameter();
    try {
        if (attribute.getValue() == null) {
        //This is ok, for now no changes for removing some GroupName of this Group
        } else {
            //First need to find all facilities for the group
            Set<Facility> facilitiesOfGroup = new HashSet<Facility>();
            List<Resource> resourcesOfGroup = session.getPerunBl().getResourcesManagerBl().getAssignedResources(session, group);
            for (Resource r : resourcesOfGroup) {
                facilitiesOfGroup.add(session.getPerunBl().getResourcesManagerBl().getFacility(session, r));
            }
            //Prepare list of gid namespaces of all facilities which have the same groupName namespace like this unixGroupName namespace
            Set<String> gidNamespaces;
            gidNamespaces = session.getPerunBl().getModulesUtilsBl().getSetOfGIDNamespacesWhereFacilitiesHasTheSameGroupNameNamespace(session, new ArrayList<Facility>(facilitiesOfGroup), attribute);
            //If there is any gidNamespace which is need to be set, do it there
            if (!gidNamespaces.isEmpty()) {
                List<Attribute> gidsToSet = new ArrayList<>();
                for (String s : gidNamespaces) {
                    Attribute groupUnixGIDNamespace = session.getPerunBl().getAttributesManagerBl().getAttribute(session, group, A_G_unixGID_namespace + ":" + s);
                    //If attribute is not set, then set it (first fill, then set)
                    if (groupUnixGIDNamespace.getValue() == null) {
                        groupUnixGIDNamespace = session.getPerunBl().getAttributesManagerBl().fillAttribute(session, group, groupUnixGIDNamespace);
                        if (groupUnixGIDNamespace.getValue() == null)
                            throw new WrongReferenceAttributeValueException(attribute, groupUnixGIDNamespace);
                        //Set after fill (without check because all namespaces must be set before check (there can be relation between namespaces)
                        gidsToSet.add(groupUnixGIDNamespace);
                    }
                }
                //set and check if there is some gid to set
                if (!gidsToSet.isEmpty()) {
                    try {
                        session.getPerunBl().getAttributesManagerBl().setAttributes(session, group, gidsToSet);
                    } catch (WrongAttributeValueException e) {
                        throw new WrongReferenceAttributeValueException(attribute, e.getAttribute(), group, null, e.getAttributeHolder(), e.getAttributeHolderSecondary(), "Problem when setting all needed GIDs in hook.", e);
                    }
                }
            }
        }
    } catch (WrongAttributeAssignmentException ex) {
        //TODO: need to add WrongAttributeAssignmentException to header of modules methods
        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) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) HashSet(java.util.HashSet)

Example 44 with InternalErrorException

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

the class urn_perun_member_attribute_def_def_mail method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, Member member, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    User user = session.getPerunBl().getUsersManagerBl().getUserByMember(session, member);
    if (attribute.getValue() != null) {
        Attribute userPreferredMail = null;
        try {
            userPreferredMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, A_U_preferredMail);
            if (userPreferredMail.getValue() == null) {
                userPreferredMail.setValue(attribute.getValue());
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);
            }
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeValueException ex) {
            throw new WrongReferenceAttributeValueException(attribute, userPreferredMail, "Mismatch in checking of member mail and user preferredMail (different checking rules).", ex);
        }
    }
/* This funcionality is not needed now
		//if this mail has been removed, check user preffered mail if the value is still correct, if not set a new one or remove it if no other exists
		User user = session.getPerunBl().getUsersManagerBl().getUserByMember(session, member);
		try {
		Attribute userPreferredMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, A_U_preferredMail);
		//TODO: if userPreferredMail is null when memberMail has been removed, its error in consistency, but exception is not solution, need to log it

		//If member mail has been removed
		if(attribute.getValue() == null) {
		try {
		session.getPerunBl().getAttributesManagerBl().checkAttributeValue(session, user, userPreferredMail);
		} catch (WrongAttributeValueException ex) {
		List<Member> membersOfUser = session.getPerunBl().getMembersManagerBl().getMembersByUser(session, user);
		for(Member m: membersOfUser) {
		Attribute memberMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, member, A_M_mail);
		if(memberMail.getValue() != null) {
		userPreferredMail.setValue(memberMail.getValue());
		session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);
		break;
		}
		}
		userPreferredMail.setValue(null);
		session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);

		}
		//if member mail was new set or set to another value
		} else {
		//if userPreferredMail is null, so can save this new value there
		if(userPreferredMail.getValue() == null) {
		userPreferredMail.setValue(attribute.getValue());
		session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);

		//if userPreferredMail is not null, need to try if the old value is still correct, if not, save new value there
		} else {
		try {
		session.getPerunBl().getAttributesManagerBl().checkAttributeValue(session, user, userPreferredMail);
		} catch (WrongAttributeValueException ex) {
		//old value of userPreferredMail is not correct now, save the new value from member mail there
		userPreferredMail.setValue(attribute.getValue());
		session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, attribute);
		}
		}
		}
		} catch(WrongAttributeAssignmentException ex) {
		throw new InternalErrorException(ex);
		} catch(AttributeNotExistsException ex) {
		throw new ConsistencyErrorException(ex);
		} catch(WrongAttributeValueException ex) {
		throw new WrongReferenceAttributeValueException("There is mismatch between possible format of member mail and userPreferredMail", 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) 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 45 with InternalErrorException

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

the class urn_perun_group_resource_attribute_def_virt_unixGID method setAttributeValue.

@Override
public boolean setAttributeValue(PerunSessionImpl sess, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    Attribute unixGIDNamespaceAttribute = sess.getPerunBl().getModulesUtilsBl().getUnixGIDNamespaceAttributeWithNotNullValue(sess, resource);
    try {
        Attribute gidAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:" + unixGIDNamespaceAttribute.getValue()));
        gidAttribute.setValue(attribute.getValue());
        return sess.getPerunBl().getAttributesManagerBl().setAttributeWithoutCheck(sess, group, gidAttribute);
    } catch (WrongAttributeValueException ex) {
        throw new InternalErrorException(ex);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (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) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Aggregations

InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)376 Attribute (cz.metacentrum.perun.core.api.Attribute)119 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)104 ArrayList (java.util.ArrayList)94 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)89 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)78 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)68 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)67 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)44 User (cz.metacentrum.perun.core.api.User)37 Group (cz.metacentrum.perun.core.api.Group)36 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)33 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)33 HashMap (java.util.HashMap)30 IOException (java.io.IOException)28 Member (cz.metacentrum.perun.core.api.Member)24 Map (java.util.Map)24 Facility (cz.metacentrum.perun.core.api.Facility)23 List (java.util.List)23 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)22