Search in sources :

Example 1 with MailSentForApplication

use of cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication 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)

Aggregations

MailSentForApplication (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)1 RegistrarException (cz.metacentrum.perun.registrar.exceptions.RegistrarException)1 ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)1 ApplicationMail (cz.metacentrum.perun.registrar.model.ApplicationMail)1