Search in sources :

Example 6 with UserExtSourceNotExistsException

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

the class urn_perun_user_attribute_def_def_login_namespace_vsup method changedAttributeHook.

/**
 * When login changes: first set / changed always change eduroam-vsup login too !!
 * When login is set add UserExtSource, since logins are generated in Perun.
 * When login is set, set also school mail u:d:vsupMail
 *
 * @param session
 * @param user
 * @param attribute
 * @throws InternalErrorException
 * @throws WrongReferenceAttributeValueException
 */
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws WrongReferenceAttributeValueException {
    if (attribute.getValue() != null) {
        // add UES
        ExtSource es;
        try {
            es = session.getPerunBl().getExtSourcesManagerBl().getExtSourceByName(session, "AD");
        } catch (ExtSourceNotExistsException ex) {
            throw new InternalErrorException("AD ext source on VŠUP doesn't exists.", ex);
        }
        try {
            session.getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(session, es, (String) attribute.getValue());
        } catch (UserExtSourceNotExistsException ex) {
            // add UES
            UserExtSource ues = new UserExtSource(es, 2, (String) attribute.getValue());
            try {
                session.getPerunBl().getUsersManagerBl().addUserExtSource(session, user, ues);
            } catch (UserExtSourceExistsException ex2) {
                throw new ConsistencyErrorException(ex2);
            }
        }
        // set eduroam-login
        Attribute eduroamLogin = null;
        try {
            eduroamLogin = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, EDUROAM_VSUP_NAMESPACE);
            if (!Objects.equals(attribute.getValue(), eduroamLogin.getValue())) {
                eduroamLogin.setValue(attribute.getValue());
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, eduroamLogin);
            }
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeValueException ex) {
            throw new WrongReferenceAttributeValueException(attribute, eduroamLogin, "Mismatch in checking of users VŠUP login and eduroam login.", ex);
        }
        // set všup school mail
        Attribute schoolMail = null;
        try {
            schoolMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, VSUP_MAIL_NAMESPACE);
            if (!Objects.equals(attribute.getValue(), schoolMail.getValue())) {
                schoolMail.setValue(attribute.getValue() + "@vsup.cz");
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, schoolMail);
            }
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeValueException ex) {
            throw new WrongReferenceAttributeValueException(attribute, schoolMail, "Mismatch in checking of users VŠUP login and schoolMail.", ex);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) 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) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 7 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 processAddUserExtSource.

/**
 * Set userExtSource with attributes for member's user if not 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 processAddUserExtSource(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());
    // Create and set userExtSource if not exists
    try {
        sess.getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, extSource, userLoginID.valueAsString());
    } catch (UserExtSourceNotExistsException e) {
        UserExtSource ues = new UserExtSource(extSource, userLoginID.valueAsString());
        try {
            ues = sess.getPerunBl().getUsersManagerBl().addUserExtSource(sess, user, ues);
        } catch (UserExtSourceExistsException userExtSourceExistsException) {
            // Should not happened
            throw new InternalErrorException(e);
        }
        Attribute schacHomeOrganization = tryGetAttribute(sess, ues, A_UES_D_schacHomeOrganizationFriendlyName);
        Attribute eduPersonScopedAffiliation = tryGetAttribute(sess, ues, A_UES_D_eduPersonScopedAffiliationFriendlyName);
        Attribute schacPersonalUniqueCode = tryGetAttribute(sess, ues, A_UES_D_schacPersonalUniqueCodeFriendlyName);
        schacHomeOrganization.setValue(organizationScope.valueAsString());
        eduPersonScopedAffiliation.setValue(affiliationPrefix + organizationScope.valueAsString());
        List<String> spucValue = new ArrayList<>();
        spucValue.add(studentIdentifiersValuePrefix + organizationScope.valueAsString() + ":" + userLoginID.valueAsString());
        schacPersonalUniqueCode.setValue(spucValue);
        try {
            sess.getPerunBl().getAttributesManagerBl().setAttributes(sess, ues, Arrays.asList(schacHomeOrganization, eduPersonScopedAffiliation, schacPersonalUniqueCode));
        } catch (WrongAttributeValueException | WrongAttributeAssignmentException | WrongReferenceAttributeValueException ex) {
            // Should not happened
            throw new InternalErrorException(ex);
        }
    }
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) 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) ArrayList(java.util.ArrayList) List(java.util.List) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 8 with UserExtSourceNotExistsException

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

the class GroupsManagerBlImpl method categorizeMembersForLightweightSynchronization.

/**
 * For lightweight synchronization prepare candidate to add and members to remove.
 *
 * Get all subjects from loginSource and try to find users in Perun by their login and this ExtSource.
 * If found, look if this user is already in synchronized Group. If yes skip him, if not add him to candidateToAdd
 * If not found in vo of the group, skip him.
 *
 * Rest of former members need to be add to membersToRemove to remove them from group.
 *
 * This method fill 2 member structures which get as parameters:
 * 1. candidateToAdd - New members of the group
 * 2. membersToRemove - Former members who are not in synchronized ExtSource now
 *
 * @param sess
 * @param group
 * @param loginSource
 * @param memberSource
 * @param groupMembers
 * @param candidatesToAdd
 * @param membersToRemove
 * @param skippedMembers
 */
