Search in sources :

Example 1 with UserExtSourceNotExistsException

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

the class MembersManagerBlImpl method createMember.

// MAIN METHOD
@Override
public Member createMember(PerunSession sess, Vo vo, SpecificUserType specificUserType, Candidate candidate, List<Group> groups, List<String> overwriteUserAttributes) throws WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException {
    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 | 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);
                }
            } catch (UserExtSourceExistsException e1) {
                throw new ConsistencyErrorException("Updating login of userExtSource to value 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, new MemberCreated(member));
    // Create the member's attributes
    List<Attribute> membersAttributes = new ArrayList<>();
    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, user, AttributesManager.NS_USER_ATTR_VIRT + ":loa");
        memberLoa = Integer.toString((Integer) 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 (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) RichUser(cz.metacentrum.perun.core.api.RichUser) 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) MemberCreated(cz.metacentrum.perun.audit.events.MembersManagerEvents.MemberCreated) 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) 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 2 with UserExtSourceNotExistsException

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

the class PerunBlImpl method getPerunSession.

@Override
public PerunSession getPerunSession(PerunPrincipal principal, PerunClient client) {
    PerunSessionImpl perunSession = new PerunSessionImpl(this, principal, client);
    log.debug("creating PerunSession for user {}", principal.getActor());
    if (principal.getUser() == null && usersManagerBl != null && !dontLookupUsersForLogins.contains(principal.getActor())) {
        // Get the user if we are completely initialized
        try {
            PerunSession internalSession = getPerunSession();
            User user = usersManagerBl.getUserByExtSourceInformation(internalSession, principal);
            principal.setUser(user);
            if (client.getType() != PerunClient.Type.OAUTH) {
                // Try to update LoA for userExtSource
                UserExtSource ues;
                String shibIdentityProvider = principal.getAdditionalInformations().get(UsersManagerBl.ORIGIN_IDENTITY_PROVIDER_KEY);
                if (shibIdentityProvider != null && extSourcesWithMultipleIdentifiers.contains(shibIdentityProvider)) {
                    ues = usersManagerBl.getUserExtSourceFromMultipleIdentifiers(internalSession, principal);
                } else {
                    ExtSource es = extSourcesManagerBl.getExtSourceByName(internalSession, principal.getExtSourceName());
                    ues = usersManagerBl.getUserExtSourceByExtLogin(internalSession, es, principal.getActor());
                }
                if (!BeansUtils.isPerunReadOnly()) {
                    if (ues != null && ues.getLoa() != principal.getExtSourceLoa()) {
                        ues.setLoa(principal.getExtSourceLoa());
                        usersManagerBl.updateUserExtSource(internalSession, ues);
                    }
                    // Update last access for userExtSource
                    usersManagerBl.updateUserExtSourceLastAccess(internalSession, ues);
                    // update selected attributes for given extsourcetype
                    setUserExtSourceAttributes(perunSession, ues, principal.getAdditionalInformations());
                }
            }
        } catch (ExtSourceNotExistsException | UserExtSourceNotExistsException | UserNotExistsException | UserExtSourceExistsException e) {
        // OK - We don't know user yet or we are modifying more than a LoA and we shouldn't !!
        }
    }
    return perunSession;
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) PerunSession(cz.metacentrum.perun.core.api.PerunSession) User(cz.metacentrum.perun.core.api.User) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) 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) PerunSessionImpl(cz.metacentrum.perun.core.impl.PerunSessionImpl)

Example 3 with UserExtSourceNotExistsException

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

the class ExtSourceREMS method existsSubjectWithUes.

/**
 * Finds out for given ues and login exits user in Perun.
 * Format of ues is {extSourceName}|{extSourceClass}|{eppn}|0.
 * The eppn is used as a 'login'.
 *
 * @param ues ues with user login: {extSourceName}|{extSourceClass}|{eppn}|0
 * @return true if is found existing ues with given login, false otherwise
 * @throws InternalErrorException internalError
 */
private boolean existsSubjectWithUes(String ues) {
    String[] extSourceSplit = ues.split("\\|", 4);
    if (extSourceSplit.length != 4) {
        log.error("Ivalid format of additionalues_1. It should be '{extSourceName}|{extSourceClass}|{eppn}|0'. Actual: {}", ues);
        return false;
    }
    PerunSession sess = getSession();
    String extSourceName = extSourceSplit[0];
    String eppn = extSourceSplit[2];
    try {
        // try to find user by additionalues
        perunBl.getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, extSourceName, eppn);
        return true;
    } catch (ExtSourceNotExistsException | UserExtSourceNotExistsException e) {
        log.error("Failed to get extSource with name '{}'", extSourceName);
    } catch (UserNotExistsException e) {
        return false;
    }
    return false;
}
Also used : PerunSession(cz.metacentrum.perun.core.api.PerunSession) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)

