Search in sources :

Example 41 with AttributeDefinition

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

the class MembersManagerBlImpl method getRichMembersWithAttributesByNames.

@Override
public List<RichMember> getRichMembersWithAttributesByNames(PerunSession sess, Vo vo, List<String> attrsNames) throws AttributeNotExistsException {
    List<Member> members = new ArrayList<>(perunBl.getMembersManagerBl().getMembers(sess, vo));
    List<RichMember> richMembers = this.convertMembersToRichMembers(sess, members);
    List<AttributeDefinition> attrsDef = new ArrayList<>();
    for (String atrrName : attrsNames) {
        AttributeDefinition attrDef = perunBl.getAttributesManagerBl().getAttributeDefinition(sess, atrrName);
        attrsDef.add(attrDef);
    }
    return this.convertMembersToRichMembersWithAttributes(sess, richMembers, attrsDef);
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) RichMember(cz.metacentrum.perun.core.api.RichMember)

Example 42 with AttributeDefinition

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

the class MembersManagerBlImpl method sponsorMember.

@Override
public Member sponsorMember(PerunSession session, Member sponsoredMember, User sponsor, LocalDate validityTo) throws MemberNotSponsoredException, AlreadySponsorException, UserNotInRoleException {
    // check that sponsoring user has role SPONSOR for the VO
    Vo vo = getMemberVo(session, sponsoredMember);
    if (!getPerunBl().getVosManagerBl().isUserInRoleForVo(session, sponsor, Role.SPONSOR, vo, true)) {
        throw new UserNotInRoleException("user " + sponsor.getId() + " is not in role SPONSOR for VO " + vo.getId());
    }
    if (!sponsoredMember.isSponsored()) {
        throw new MemberNotSponsoredException("member " + sponsoredMember.getId() + " is not marked as sponsored");
    }
    // check whether the user is already sponsor
    List<User> sponsors = getPerunBl().getUsersManagerBl().getSponsors(session, sponsoredMember);
    if (sponsors.stream().map(PerunBean::getId).anyMatch(id -> id == sponsor.getId())) {
        throw new AlreadySponsorException("member " + sponsoredMember.getId() + " is already sponsored by user " + sponsor.getId());
    }
    // add the sponsor
    getMembersManagerImpl().addSponsor(session, sponsoredMember, sponsor, validityTo);
    // remove expiration and validate member
    try {
        AttributeDefinition expiration = getPerunBl().getAttributesManagerBl().getAttributeDefinition(session, EXPIRATION);
        getPerunBl().getAttributesManagerBl().removeAttribute(session, sponsoredMember, expiration);
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException | WrongAttributeValueException | WrongReferenceAttributeValueException ex) {
        throw new InternalErrorException("cannot remove expiration date for sponsored member " + sponsoredMember.getId(), ex);
    }
    try {
        validateMember(session, sponsoredMember);
    } catch (WrongReferenceAttributeValueException | WrongAttributeValueException ex) {
        throw new InternalErrorException("cannot validate sponsored member " + sponsoredMember.getId(), ex);
    }
    getPerunBl().getAuditer().log(session, new SponsorshipEstablished(sponsoredMember, sponsor, validityTo));
    return sponsoredMember;
}
Also used : User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) SponsorshipEstablished(cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsorshipEstablished) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Vo(cz.metacentrum.perun.core.api.Vo) BanOnVo(cz.metacentrum.perun.core.api.BanOnVo) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 43 with AttributeDefinition

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

the class MembersManagerBlImpl method convertMembersToRichMembersWithAttributes.

/**
 * Adds userAttributes and memberAttributes to rich members.
 * Specifically adds attributes that are associated with the members and the resource. Attributes are also limited by the list of attributes definitions.
 * Adds member and member-resource attributes to memberAttributes and user and user-facility attributes to userAttributes.
 * The method returns list of rich members with userAttributes and memberAttributes filled.
 *
 * @param sess
 * @param richMembers
 * @param resource
 * @param attrsDef
 * @return list of rich members with userAttributes and memberAttributes filled
 * @throws InternalErrorException
 * @throws WrongAttributeAssignmentException
 */
@Override
public List<RichMember> convertMembersToRichMembersWithAttributes(PerunSession sess, List<RichMember> richMembers, Resource resource, List<AttributeDefinition> attrsDef) throws MemberResourceMismatchException {
    List<String> attrNames = new ArrayList<>();
    for (AttributeDefinition attributeDefinition : attrsDef) {
        attrNames.add(attributeDefinition.getName());
    }
    for (RichMember richMember : richMembers) {
        List<Attribute> userAttributes = new ArrayList<>();
        List<Attribute> memberAttributes = new ArrayList<>();
        List<Attribute> attributes = getPerunBl().getAttributesManagerBl().getAttributes(sess, richMember, resource, attrNames, true);
        for (Attribute attribute : attributes) {
            if (attribute.getName().startsWith(AttributesManager.NS_USER_ATTR))
                userAttributes.add(attribute);
            else if (attribute.getName().startsWith(AttributesManager.NS_USER_FACILITY_ATTR))
                userAttributes.add(attribute);
            else if (attribute.getName().startsWith(AttributesManager.NS_MEMBER_ATTR))
                memberAttributes.add(attribute);
            else if (attribute.getName().startsWith(AttributesManager.NS_MEMBER_RESOURCE_ATTR))
                memberAttributes.add(attribute);
            else {
                throw new InternalErrorException(attribute + " is not from user or member namespace (member-resource, user-facility included)!");
            }
        }
        richMember.setUserAttributes(userAttributes);
        richMember.setMemberAttributes(memberAttributes);
    }
    return richMembers;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) RichMember(cz.metacentrum.perun.core.api.RichMember)