private void categorizeMembersForLightweightSynchronization(PerunSession sess, Group group, ExtSource loginSource, ExtSource memberSource, List<RichMember> groupMembers, List<Candidate> candidatesToAdd, List<RichMember> membersToRemove, List<String> skippedMembers) {
    // Get subjects from loginSource
    List<Map<String, String>> subjects = getSubjectsFromExtSource(sess, loginSource, group);
    // Prepare structure of userIds with richMembers to better work with actual members
    Map<Integer, RichMember> idsOfUsersInGroup = new HashMap<>();
    for (RichMember richMember : groupMembers) {
        idsOfUsersInGroup.put(richMember.getUserId(), richMember);
    }
    // try to find users by login and loginSource
    for (Map<String, String> subjectFromLoginSource : subjects) {
        if (subjectFromLoginSource == null) {
            log.error("Null value in the subjects list. Skipping.");
            continue;
        }
        String login = subjectFromLoginSource.get("login");
        // Skip subjects, which doesn't have login
        if (login == null || login.isEmpty()) {
            log.debug("Subject {} doesn't contain attribute login, skipping.", subjectFromLoginSource);
            skippedMembers.add("MemberEntry:[" + subjectFromLoginSource + "] was skipped because login is missing");
            continue;
        }
        // try to find user from perun by login and member extSource (need to use memberSource because loginSource is not saved by synchronization)
        User user = null;
        List<UserExtSource> userExtSources = new ArrayList<>();
        try {
            UserExtSource userExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, memberSource, login);
            userExtSources.add(userExtSource);
        } catch (UserExtSourceNotExistsException e) {
        // skipping, this extSource does not exist and thus won't be in the list
        }
        Vo groupVo = getVo(sess, group);
        List<UserExtSource> additionalUserExtSources = Utils.extractAdditionalUserExtSources(sess, subjectFromLoginSource).stream().map(RichUserExtSource::asUserExtSource).collect(toList());
        userExtSources.addAll(additionalUserExtSources);
        for (UserExtSource source : userExtSources) {
            try {
                user = getPerunBl().getUsersManagerBl().getUserByUserExtSource(sess, source);
                // check if user is already member of group's vo
                if (getPerunBl().getUsersManagerBl().getVosWhereUserIsMember(sess, user).contains(groupVo)) {
                    if (idsOfUsersInGroup.containsKey(user.getId())) {
                        // we can skip this one, because he is already in group, and remove him from the map
                        // but first we need to also validate him if he was disabled before (invalidate and then validate)
                        RichMember richMember = idsOfUsersInGroup.get(user.getId());
                        if (richMember != null && Status.DISABLED.equals(richMember.getStatus())) {
                            getPerunBl().getMembersManagerBl().invalidateMember(sess, richMember);
                            try {
                                getPerunBl().getMembersManagerBl().validateMember(sess, richMember);
                            } catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
                                log.info("Switching member id {} into INVALID state from DISABLED, because there was problem with attributes {}.", richMember.getId(), e);
                            }
                        }
                        idsOfUsersInGroup.remove(user.getId());
                    } else {
                        // he is not yet in group, so we need to create a candidate
                        Candidate candidate = new Candidate(user, source);
                        // for lightweight synchronization we want to skip all update of attributes
                        candidate.setAttributes(new HashMap<>());
                        candidatesToAdd.add(candidate);
                    }
                    break;
                }
            } catch (UserNotExistsException e) {
            // skip because the user from this ExtSource does not exist so we can continue
            }
        }
        // If user not found in group's vo, skip him and log it
        if (user == null) {
            log.debug("Subject {} with login {} was skipped during lightweight synchronization of group {} because he is not in vo of the group yet.", subjectFromLoginSource, login, group);
        }
    }
    // Rest of them need to be removed
    membersToRemove.addAll(idsOfUsersInGroup.values());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) ArrayList(java.util.ArrayList) RichMember(cz.metacentrum.perun.core.api.RichMember) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Vo(cz.metacentrum.perun.core.api.Vo) GroupCreatedInVo(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupCreatedInVo) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 9 with UserExtSourceNotExistsException

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

