Search in sources :

Example 1 with BanOnResource

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

the class urn_perun_member_resource_attribute_def_virt_isBannedTest method setUp.

@Before
public void setUp() throws Exception {
    classInstance = new urn_perun_member_resource_attribute_def_virt_isBanned();
    session = mock(PerunSessionImpl.class, RETURNS_DEEP_STUBS);
    facility = new Facility(1, "testFacility");
    resource = new Resource(1, "testResource", "desc", 1, 1);
    vo = new Vo(1, "testVo", "desc");
    user = new User(1, "name", "surname", "middlename", "title", "title");
    member = new Member(1, 1, 1, Status.VALID);
    banOnResource = new BanOnResource(1, new Date(), "test", 1, 1);
    banOnFacility = new BanOnFacility(1, new Date(), "test", 1, 1);
    isBanned = new Attribute(classInstance.getAttributeDefinition());
    isBanned.setValue(true);
    message1 = "Ban " + banOnResource.serializeToString() + " was set for memberId 1 on resourceId 1";
    message2 = "Ban " + banOnResource.serializeToString() + " was updated for memberId 1 on resourceId 1";
    message3 = "Ban " + banOnResource.serializeToString() + " was removed for memberId 1 on resourceId 1";
    message4 = "Ban " + banOnFacility.serializeToString() + " was set for userId 1 on facilityId 1";
    message5 = "Ban " + banOnFacility.serializeToString() + " was updated for userId 1 on facilityId 1";
    message6 = "Ban " + banOnFacility.serializeToString() + " was removed for userId 1 on facilityId 1";
    wrongMessage = "Ban " + banOnFacility.serializeToString() + " was destroyed for userId 1 on facilityId 1";
}
Also used : User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) BanOnFacility(cz.metacentrum.perun.core.api.BanOnFacility) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) Resource(cz.metacentrum.perun.core.api.Resource) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility) BanOnFacility(cz.metacentrum.perun.core.api.BanOnFacility) Member(cz.metacentrum.perun.core.api.Member) PerunSessionImpl(cz.metacentrum.perun.core.impl.PerunSessionImpl) Date(java.util.Date) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) Before(org.junit.Before)

Example 2 with BanOnResource

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

the class MembersManagerBlImpl method deleteMember.