Example 4 with UserExtSourceNotExistsException

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

the class urn_perun_user_attribute_def_virt_studentIdentifiers method processRemoveUserExtSource.

/**
 * Remove userExtSource with attributes for member's user if exists.
 *
 * @param sess Perun session
 * @param group from which appropriate attributes will be obtained
 * @param member for which the xtSource with attributes will be processed
 */
private void processRemoveUserExtSource(PerunSessionImpl sess, Group group, Member member) {
    User user = sess.getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
    Attribute organizationScope = tryGetAttribute(sess, group, A_G_D_organizationScopeFriendlyName);
    if (organizationScope == null || organizationScope.getValue() == null) {
        return;
    }
    Attribute organizationNamespace = this.tryGetAttribute(sess, group, A_G_D_organizationNamespaceFriendlyName);
    if (organizationNamespace == null || organizationNamespace.getValue() == null) {
        return;
    }
    Attribute userLoginID = tryGetAttribute(sess, user, A_U_D_loginNamespaceFriendlyNamePrefix + organizationNamespace.valueAsString());
    if (userLoginID == null || userLoginID.getValue() == null) {
        return;
    }
    ExtSource extSource = tryGetExtSource(sess, organizationScope.valueAsString());
    // Remove userExtSource if exists
    try {
        UserExtSource ues = sess.getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, extSource, userLoginID.valueAsString());
        sess.getPerunBl().getUsersManagerBl().removeUserExtSource(sess, user, ues);
    } catch (UserExtSourceNotExistsException e) {
    // Means that the ues was already removed, which is ok
    } catch (UserExtSourceAlreadyRemovedException e) {
        // Should not happened
        throw new InternalErrorException(e);
    }
}
Also used : User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) UserExtSourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceAlreadyRemovedException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 5 with UserExtSourceNotExistsException

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

the class UsersManagerBlImpl method getUserExtSourceFromMultipleIdentifiers.

@Override
public UserExtSource getUserExtSourceFromMultipleIdentifiers(PerunSession sess, PerunPrincipal principal) throws UserExtSourceNotExistsException {
    String additionalIdentifiers = principal.getAdditionalInformations().get(ADDITIONAL_IDENTIFIERS_ATTRIBUTE_NAME);
    if (additionalIdentifiers == null) {
        throw new InternalErrorException("Entry " + ADDITIONAL_IDENTIFIERS_ATTRIBUTE_NAME + " is not defined in the principal's additional information. Either it was not provided by external source used for sign-in or the mapping configuration is wrong.");
    }
    UserExtSource ues = null;
    for (String identifier : additionalIdentifiers.split(MULTIVALUE_ATTRIBUTE_SEPARATOR_REGEX)) {
        try {
            ues = perunBl.getUsersManagerBl().getUserExtSourceByUniqueAttributeValue(sess, ADDITIONAL_IDENTIFIERS_PERUN_ATTRIBUTE_NAME, identifier);
            log.debug("UserExtSource found using additional identifiers: " + ues);
            break;
        } catch (UserExtSourceNotExistsException ex) {
        // try to find user ext source using different identifier in the next iteration of for cycle
        } catch (AttributeNotExistsException ex) {
            String errorMessage = "Mandatory attribute is not defined: ".concat(ADDITIONAL_IDENTIFIERS_PERUN_ATTRIBUTE_NAME);
            log.error(errorMessage);
            throw new InternalErrorException(errorMessage, ex);
        }
    }
    if (ues == null)
        throw new UserExtSourceNotExistsException("User ext source was not found. Searched value is any from \"" + additionalIdentifiers + "\" in " + ADDITIONAL_IDENTIFIERS_PERUN_ATTRIBUTE_NAME);
    return ues;
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

UserExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)10 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)9 ExtSource (cz.metacentrum.perun.core.api.ExtSource)6 User (cz.metacentrum.perun.core.api.User)6 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)6 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)5 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)5 Attribute (cz.metacentrum.perun.core.api.Attribute)4 RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)4 RichUser (cz.metacentrum.perun.core.api.RichUser)3 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)3 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)3 ArrayList (java.util.ArrayList)3 PerunSession (cz.metacentrum.perun.core.api.PerunSession)2 RichMember (cz.metacentrum.perun.core.api.RichMember)2 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)2 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)2 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)2 GroupCreatedInVo (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupCreatedInVo)1