Example 44 with AttributeDefinition

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

the class MembersManagerBlImpl method convertMembersToRichMembersWithAttributes.

/**
 * Adds userAttributes and memberAttributes to rich members.
 * Attributes are limited by the list of attributes definitions.
 * The method returns list of rich members with userAttributes and memberAttributes filled.
 *
 * @param sess
 * @param richMembers
 * @param attrsDef
 * @return list of rich members with userAttributes and memberAttributes filled
 * @throws InternalErrorException
 */
@Override
public List<RichMember> convertMembersToRichMembersWithAttributes(PerunSession sess, List<RichMember> richMembers, List<AttributeDefinition> attrsDef) {
    List<AttributeDefinition> usersAttributesDef = new ArrayList<>();
    List<AttributeDefinition> membersAttributesDef = new ArrayList<>();
    for (AttributeDefinition attrd : attrsDef) {
        if (attrd.getName().startsWith(AttributesManager.NS_USER_ATTR))
            usersAttributesDef.add(attrd);
        else if (attrd.getName().startsWith(AttributesManager.NS_MEMBER_ATTR))
            membersAttributesDef.add(attrd);
    }
    for (RichMember richMember : richMembers) {
        List<String> userAttrNames = new ArrayList<>();
        for (AttributeDefinition ad : usersAttributesDef) {
            userAttrNames.add(ad.getName());
        }
        List<Attribute> userAttributes = new ArrayList<>(getPerunBl().getAttributesManagerBl().getAttributes(sess, richMember.getUser(), userAttrNames));
        List<String> memberAttrNames = new ArrayList<>();
        for (AttributeDefinition ad : membersAttributesDef) {
            memberAttrNames.add(ad.getName());
        }
        List<Attribute> memberAttributes = new ArrayList<>(getPerunBl().getAttributesManagerBl().getAttributes(sess, richMember, memberAttrNames));
        richMember.setUserAttributes(userAttributes);
        richMember.setMemberAttributes(memberAttributes);
    }
    return richMembers;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) RichMember(cz.metacentrum.perun.core.api.RichMember)

Example 45 with AttributeDefinition

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

the class SearcherBlImpl method getFacilities.

@Override
public List<Facility> getFacilities(PerunSession sess, Map<String, String> attributesWithSearchingValues) throws AttributeNotExistsException, WrongAttributeAssignmentException {
    if (attributesWithSearchingValues == null || attributesWithSearchingValues.isEmpty()) {
        return perunBl.getFacilitiesManagerBl().getFacilities(sess);
    }
    Map<Attribute, String> mapOfAttrsWithValues = new HashMap<>();
    Map<AttributeDefinition, String> mapOfCoreAttributesWithValues = new HashMap<>();
    for (String name : attributesWithSearchingValues.keySet()) {
        if (name == null || name.equals("")) {
            throw new AttributeNotExistsException("There is no attribute with specified name!");
        }
        AttributeDefinition attrDef = perunBl.getAttributesManagerBl().getAttributeDefinition(sess, name);
        if (getPerunBl().getAttributesManagerBl().isCoreAttribute(sess, attrDef)) {
            mapOfCoreAttributesWithValues.put(attrDef, attributesWithSearchingValues.get(name));
        } else {
            mapOfAttrsWithValues.put(new Attribute(attrDef), attributesWithSearchingValues.get(name));
        }
    }
    List<Facility> facilitiesFromCoreAttributes = getFacilitiesForCoreAttributesByMapOfAttributes(sess, mapOfCoreAttributesWithValues);
    List<Facility> facilitiesFromAttributes = getSearcherImpl().getFacilities(sess, mapOfAttrsWithValues);
    facilitiesFromCoreAttributes.retainAll(facilitiesFromAttributes);
    return facilitiesFromCoreAttributes;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) Facility(cz.metacentrum.perun.core.api.Facility)

Aggregations

AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)764 Attribute (cz.metacentrum.perun.core.api.Attribute)311 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)171 ArrayList (java.util.ArrayList)111 Test (org.junit.Test)99 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)79 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)59 LinkedHashMap (java.util.LinkedHashMap)59 User (cz.metacentrum.perun.core.api.User)58 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)56 HashMap (java.util.HashMap)42 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)40 Facility (cz.metacentrum.perun.core.api.Facility)38 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)37 Resource (cz.metacentrum.perun.core.api.Resource)28 Member (cz.metacentrum.perun.core.api.Member)25 Group (cz.metacentrum.perun.core.api.Group)24 Vo (cz.metacentrum.perun.core.api.Vo)23 HashSet (java.util.HashSet)23 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)20