Search in sources :

Example 36 with WrongAttributeAssignmentException

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

the class RTMessagesManagerBlImpl method prepareDataAndGetHttpRequest.

private HttpUriRequest prepareDataAndGetHttpRequest(PerunSession sess, int voId, String queue, String requestor, String subject, String text) throws InternalErrorException {
    //Ticket from this part is already evidet like 'new'
    String id = "ticket/new";
    //If there is no requestor, it is uknown requestor
    if (requestor == null || requestor.isEmpty()) {
        requestor = "unknown";
    }
    //If queue is null, try to check if exist value in attribute rtVoQueue, if not, use default
    if (queue == null || queue.isEmpty()) {
        Vo vo = null;
        if (voId != 0) {
            try {
                vo = perunBl.getVosManagerBl().getVoById(sess, voId);
            } catch (VoNotExistsException ex) {
                throw new InternalErrorException("VoId with Id=" + voId + " not exists.", ex);
            }
            Attribute voQueue = null;
            try {
                voQueue = perunBl.getAttributesManagerBl().getAttribute(sess, vo, AttributesManager.NS_VO_ATTR_DEF + ":RTVoQueue");
            } catch (AttributeNotExistsException ex) {
                throw new InternalErrorException("Attribute RTVoQueue not exists.", ex);
            } catch (WrongAttributeAssignmentException ex) {
                throw new InternalErrorException(ex);
            }
            if (voQueue.getValue() != null) {
                queue = (String) voQueue.getValue();
            } else
                queue = rtDefaultQueue;
        } else
            queue = rtDefaultQueue;
    }
    //If subject is null or empty, use Unspecified instead
    if (subject == null || subject.isEmpty())
        subject = "(No subject)";
    //Text can be null so if it is, put empty string
    if (text == null)
        text = "";
    //Prepare credentials
    String username = BeansUtils.getCoreConfig().getRtServiceuserUsername();
    String password = BeansUtils.getCoreConfig().getRtServiceuserPassword();
    //Prepare content of message
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    try {
        entityBuilder.addPart("Content-Type", new StringBody("application/x-www-form-urlencoded", ContentType.create("text/plain", Consts.UTF_8)));
        entityBuilder.addPart("charset", new StringBody("utf-8", ContentType.create("text/plain", Consts.UTF_8)));
        entityBuilder.addPart("Connection", new StringBody("Close", ContentType.create("text/plain", Consts.UTF_8)));
        StringBody content = new StringBody("id: " + id + '\n' + "Queue: " + queue + '\n' + "Requestor: " + requestor + '\n' + "Subject: " + subject + '\n' + "Text: " + text, ContentType.create("text/plain", Consts.UTF_8));
        entityBuilder.addPart("content", content);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    //Test rtURL for null
    if (rtURL == null || rtURL.length() == 0)
        throw new InternalErrorException("rtURL is not prepared and is null in the moment of posting.");
    // prepare post request
    HttpPost post = new HttpPost(rtURL);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    post.addHeader(BasicScheme.authenticate(credentials, "utf-8", false));
    post.setEntity(entityBuilder.build());
    return post;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) 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) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) StringBody(org.apache.http.entity.mime.content.StringBody) Vo(cz.metacentrum.perun.core.api.Vo)

Example 37 with WrongAttributeAssignmentException

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

the class MembersManagerBlImpl method createMember.

