Search in sources :

Example 1 with UserExtSource

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

the class urn_perun_user_attribute_def_def_login_namespace_bbmri_persistent_shadow method changedAttributeHook.

/**
	 * ChangedAttributeHook() sets UserExtSource with following properties:
	 *  - extSourceType is IdP
	 *  - extSourceName is https://login.bbmri-eric.eu/idp/
	 *  - user's extSource login is the same as his bbmri-persistent attribute
	 *
	 * @param session PerunSession
	 * @param user User to set UserExtSource for
	 * @param attribute Attribute containing bbmriID
	 * @throws cz.metacentrum.perun.core.api.exceptions.InternalErrorException
	 * @throws cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException
	 */
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    try {
        String userNamespace = attribute.getFriendlyNameParameter();
        if (userNamespace.equals("bbmri-persistent-shadow") && attribute.getValue() != null) {
            ExtSource extSource = session.getPerunBl().getExtSourcesManagerBl().getExtSourceByName(session, extSourceNameBbmri);
            UserExtSource userExtSource = new UserExtSource(extSource, 0, attribute.getValue().toString());
            session.getPerunBl().getUsersManagerBl().addUserExtSource(session, user, userExtSource);
        }
    } catch (UserExtSourceExistsException ex) {
        log.warn("BBMRI IdP external source already exists for the user.", ex);
    } catch (ExtSourceNotExistsException ex) {
        throw new InternalErrorException("IdP external source for BBMRI doesn't exist.", ex);
    }
}
Also used : UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource)

Example 2 with UserExtSource

