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;
}
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());
}
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;
}
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;
}
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);
}
Aggregations