the class UsersManagerBlImpl method checkThatCandidateUesesDontExist.

/**
 * Check that none of the given userExtSources exist. If so, the UserExtSourceExistsException
 * is thrown.
 *
 * @param sess session
 * @param candidate candidate
 * @throws UserExtSourceExistsException if some of the given userExtSources already exist.
 */
private void checkThatCandidateUesesDontExist(PerunSession sess, Candidate candidate) throws UserExtSourceExistsException {
    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 user = getPerunBl().getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, ues.getExtSource().getName(), ues.getLogin());
                if (user != null) {
                    throw new UserExtSourceExistsException(ues);
                }
            } catch (UserExtSourceNotExistsException | UserNotExistsException | ExtSourceNotExistsException e) {
            // This is OK, we don't want it to exist
            }
        }
    }
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) OwnershipRemovedForSpecificUser(cz.metacentrum.perun.audit.events.UserManagerEvents.OwnershipRemovedForSpecificUser) User(cz.metacentrum.perun.core.api.User) OwnershipEnabledForSpecificUser(cz.metacentrum.perun.audit.events.UserManagerEvents.OwnershipEnabledForSpecificUser) UserAddedToOwnersOfSpecificUser(cz.metacentrum.perun.audit.events.UserManagerEvents.UserAddedToOwnersOfSpecificUser) UserExtSourceRemovedFromUser(cz.metacentrum.perun.audit.events.UserManagerEvents.UserExtSourceRemovedFromUser) RichUser(cz.metacentrum.perun.core.api.RichUser) OwnershipDisabledForSpecificUser(cz.metacentrum.perun.audit.events.UserManagerEvents.OwnershipDisabledForSpecificUser) UserExtSourceAddedToUser(cz.metacentrum.perun.audit.events.UserManagerEvents.UserExtSourceAddedToUser) AllUserExtSourcesDeletedForUser(cz.metacentrum.perun.audit.events.UserManagerEvents.AllUserExtSourcesDeletedForUser) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) 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)

Example 10 with UserExtSourceNotExistsException

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

the class UsersManagerBlImpl method addMissingCandidatesUes.

/**
 * For given user, set user extsources from candiate, which have not been set before.
 *
 * @param sess session
 * @param user user
 * @param candidate candidate to take userExtSources
 */
private void addMissingCandidatesUes(PerunSession sess, User user, Candidate candidate) {
    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, e1);
                }
            } catch (UserExtSourceExistsException e1) {
                throw new ConsistencyErrorException("Updating login of userExtSource to value which already" + " exists: " + userExtSource, e1);
            }
        }
    }
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)

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