use of cz.metacentrum.perun.core.api.UserExtSource 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) throws InternalErrorException {
    /*
		 * "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
		 */
    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 {
            String 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("This module should check if ceitec login already exists " + "and call update method.", 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 UserExtSource

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

the class urn_perun_user_attribute_def_virt_organizationsWithLoa method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attribute = new Attribute(attributeDefinition);
    HashMap<String, String> organizationsWithLoa = new LinkedHashMap<String, String>();
    List<UserExtSource> extSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
    //If no userExtSources, so no Loa for any of them.
    if (extSources == null || extSources.isEmpty())
        return attribute;
    String version = attributeDefinition.getFriendlyNameParameter();
    if (version == null)
        throw new InternalErrorException("There is no parameter (cs or en) in attribute " + attributeDefinition);
    UserExtSource userExtSourceForCreating = null;
    UserExtSource userExtSourceForModifiing = null;
    //Initialize MapOfExtSource
    initializeMapOfExtSourceName();
    for (UserExtSource uES : extSources) {
        String uEName = uES.getExtSource().getName();
        String uELoa = String.valueOf(uES.getLoa());
        if (uES.getCreatedAt() != null) {
            Date testingDate = null;
            Date lastUsedDate = null;
            boolean parsed = true;
            try {
                testingDate = BeansUtils.getDateFormatter().parse(uES.getCreatedAt());
            } catch (Exception ex) {
                //Not Parsed correctly
                parsed = false;
            }
            if (parsed) {
                if (userExtSourceForCreating == null || userExtSourceForCreating.getCreatedAt() == null)
                    userExtSourceForCreating = uES;
                else {
                    try {
                        lastUsedDate = BeansUtils.getDateFormatter().parse(userExtSourceForCreating.getCreatedAt());
                        if (testingDate != null && testingDate.compareTo(lastUsedDate) < 0) {
                            userExtSourceForCreating = uES;
                        }
                    } catch (Exception ex) {
                        //Not Parsed correctly
                        userExtSourceForCreating = uES;
                    }
                }
            }
        }
        if (uES.getModifiedAt() != null) {
            Date testingDate = null;
            Date lastUsedDate = null;
            boolean parsed = true;
            try {
                testingDate = BeansUtils.getDateFormatter().parse(uES.getModifiedAt());
            } catch (Exception ex) {
                //Not Parsed correctly
                parsed = false;
            }
            if (parsed) {
                if (userExtSourceForModifiing == null || userExtSourceForModifiing.getModifiedAt() == null)
                    userExtSourceForModifiing = uES;
                else {
                    try {
                        lastUsedDate = BeansUtils.getDateFormatter().parse(userExtSourceForModifiing.getModifiedAt());
                        if (testingDate != null && testingDate.compareTo(lastUsedDate) < 0) {
                            userExtSourceForModifiing = uES;
                        }
                    } catch (Exception ex) {
                        //Not Parsed correctly
                        userExtSourceForModifiing = uES;
                    }
                }
            }
        }
        String uESimpleName = getSimpleNameOfExtSource(uEName, version.equals("cs"));
        organizationsWithLoa.put(uESimpleName, uELoa);
    }
    //Set created,modified by userExtSources
    if (userExtSourceForCreating != null) {
        attribute.setValueCreatedAt(userExtSourceForCreating.getCreatedAt());
        attribute.setValueCreatedBy(userExtSourceForCreating.getCreatedBy());
    }
    if (userExtSourceForModifiing != null) {
        attribute.setValueModifiedAt(userExtSourceForModifiing.getModifiedAt());
        attribute.setValueModifiedBy(userExtSourceForModifiing.getModifiedBy());
    }
    attribute.setValue(organizationsWithLoa);
    return attribute;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ParseException(java.text.ParseException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) CertificateException(javax.security.cert.CertificateException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with UserExtSource

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

the class urn_perun_user_attribute_def_virt_logins_namespace_google method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Set<String> googleLogins = new HashSet<>();
    List<UserExtSource> userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
    for (UserExtSource uES : userExtSources) {
        if (uES.getExtSource() != null && EXTSOURCE.equals(uES.getExtSource().getName())) {
            String login = uES.getLogin();
            if (login != null && !login.isEmpty() && login.matches(LOGIN_REGEX)) {
                googleLogins.add(login.replaceAll("[@].*$", ""));
            }
        }
    }
    Attribute attribute = new Attribute(attributeDefinition);
    attribute.setValue(new ArrayList<>(googleLogins));
    return attribute;
}
Also used : UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Attribute(cz.metacentrum.perun.core.api.Attribute) HashSet(java.util.HashSet)

Example 5 with UserExtSource

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

the class urn_perun_user_attribute_def_virt_shibbolethExtSources method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Map<String, String> idpLogins = new LinkedHashMap<String, String>();
    List<UserExtSource> userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
    for (UserExtSource uES : userExtSources) {
        if (uES.getExtSource() != null) {
            String login = uES.getLogin();
            String type = uES.getExtSource().getType();
            String idpIdentifier = uES.getExtSource().getName();
            if (type != null && login != null) {
                if (type.equals(ExtSourcesManager.EXTSOURCE_IDP)) {
                    idpLogins.put(idpIdentifier, login);
                }
            }
        }
    }
    Attribute attribute = new Attribute(attributeDefinition);
    attribute.setValue(idpLogins);
    return attribute;
}
Also used : UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Attribute(cz.metacentrum.perun.core.api.Attribute) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)79 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)34 Test (org.junit.Test)34 ExtSource (cz.metacentrum.perun.core.api.ExtSource)28 Attribute (cz.metacentrum.perun.core.api.Attribute)19 Candidate (cz.metacentrum.perun.core.api.Candidate)18 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)14 User (cz.metacentrum.perun.core.api.User)12 Member (cz.metacentrum.perun.core.api.Member)11 ArrayList (java.util.ArrayList)11 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)8 Group (cz.metacentrum.perun.core.api.Group)7 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)6 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)5 HashMap (java.util.HashMap)5 RichUser (cz.metacentrum.perun.core.api.RichUser)4 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)4 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)4 Matcher (java.util.regex.Matcher)4 RichMember (cz.metacentrum.perun.core.api.RichMember)3