Search in sources :

Example 51 with ExtSource

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

the class ExtSourcePerunTest method getUsersSubjectsTest.

@Test
public void getUsersSubjectsTest() throws Exception {
    System.out.println("getUsersSubjectsTest");
    // set up list of rich users
    AttributeDefinition attributeDefinition = new AttributeDefinition();
    attributeDefinition.setFriendlyName("firstName");
    attributeDefinition.setNamespace(AttributesManager.NS_USER_ATTR_DEF);
    attributeDefinition.setType(String.class.getName());
    Attribute attribute = new Attribute(attributeDefinition, "metodej");
    ExtSource extSource = new ExtSource();
    extSource.setName("extSourceNameForLogin");
    UserExtSource userExtSource = new UserExtSource();
    userExtSource.setLogin("extSourceNameForLogin");
    userExtSource.setExtSource(extSource);
    RichUser richUser = new RichUser(new User(), Collections.singletonList(userExtSource), Collections.singletonList(attribute));
    List<RichUser> richUserList = new ArrayList<>();
    richUserList.add(richUser);
    JsonDeserializer deserializer = mock(JsonDeserializer.class);
    doReturn(deserializer).when(extSourcePerun).call("usersManager", "findRichUsers", "searchString=query");
    doReturn(richUserList).when(deserializer).readList(RichUser.class);
    // define needed attributes
    Map<String, String> mapOfAttributes = new HashMap<>();
    mapOfAttributes.put("usersQuery", "query");
    mapOfAttributes.put("perunUrl", "perunUrl");
    mapOfAttributes.put("username", "username");
    mapOfAttributes.put("password", "password");
    mapOfAttributes.put("extSourceNameForLogin", "extSourceNameForLogin");
    mapOfAttributes.put("xmlMapping", "firstName={urn:perun:user:attribute-def:def:firstName},\nlogin={login}");
    doReturn(mapOfAttributes).when(extSourcePerun).getAttributes();
    // create expected subject to get
    List<Map<String, String>> expectedSubjects = new ArrayList<>();
    Map<String, String> subject = new HashMap<>();
    subject.put("login", "extSourceNameForLogin");
    subject.put("firstName", "metodej");
    expectedSubjects.add(subject);
    // test the method
    List<Map<String, String>> actualSubjects = extSourcePerun.getUsersSubjects();
    assertEquals("subjects should be same", expectedSubjects, actualSubjects);
}
Also used : RichUser(cz.metacentrum.perun.core.api.RichUser) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) RichUser(cz.metacentrum.perun.core.api.RichUser) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) JsonDeserializer(cz.metacentrum.perun.rpc.deserializer.JsonDeserializer) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 52 with ExtSource

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

the class UtilsIntegrationTest method setUpUserExtSource.

private void setUpUserExtSource() throws Exception {
    ExtSource externalSource = perun.getExtSourcesManager().getExtSourceByName(sess, extSourceName);
    userExtSource.setExtSource(externalSource);
    userExtSource.setLogin(extLogin);
    assertNotNull(usersManager.addUserExtSource(sess, user, userExtSource));
    ExtSource externalSource2 = perun.getExtSourcesManager().getExtSourceByName(sess, extSourceName2);
    userExtSource2.setExtSource(externalSource2);
    userExtSource2.setLogin(extLogin2);
    assertNotNull(usersManager.addUserExtSource(sess, user, userExtSource2));
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource)

Example 53 with ExtSource

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

the class UtilsIntegrationTest 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 and create real external source from DB
    perun.getExtSourcesManager().addExtSource(sess, returnedVo, es);
    // add real ext source to our VO
    ExtSource newExtSource2 = new ExtSource(extSourceName2, ExtSourcesManager.EXTSOURCE_INTERNAL);
    ExtSource es2 = perun.getExtSourcesManager().createExtSource(sess, newExtSource2, null);
    // get and create real external source from DB
    perun.getExtSourcesManager().addExtSource(sess, returnedVo, es2);
    return returnedVo;
}
Also used : Vo(cz.metacentrum.perun.core.api.Vo) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource)

Example 54 with ExtSource

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

the class LifeScienceHostelRIAcc method approveApplication.

/**
 * Create proper UserExtSource
 */
