Search in sources :

Example 1 with ApplicationForm

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

the class RegistrarManagerImpl method tryToAutoApproveApplication.

/**
	 * Try to approve application if auto-approve is possible
	 *
	 * @param sess user who try to approves application
	 * @param app application to approve
	 * @throws InternalErrorException
	 */
private void tryToAutoApproveApplication(PerunSession sess, Application app) throws PerunException {
    ApplicationForm form;
    if (app.getGroup() != null) {
        // group application
        form = getFormForGroup(app.getGroup());
    } else {
        // vo application
        form = getFormForVo(app.getVo());
    }
    AppType type = app.getType();
    if (AppType.INITIAL.equals(type) && !form.isAutomaticApproval())
        return;
    if (AppType.EXTENSION.equals(type) && !form.isAutomaticApprovalExtension())
        return;
    // do not auto-approve Group applications, if user is not member of VO
    if (app.getGroup() != null && app.getVo() != null) {
        try {
            User u = null;
            if (app.getUser() == null) {
                u = usersManager.getUserByExtSourceNameAndExtLogin(registrarSession, app.getExtSourceName(), app.getCreatedBy());
                if (u != null) {
                    membersManager.getMemberByUser(registrarSession, app.getVo(), u);
                } else {
                    // user not found or null, hence can't be member of VO -> do not approve.
                    return;
                }
            } else {
                // user known, but maybe not member of a vo
                membersManager.getMemberByUser(registrarSession, app.getVo(), app.getUser());
            }
        } catch (MemberNotExistsException ex) {
            return;
        } catch (UserNotExistsException ex) {
            return;
        } catch (UserExtSourceNotExistsException ex) {
            return;
        } catch (ExtSourceNotExistsException ex) {
            return;
        }
    }
    try {
        if (AppState.VERIFIED.equals(app.getState())) {
            // with registrar session, since only VO admin can approve application
            // check if can be approved (we normally call this manually from GUI before calling approve)
            canBeApproved(registrarSession, app);
            /*

				FIXME - temporarily disabled checking

				if (app.getUser() == null && !app.getExtSourceName().equalsIgnoreCase("LOCAL")) {
					List<RichUser> list = checkForSimilarUsers(registrarSession, app.getId());
					if (!list.isEmpty()) {
						// found similar
						throw new RegistrarException("Similar users are already registered in system. Automatic approval of application was canceled to prevent creation of duplicate user entry. Please check and approve application manually.");
					} else {
						// similar NOT found - continue
						approveApplication(registrarSession, app.getId());
					}
				} else { }

				*/
            // other types of application doesn't create new user - continue
            approveApplication(registrarSession, app.getId());
        }
    } catch (Exception ex) {
        ArrayList<Exception> list = new ArrayList<Exception>();
        list.add(ex);
        getMailManager().sendMessage(app, MailType.APP_ERROR_VO_ADMIN, null, list);
        throw ex;
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) AppType(cz.metacentrum.perun.registrar.model.Application.AppType) SQLException(java.sql.SQLException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 2 with ApplicationForm

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

the class MailManagerImpl method copyMailsFromGroupToGroup.

@Override
public void copyMailsFromGroupToGroup(PerunSession sess, Group fromGroup, Group toGroup) throws PerunException {
    perun.getGroupsManagerBl().checkGroupExists(sess, fromGroup);
    perun.getGroupsManagerBl().checkGroupExists(sess, toGroup);
    // Authorization
    if (!AuthzResolver.authorizedInternal(sess, "copyMailsFromGroupToGroup_Group_Group_policy", fromGroup) || !AuthzResolver.authorizedInternal(sess, "copyMailsFromGroupToGroup_Group_Group_policy", toGroup)) {
        throw new PrivilegeException(sess, "copyMailsFromGroupToGroup");
    }
    ApplicationForm formFrom = registrarManager.getFormForGroup(fromGroup);
    ApplicationForm formTo = registrarManager.getFormForGroup(toGroup);
    copyApplicationMails(sess, formFrom, formTo);
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm)

Example 3 with ApplicationForm

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

the class MailManagerImpl method sendMessage.

@Override
public void sendMessage(PerunSession sess, Application app, MailType mailType, String reason) throws PerunException {
    if (MailType.USER_INVITE.equals(mailType)) {
        throw new RegistrarException("USER_INVITE notification can't be sent this way. Use sendInvitation() instead.");
    }
    // Authorization
    if (app.getGroup() != null) {
        if (!AuthzResolver.authorizedInternal(sess, "group-sendMessage_Application_MailType_String_policy", Arrays.asList(app.getGroup(), app.getVo())) && !AuthzResolver.selfAuthorizedForApplication(sess, app)) {
            throw new PrivilegeException(sess, "sendMessage");
        }
    } else {
        if (!AuthzResolver.authorizedInternal(sess, "vo-sendMessage_Application_MailType_String_policy", Collections.singletonList(app.getVo())) && !AuthzResolver.selfAuthorizedForApplication(sess, app)) {
            throw new PrivilegeException(sess, "sendMessage");
        }
    }
    ApplicationForm form = getForm(app);
    ApplicationMail mail = getMailByParams(form.getId(), app.getType(), mailType);
    if (mail == null)
        throw new RegistrarException("Notification template for " + mailType + " is not defined.");
    if (!mail.getSend())
        throw new RegistrarException("Sending of notification " + mailType + " is disabled.");
    if (!AuthzResolver.hasRole(sess.getPerunPrincipal(), Role.PERUNADMIN)) {
        if (MailType.APP_ERROR_VO_ADMIN.equals(mailType)) {
            throw new RegistrarException("APP_ERROR_VO_ADMIN notification can't be sent this way, since it's bound to each approval process. Try to approve application once again to receive this message.");
        }
        switch(mailType) {
            case APP_CREATED_USER:
            case APPROVABLE_GROUP_APP_USER:
            case APP_CREATED_VO_ADMIN:
                {
                    if (app.getState().equals(Application.AppState.NEW) || app.getState().equals(Application.AppState.VERIFIED)) {
                        sendMessage(app, mailType, null, null);
                    } else {
                        throw new RegistrarException("Application must be in state NEW or VERIFIED to allow sending of " + mailType + " notification.");
                    }
                }
                break;
            case MAIL_VALIDATION:
                {
                    if (app.getState().equals(Application.AppState.NEW)) {
                        sendMessage(app, mailType, null, null);
                    } else {
                        throw new RegistrarException("Application must be in state NEW to allow sending of " + mailType + " notification.");
                    }
                }
                break;
            case APP_APPROVED_USER:
                {
                    if (Application.AppState.APPROVED.equals(app.getState())) {
                        sendMessage(app, mailType, null, null);
                    } else {
                        throw new RegistrarException("Application must be in state APPROVED to allow sending of " + mailType + " notification.");
                    }
                }
                break;
            case APP_REJECTED_USER:
                {
                    if (Application.AppState.REJECTED.equals(app.getState())) {
                        sendMessage(app, mailType, reason, null);
                    } else {
                        throw new RegistrarException("Application must be in state REJECTED to allow sending of " + mailType + " notification.");
                    }
                }
                break;
        }
    } else {
        // perun admin can always be sent any message with an exception to the USER_INVITE
        sendMessage(app, mailType, reason, null);
    }
    perun.getAuditer().log(sess, new MailSentForApplication(mailType, app.getId()));
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) MailSentForApplication(cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail)

Example 4 with ApplicationForm

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

the class MailManagerImpl method updateMailById.

@Override
@Transactional(rollbackFor = Exception.class)
public void updateMailById(PerunSession sess, ApplicationMail mail) throws FormNotExistsException, ApplicationMailNotExistsException, PrivilegeException {
    ApplicationForm form = registrarManager.getFormById(sess, mail.getFormId());
    int numberOfExistences = jdbc.queryForInt("select count(1) from application_mails where id=?", mail.getId());
    if (numberOfExistences < 1)
        throw new ApplicationMailNotExistsException("Application mail does not exist.", mail);
    if (numberOfExistences > 1)
        throw new ConsistencyErrorException("There is more than one mail with id = " + mail.getId());
    // update sending (enabled / disabled)
    jdbc.update("update application_mails set send=? where id=?", mail.getSend(), mail.getId());
    // update texts (easy way = delete and new insert)
    jdbc.update("delete from application_mail_texts where mail_id=?", mail.getId());
    for (Locale loc : mail.getMessage().keySet()) {
        MailText text = mail.getMessage(loc);
        jdbc.update("insert into application_mail_texts(mail_id,locale,subject,text) values (?,?,?,?)", mail.getId(), loc.toString(), text.getSubject(), text.getText());
    }
    if (form.getGroup() != null) {
        perun.getAuditer().log(sess, new MailForGroupIdUpdated(mail, form.getGroup()));
    } else {
        perun.getAuditer().log(sess, new MailForVoIdUpdated(mail, form.getVo()));
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) ApplicationMailNotExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException) MailForGroupIdUpdated(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdUpdated) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) MailForVoIdUpdated(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdUpdated) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ApplicationForm

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

the class MailManagerImpl method sendMessage.

@Override
public void sendMessage(Application app, MailType mailType, String reason, List<Exception> exceptions) {
    try {
        // get form
        ApplicationForm form = getForm(app);
        // get mail definition
        ApplicationMail mail = getMailByParams(form.getId(), app.getType(), mailType);
        if (mail == null) {
            log.error("[MAIL MANAGER] Mail not sent. Definition (or mail text) for: {} do not exists for " + "VO: {} and Group: {}", mailType.toString(), app.getVo(), app.getGroup());
            // mail not found
            return;
        } else if (!mail.getSend()) {
            log.info("[MAIL MANAGER] Mail not sent. Disabled by VO admin for: {} / appID: {} / {} / {}", mail.getMailType(), app.getId(), app.getVo(), app.getGroup());
            // sending this mail has been disabled by VO admin
            return;
        }
        // get app data
        List<ApplicationFormItemData> data = registrarManager.getApplicationDataById(registrarSession, app.getId());
        // different behavior based on mail type
        switch(mail.getMailType()) {
            case APP_CREATED_USER:
                sendUserMessage(app, mail, data, reason, exceptions, MailType.APP_CREATED_USER);
                break;
            case APPROVABLE_GROUP_APP_USER:
                sendUserMessage(app, mail, data, reason, exceptions, MailType.APPROVABLE_GROUP_APP_USER);
                break;
            case APP_CREATED_VO_ADMIN:
                appCreatedVoAdmin(app, mail, data, reason, exceptions);
                break;
            case MAIL_VALIDATION:
                mailValidation(app, mail, data, reason, exceptions);
                break;
            case APP_APPROVED_USER:
                sendUserMessage(app, mail, data, reason, exceptions, MailType.APP_APPROVED_USER);
                break;
            case APP_REJECTED_USER:
                sendUserMessage(app, mail, data, reason, exceptions, MailType.APP_REJECTED_USER);
                break;
            case APP_ERROR_VO_ADMIN:
                appErrorVoAdmin(app, mail, data, reason, exceptions);
                break;
            default:
                log.error("[MAIL MANAGER] Sending mail type: {} is not supported.", mail.getMailType());
                break;
        }
    } catch (Exception ex) {
        // catch all exceptions and log to: perun-registrar.log
        log.error("[MAIL MANAGER] Exception thrown when sending email.", ex);
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) 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) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail)

Aggregations

ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)22 Test (org.junit.Test)13 ApplicationFormItem (cz.metacentrum.perun.registrar.model.ApplicationFormItem)11 Group (cz.metacentrum.perun.core.api.Group)8 ApplicationFormItemData (cz.metacentrum.perun.registrar.model.ApplicationFormItemData)7 User (cz.metacentrum.perun.core.api.User)6 Application (cz.metacentrum.perun.registrar.model.Application)6 GroupsManager (cz.metacentrum.perun.core.api.GroupsManager)5 RichApplication (cz.metacentrum.perun.registrar.model.RichApplication)5 ApplicationMail (cz.metacentrum.perun.registrar.model.ApplicationMail)4 ExtSource (cz.metacentrum.perun.core.api.ExtSource)2 Vo (cz.metacentrum.perun.core.api.Vo)2 ApplicationMailNotExistsException (cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException)2 RegistrarException (cz.metacentrum.perun.registrar.exceptions.RegistrarException)2 MailText (cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)2 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 MailForGroupIdUpdated (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdUpdated)1 MailForVoIdUpdated (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdUpdated)1 MailSentForApplication (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)1