Search in sources :

Example 1 with ApplicationFormItem

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(SimpleMailMessage message, Application app, List<ApplicationFormItemData> data) {
    message.setTo("");
    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()) {
                    message.setTo(d.getValue());
                    // 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 (item.getPerunDestinationAttribute() != null && !item.getPerunDestinationAttribute().isEmpty()) {
                if (item.getPerunDestinationAttribute().equalsIgnoreCase(URN_USER_PREFERRED_MAIL)) {
                    if (value != null && !value.isEmpty()) {
                        message.setTo(d.getValue());
                        // 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 (item.getPerunDestinationAttribute() != null && !item.getPerunDestinationAttribute().isEmpty()) {
                if (item.getPerunDestinationAttribute().equalsIgnoreCase(URN_MEMBER_MAIL)) {
                    if (value != null && !value.isEmpty()) {
                        message.setTo(d.getValue());
                        // 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) {
                message.setTo(BeansUtils.attributeValueToString(a));
            }
        }
    } 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);
    }
}
Also used : ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) SQLException(java.sql.SQLException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) MailException(org.springframework.mail.MailException)

Example 2 with ApplicationFormItem

use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.

the class RegistrarManagerImpl method copyFormFromVoToGroup.

@Override
public void copyFormFromVoToGroup(PerunSession sess, Vo fromVo, Group toGroup, boolean reverse) throws PerunException {
    if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, fromVo) || ((!AuthzResolver.isAuthorized(sess, Role.VOADMIN, toGroup) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, toGroup)))) {
        throw new PrivilegeException(sess, "copyFormFromVoToGroup");
    }
    if (reverse) {
        // copy from group to VO
        List<ApplicationFormItem> items = getFormItems(sess, getFormForGroup(toGroup));
        for (ApplicationFormItem item : items) {
            // reset order, id is always new inside add method
            item.setOrdnum(null);
            addFormItem(sess, getFormForVo(fromVo), item);
        }
    } else {
        // copy from VO to group
        List<ApplicationFormItem> items = getFormItems(sess, getFormForVo(fromVo));
        for (ApplicationFormItem item : items) {
            // reset order, id is always new inside add method
            item.setOrdnum(null);
            addFormItem(sess, getFormForGroup(toGroup), item);
        }
    }
}
Also used : ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem)

Example 3 with ApplicationFormItem

use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.

the class RegistrarManagerImpl method copyFormFromGroupToGroup.

@Override
public void copyFormFromGroupToGroup(PerunSession sess, Group fromGroup, Group toGroup) throws PerunException {
    Vo fromVO = perun.getVosManagerBl().getVoById(registrarSession, fromGroup.getVoId());
    if ((!AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, fromGroup) && !AuthzResolver.isAuthorized(sess, Role.VOADMIN, fromGroup) && !AuthzResolver.isAuthorized(sess, Role.TOPGROUPCREATOR, fromVO)) || (!AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, toGroup) && !AuthzResolver.isAuthorized(sess, Role.VOADMIN, toGroup))) {
        throw new PrivilegeException(sess, "copyFormFromGroupToGroup");
    }
    List<ApplicationFormItem> items = getFormItems(sess, getFormForGroup(fromGroup));
    for (ApplicationFormItem item : items) {
        // reset order, id is always new inside add method
        item.setOrdnum(null);
        addFormItem(sess, getFormForGroup(toGroup), item);
    }
}
Also used : ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem)

Example 4 with ApplicationFormItem

use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.

the class RegistrarManagerImpl method getFormItemById.

@Override
public ApplicationFormItem getFormItemById(int id) {
    ApplicationFormItem item;
    item = jdbc.queryForObject(FORM_ITEM_SELECT + " where id=?", ITEM_MAPPER, id);
    if (item != null) {
        List<ItemTexts> texts = jdbc.query(FORM_ITEM_TEXTS_SELECT + " where item_id=?", ITEM_TEXTS_MAPPER, item.getId());
        for (ItemTexts itemTexts : texts) {
            item.getI18n().put(itemTexts.getLocale(), itemTexts);
        }
        List<AppType> appTypes = jdbc.query(APP_TYPE_SELECT + " where item_id=?", APP_TYPE_MAPPER, item.getId());
        item.setApplicationTypes(appTypes);
    }
    return item;
}
Also used : ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem) ItemTexts(cz.metacentrum.perun.registrar.model.ApplicationFormItem.ItemTexts) AppType(cz.metacentrum.perun.registrar.model.Application.AppType)