public void deleteMember(PerunSession sess, Member member) throws InternalErrorException, MemberAlreadyRemovedException, GroupOperationsException {
    Vo vo = this.getMemberVo(sess, member);
    User user;
    try {
        user = getPerunBl().getUsersManagerBl().getUserById(sess, member.getUserId());
    } catch (UserNotExistsException e1) {
        throw new ConsistencyErrorException("Removing member who doesn't have corresponding user.", e1);
    }
    List<Facility> allowedFacilities = getPerunBl().getFacilitiesManagerBl().getAllowedFacilities(sess, user);
    Map<Facility, List<Attribute>> requiredAttributesBeforeMemberRemove = new HashMap<Facility, List<Attribute>>();
    for (Facility facility : allowedFacilities) {
        // Get actually required attributes, they will be later compared with list of required attributes when the member will be removed from all resources in this VO
        requiredAttributesBeforeMemberRemove.put(facility, getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, facility, user));
    }
    // Remove member from all groups
    List<Group> memberGroups = getPerunBl().getGroupsManagerBl().getMemberDirectGroups(sess, member);
    for (Group group : memberGroups) {
        // Member must be removed from the members group using separate method
        if (group.getName().equals(VosManager.MEMBERS_GROUP))
            continue;
        try {
            getPerunBl().getGroupsManagerBl().removeMember(sess, group, member);
        } catch (NotGroupMemberException e) {
            throw new ConsistencyErrorException("getMemberGroups return group where the member is not member", e);
        } catch (GroupNotExistsException e) {
            throw new ConsistencyErrorException(e);
        }
    }
    // Remove member from the VO members group
    try {
        Group g = getPerunBl().getGroupsManagerBl().getGroupByName(sess, vo, VosManager.MEMBERS_GROUP);
        try {
            getPerunBl().getGroupsManagerBl().removeMemberFromMembersOrAdministratorsGroup(sess, g, member);
        } catch (NotGroupMemberException e) {
            throw new ConsistencyErrorException("Member is not in the \"members\" group." + member + "  " + g, e);
        }
    } catch (GroupNotExistsException e) {
        throw new InternalErrorException(e);
    }
    // Remove member's  attributes (namespaces: member and resource-member)
    try {
        getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, member);
        List<Resource> resources = getPerunBl().getResourcesManagerBl().getResources(sess, vo);
        for (Resource resource : resources) {
            getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, resource, member);
        }
    } catch (AttributeValueException ex) {
        throw new ConsistencyErrorException("Member is removed from all groups. There are no required attribute for this member. Member's attributes can be removed without problem.", ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    // Remove user-facility attributes which are no longer required
    for (Facility facility : allowedFacilities) {
        List<Attribute> requiredAttributes = requiredAttributesBeforeMemberRemove.get(facility);
        //remove currently required attributes from requiredAttributesBeforeMemberRemove
        requiredAttributes.removeAll(getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, facility, user));
        //remove attributes which are no longer required
        try {
            getPerunBl().getAttributesManagerBl().removeAttributes(sess, facility, user, requiredAttributes);
        } catch (AttributeValueException | WrongAttributeAssignmentException ex) {
            throw new ConsistencyErrorException(ex);
        }
    }
    //Remove all members bans
    List<BanOnResource> bansOnResource = getPerunBl().getResourcesManagerBl().getBansForMember(sess, member.getId());
    for (BanOnResource banOnResource : bansOnResource) {
        try {
            getPerunBl().getResourcesManagerBl().removeBan(sess, banOnResource.getId());
        } catch (BanNotExistsException ex) {
        //it is ok, we just want to remove it anyway
        }
    }
    /* TODO this can be used for future optimization. If the user is not asigned to the facility anymore all user-facility attributes (for this facility) can be safely removed.
			 for (Facility facility: facilitiesBeforeMemberRemove) {
		// Remove user-facility attributes
		try {
		getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, facility, user);
		log.debug("Removing user-facility attributes for facility {}", facility);
		} catch (AttributeValueException e) {
		throw new ConsistencyErrorException("Member is removed from all resources. There are no required attribute for this member. User-facility attributes can be removed without problem.", e);
		}
			 }
			 */
    // Remove member from the DB
    getMembersManagerImpl().deleteMember(sess, member);
    getPerunBl().getAuditer().log(sess, "{} deleted.", member);
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) Resource(cz.metacentrum.perun.core.api.Resource) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) AttributeValueException(cz.metacentrum.perun.core.api.exceptions.AttributeValueException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) Vo(cz.metacentrum.perun.core.api.Vo) List(java.util.List) ArrayList(java.util.ArrayList) Facility(cz.metacentrum.perun.core.api.Facility) BanNotExistsException(cz.metacentrum.perun.core.api.exceptions.BanNotExistsException)

Aggregations

Attribute (cz.metacentrum.perun.core.api.Attribute)2 BanOnResource (cz.metacentrum.perun.core.api.BanOnResource)2 Facility (cz.metacentrum.perun.core.api.Facility)2 Resource (cz.metacentrum.perun.core.api.Resource)2 User (cz.metacentrum.perun.core.api.User)2 Vo (cz.metacentrum.perun.core.api.Vo)2 BanOnFacility (cz.metacentrum.perun.core.api.BanOnFacility)1 Group (cz.metacentrum.perun.core.api.Group)1 Member (cz.metacentrum.perun.core.api.Member)1 AttributeValueException (cz.metacentrum.perun.core.api.exceptions.AttributeValueException)1 BanNotExistsException (cz.metacentrum.perun.core.api.exceptions.BanNotExistsException)1 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)1 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 NotGroupMemberException (cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException)1 ParentGroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)1 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)1 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)1