Search in sources :

Example 1 with GroupMoveNotAllowedException

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

the class GroupsManagerBlImpl method updateExistingGroupsWhileSynchronization.

/**
 * Get Map groupsToUpdate and update their parent group and description.
 * We don't have to update short name, because if short name has changed, group will be removed and created with new name.
 *
 * If some problem occurs, add groupToUpdate to skippedGroups and skip it.
 *
 * Method is used by group structure synchronization.
 *
 * @param sess
 * @param baseGroup under which will be group structure synchronized
 * @param groupsToUpdate list of groups for updating in Perun by information from extSource
 * @param removedGroupsIds list of ids already removed groups (these groups not exists in Perun anymore)
 * @param loginAttributeDefinition attribute definition for login of group
 * @param skippedGroups groups to be skipped because of any expected problem
 *
 * @throws InternalErrorException if some internal error occurs
 */
private void updateExistingGroupsWhileSynchronization(PerunSession sess, Group baseGroup, Map<CandidateGroup, Group> groupsToUpdate, List<Integer> removedGroupsIds, AttributeDefinition loginAttributeDefinition, List<String> skippedGroups, List<String> mergeAttributes) {
    for (CandidateGroup candidateGroup : groupsToUpdate.keySet()) {
        Group groupToUpdate = groupsToUpdate.get(candidateGroup);
        // If group had parent which was already removed from perun, it was moved under base group, change it's parent group id properly for updating
        if (removedGroupsIds.contains(groupToUpdate.getParentGroupId()))
            groupToUpdate.setParentGroupId(baseGroup.getId());
        setUpAdditionalAttributes(sess, groupToUpdate, candidateGroup.getAdditionalAttributes(), mergeAttributes);
        Group newParentGroup = specifyParentForUpdatedGroup(sess, groupToUpdate, baseGroup, candidateGroup, loginAttributeDefinition.getName());
        if (newParentGroup != null) {
            try {
                moveGroup(sess, newParentGroup, groupToUpdate);
                log.trace("Group structure synchronization {}: value of the parentGroupId for groupId {} changed. Original value {}, new value {}.", baseGroup, groupToUpdate.getId(), groupToUpdate.getParentGroupId(), newParentGroup.getId());
            } catch (GroupMoveNotAllowedException e) {
                log.warn("Can't update group {} due to group move not allowed exception {}.", groupToUpdate, e);
                skippedGroups.add("GroupEntry:[" + groupToUpdate + "] was skipped because group move is not allowed: Exception: " + e.getName() + " => " + e.getMessage() + "]");
                continue;
            } catch (WrongAttributeValueException e) {
                log.warn("Can't update group {} from baseGroup {} due to wrong attribute value exception {}.", groupToUpdate, e);
                skippedGroups.add("GroupEntry:[" + groupToUpdate + "] was skipped because of wrong attribute value: Exception: " + e.getName() + " => " + e.getMessage() + "]");
                continue;
            } catch (WrongReferenceAttributeValueException e) {
                log.warn("Can't update group {} from baseGroup {} due to wrong reference attribute value exception {}.", groupToUpdate, e);
                skippedGroups.add("GroupEntry:[" + groupToUpdate + "] was skipped because of wrong reference attribute value: Exception: " + e.getName() + " => " + e.getMessage() + "]");
                continue;
            }
        }
        boolean changed = updateGroupDescription(sess, groupToUpdate, candidateGroup);
        if (changed) {
            log.trace("Group structure synchronization {}: value of the group description for groupId {} changed. Original value {}, new value {}.", baseGroup, groupToUpdate.getId(), groupToUpdate.getDescription(), candidateGroup.asGroup().getDescription());
        }
    }
}
Also used : EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) IndirectMemberRemovedFromGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberRemovedFromGroup) CandidateGroup(cz.metacentrum.perun.core.api.CandidateGroup) RichGroup(cz.metacentrum.perun.core.api.RichGroup) MemberExpiredInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberExpiredInGroup) MemberValidatedInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberValidatedInGroup) DirectMemberRemovedFromGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberRemovedFromGroup) Group(cz.metacentrum.perun.core.api.Group) DirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup) IndirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberAddedToGroup) GroupMoveNotAllowedException(cz.metacentrum.perun.core.api.exceptions.GroupMoveNotAllowedException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) CandidateGroup(cz.metacentrum.perun.core.api.CandidateGroup)

Example 2 with GroupMoveNotAllowedException

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

the class GroupsManagerBlImpl method moveGroup.

