Search in sources :

Example 1 with UserExtSourceAlreadyRemovedException

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

the class UsersManagerBlImpl method moveUserExtSource.

@Override
public void moveUserExtSource(PerunSession sess, User sourceUser, User targetUser, UserExtSource userExtSource) {
    List<Attribute> userExtSourceAttributes = getPerunBl().getAttributesManagerBl().getAttributes(sess, userExtSource);
    // remove all virtual attributes (we don't need to take care about them)
    userExtSourceAttributes.removeIf(attribute -> getPerunBl().getAttributesManagerBl().isVirtAttribute(sess, attribute));
    // cannot move duplicated IDPs
    if (Objects.equals(userExtSource.getExtSource().getType(), ExtSourcesManager.EXTSOURCE_IDP) && getAllUserExtSourcesByTypeAndLogin(sess, ExtSourcesManager.EXTSOURCE_IDP, userExtSource.getLogin()).size() > 1) {
        throw new InternalErrorException("IDP UserExtSource " + userExtSource + " with duplicated login cannot be moved.");
    }
    // remove userExtSource
    try {
        this.removeUserExtSource(sess, sourceUser, userExtSource);
    } catch (UserExtSourceAlreadyRemovedException ex) {
        // this is little weird, will be better to report exception
        throw new InternalErrorException("UserExtSource was unexpectedly removed while moving " + userExtSource + " from " + sourceUser + " to " + targetUser);
    }
    // change userId for userExtSource
    userExtSource.setUserId(targetUser.getId());
    // add userExtSource to the targetUser
    try {
        userExtSource = this.addUserExtSource(sess, targetUser, userExtSource);
    } catch (UserExtSourceExistsException ex) {
        // someone moved this UserExtSource before us
        throw new InternalErrorException("Moving " + userExtSource + " from " + sourceUser + " to " + targetUser + " failed because someone already moved this UserExtSource.", ex);
    }
    // set all attributes back to this UserExtSource when it is already assigned to the targetUser
    try {
        getPerunBl().getAttributesManagerBl().setAttributes(sess, userExtSource, userExtSourceAttributes);
    } catch (WrongAttributeAssignmentException | WrongReferenceAttributeValueException | WrongAttributeValueException ex) {
        throw new InternalErrorException("Moving " + userExtSource + " from " + sourceUser + " to " + targetUser + " failed because of problem with setting removed attributes back to the UserExtSource.", ex);
    }
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) UserExtSourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceAlreadyRemovedException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 2 with UserExtSourceAlreadyRemovedException

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

the class urn_perun_user_attribute_def_def_login_namespace_ceitec method handleChangedAttributeHook.

private void handleChangedAttributeHook(PerunSessionImpl session, User user, Attribute attribute, String entityId, String scope) {
    /*
		 * "Synchornize" this attribute to user extSource. Means it creates, updates and removes userExtSource
		 * whenever this attribute is added, edited or removed.
		 *
		 * Ceitec proxy UserExtSourceLogin has form: {login-namespace:ceitec}@ceitec.cz
		 */
    String newLogin = "";
    try {
        ExtSource ceitecProxyIdp = session.getPerunBl().getExtSourcesManagerBl().getExtSourceByName(session, entityId);
        UserExtSource ceitecUes = getCeitecProxyUserExtSource(session, user, ceitecProxyIdp, scope);
        log.debug("changedAttributeHook UserExtSourceLogin to be synchronized: " + ceitecUes);
        if (attribute.getValue() == null) {
            // Deleting attribute
            if (ceitecUes == null) {
                log.debug("Deleting ceitec login but proxy UES does not exist. Probably ceitec login was not set before.");
            } else {
                session.getPerunBl().getUsersManagerBl().removeUserExtSource(session, user, ceitecUes);
            }
        } else {
            newLogin = attribute.getValue() + "@" + scope;
            if (ceitecUes == null) {
                // Creating UES
                ceitecUes = new UserExtSource(ceitecProxyIdp, 0, newLogin);
                session.getPerunBl().getUsersManagerBl().addUserExtSource(session, user, ceitecUes);
            } else {
                // Updating UES
                ceitecUes.setLogin(newLogin);
                session.getPerunBl().getUsersManagerBl().updateUserExtSource(session, ceitecUes);
            }
        }
    } catch (ExtSourceNotExistsException e) {
        throw new InternalErrorException("Attribute module 'urn_perun_user_attribute_def_def_login_namespace_ceitec' " + " require extSource with name (entityId): " + entityId + ". User: " + user, e);
    } catch (UserExtSourceAlreadyRemovedException e) {
        throw new InternalErrorException("Inconsistency. Attribute module 'urn_perun_user_attribute_def_def_login_namespace_ceitec' " + " tries to delete extSource but it does not exists. " + "extSource with name (entityId): " + entityId + ". User: " + user, e);
    } catch (UserExtSourceExistsException e) {
        throw new InternalErrorException("Login: '" + newLogin + "' of user: " + user + " is already taken by another user in namespace ceitec. Cannot add UserExtSource to the user. Different login must be used", e);
    }
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceAlreadyRemovedException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)

Example 3 with UserExtSourceAlreadyRemovedException

use of cz.metacentrum.perun.core.api.exceptions.UserExtSourceAlreadyRemovedException 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)

Aggregations

InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 UserExtSourceAlreadyRemovedException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceAlreadyRemovedException)3 Attribute (cz.metacentrum.perun.core.api.Attribute)2 ExtSource (cz.metacentrum.perun.core.api.ExtSource)2 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)2 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)2 User (cz.metacentrum.perun.core.api.User)1 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)1 UserExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)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