Example 5 with ApplicationFormItem

use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.

the class MailManagerImpl method substituteCommonStrings.

/**
 * Substitute common strings in mail text by data provided by
 * application, application data and perun itself.
 *
 * Substituted strings are:
 *
 * {voName} - full vo name
 * {groupName} - group short name
 * {displayName} - user's display name returned from federation if present on form
 * {firstName} - first name of user if present on form as separate form item
 * {lastName} - last name of user if present on form as separate form item
 * {appId} - application id
 * {actor} - login in external system used when submitting application
 * {extSource} - external system used for authentication when submitting application
 * {appGuiUrl} - url to application GUI for user to see applications state
 *
 * {appGuiUrl-[authz]} - url to application GUI for user to see applications state
 * {perunGuiUrl-[authz]} - url to perun GUI (user detail)
 * {appDetailUrl-[authz]} - link for VO admin to approve / reject application
 *
 * {logins} - list of all logins from application
 * {membershipExpiration} - membership expiration date
 * {mail} - user preferred mail submitted on application or stored in a system
 * {phone} - user phone submitted on application or stored in a system
 *
 * {customMessage} - message passed by the admin to mail (e.g. reason of application reject)
 * {errors} - include errors, which occurred when processing registrar actions
 * (e.g. login reservation errors passed to mail for VO admin)
 *
 * (if possible links are for: Kerberos, Federation and Certificate authz)
 *
 * @param app Application to substitute strings for (get VO etc.)
 * @param data ApplicationData needed for substitution (displayName etc.)
 * @param mailText String to substitute parts of
 * @param reason Custom message passed by vo admin
 * @param exceptions list of exceptions thrown when processing registrar actions
 * @return modified text
 */