//MAIN METHOD
public Member createMember(PerunSession sess, Vo vo, SpecificUserType specificUserType, Candidate candidate, List<Group> groups, List<String> overwriteUserAttributes) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, GroupOperationsException {
    log.debug("Creating member for VO {} from candidate {}", vo, candidate);
    // Get the user
    User user = null;
    if (candidate.getUserExtSources() != null) {
        for (UserExtSource ues : candidate.getUserExtSources()) {
            // Check if the extSource exists
            ExtSource tmpExtSource = getPerunBl().getExtSourcesManagerBl().checkOrCreateExtSource(sess, ues.getExtSource().getName(), ues.getExtSource().getType());
            // Set the extSource ID
            ues.getExtSource().setId(tmpExtSource.getId());
            try {
                // Try to find the user by userExtSource
                user = getPerunBl().getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, ues.getExtSource().getName(), ues.getLogin());
            } catch (UserExtSourceNotExistsException e) {
            // This is OK, non-existent userExtSource will be assigned later
            } catch (UserNotExistsException e) {
            // Ignore, we are only checking if the user exists
            } catch (ExtSourceNotExistsException e) {
            // Ignore, we are only checking if the user exists
            }
        }
    }
    // If user hasn't been found, then create him
    if (user == null) {
        user = new User();
        user.setFirstName(candidate.getFirstName());
        user.setLastName(candidate.getLastName());
        user.setMiddleName(candidate.getMiddleName());
        user.setTitleAfter(candidate.getTitleAfter());
        user.setTitleBefore(candidate.getTitleBefore());
        if (specificUserType.equals(specificUserType.SERVICE))
            user.setServiceUser(true);
        if (specificUserType.equals(specificUserType.SPONSORED))
            user.setSponsoredUser(true);
        // Store the user, this must be done in separate transaction
        user = getPerunBl().getUsersManagerBl().createUser(sess, user);
        log.debug("createMember: new user: {}", user);
    }
    // Assign missing userExtSource and update LoA
    if (candidate.getUserExtSources() != null) {
        for (UserExtSource userExtSource : candidate.getUserExtSources()) {
            try {
                UserExtSource currentUserExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, userExtSource.getExtSource(), userExtSource.getLogin());
                // Update LoA
                currentUserExtSource.setLoa(userExtSource.getLoa());
                getPerunBl().getUsersManagerBl().updateUserExtSource(sess, currentUserExtSource);
            } catch (UserExtSourceNotExistsException e) {
                // Create userExtSource
                try {
                    getPerunBl().getUsersManagerBl().addUserExtSource(sess, user, userExtSource);
                } catch (UserExtSourceExistsException e1) {
                    throw new ConsistencyErrorException("Adding userExtSource which already exists: " + userExtSource);
                }
            }
        }
    }
    try {
        Member member = getMemberByUser(sess, vo, user);
        throw new AlreadyMemberException(member);
    } catch (MemberNotExistsException IGNORE) {
    }
    // Create the member
    Member member = getMembersManagerImpl().createMember(sess, vo, user);
    getPerunBl().getAuditer().log(sess, "{} created.", member);
    // Create the member's attributes
    List<Attribute> membersAttributes = new ArrayList<Attribute>();
    List<Attribute> usersAttributesToMerge = new ArrayList<>();
    List<Attribute> usersAttributesToModify = new ArrayList<>();
    if (candidate.getAttributes() != null) {
        for (String attributeName : candidate.getAttributes().keySet()) {
            AttributeDefinition attributeDefinition;
            try {
                attributeDefinition = getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, attributeName);
            } catch (AttributeNotExistsException ex) {
                throw new InternalErrorException(ex);
            }
            Attribute attribute = new Attribute(attributeDefinition);
            attribute.setValue(getPerunBl().getAttributesManagerBl().stringToAttributeValue(candidate.getAttributes().get(attributeName), attribute.getType()));
            if (getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_MEMBER_ATTR_DEF) || getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_MEMBER_ATTR_OPT)) {
                // This is member's attribute
                membersAttributes.add(attribute);
            } else if (getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_USER_ATTR_DEF) || getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_USER_ATTR_OPT)) {
                if (overwriteUserAttributes != null && !overwriteUserAttributes.isEmpty() && overwriteUserAttributes.contains(attribute.getName())) {
                    usersAttributesToModify.add(attribute);
                } else {
                    usersAttributesToMerge.add(attribute);
                }
            }
        }
    }
    // Store the attributes
    try {
        //if empty, skip setting or merging empty arrays of attributes at all
        if (!membersAttributes.isEmpty())
            getPerunBl().getAttributesManagerBl().setAttributes(sess, member, membersAttributes);
        if (!usersAttributesToMerge.isEmpty())
            getPerunBl().getAttributesManagerBl().mergeAttributesValues(sess, user, usersAttributesToMerge);
        if (!usersAttributesToModify.isEmpty())
            getPerunBl().getAttributesManagerBl().setAttributes(sess, user, usersAttributesToModify);
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    }
    // Set the initial membershipExpiration
    // Get user LOA
    String memberLoa = null;
    try {
        Attribute loa = getPerunBl().getAttributesManagerBl().getAttribute(sess, member, AttributesManager.NS_MEMBER_ATTR_VIRT + ":loa");
        memberLoa = (String) loa.getValue();
    } catch (AttributeNotExistsException e) {
    // user has no loa defined - if required by VO, it will be stopped in checking method later
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    }
    // check if user can be member
    this.canBeMemberInternal(sess, vo, user, memberLoa, true);
    // set initial membership expiration
    this.extendMembership(sess, member);
    insertToMemberGroup(sess, member, vo);
    // add member also to all groups in list
    if (groups != null && !groups.isEmpty()) {
        for (Group group : groups) {
            try {
                perunBl.getGroupsManagerBl().addMember(sess, group, member);
            } catch (NotMemberOfParentGroupException ex) {
                throw new InternalErrorException("Member " + member + " can't be add to the group " + group + " because he is not member of it's parent group.", ex);
            } catch (GroupNotExistsException e) {
                throw new ConsistencyErrorException(e);
            }
        }
    }
    return 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) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) NotMemberOfParentGroupException(cz.metacentrum.perun.core.api.exceptions.NotMemberOfParentGroupException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

Example 38 with WrongAttributeAssignmentException

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

the class MembersManagerBlImpl method createSpecificMember.

public Member createSpecificMember(PerunSession sess, Vo vo, Candidate candidate, List<User> specificUserOwners, SpecificUserType specificUserType, List<Group> groups) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, GroupOperationsException {
    if (specificUserType.equals(SpecificUserType.SERVICE))
        candidate.setFirstName("(Service)");
    //Set organization only if user in sessione exists (in tests there is no user in session)
    if (sess.getPerunPrincipal().getUser() != null) {
        String userOrganization = AttributesManager.NS_USER_ATTR_DEF + ":organization";
        String memberOrganization = AttributesManager.NS_MEMBER_ATTR_DEF + ":organization";
        Map<String, String> candidateAttributes = new HashMap<>();
        if (candidate.getAttributes() != null)
            candidateAttributes.putAll(candidate.getAttributes());
        if (candidateAttributes.get(memberOrganization) == null) {
            Attribute actorUserOrganization;
            String actorUserOrganizationValue;
            try {
                actorUserOrganization = perunBl.getAttributesManagerBl().getAttribute(sess, sess.getPerunPrincipal().getUser(), userOrganization);
                actorUserOrganizationValue = (String) actorUserOrganization.getValue();
            } catch (WrongAttributeAssignmentException | AttributeNotExistsException ex) {
                throw new InternalErrorException(ex);
            }
            if (actorUserOrganizationValue != null) {
                candidateAttributes.put(memberOrganization, actorUserOrganizationValue);
                candidate.setAttributes(candidateAttributes);
            }
        }
    }
    //create member for service user from candidate
    Member member = createMember(sess, vo, specificUserType, candidate, groups, null);
    //set specific user owners or sponsors
    User specificUser = getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
    for (User u : specificUserOwners) {
        try {
            getPerunBl().getUsersManagerBl().addSpecificUserOwner(sess, u, specificUser);
        } catch (RelationExistsException ex) {
            throw new InternalErrorException(ex);
        }
    }
    return member;
}
Also used : User(cz.metacentrum.perun.core.api.User) RelationExistsException(cz.metacentrum.perun.core.api.exceptions.RelationExistsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

Example 39 with WrongAttributeAssignmentException

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

the class MembersManagerBlImpl method canBeMemberInternal.

/**
	 * More info on https://wiki.metacentrum.cz/wiki/VO_managers%27s_manual
	 *
	 * Check if the user can apply for VO membership. VO restrictions doesn't apply to service users.
	 *
	 * @param sess session
	 * @param vo VO to apply for
	 * @param user User applying for membership
	 * @param loa level of assurance provided by user's external identity
	 * @param throwExceptions TRUE = throw exceptions / FALSE = return false when user can't be member of VO
	 * @return True if user can become member of VO / false or exception otherwise.
	 *
	 * @throws ExtendMembershipException When user can't be member of VO and throwExceptions is set to true
	 * @throws InternalErrorException
	*/
protected boolean canBeMemberInternal(PerunSession sess, Vo vo, User user, String loa, boolean throwExceptions) throws InternalErrorException, ExtendMembershipException {
    if (user != null && user.isServiceUser())
        return true;
    // Check if the VO has set membershipExpirationRules attribute
    LinkedHashMap<String, String> membershipExpirationRules;
    Attribute membershipExpirationRulesAttribute = null;
    try {
        membershipExpirationRulesAttribute = getPerunBl().getAttributesManagerBl().getAttribute(sess, vo, MembersManager.membershipExpirationRulesAttributeName);
        membershipExpirationRules = (LinkedHashMap<String, String>) membershipExpirationRulesAttribute.getValue();
        // If attribute was not filled, then silently exit
        if (membershipExpirationRules == null)
            return true;
    } catch (AttributeNotExistsException e) {
        // No rules set, so leave it as it is
        return true;
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException("Shouldn't happen.");
    }
    // Which LOA we won't allow?
    if (membershipExpirationRules.get(MembersManager.membershipDoNotAllowLoaKeyName) != null) {
        if (loa == null) {
            // User doesn't have LOA defined and LOA is required for getting in, so do not allow membership.
            log.warn("User {} doesn't have LOA defined, but 'doNotAllowLoa' option is set for VO {}.", user, vo);
            if (throwExceptions) {
                throw new ExtendMembershipException(ExtendMembershipException.Reason.NOUSERLOA, "User " + user + " doesn't have LOA defined, but 'doNotExtendLoa' option is set for VO id " + vo.getId() + ".");
            } else {
                return false;
            }
        }
        String[] doNotAllowLoas = membershipExpirationRules.get(MembersManager.membershipDoNotAllowLoaKeyName).split(",");
        for (String doNotAllowLoa : doNotAllowLoas) {
            if (doNotAllowLoa.equals(loa)) {
                // User has LOA which is not allowed for getting in
                if (throwExceptions) {
                    throw new ExtendMembershipException(ExtendMembershipException.Reason.INSUFFICIENTLOA, "User " + user + " doesn't have required LOA for VO id " + vo.getId() + ".");
                } else {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : 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) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Example 40 with WrongAttributeAssignmentException

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

the class ServicesManagerBlImpl method getData.

private ServiceAttributes getData(PerunSession sess, Service service, Facility facility, Resource resource, Group group, Map<Member, ServiceAttributes> memberAttributes) throws InternalErrorException {
    ServiceAttributes groupServiceAttributes = new ServiceAttributes();
    try {
        groupServiceAttributes.addAttributes(getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, service, resource, group, true));
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    ServiceAttributes groupsSubGroupsElement = new ServiceAttributes();
    // FIXME Do not get subgroups of the members group
    if (!group.getName().equals(VosManager.MEMBERS_GROUP)) {
        List<Group> subGroups = getPerunBl().getGroupsManagerBl().getSubGroups(sess, group);
        for (Group subGroup : subGroups) {
            groupsSubGroupsElement.addChildElement(getData(sess, service, facility, resource, subGroup, memberAttributes));
        }
    }
    ServiceAttributes groupsMembersElement = new ServiceAttributes();
    //Invalid and disabled are not allowed here
    List<Member> members = getPerunBl().getGroupsManagerBl().getGroupMembersExceptInvalidAndDisabled(sess, group);
    for (Member member : members) {
        groupsMembersElement.addChildElement(memberAttributes.get(member));
    }
    groupServiceAttributes.addChildElement(groupsSubGroupsElement);
    groupServiceAttributes.addChildElement(groupsMembersElement);
    return groupServiceAttributes;
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ServiceAttributes(cz.metacentrum.perun.core.api.ServiceAttributes) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Member(cz.metacentrum.perun.core.api.Member)

Aggregations

WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)127 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)97 Attribute (cz.metacentrum.perun.core.api.Attribute)95 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)61 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)59 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)55 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)52 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)42 User (cz.metacentrum.perun.core.api.User)31 ArrayList (java.util.ArrayList)31 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)14 Facility (cz.metacentrum.perun.core.api.Facility)14 LinkedHashMap (java.util.LinkedHashMap)11 Member (cz.metacentrum.perun.core.api.Member)10 Map (java.util.Map)9 Group (cz.metacentrum.perun.core.api.Group)8 List (java.util.List)8 Resource (cz.metacentrum.perun.core.api.Resource)7 Vo (cz.metacentrum.perun.core.api.Vo)6 HashMap (java.util.HashMap)6