@Override
public void moveGroup(PerunSession sess, Group destinationGroup, Group movingGroup) throws GroupMoveNotAllowedException, WrongAttributeValueException, WrongReferenceAttributeValueException {
    // check if moving group is null
    if (movingGroup == null) {
        throw new GroupMoveNotAllowedException("Moving group: cannot be null.", null, destinationGroup);
    }
    // check if moving group is members group
    if (movingGroup.getName().equals(VosManager.MEMBERS_GROUP)) {
        throw new GroupMoveNotAllowedException("It is not possible to move Members group.", movingGroup, destinationGroup);
    }
    Map<Integer, Map<Integer, MemberGroupStatus>> previousStatuses = new HashMap<>();
    // check if had parent group
    Group previousParent;
    try {
        previousParent = getParentGroup(sess, movingGroup);
        previousStatuses = getPreviousStatuses(sess, movingGroup, getGroupMembers(sess, movingGroup));
    } catch (ParentGroupNotExistsException e) {
        previousParent = null;
    }
    // if destination group is null, it means group will be moved as top level group
    if (destinationGroup != null) {
        // check if both groups are from same VO
        if (destinationGroup.getVoId() != movingGroup.getVoId()) {
            throw new GroupMoveNotAllowedException("Groups are not from same VO. Moving group: " + movingGroup + " has VO:" + movingGroup.getVoId() + " and destination group: " + destinationGroup + " has VO:" + movingGroup.getVoId() + ".", movingGroup, destinationGroup);
        }
        // check if moving group is the same as destination group
        if (destinationGroup.getId() == movingGroup.getId()) {
            throw new GroupMoveNotAllowedException("Moving group: " + movingGroup + " cannot be the same as destination group: " + destinationGroup + ".", movingGroup, destinationGroup);
        }
        // check if destination group is members group
        if (destinationGroup.getName().equals(VosManager.MEMBERS_GROUP)) {
            throw new GroupMoveNotAllowedException("It is not possible to move group under Members group.", movingGroup, destinationGroup);
        }
        // check if moving group is already under destination group
        if (movingGroup.getParentGroupId() != null && destinationGroup.getId() == movingGroup.getParentGroupId()) {
            throw new GroupMoveNotAllowedException("Moving group: " + movingGroup + " is already under destination group: " + destinationGroup + " as subGroup.", movingGroup, destinationGroup);
        }
        List<Group> movingGroupAllSubGroups = getAllSubGroups(sess, movingGroup);
        // check if destination group exists as subGroup in moving group subGroups
        if (movingGroupAllSubGroups.contains(destinationGroup)) {
            throw new GroupMoveNotAllowedException("Destination group: " + destinationGroup + " is subGroup of Moving group: " + movingGroup + ".", movingGroup, destinationGroup);
        }
        // check if this operation would create cycle
        if (checkGroupsCycle(sess, destinationGroup.getId(), movingGroup.getId())) {
            throw new GroupMoveNotAllowedException("There would be cycle created after moving group: " + movingGroup + " under destination group: " + destinationGroup + ".", movingGroup, destinationGroup);
        }
        List<Group> destinationGroupSubGroups = getSubGroups(sess, destinationGroup);
        // check if under destination group is group with same short name as Moving group short name
        for (Group group : destinationGroupSubGroups) {
            if (movingGroup.getShortName().equals(group.getShortName())) {
                throw new GroupMoveNotAllowedException("Under destination group: " + destinationGroup + " is group with the same name as moving group: " + movingGroup + ".", movingGroup, destinationGroup);
            }
        }
        // check if there is union between destination group and moving group
        if (groupsManagerImpl.isOneWayRelationBetweenGroups(destinationGroup, movingGroup)) {
            throw new GroupMoveNotAllowedException("There is already group union between moving group: " + movingGroup + " and destination group: " + destinationGroup + ".", movingGroup, destinationGroup);
        }
        // prevent existence forbidden unions between groups after moving
        // example: there are groups "A", "A:B" and "C", where "C" is included into "A" (there is a union), if we move "C" under "A:B", existing union will change to forbidden one
        // Get all unions for moving group and all of its subgroups
        Set<Group> movingGroupStructureUnions = new HashSet<>();
        movingGroupStructureUnions.addAll(getGroupUnions(sess, movingGroup, true));
        for (Group subGroup : getAllSubGroups(sess, movingGroup)) {
            movingGroupStructureUnions.addAll(getGroupUnions(sess, subGroup, true));
        }
        // prevent wrong exception in situation like "A", "A:B", "A:C" and moving "A:C" under "A:B", which is correct
        if (movingGroup.getParentGroupId() != null) {
            try {
                Group parentGroupOfMovingGroup = getParentGroup(sess, movingGroup);
                movingGroupStructureUnions.remove(parentGroupOfMovingGroup);
            } catch (ParentGroupNotExistsException ex) {
                throw new InternalErrorException("Can't find parentGroup for " + movingGroup);
            }
        }
        // Get all group stucture of destination group (destination group and all its parent groups)
        Set<Group> destinationGroupStructure = new HashSet<>();
        destinationGroupStructure.add(destinationGroup);
        destinationGroupStructure.addAll(getParentGroups(sess, destinationGroup));
        movingGroupStructureUnions.retainAll(destinationGroupStructure);
        if (!movingGroupStructureUnions.isEmpty()) {
            throw new GroupMoveNotAllowedException("After moving of moving group: " + movingGroup + " under destination group: " + destinationGroup + " there will exist forbidden indirect relationship.", movingGroup, destinationGroup);
        }
        processRelationsWhileMovingGroup(sess, destinationGroup, movingGroup);
        // We have to set group attributes so we can update it in database
        movingGroup.setParentGroupId(destinationGroup.getId());
        String finalGroupName = destinationGroup.getName() + ":" + movingGroup.getShortName();
        movingGroup.setName(finalGroupName);
    } else {
        // check if moving group is already top level group
        if (movingGroup.getParentGroupId() == null) {
            throw new GroupMoveNotAllowedException("Moving group: " + movingGroup + " is already top level group.", movingGroup, null);
        }
        List<Group> destinationGroupSubGroups = getGroups(sess, getVo(sess, movingGroup));
        // check if there is top level group with same short name as Moving group short name
        for (Group group : destinationGroupSubGroups) {
            if (movingGroup.getShortName().equals(group.getName())) {
                throw new GroupMoveNotAllowedException("There is already top level group with the same name as moving group: " + movingGroup + ".", movingGroup, null);
            }
        }
        processRelationsWhileMovingGroup(sess, null, movingGroup);
        // We have to set group attributes so we can update it in database
        movingGroup.setParentGroupId(null);
        movingGroup.setName(movingGroup.getShortName());
    }
    // we have to update group name in database
    getGroupsManagerImpl().updateGroupName(sess, movingGroup);
    // We have to properly set all subGroups names level by level
    setSubGroupsNames(sess, getSubGroups(sess, movingGroup), movingGroup);
    // Remove movingGroup-resource autoassignments where destination group is not subgroup of assignment's source group
    fixMovedTreeAutoassignments(sess, destinationGroup, movingGroup);
    // And finally update parentGroupId for moving group in database
    this.updateParentGroupId(sess, movingGroup);
    List<Member> movingGroupMembers = getGroupMembers(sess, movingGroup);
    for (Member member : movingGroupMembers) {
        if (previousParent != null) {
            // calculate new member-group statuses for members from previous moving group parent
            recalculateMemberGroupStatusRecursively(sess, member, previousParent, previousStatuses);
        }
    }
    getPerunBl().getAuditer().log(sess, new GroupMoved(movingGroup));
    // Check if moved group should be autoassigned to destination's group resource
    if (destinationGroup != null) {
        autoassignMovedTree(sess, destinationGroup, movingGroup);
    }
}
Also used : EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) IndirectMemberRemovedFromGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberRemovedFromGroup) CandidateGroup(cz.metacentrum.perun.core.api.CandidateGroup) RichGroup(cz.metacentrum.perun.core.api.RichGroup) MemberExpiredInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberExpiredInGroup) MemberValidatedInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberValidatedInGroup) DirectMemberRemovedFromGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberRemovedFromGroup) Group(cz.metacentrum.perun.core.api.Group) DirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup) IndirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberAddedToGroup) GroupMoveNotAllowedException(cz.metacentrum.perun.core.api.exceptions.GroupMoveNotAllowedException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) GroupMoved(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupMoved) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet)