private String substituteCommonStrings(Application app, List<ApplicationFormItemData> data, String mailText, String reason, List<Exception> exceptions) {
    LinkedHashMap<String, String> additionalAttributes = BeansUtils.stringToMapOfAttributes(app.getFedInfo());
    PerunPrincipal applicationPrincipal = new PerunPrincipal(app.getCreatedBy(), app.getExtSourceName(), app.getExtSourceType(), app.getExtSourceLoa(), additionalAttributes);
    // replace app ID
    if (mailText.contains(FIELD_APP_ID)) {
        mailText = mailText.replace(FIELD_APP_ID, app.getId() + EMPTY_STRING);
    }
    // replace actor (app created by)
    if (mailText.contains(FIELD_ACTOR)) {
        mailText = mailText.replace(FIELD_ACTOR, app.getCreatedBy() + EMPTY_STRING);
    }
    // replace ext source (app created by)
    if (mailText.contains(FIELD_EXT_SOURCE)) {
        mailText = mailText.replace(FIELD_EXT_SOURCE, app.getExtSourceName() + EMPTY_STRING);
    }
    // replace voName
    if (mailText.contains(FIELD_VO_NAME)) {
        mailText = mailText.replace(FIELD_VO_NAME, app.getVo().getName());
    }
    // replace groupName
    if (mailText.contains(FIELD_GROUP_NAME)) {
        if (app.getGroup() != null) {
            mailText = mailText.replace(FIELD_GROUP_NAME, app.getGroup().getShortName());
        } else {
            mailText = mailText.replace(FIELD_GROUP_NAME, EMPTY_STRING);
        }
    }
    // replace customMessage (reason)
    if (mailText.contains(FIELD_CUSTOM_MESSAGE)) {
        if (reason != null && !reason.isEmpty()) {
            mailText = mailText.replace(FIELD_CUSTOM_MESSAGE, reason);
        } else {
            mailText = mailText.replace(FIELD_CUSTOM_MESSAGE, EMPTY_STRING);
        }
    }
    // replace {fromApp-*}
    Matcher matcher = FROM_APP_PATTERN.matcher(mailText);
    while (matcher.find()) {
        String itemName = matcher.group(1);
        for (ApplicationFormItemData item : data) {
            if (itemName.equals(item.getShortname())) {
                String newValue = item.getValue();
                mailText = mailText.replace("{fromApp-" + itemName + "}", newValue != null ? newValue : EMPTY_STRING);
                break;
            }
        }
    }
    // replace displayName
    if (mailText.contains(FIELD_DISPLAY_NAME)) {
        // backup
        String nameText = EMPTY_STRING;
        for (ApplicationFormItemData d : data) {
            // core attribute
            if (URN_USER_DISPLAY_NAME.equals(d.getFormItem().getPerunDestinationAttribute())) {
                if (d.getValue() != null && !d.getValue().isEmpty()) {
                    nameText = d.getValue();
                    break;
                }
            }
            // federation attribute
            if (CN.equals(d.getFormItem().getFederationAttribute()) || DISPLAY_NAME.equals(d.getFormItem().getFederationAttribute())) {
                if (d.getValue() != null && !d.getValue().isEmpty()) {
                    nameText = d.getValue();
                    break;
                }
            }
        }
        if (nameText.isEmpty()) {
            User user = null;
            if (app.getUser() != null) {
                user = app.getUser();
            } else {
                try {
                    user = usersManager.getUserByExtSourceInformation(registrarSession, applicationPrincipal);
                } catch (Exception ex) {
                // user not found is ok
                }
            }
            if (user != null)
                nameText = user.getDisplayName();
        }
        mailText = mailText.replace(FIELD_DISPLAY_NAME, nameText);
    }
    // replace firstName
    if (mailText.contains(FIELD_FIRST_NAME)) {
        // backup
        String nameText = EMPTY_STRING;
        for (ApplicationFormItemData d : data) {
            if ("urn:perun:user:attribute-def:core:firstName".equals(d.getFormItem().getPerunDestinationAttribute())) {
                if (d.getValue() != null && !d.getValue().isEmpty()) {
                    nameText = d.getValue();
                    break;
                }
            }
        }
        if (nameText.isEmpty()) {
            User user = null;
            if (app.getUser() != null) {
                user = app.getUser();
            } else {
                try {
                    user = usersManager.getUserByExtSourceInformation(registrarSession, applicationPrincipal);
                } catch (Exception ex) {
                // user not found is ok
                }
            }
            if (user != null)
                nameText = user.getFirstName();
        }
        mailText = mailText.replace(FIELD_FIRST_NAME, nameText);
    }
    // replace lastName
    if (mailText.contains(FIELD_LAST_NAME)) {
        // backup
        String nameText = EMPTY_STRING;
        for (ApplicationFormItemData d : data) {
            if (URN_USER_LAST_NAME.equals(d.getFormItem().getPerunDestinationAttribute())) {
                if (d.getValue() != null && !d.getValue().isEmpty()) {
                    nameText = d.getValue();
                    break;
                }
            }
        }
        if (nameText.isEmpty()) {
            User user = null;
            if (app.getUser() != null) {
                user = app.getUser();
            } else {
                try {
                    user = usersManager.getUserByExtSourceInformation(registrarSession, applicationPrincipal);
                } catch (Exception ex) {
                // user not found is ok
                }
            }
            if (user != null)
                nameText = user.getLastName();
        }
        mailText = mailText.replace(FIELD_LAST_NAME, nameText);
    }
    // replace exceptions
    if (mailText.contains(FIELD_ERRORS)) {
        String errorText = EMPTY_STRING;
        if (exceptions != null && !exceptions.isEmpty()) {
            for (Exception ex : exceptions) {
                errorText = errorText.concat("\n\n" + ex.toString());
            }
        }
        mailText = mailText.replace(FIELD_ERRORS, errorText);
    }
    // replace logins
    if (mailText.contains("{login-")) {
        Pattern pattern = Pattern.compile("\\{login-[^}]+}");
        Matcher m = pattern.matcher(mailText);
        while (m.find()) {
            // whole "{login-something}"
            String toSubstitute = m.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 "meta", "egi-ui",...
                String namespace = m2.group(1);
                // if user not known -> search through form items to get login
                for (ApplicationFormItemData d : data) {
                    ApplicationFormItem item = d.getFormItem();
                    if (item != null) {
                        if (ApplicationFormItem.Type.USERNAME.equals(item.getType())) {
                            // if username match namespace
                            if (item.getPerunDestinationAttribute().contains("login-namespace:" + namespace)) {
                                if (d.getValue() != null && !d.getValue().isEmpty()) {
                                    // save not null or empty value and break cycle
                                    newValue = d.getValue();
                                    break;
                                }
                            }
                        }
                    }
                }
                // since we do no allow to overwrite login by application
                try {
                    if (app.getUser() != null) {
                        List<Attribute> logins = attrManager.getLogins(registrarSession, app.getUser());
                        for (Attribute a : logins) {
                            // replace only correct namespace
                            if (a.getFriendlyNameParameter().equalsIgnoreCase(namespace)) {
                                if (a.getValue() != null) {
                                    newValue = BeansUtils.attributeValueToString(a);
                                    break;
                                }
                            }
                        }
                    }
                } catch (Exception ex) {
                    log.error("[MAIL MANAGER] Error thrown when replacing login in namespace \"{}\" for mail. {}", namespace, ex);
                }
            }
            // substitute {login-namespace} with actual value or empty string
            mailText = mailText.replace(toSubstitute, newValue != null ? newValue : EMPTY_STRING);
        }
    }
    mailText = replaceAppDetailUrl(mailText, app.getId(), app.getVo(), app.getGroup());
    mailText = replaceAppGuiUrl(mailText, app.getVo(), app.getGroup());
    mailText = replacePerunGuiUrl(mailText, app.getVo(), app.getGroup());
    // membership expiration
    if (mailText.contains(FIELD_MEMBERSHIP_EXPIRATION)) {
        String expiration = EMPTY_STRING;
        if (app.getUser() != null) {
            try {
                User u = usersManager.getUserById(registrarSession, app.getUser().getId());
                Member m = membersManager.getMemberByUser(registrarSession, app.getVo(), u);
                Attribute a = attrManager.getAttribute(registrarSession, m, URN_MEMBER_EXPIRATION);
                if (a != null && a.getValue() != null) {
                    // attribute value is string
                    expiration = ((String) a.getValue());
                }
            } catch (Exception ex) {
                log.error("[MAIL MANAGER] Error thrown when getting membership expiration param for mail.", ex);
            }
        }
        // replace by date or empty
        mailText = mailText.replace(FIELD_MEMBERSHIP_EXPIRATION, expiration);
    }
    // user mail
    if (mailText.contains(FIELD_MAIL)) {
        String mail = EMPTY_STRING;
        if (app.getUser() != null) {
            try {
                User u = usersManager.getUserById(registrarSession, app.getUser().getId());
                Attribute a = attrManager.getAttribute(registrarSession, u, URN_USER_PREFERRED_MAIL);
                if (a != null && a.getValue() != null) {
                    // attribute value is string
                    mail = ((String) a.getValue());
                }
            } catch (Exception ex) {
                log.error("[MAIL MANAGER] Error thrown when getting preferred mail param for mail.", ex);
            }
        } else {
            for (ApplicationFormItemData d : data) {
                if (URN_MEMBER_MAIL.equals(d.getFormItem().getPerunDestinationAttribute())) {
                    if (d.getValue() != null && !d.getValue().isEmpty()) {
                        mail = d.getValue();
                        break;
                    }
                }
            }
            for (ApplicationFormItemData d : data) {
                if (URN_USER_PREFERRED_MAIL.equals(d.getFormItem().getPerunDestinationAttribute())) {
                    if (d.getValue() != null && !d.getValue().isEmpty()) {
                        mail = d.getValue();
                        break;
                    }
                }
            }
        }
        // replace by mail or empty
        mailText = mailText.replace(FIELD_MAIL, mail);
    }
    // user phone
    if (mailText.contains(FIELD_PHONE)) {
        String phone = EMPTY_STRING;
        if (app.getUser() != null) {
            try {
                User u = usersManager.getUserById(registrarSession, app.getUser().getId());
                Attribute a = attrManager.getAttribute(registrarSession, u, URN_USER_PHONE);
                if (a != null && a.getValue() != null) {
                    // attribute value is string
                    phone = ((String) a.getValue());
                }
            } catch (Exception ex) {
                log.error("[MAIL MANAGER] Error thrown when getting phone param for mail.", ex);
            }
        } else {
            for (ApplicationFormItemData d : data) {
                if (URN_MEMBER_PHONE.equals(d.getFormItem().getPerunDestinationAttribute())) {
                    if (d.getValue() != null && !d.getValue().isEmpty()) {
                        phone = d.getValue();
                        break;
                    }
                }
            }
            for (ApplicationFormItemData d : data) {
                if (URN_USER_PHONE.equals(d.getFormItem().getPerunDestinationAttribute())) {
                    if (d.getValue() != null && !d.getValue().isEmpty()) {
                        phone = d.getValue();
                        break;
                    }
                }
            }
        }
        // replace by phone or empty
        mailText = mailText.replace(FIELD_PHONE, phone);
    }
    // mail footer
    if (mailText.contains(FIELD_MAIL_FOOTER)) {
        String footer = EMPTY_STRING;
        // get proper value from attribute
        try {
            Attribute attribute;
            if (app.getGroup() != null) {
                attribute = attrManager.getAttribute(registrarSession, app.getGroup(), URN_GROUP_MAIL_FOOTER);
                if (attribute == null || attribute.getValue() == null) {
                    attribute = attrManager.getAttribute(registrarSession, app.getVo(), URN_VO_MAIL_FOOTER);
                }
            } else {
                attribute = attrManager.getAttribute(registrarSession, app.getVo(), URN_VO_MAIL_FOOTER);
            }
            if (attribute != null && attribute.getValue() != null) {
                footer = BeansUtils.attributeValueToString(attribute);
            }
        } catch (Exception ex) {
            // we dont care about exceptions here
            log.error("[MAIL MANAGER] Exception thrown when getting VO's footer for email from attribute.", ex);
        }
        // replace by footer or empty
        mailText = mailText.replace(FIELD_MAIL_FOOTER, (footer != null) ? footer : EMPTY_STRING);
    }
    return mailText;
}
Also used : Pattern(java.util.regex.Pattern) ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem) Matcher(java.util.regex.Matcher) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) ApplicationMailNotExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException) MessagingException(javax.mail.MessagingException) ApplicationMailExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailExistsException) ApplicationMailAlreadyRemovedException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailAlreadyRemovedException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) MailException(org.springframework.mail.MailException) FormNotExistsException(cz.metacentrum.perun.registrar.exceptions.FormNotExistsException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Aggregations

ApplicationFormItem (cz.metacentrum.perun.registrar.model.ApplicationFormItem)23 ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)10 ApplicationFormItemData (cz.metacentrum.perun.registrar.model.ApplicationFormItemData)10 Test (org.junit.Test)10 Group (cz.metacentrum.perun.core.api.Group)5 Application (cz.metacentrum.perun.registrar.model.Application)5 RichApplication (cz.metacentrum.perun.registrar.model.RichApplication)5 User (cz.metacentrum.perun.core.api.User)4 AppType (cz.metacentrum.perun.registrar.model.Application.AppType)4 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)4 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)4 MailException (org.springframework.mail.MailException)4 GroupsManager (cz.metacentrum.perun.core.api.GroupsManager)3 RegistrarException (cz.metacentrum.perun.registrar.exceptions.RegistrarException)3 ItemTexts (cz.metacentrum.perun.registrar.model.ApplicationFormItem.ItemTexts)3 RegistrarModule (cz.metacentrum.perun.registrar.RegistrarModule)2 ApplicationMailAlreadyRemovedException (cz.metacentrum.perun.registrar.exceptions.ApplicationMailAlreadyRemovedException)2 ApplicationMailExistsException (cz.metacentrum.perun.registrar.exceptions.ApplicationMailExistsException)2 ApplicationMailNotExistsException (cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException)2 FormNotExistsException (cz.metacentrum.perun.registrar.exceptions.FormNotExistsException)2