Search in sources :

Example 81 with ExtSource

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

the class ResourcesManagerEntryIntegrationTest method setUpCandidate.

private Candidate setUpCandidate() {
    String userFirstName = Long.toHexString(Double.doubleToLongBits(Math.random()));
    String userLastName = Long.toHexString(Double.doubleToLongBits(Math.random()));
    // his login in external source
    String extLogin = Long.toHexString(Double.doubleToLongBits(Math.random()));
    // Mockito.mock(Candidate.class);
    Candidate candidate = new Candidate();
    candidate.setFirstName(userFirstName);
    candidate.setId(0);
    candidate.setMiddleName("");
    candidate.setLastName(userLastName);
    candidate.setTitleBefore("");
    candidate.setTitleAfter("");
    ExtSource extSource = new ExtSource(0, "testExtSource", "cz.metacentrum.perun.core.impl.ExtSourceInternal");
    UserExtSource userExtSource = new UserExtSource(extSource, extLogin);
    candidate.setUserExtSource(userExtSource);
    candidate.setAttributes(new HashMap<>());
    return candidate;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource)

Example 82 with ExtSource

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

the class SearcherEntryIntegrationTest method setUpVo.

private Vo setUpVo() throws Exception {
    Vo newVo = new Vo(0, "UserManagerTestVo", "UMTestVo");
    Vo returnedVo = perun.getVosManager().createVo(sess, newVo);
    // create test VO in database
    assertNotNull("unable to create testing Vo", returnedVo);
    newVo.setId(returnedVo.getId());
    assertEquals("both VOs should be the same", newVo, returnedVo);
    ExtSource newExtSource = new ExtSource(extSourceName, ExtSourcesManager.EXTSOURCE_INTERNAL);
    ExtSource es = perun.getExtSourcesManager().createExtSource(sess, newExtSource, null);
    // get real external source from DB
    perun.getExtSourcesManager().addExtSource(sess, returnedVo, es);
    // add real ext source to our VO
    return returnedVo;
}
Also used : Vo(cz.metacentrum.perun.core.api.Vo) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource)

Example 83 with ExtSource

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

the class ExtSourcesManagerImpl method createExtSource.

@Override
public ExtSource createExtSource(PerunSession sess, ExtSource extSource, Map<String, String> attributes) throws ExtSourceExistsException {
    Utils.notNull(extSource.getName(), "extSource.getName()");
    Utils.notNull(extSource.getType(), "extSource.getType()");
    try {
        // Check if the extSources already exists
        if (0 < jdbc.queryForInt("select count(id) from ext_sources where name=? and type=?", extSource.getName(), extSource.getType())) {
            throw new ExtSourceExistsException(extSource);
        }
        // Get a new Id
        int newId = Utils.getNewId(jdbc, "ext_sources_id_seq");
        jdbc.update("insert into ext_sources (id, name, type, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) " + "values (?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", newId, extSource.getName(), extSource.getType(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
        extSource.setId(newId);
        ExtSource es;
        // Get the instance by the type of the extSource
        try {
            Class<?> extSourceClass = Class.forName(extSource.getType());
            es = (ExtSource) extSourceClass.newInstance();
        } catch (ClassNotFoundException e) {
            throw new InternalErrorException(e);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new InternalErrorException(e);
        }
        // Set the properties
        es.setId(extSource.getId());
        es.setName(extSource.getName());
        es.setType(extSource.getType());
        // Now store the attributes
        if (attributes != null) {
            for (String attr_name : attributes.keySet()) {
                jdbc.update("insert into ext_sources_attributes (attr_name, attr_value, ext_sources_id,created_by, created_at, modified_by, modified_at, created_by_uid, modified_by_uid) " + "values (?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", attr_name, attributes.get(attr_name), extSource.getId(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
            }
        }
        // Assign newly created extSource
        extSource = es;
        return extSource;
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : ExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 84 with ExtSource

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

the class urn_perun_user_attribute_def_virt_optionalLogin_namespace_mu method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) {
    Attribute attribute = new Attribute(attributeDefinition);
    try {
        Attribute loginInMU = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, A_U_D_loginNamespace_mu);
        Utils.copyAttributeToVirtualAttributeWithValue(loginInMU, attribute);
    } catch (AttributeNotExistsException ex) {
    // That means that mu login attribute not exists at all
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    // if attribute is still null (empty login in mu or not existing attribute), try to find uco in user ext sources
    if (attribute.getValue() == null) {
        List<UserExtSource> userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
        for (UserExtSource userExtSource : userExtSources) {
            ExtSource extSource = userExtSource.getExtSource();
            // Skip if extSource is not the one we are looking for
            if (userExtSource.getLogin() == null || extSource == null)
                continue;
            if (!ExtSourcesManager.EXTSOURCE_IDP.equals(extSource.getType()))
                continue;
            if (!EXTSOURCE_MUNI_IDP2.equals(extSource.getName()))
                continue;
            // Get login from this extSource and get only UCO from it
            String login = userExtSource.getLogin();
            Matcher loginMUMatcher = loginMUPattern.matcher(login);
            // This user has login in mu, but in weird format so skip this one
            if (!loginMUMatcher.find())
                continue;
            // It is ok, take UCO from login and set it to attribute value
            String UCO = loginMUMatcher.group(1);
            attribute.setValue(UCO);
            break;
        }
    }
    return attribute;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Matcher(java.util.regex.Matcher) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource)

Example 85 with ExtSource

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

Aggregations

ExtSource (cz.metacentrum.perun.core.api.ExtSource)135 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)110 Test (org.junit.Test)57 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)52 Attribute (cz.metacentrum.perun.core.api.Attribute)40 User (cz.metacentrum.perun.core.api.User)40 Vo (cz.metacentrum.perun.core.api.Vo)38 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)35 Member (cz.metacentrum.perun.core.api.Member)31 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)30 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)28 Candidate (cz.metacentrum.perun.core.api.Candidate)27 RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)27 ArrayList (java.util.ArrayList)25 Group (cz.metacentrum.perun.core.api.Group)23 LinkedHashMap (java.util.LinkedHashMap)20 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)19 RichMember (cz.metacentrum.perun.core.api.RichMember)17 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)17 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)16