Search in sources :

Example 91 with ExtSource

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

the class Vsup method approveApplication.

/**
 * Set "membershipExpiration" attribute value to "expirationManual" so it's consumed by services.
 * Set value only if membershipExpiration is after manual, or manual is empty.
 * If membershipExpiration is null, set 4000-01-01 as unlimited.
 *
 * Create userExtSource RC to user for future merging.
 */
@Override
public Application approveApplication(PerunSession session, Application app) throws MemberNotExistsException, AttributeNotExistsException, WrongAttributeAssignmentException, UserNotExistsException, WrongAttributeValueException, WrongReferenceAttributeValueException, PrivilegeException {
    PerunBl perun = (PerunBl) session.getPerun();
    Vo vo = app.getVo();
    User user = app.getUser();
    if (user == null) {
        log.error("At the end of approval action, we should have user present in application: {}", app);
    } else {
        Member member = perun.getMembersManagerBl().getMemberByUser(session, vo, user);
        Date membershipExpiration = null;
        Date manualExpiration = null;
        Attribute membershipExpirationAttr = perun.getAttributesManagerBl().getAttribute(session, member, AttributesManager.NS_MEMBER_ATTR_DEF + ":membershipExpiration");
        if (membershipExpirationAttr.getValue() != null) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            df.setLenient(false);
            String expiration = (String) membershipExpirationAttr.getValue();
            try {
                membershipExpiration = df.parse(expiration);
            } catch (ParseException e) {
                log.error("Can't parse manual expiration date.", e);
            }
        }
        Attribute manualExpirationAttr = perun.getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":expirationManual");
        if (manualExpirationAttr.getValue() != null) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            df.setLenient(false);
            String expiration = (String) manualExpirationAttr.getValue();
            try {
                manualExpiration = df.parse(expiration);
            } catch (ParseException e) {
                log.error("Can't parse manual expiration date.", e);
            }
        }
        boolean changed = false;
        if (membershipExpiration == null) {
            // has no membership expiration - set as unlimited - but it shouldn't happened based on VO rules
            manualExpirationAttr.setValue("4000-01-01");
            changed = true;
        } else if (manualExpiration == null || membershipExpiration.after(manualExpiration)) {
            // has no manual expiration - set from membership expiration
            // OR
            // has membership expiration after manual
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String value = df.format(membershipExpiration);
            manualExpirationAttr.setValue(value);
            changed = true;
        }
        if (changed) {
            // update manual expiration attribute
            perun.getAttributesManagerBl().setAttribute(session, user, manualExpirationAttr);
        }
    }
    // create ues RC for future merging
    List<ApplicationFormItemData> data = new ArrayList<>();
    try {
        data = registrar.getApplicationDataById(session, app.getId());
    } catch (RegistrarException e) {
    // ignore because application's id is not null
    }
    for (ApplicationFormItemData item : data) {
        if (item.getFormItem() != null && Objects.equals(AttributesManager.NS_USER_ATTR_DEF + ":birthNumber", item.getFormItem().getPerunDestinationAttribute())) {
            String rc = item.getValue();
            if (rc != null && !rc.isEmpty()) {
                ExtSource es = perun.getExtSourcesManager().checkOrCreateExtSource(session, "RC", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL);
                UserExtSource ues = new UserExtSource(es, 0, rc);
                try {
                    perun.getUsersManagerBl().addUserExtSource(session, app.getUser(), ues);
                } catch (UserExtSourceExistsException e) {
                    log.info("User external source from RC already created.");
                }
            }
            break;
        }
    }
    return app;
}
Also used : User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) Date(java.util.Date) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Vo(cz.metacentrum.perun.core.api.Vo) ParseException(java.text.ParseException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) SimpleDateFormat(java.text.SimpleDateFormat)

Example 92 with ExtSource

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

the class RegistrarBaseIntegrationTest method testHandleGroupApplications.

