Search in sources :

Example 1 with GroupExistsException

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

the class GroupsManagerImpl method createGroup.

@Override
public Group createGroup(PerunSession sess, Vo vo, Group group) throws GroupExistsException {
    Utils.notNull(group, "group");
    Utils.notNull(group.getName(), "group.getName()");
    // Check if the group already exists
    if (group.getParentGroupId() == null) {
        // check if the TOP level group exists
        if (1 == jdbc.queryForInt("select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id IS NULL", group.getName(), vo.getId())) {
            throw new GroupExistsException("Group [" + group.getName() + "] already exists under VO [" + vo.getShortName() + "] and has parent Group with id is [NULL]");
        }
    } else {
        // check if subgroup exists under parent group
        if (1 == jdbc.queryForInt("select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id=?", group.getName(), vo.getId(), group.getParentGroupId())) {
            throw new GroupExistsException("Group [" + group.getName() + "] already exists under VO [" + vo.getShortName() + "] and has parent Group with id [" + group.getParentGroupId() + "]");
        }
    }
    Utils.validateGroupName(group.getShortName());
    int newId;
    try {
        // Store the group into the DB
        newId = Utils.getNewId(jdbc, "groups_id_seq");
        jdbc.update("insert into groups (id, parent_group_id, name, dsc, vo_id, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) " + "values (?,?,?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", newId, group.getParentGroupId(), group.getName(), group.getDescription(), vo.getId(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
    } catch (RuntimeException err) {
        throw new InternalErrorException(err);
    }
    Group newGroup;
    try {
        newGroup = getGroupById(sess, newId);
    } catch (GroupNotExistsException e) {
        throw new InternalErrorException("Failed to read newly created group with id: " + newId, e);
    }
    group.setId(newId);
    group.setUuid(newGroup.getUuid());
    group.setVoId(newGroup.getVoId());
    return newGroup;
}
Also used : EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) Group(cz.metacentrum.perun.core.api.Group) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with GroupExistsException

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

the class VosManagerBlImpl method createVo.

@Override
public Vo createVo(PerunSession sess, Vo vo) throws VoExistsException {
    // Create entries in the DB and Grouper
    vo = getVosManagerImpl().createVo(sess, vo);
    getPerunBl().getAuditer().log(sess, new VoCreated(vo));
    User user = sess.getPerunPrincipal().getUser();
    // set creator as VO manager
    if (user != null) {
        try {
            AuthzResolverBlImpl.setRole(sess, user, vo, Role.VOADMIN);
            log.debug("User {} added like administrator to VO {}", user, vo);
        } catch (AlreadyAdminException ex) {
            throw new ConsistencyErrorException("Add manager to newly created VO failed because there is a particular manager already assigned", ex);
        } catch (RoleCannotBeManagedException e) {
            throw new InternalErrorException(e);
        }
    } else {
        log.error("Can't set VO manager during creating of the VO. User from perunSession is null. {} {}", vo, sess);
    }
    try {
        // Create group containing VO members
        Group members = new Group(VosManager.MEMBERS_GROUP, VosManager.MEMBERS_GROUP_DESCRIPTION + " for VO " + vo.getName());
        getPerunBl().getGroupsManagerBl().createGroup(sess, vo, members);
        log.debug("Members group created, vo '{}'", vo);
    } catch (GroupExistsException e) {
        throw new ConsistencyErrorException("Group already exists", e);
    }
    // create empty application form
    getVosManagerImpl().createApplicationForm(sess, vo);
    log.info("Vo {} created", vo);
    return vo;
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) VoCreated(cz.metacentrum.perun.audit.events.VoManagerEvents.VoCreated) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException)

Example 3 with GroupExistsException

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

the class GroupsManagerBlImpl method updateGroupDescription.

/**
 * Update group description.
 *
 * Method is used by group structure synchronization.
 *
 * @param sess
 * @param groupWithOldDescription group with description which will be chaged
 * @param groupWithNewDescription candidate group with description which will replace the old one
 * @return true if description changed, false otherwise
 * @throws InternalErrorException
 */
private boolean updateGroupDescription(PerunSession sess, Group groupWithOldDescription, CandidateGroup groupWithNewDescription) {
    // If the old description is not null, compare it with the newDescription and update it if they differ
    if (groupWithOldDescription.getDescription() != null) {
        if (!groupWithOldDescription.getDescription().equals(groupWithNewDescription.asGroup().getDescription())) {
            groupWithOldDescription.setDescription(groupWithNewDescription.asGroup().getDescription());
            try {
                groupsManagerImpl.updateGroup(sess, groupWithOldDescription);
            } catch (GroupExistsException ex) {
                throw new InternalErrorException("Unexpected exception when trying to modify group description!");
            }
            getPerunBl().getAuditer().log(sess, new GroupUpdated(groupWithOldDescription));
            return true;
        }
    // If the new description is not null set the old description to new one
    } else if (groupWithNewDescription.asGroup().getDescription() != null) {
        groupWithOldDescription.setDescription(groupWithNewDescription.asGroup().getDescription());
        try {
            groupsManagerImpl.updateGroup(sess, groupWithOldDescription);
        } catch (GroupExistsException ex) {
            throw new InternalErrorException("Unexpected exception when trying to modify group description!");
        }
        getPerunBl().getAuditer().log(sess, new GroupUpdated(groupWithOldDescription));
        return true;
    }
    return false;
}
Also used : GroupUpdated(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupUpdated) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 4 with GroupExistsException

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

the class GroupsManagerBlImpl method addMissingGroupsWhileSynchronization.

