use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class MailManagerImpl method setUsersMailAsTo.
/**
* Set users mail as TO param for mail message.
*
* Default value is empty (mail won't be sent).
*
* Mail is taken from first founded form item of type VALIDATED_MAIL.
* If none found and user exists, it's taken from
* user's attribute: preferredMail
*
* @param message message to set TO param
* @param app application
* @param data application data
*/
private void setUsersMailAsTo(MimeMessage message, Application app, List<ApplicationFormItemData> data) throws MessagingException {
setRecipient(message, null);
try {
// get TO param from VALIDATED_EMAIL form items (it's best fit)
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
if (ApplicationFormItem.Type.VALIDATED_EMAIL.equals(item.getType())) {
if (value != null && !value.isEmpty()) {
setRecipient(message, value);
// use first mail address
return;
}
}
}
// get TO param from other form items related to "user - preferredMail"
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
if (URN_USER_PREFERRED_MAIL.equalsIgnoreCase(item.getPerunDestinationAttribute())) {
if (value != null && !value.isEmpty()) {
setRecipient(message, value);
// use first mail address
return;
}
}
}
// get TO param from other form items related to "member - mail"
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
if (URN_MEMBER_MAIL.equalsIgnoreCase(item.getPerunDestinationAttribute())) {
if (value != null && !value.isEmpty()) {
setRecipient(message, value);
// use first mail address
return;
}
}
}
// get TO param from user if not present on application form
if (app.getUser() != null) {
User u = usersManager.getUserById(registrarSession, app.getUser().getId());
Attribute a = attrManager.getAttribute(registrarSession, u, URN_USER_PREFERRED_MAIL);
if (a != null && a.getValue() != null) {
String possibleTo = BeansUtils.attributeValueToString(a);
if (possibleTo != null && !possibleTo.trim().isEmpty()) {
setRecipient(message, possibleTo);
}
}
}
} catch (Exception ex) {
// we don't care about exceptions - we have backup address (empty = mail not sent)
log.error("[MAIL MANAGER] Exception thrown when getting users mail address for application: {}", app);
}
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class MailManagerImpl method mailValidation.
private void mailValidation(Application app, ApplicationMail mail, List<ApplicationFormItemData> data, String reason, List<Exception> exceptions) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
// set FROM
setFromMailAddress(message, app);
// set TO
// empty = not sent
message.setRecipient(Message.RecipientType.TO, null);
// get language
Locale lang = new Locale(getLanguageFromAppData(app, data));
// get localized subject and text
String mailText = getMailText(mail, lang, app, data, reason, exceptions);
message.setText(mailText);
String mailSubject = getMailSubject(mail, lang, app, data, reason, exceptions);
message.setSubject(mailSubject);
// send to all emails, which needs to be validated
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
// if mail field and not validated
int loa = 0;
try {
loa = Integer.parseInt(d.getAssuranceLevel());
} catch (NumberFormatException ex) {
// ignore
}
if (ApplicationFormItem.Type.VALIDATED_EMAIL.equals(item.getType()) && loa < 1) {
if (value != null && !value.isEmpty()) {
// set TO
message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(value) });
// get validation link params
String i = Integer.toString(d.getId(), Character.MAX_RADIX);
String m = getMessageAuthenticationCode(i);
// replace new validation link
if (mailText.contains("{validationLink-")) {
Pattern pattern = Pattern.compile("\\{validationLink-[^}]+}");
Matcher matcher = pattern.matcher(mailText);
while (matcher.find()) {
// whole "{validationLink-something}"
String toSubstitute = matcher.group(0);
// new login value to replace in text
String newValue = EMPTY_STRING;
Pattern namespacePattern = Pattern.compile("-(.*?)}");
Matcher m2 = namespacePattern.matcher(toSubstitute);
while (m2.find()) {
// only namespace "fed", "cert",...
String namespace = m2.group(1);
newValue = getPerunUrl(app.getVo(), app.getGroup());
if (newValue != null && !newValue.isEmpty()) {
if (!newValue.endsWith("/"))
newValue += "/";
newValue += namespace + "/registrar/";
newValue += "?vo=" + getUrlEncodedString(app.getVo().getShortName());
newValue += ((app.getGroup() != null) ? "&group=" + getUrlEncodedString(app.getGroup().getName()) : EMPTY_STRING);
newValue += "&i=" + getUrlEncodedString(i) + "&m=" + getUrlEncodedString(m);
}
}
// substitute {validationLink-authz} with actual value or empty string
mailText = mailText.replace(toSubstitute, newValue != null ? newValue : EMPTY_STRING);
}
}
if (mailText.contains(FIELD_VALIDATION_LINK)) {
// new backup if validation URL is missing
String url = getPerunUrl(app.getVo(), app.getGroup());
if (url != null && !url.isEmpty()) {
if (!url.endsWith("/"))
url += "/";
url += "registrar/";
url = url + "?vo=" + getUrlEncodedString(app.getVo().getShortName());
if (app.getGroup() != null) {
// append group name for
url += "&group=" + getUrlEncodedString(app.getGroup().getName());
}
// construct whole url
StringBuilder url2 = new StringBuilder(url);
if (url.contains("?")) {
if (!url.endsWith("?")) {
url2.append("&");
}
} else {
if (!url2.toString().isEmpty())
url2.append("?");
}
if (!url2.toString().isEmpty())
url2.append("i=").append(getUrlEncodedString(i)).append("&m=").append(getUrlEncodedString(m));
// replace validation link
mailText = mailText.replace(FIELD_VALIDATION_LINK, url2.toString());
}
}
if (mailText.contains(FIELD_REDIRECT_URL)) {
String redirectURL = BeansUtils.stringToMapOfAttributes(app.getFedInfo()).get("redirectURL");
mailText = mailText.replace(FIELD_REDIRECT_URL, redirectURL != null ? "&target=" + getUrlEncodedString(redirectURL) : EMPTY_STRING);
}
// set replaced text
message.setText(mailText);
try {
mailSender.send(message);
log.info("[MAIL MANAGER] Sending mail: MAIL_VALIDATION to: {} / appID: {} / {} / {}", message.getAllRecipients(), app.getId(), app.getVo(), app.getGroup());
} catch (MailException ex) {
log.error("[MAIL MANAGER] Sending mail: MAIL_VALIDATION failed because of exception.", ex);
}
} else {
log.error("[MAIL MANAGER] Sending mail: MAIL_VALIDATION failed. Not valid value of VALIDATED_MAIL field: {}", value);
}
}
}
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class RegistrarBaseIntegrationTest method testEmbeddedGroupsSubmission_initEmbeddedConflict.
@Test
public void testEmbeddedGroupsSubmission_initEmbeddedConflict() throws PerunException {
GroupsManager groupsManager = perun.getGroupsManager();
ApplicationForm form = registrarManager.getFormForVo(vo);
// create embedded groups form item
ApplicationFormItem embeddedGroupsItem = new ApplicationFormItem();
embeddedGroupsItem.setType(ApplicationFormItem.Type.EMBEDDED_GROUP_APPLICATION);
embeddedGroupsItem.setShortname("embeddedGroups");
embeddedGroupsItem = registrarManager.addFormItem(session, form, embeddedGroupsItem);
registrarManager.updateFormItems(session, form, Collections.singletonList(embeddedGroupsItem));
// create group in VO, generate group application form
Group group1 = new Group("GroupA", "Cool folks");
groupsManager.createGroup(session, vo, group1);
registrarManager.addGroupsToAutoRegistration(session, List.of(group1));
// create user
User user = new User(-1, "Jo", "Doe", "", "", "");
user = perun.getUsersManagerBl().createUser(session, user);
Application application = prepareApplicationToVo(user);
// set embedded groups in VO application
String embGroupsValue = String.format("Group A#%d", group1.getId());
List<ApplicationFormItemData> appItemsData = new ArrayList<>();
appItemsData.add(new ApplicationFormItemData(embeddedGroupsItem, "Embedded groups", embGroupsValue, "0"));
registrarManager.submitApplication(session, application, appItemsData);
// prepare group application and approve vo application
Application groupApp = prepareApplicationToVo(user);
groupApp.setGroup(group1);
registrarManager.submitApplication(session, groupApp, new ArrayList<>());
// normally, approval of VO generates and submits embedded groups applications
registrarManager.approveApplication(session, application.getId());
// embedded application is expected to not be created as init already exists
List<Application> group1Apps = registrarManager.getApplicationsForGroup(session, group1, List.of("NEW", "VERIFIED"));
assertEquals(1, group1Apps.size());
assertEquals(INITIAL, group1Apps.get(0).getType());
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class RegistrarBaseIntegrationTest method setGroupOptionsToGroupCheckbox.
@Test
// we know the exact type of the data
@SuppressWarnings("unchecked")
public void setGroupOptionsToGroupCheckbox() throws Exception {
Group groupA = new Group("A", "test");
groupA = perun.getGroupsManagerBl().createGroup(session, vo, groupA);
ApplicationForm formForVo = registrarManager.getFormForVo(vo);
ApplicationFormItem groupCheckboxItem = new ApplicationFormItem();
groupCheckboxItem.setShortname("groups");
groupCheckboxItem.setType(ApplicationFormItem.Type.EMBEDDED_GROUP_APPLICATION);
registrarManager.addFormItem(session, formForVo, groupCheckboxItem);
perun.getGroupsManagerBl().addGroupsToAutoRegistration(session, List.of(groupA));
Map<String, Object> data = registrarManager.initRegistrar(session, vo.getShortName(), null);
var items = (List<ApplicationFormItemWithPrefilledValue>) data.get("voFormInitial");
String expectedOptions = groupA.getId() + "#A";
assertThat(items.get(0).getFormItem().getI18n().get(ApplicationFormItem.EN).getOptions()).isEqualTo(expectedOptions);
assertThat(items.get(0).getFormItem().getI18n().get(ApplicationFormItem.CS).getOptions()).isEqualTo(expectedOptions);
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class RegistrarBaseIntegrationTest method testAddFormItem_multipleEmbeddedGroupsItems.
@Test
public void testAddFormItem_multipleEmbeddedGroupsItems() throws PerunException {
ApplicationForm form = registrarManager.getFormForVo(vo);
// create 2 embedded groups form items
ApplicationFormItem embeddedGroupsItem = new ApplicationFormItem();
embeddedGroupsItem.setType(ApplicationFormItem.Type.EMBEDDED_GROUP_APPLICATION);
embeddedGroupsItem.setShortname("embeddedGroups");
registrarManager.addFormItem(session, form, embeddedGroupsItem);
ApplicationFormItem embeddedGroupsItem2 = new ApplicationFormItem();
embeddedGroupsItem2.setType(ApplicationFormItem.Type.EMBEDDED_GROUP_APPLICATION);
embeddedGroupsItem2.setShortname("embeddedGroups2");
assertThrows(MultipleApplicationFormItemsException.class, () -> {
registrarManager.addFormItem(session, form, embeddedGroupsItem2);
});
}
Aggregations