@Override
public Application approveApplication(PerunSession session, Application app) throws PrivilegeException, GroupNotExistsException, MemberNotExistsException, ExternallyManagedException, WrongReferenceAttributeValueException, WrongAttributeValueException, RegistrarException, ExtSourceNotExistsException, AttributeNotExistsException, WrongAttributeAssignmentException, VoNotExistsException, ExtendMembershipException, AlreadyMemberException {
    PerunBl perun = (PerunBl) session.getPerun();
    User user = app.getUser();
    if (user != null) {
        // Create UES for user
        Attribute userLogin = perun.getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":" + LOGIN_NAMESPACE);
        if (userLogin != null && userLogin.getValue() != null) {
            ExtSource extSource = perun.getExtSourcesManagerBl().getExtSourceByName(session, LS_HOSTEL_EXT_SOURCE_NAME);
            String login = userLogin.valueAsString();
            UserExtSource ues = new UserExtSource(extSource, login + LS_HOSTEL_SCOPE);
            ues.setLoa(0);
            try {
                perun.getUsersManagerBl().addUserExtSource(session, user, ues);
            } catch (UserExtSourceExistsException ex) {
            // this is OK
            }
        }
        if (Application.AppType.INITIAL.equals(app.getType())) {
            try {
                Vo vo = perun.getVosManagerBl().getVoByShortName(session, VO_SHORTNAME);
                Member member = perun.getMembersManagerBl().createMember(session, vo, user);
                log.debug("LS Hostel member added to the main VO Lifescience {}", member);
            } catch (VoNotExistsException e) {
                log.warn("VO: " + VO_SHORTNAME + " not exists, can't add member into it.");
            } catch (AlreadyMemberException ignore) {
            // user is already in lifescience
            } catch (ExtendMembershipException e) {
                // can't be member of lifescience, shouldn't happen
                log.error("LS Hostel member can't be added to VO: " + VO_SHORTNAME, e);
            }
        }
    // User doesn't have login - don't set UES
    }
    return app;
}
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) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Vo(cz.metacentrum.perun.core.api.Vo) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)

Example 55 with ExtSource

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

the class Vsup method beforeApprove.

@Override
public Application beforeApprove(PerunSession session, Application app) throws RegistrarException, PrivilegeException {
    List<ApplicationFormItemData> data = registrar.getApplicationDataById(session, app.getId());
    if (app.getUser() == null) {
        for (ApplicationFormItemData item : data) {
            if (item.getFormItem() != null && Objects.equals(AttributesManager.NS_USER_ATTR_DEF + ":birthNumber", item.getFormItem().getPerunDestinationAttribute())) {
                // if application contains birth number, try to map to existing user
                String rc = item.getValue();
                if (rc != null && !rc.isEmpty()) {
                    try {
                        User user = ((PerunBl) session.getPerun()).getUsersManagerBl().getUserByExtSourceNameAndExtLogin(session, "RC", rc);
                        app.setUser(user);
                        registrar.updateApplicationUser(session, app);
                        log.debug("Existing user found by RC for {}", app);
                    } catch (Exception ex) {
                        log.warn("Couldn't find or set user to application {} by RC: {}", app, ex);
                    }
                    // associate existing user with the identity used on registration form
                    if (app.getUser() != null) {
                        PerunBl perunBl = (PerunBl) session.getPerun();
                        ExtSource es = perunBl.getExtSourcesManager().checkOrCreateExtSource(session, app.getExtSourceName(), app.getExtSourceType());
                        UserExtSource ues = new UserExtSource(es, app.getExtSourceLoa(), app.getCreatedBy());
                        try {
                            ues = perunBl.getUsersManagerBl().addUserExtSource(session, app.getUser(), ues);
                            log.debug("{} associated with {} from application {}", app.getUser(), ues, app);
                        } catch (UserExtSourceExistsException ex) {
                            // we can ignore, user will be paired with application
                            log.warn("{} already had identity associated from application {}", app.getUser(), app);
                        }
                        try {
                            Member member = ((PerunBl) session.getPerun()).getMembersManagerBl().getMemberByUser(session, app.getVo(), app.getUser());
                            // user is already a member, switch application type
                            if (Application.AppType.INITIAL.equals(app.getType())) {
                                app.setType(Application.AppType.EXTENSION);
                                registrar.updateApplicationType(session, app);
                                log.debug("Updating application type to EXTENSION since we matched user which is VO member!");
                            }
                        } catch (MemberNotExistsException e) {
                        // OK state
                        }
                    }
                }
                break;
            }
        }
    }
    return app;
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) User(cz.metacentrum.perun.core.api.User) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) ParseException(java.text.ParseException) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) CantBeApprovedException(cz.metacentrum.perun.registrar.exceptions.CantBeApprovedException)

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