/**
 * Add missing groups under base group in Perun
 *
 * If some problem occurs, add candidateGroup to skippedGroups and skip it.
 *
 * Method is used by group structure synchronization.
 *
 * @param sess
 * @param baseGroup under which we will be synchronizing groups
 * @param candidateGroupsToAdd list of new groups (candidateGroups)
 * @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 addMissingGroupsWhileSynchronization(PerunSession sess, Group baseGroup, List<CandidateGroup> candidateGroupsToAdd, AttributeDefinition loginAttributeDefinition, List<String> skippedGroups, List<String> mergeAttributes) {
    Map<CandidateGroup, Group> groupsToUpdate = new HashMap<>();
    // create all groups under base group first
    for (CandidateGroup candidateGroup : candidateGroupsToAdd) {
        try {
            // create group
            Group createdGroup = createGroup(sess, baseGroup, candidateGroup.asGroup());
            groupsToUpdate.put(candidateGroup, createdGroup);
            log.info("Group structure synchronization under base group {}: New Group id {} created during synchronization.", baseGroup, createdGroup.getId());
            // set login for group
            String login = candidateGroup.getLogin();
            if (login == null)
                throw new InternalErrorException("Login of candidate group " + candidateGroup + " can't be null!");
            Attribute loginAttribute = new Attribute(loginAttributeDefinition);
            loginAttribute.setValue(login);
            getPerunBl().getAttributesManagerBl().setAttribute(sess, createdGroup, loginAttribute);
        } catch (GroupExistsException e) {
            log.warn("Group {} was added to group structure {} before adding process. Skip this group.", candidateGroup, baseGroup);
            skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because it was added to group structure before adding process: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (GroupRelationNotAllowed e) {
            log.warn("Can't create group from candidate group {} due to group relation not allowed exception {}.", candidateGroup, e);
            skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation was not allowed: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (GroupRelationAlreadyExists e) {
            log.warn("Can't create group from candidate group {} due to group relation already exists exception {}.", candidateGroup, e);
            skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation already exists: Exception: " + e.getName() + " => " + e.getMessage() + "]");
        } catch (WrongAttributeAssignmentException ex) {
            // this means wrong setting of login attribute
            throw new InternalErrorException(ex);
        } catch (WrongAttributeValueException | WrongReferenceAttributeValueException ex) {
            throw new InternalErrorException("Group login can't be set because of wrong value!", ex);
        }
    }
    // update newly added groups cause the hierarchy could be incorrect
    // no need to send list of removed parent groups here, because it is no need to resolve it for new groups at all
    updateExistingGroupsWhileSynchronization(sess, baseGroup, groupsToUpdate, Collections.emptyList(), loginAttributeDefinition, skippedGroups, mergeAttributes);
}
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) GroupRelationNotAllowed(cz.metacentrum.perun.core.api.exceptions.GroupRelationNotAllowed) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) CandidateGroup(cz.metacentrum.perun.core.api.CandidateGroup) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) GroupRelationAlreadyExists(cz.metacentrum.perun.core.api.exceptions.GroupRelationAlreadyExists)

Example 5 with GroupExistsException

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

the class GroupsManagerImpl method updateGroup.

@Override
public Group updateGroup(PerunSession sess, Group group) throws GroupExistsException {
    Utils.notNull(group.getName(), "group.getName()");
    // Get the group stored in the DB
    Group dbGroup;
    try {
        dbGroup = this.getGroupById(sess, group.getId());
    } catch (GroupNotExistsException e) {
        throw new InternalErrorException("Group existence was checked at the higher level", e);
    }
    // we allow only update on shortName part of name
    if (!dbGroup.getShortName().equals(group.getShortName())) {
        Utils.validateGroupName(group.getShortName());
        dbGroup.setShortName(group.getShortName());
        try {
            jdbc.update("update groups set name=?,modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", dbGroup.getName(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), dbGroup.getId());
        } catch (DataIntegrityViolationException e) {
            throw new GroupExistsException("The name must be unique and it's already occupied - " + dbGroup, e);
        } catch (RuntimeException e) {
            throw new InternalErrorException(e);
        }
    }
    if (group.getDescription() != null && !group.getDescription().equals(dbGroup.getDescription())) {
        try {
            jdbc.update("update groups set dsc=?, modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", group.getDescription(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), group.getId());
        } catch (RuntimeException e) {
            throw new InternalErrorException(e);
        }
        dbGroup.setDescription(group.getDescription());
    }
    return dbGroup;
}
Also used : EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) Group(cz.metacentrum.perun.core.api.Group) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

GroupExistsException (cz.metacentrum.perun.core.api.exceptions.GroupExistsException)5 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 Group (cz.metacentrum.perun.core.api.Group)4 EnrichedGroup (cz.metacentrum.perun.core.api.EnrichedGroup)3 AssignedGroup (cz.metacentrum.perun.core.api.AssignedGroup)2 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)2 ParentGroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException)2 DirectMemberAddedToGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup)1 DirectMemberRemovedFromGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberRemovedFromGroup)1 GroupUpdated (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupUpdated)1 IndirectMemberAddedToGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberAddedToGroup)1 IndirectMemberRemovedFromGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberRemovedFromGroup)1 MemberExpiredInGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberExpiredInGroup)1 MemberValidatedInGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberValidatedInGroup)1 VoCreated (cz.metacentrum.perun.audit.events.VoManagerEvents.VoCreated)1 Attribute (cz.metacentrum.perun.core.api.Attribute)1 CandidateGroup (cz.metacentrum.perun.core.api.CandidateGroup)1 RichGroup (cz.metacentrum.perun.core.api.RichGroup)1 RichUser (cz.metacentrum.perun.core.api.RichUser)1 User (cz.metacentrum.perun.core.api.User)1