Example 3 with GroupMoveNotAllowedException

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

the class GroupsManagerBlImpl method removeFormerGroupsWhileSynchronization.

/**
 * remove groups which are not listed in extSource anymore
 *
 * If some problem occurs, add groupToRemove to skippedGroups and skip it.
 *
 * Method is used by group structure synchronization.
 *
 * @param sess
 * @param baseGroup from which we will be removing groups
 * @param groupsToRemove list of groups to be removed from baseGroup
 *
 * @return list of ids already removed groups
 * @throws InternalErrorException if some internal error occurs
 */
private List<Integer> removeFormerGroupsWhileSynchronization(PerunSession sess, Group baseGroup, List<Group> groupsToRemove, List<String> skippedGroups) {
    List<Integer> removedGroups = new ArrayList<>();
    groupsToRemove.sort(reverseOrder(comparingInt(g -> g.getName().length())));
    for (Group groupToRemove : groupsToRemove) {
        try {
            groupToRemove = moveSubGroupsUnderBaseGroup(sess, groupToRemove, baseGroup);
            deleteGroup(sess, groupToRemove, true);
            removedGroups.add(groupToRemove.getId());
            log.info("Group structure synchronization {}: Group id {} removed.", baseGroup, groupToRemove.getId());
        } catch (RelationExistsException e) {
            log.warn("Can't remove group {} from baseGroup {} due to group relation exists exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because group relation exists: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (GroupAlreadyRemovedException | GroupAlreadyRemovedFromResourceException e) {
            log.debug("Group {} was removed from group {} before removing process. Skip this group.", groupToRemove, baseGroup);
        } catch (GroupNotExistsException e) {
            log.warn("Can't remove group {} from baseGroup {} due to group not exists exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because group does not exists: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (GroupRelationDoesNotExist e) {
            log.warn("Can't remove group {} from baseGroup {} due to group relation does not exists exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because group relation does not exists: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (GroupRelationCannotBeRemoved e) {
            log.warn("Can't remove group {} from baseGroup {} due to group relation cannot be removed exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because group relation cannot be removed: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (GroupMoveNotAllowedException e) {
            log.warn("Can't remove group {} from baseGroup {} due to group move not allowed exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because group move is not allowed: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (WrongAttributeValueException e) {
            log.warn("Can't remove group {} from baseGroup {} due to wrong attribute value exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because wrong attribute value: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (WrongReferenceAttributeValueException e) {
            log.warn("Can't remove group {} from baseGroup {} due to wrong reference attribute value exception {}.", groupToRemove, e);
            skippedGroups.add("GroupEntry:[" + groupToRemove + "] was skipped because wrong reference attribute value: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        }
    }
    return removedGroups;
}
Also used : EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) IndirectMemberRemovedFromGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberRemovedFromGroup) CandidateGroup(cz.metacentrum.perun.core.api.CandidateGroup) RichGroup(cz.metacentrum.perun.core.api.RichGroup) MemberExpiredInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberExpiredInGroup) MemberValidatedInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberValidatedInGroup) DirectMemberRemovedFromGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberRemovedFromGroup) Group(cz.metacentrum.perun.core.api.Group) DirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup) IndirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberAddedToGroup) GroupRelationCannotBeRemoved(cz.metacentrum.perun.core.api.exceptions.GroupRelationCannotBeRemoved) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) GroupMoveNotAllowedException(cz.metacentrum.perun.core.api.exceptions.GroupMoveNotAllowedException) GroupRelationDoesNotExist(cz.metacentrum.perun.core.api.exceptions.GroupRelationDoesNotExist) ArrayList(java.util.ArrayList) RelationExistsException(cz.metacentrum.perun.core.api.exceptions.RelationExistsException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) GroupAlreadyRemovedFromResourceException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) GroupAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedException)

Aggregations

DirectMemberAddedToGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup)3 DirectMemberRemovedFromGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberRemovedFromGroup)3 IndirectMemberAddedToGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberAddedToGroup)3 IndirectMemberRemovedFromGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberRemovedFromGroup)3 MemberExpiredInGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberExpiredInGroup)3 MemberValidatedInGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberValidatedInGroup)3 CandidateGroup (cz.metacentrum.perun.core.api.CandidateGroup)3 EnrichedGroup (cz.metacentrum.perun.core.api.EnrichedGroup)3 Group (cz.metacentrum.perun.core.api.Group)3 RichGroup (cz.metacentrum.perun.core.api.RichGroup)3 GroupMoveNotAllowedException (cz.metacentrum.perun.core.api.exceptions.GroupMoveNotAllowedException)3 ParentGroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException)2 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)2 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)2 GroupMoved (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupMoved)1 Member (cz.metacentrum.perun.core.api.Member)1 RichMember (cz.metacentrum.perun.core.api.RichMember)1 GroupAlreadyRemovedException (cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedException)1 GroupAlreadyRemovedFromResourceException (cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException)1 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)1