@Test
public void testHandleGroupApplications() throws PerunException {
    GroupsManager groupsManager = perun.getGroupsManager();
    // create group in VO, generate group application form
    Group group1 = new Group("GroupA", "Cool folks");
    group1 = groupsManager.createGroup(session, vo, group1);
    registrarManager.createApplicationFormInGroup(session, group1);
    ApplicationForm groupForm = registrarManager.getFormForGroup(group1);
    groupForm.setAutomaticApproval(true);
    registrarManager.updateForm(session, groupForm);
    // create user
    User user = new User(-1, "Jo", "Doe", "", "", "");
    user = perun.getUsersManagerBl().createUser(session, user);
    ExtSource source = new ExtSource("ExtSource", ExtSourcesManager.EXTSOURCE_IDP);
    perun.getExtSourcesManagerBl().createExtSource(session, source, new HashMap<>());
    UserExtSource ues = new UserExtSource(source, session.getPerunPrincipal().getActor());
    perun.getUsersManagerBl().addUserExtSource(session, user, ues);
    Application voApplication = prepareApplicationToVo(user);
    Application groupApplication = prepareApplicationToGroup(null, group1);
    List<ApplicationFormItemData> appItemsData = new ArrayList<>();
    voApplication = registrarManager.submitApplication(session, voApplication, appItemsData);
    registrarManager.submitApplication(session, groupApplication, appItemsData);
    registrarManager.approveApplication(session, voApplication.getId());
    // We have to call this method explicitly due to transactions
    registrarManager.handleUsersGroupApplications(session, vo, user);
    List<Application> group1Apps = registrarManager.getApplicationsForGroup(session, group1, List.of("APPROVED"));
    assertEquals(1, group1Apps.size());
    assertEquals(user, group1Apps.get(0).getUser());
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) User(cz.metacentrum.perun.core.api.User) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) Application(cz.metacentrum.perun.registrar.model.Application) RichApplication(cz.metacentrum.perun.registrar.model.RichApplication) GroupsManager(cz.metacentrum.perun.core.api.GroupsManager) Test(org.junit.Test)

Example 93 with ExtSource

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

the class LifeScienceHostelRI 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 94 with ExtSource

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

the class LifescienceHostel method approveApplication.

/**
 * Create proper UserExtSource
 */
@Override
public Application approveApplication(PerunSession session, Application app) throws WrongAttributeAssignmentException, AttributeNotExistsException, ExtSourceNotExistsException {
    PerunBl perun = (PerunBl) session.getPerun();
    User user = app.getUser();
    if (user == null) {
        log.error("At the end of approval action, we should have user present in application: {}", app);
    } else {
        Attribute userLogin = perun.getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":" + LIFESCIENCE_HOSTEL_NS);
        if (userLogin != null && userLogin.getValue() != null) {
            ExtSource extSource = perun.getExtSourcesManagerBl().getExtSourceByName(session, LS_HOSTEL_EXT_SOURCE_NAME);
            // as user email will be used as login, we want to get rid of all '@' characters - change them to '_'
            String modifiedLogin = userLogin.valueAsString().replace('@', '_');
            UserExtSource ues = new UserExtSource(extSource, modifiedLogin + LS_HOSTEL_SCOPE);
            ues.setLoa(0);
            try {
                perun.getUsersManagerBl().addUserExtSource(session, user, ues);
            } catch (UserExtSourceExistsException ex) {
            // this is OK
            }
        }
    // 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) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource)

Example 95 with ExtSource

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

the class MembersManagerBlImpl method createMember.

/*
	 * This method with support of LoA finally has to call this.createMember(PerunSession sess, Vo vo, UserExtSource userExtSource)
	 * @see cz.metacentrum.perun.core.api.MembersManager#createMember(cz.metacentrum.perun.core.api.PerunSession, cz.metacentrum.perun.core.api.Vo, java.lang.String, java.lang.String, java.lang.String, cz.metacentrum.perun.core.api.Candidate)
	 */
@Override
public Member createMember(PerunSession sess, Vo vo, String extSourceName, String extSourceType, int loa, String login, Candidate candidate, List<Group> groups) throws WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException {
    // Create ExtSource object
    ExtSource extSource = new ExtSource();
    extSource.setName(extSourceName);
    extSource.setType(extSourceType);
    // Create UserExtSource object
    UserExtSource userExtSource = new UserExtSource();
    userExtSource.setLogin(login);
    userExtSource.setExtSource(extSource);
    userExtSource.setLoa(loa);
    // Set all above data to the candidate's userExtSource
    candidate.setUserExtSource(userExtSource);
    return this.createMember(sess, vo, candidate, groups);
}
Also used : UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource)

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