Search in sources :

Example 1 with RegistrarException

use of cz.metacentrum.perun.registrar.exceptions.RegistrarException in project perun by CESNET.

the class MailManagerImpl method sendInvitationMail.

/**
 * Send invitation email to one user
 */
private void sendInvitationMail(PerunSession sess, Vo vo, Group group, String email, String language, MimeMessage message, Application app) throws RegistrarException {
    try {
        mailSender.send(message);
        User sendingUser = sess.getPerunPrincipal().getUser();
        AuditEvent event = new InvitationSentEvent(sendingUser, email, language, group, vo);
        sess.getPerun().getAuditer().log(sess, event);
        log.info("[MAIL MANAGER] Sending mail: USER_INVITE to: {} / {} / {}", message.getAllRecipients(), app.getVo(), app.getGroup());
    } catch (MailException | MessagingException ex) {
        log.error("[MAIL MANAGER] Sending mail: USER_INVITE failed because of exception.", ex);
        throw new RegistrarException("Unable to send e-mail.", ex);
    }
}
Also used : InvitationSentEvent(cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent) MessagingException(javax.mail.MessagingException) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) MailException(org.springframework.mail.MailException)

Example 2 with RegistrarException

use of cz.metacentrum.perun.registrar.exceptions.RegistrarException 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 3 with RegistrarException

use of cz.metacentrum.perun.registrar.exceptions.RegistrarException in project perun by CESNET.

the class MailManagerImpl method sendInvitation.

@Override
public void sendInvitation(PerunSession sess, Vo vo, Group group, String name, String email, String language) throws PerunException {
    perun.getVosManagerBl().checkVoExists(sess, vo);
    if (email == null || email.isEmpty()) {
        throw new RegistrarException("You must provide non-empty email of person you are inviting.");
    }
    // Authorization
    if (group != null) {
        perun.getGroupsManagerBl().checkGroupExists(sess, group);
        if (!AuthzResolver.authorizedInternal(sess, "group-sendInvitation_Vo_Group_String_String_String_policy", Arrays.asList(vo, group))) {
            throw new PrivilegeException(sess, "sendInvitation");
        }
    } else {
        if (!AuthzResolver.authorizedInternal(sess, "vo-sendInvitation_Vo_Group_String_String_String_policy", Collections.singletonList(vo))) {
            throw new PrivilegeException(sess, "sendInvitation");
        }
    }
    Application app = getFakeApplication(vo, group);
    MimeMessage message;
    try {
        message = getInvitationMessage(vo, group, language, email, app, name, null);
    } catch (MessagingException e) {
        throw new RegistrarException("[MAIL MANAGER] Exception thrown when getting invitation message", e);
    }
    sendInvitationMail(sess, vo, group, email, language, message, app);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) Application(cz.metacentrum.perun.registrar.model.Application) MailSentForApplication(cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)

Example 4 with RegistrarException

use of cz.metacentrum.perun.registrar.exceptions.RegistrarException in project perun by CESNET.

the class MailManagerImpl method sendInvitation.

@Override
public void sendInvitation(PerunSession sess, Vo vo, Group group, User user) throws PerunException {
    perun.getVosManagerBl().checkVoExists(sess, vo);
    if (user == null)
        throw new RegistrarException("Missing user to send notification to.");
    // Authorization
    if (group != null) {
        perun.getGroupsManagerBl().checkGroupExists(sess, group);
        if (!AuthzResolver.authorizedInternal(sess, "group-sendInvitation_Vo_Group_User_policy", Arrays.asList(vo, group, user))) {
            throw new PrivilegeException(sess, "sendInvitation");
        }
    } else {
        if (!AuthzResolver.authorizedInternal(sess, "vo-sendInvitation_Vo_Group_User_policy", Arrays.asList(vo, user))) {
            throw new PrivilegeException(sess, "sendInvitation");
        }
    }
    try {
        Member m = membersManager.getMemberByUser(sess, vo, user);
        // is a member, is invited to group ?
        if (group != null) {
            List<Group> g = groupsManager.getMemberGroups(sess, m);
            if (g.contains(group)) {
                // user is member of group - can't invite him
                throw new RegistrarException("User to invite is already member of your group: " + group.getShortName());
            }
        } else {
            throw new RegistrarException("User to invite is already member of your VO:" + vo.getShortName());
        }
    } catch (Exception ex) {
        log.error("[MAIL MANAGER] Exception throw when getting member by {} from {}: {}", user, vo.toString(), ex);
    }
    String email = EMPTY_STRING;
    try {
        Attribute a = attrManager.getAttribute(registrarSession, user, URN_USER_PREFERRED_MAIL);
        if (a != null && a.getValue() != null) {
            email = BeansUtils.attributeValueToString(a);
        }
    } catch (Exception ex) {
        log.error("[MAIL MANAGER] Exception thrown when getting preferred language of notification for Group={}: {}", group, ex);
    }
    String language = LANG_EN;
    language = getLanguageForUser(user, language);
    Application app = getFakeApplication(vo, group);
    MimeMessage message = null;
    try {
        message = getInvitationMessage(vo, group, language, email, app, null, user);
    } catch (MessagingException e) {
        throw new RegistrarException("[MAIL MANAGER] Exception thrown when getting invitation message", e);
    }
    sendInvitationMail(sess, vo, group, email, language, message, app);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) Application(cz.metacentrum.perun.registrar.model.Application) MailSentForApplication(cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication) 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)

Example 5 with RegistrarException

use of cz.metacentrum.perun.registrar.exceptions.RegistrarException in project perun by CESNET.

the class Vsup method beforeApprove.

@Override
public Application beforeApprove(PerunSession session, Application app) throws RegistrarException, PrivilegeException {
    List<ApplicationFormItemData> data = registrar.getApplicationDataById(session, app.getId());
    if (app.getUser() == null) {
        for (ApplicationFormItemData item : data) {
            if (item.getFormItem() != null && Objects.equals(AttributesManager.NS_USER_ATTR_DEF + ":birthNumber", item.getFormItem().getPerunDestinationAttribute())) {
                // if application contains birth number, try to map to existing user
                String rc = item.getValue();
                if (rc != null && !rc.isEmpty()) {
                    try {
                        User user = ((PerunBl) session.getPerun()).getUsersManagerBl().getUserByExtSourceNameAndExtLogin(session, "RC", rc);
                        app.setUser(user);
                        registrar.updateApplicationUser(session, app);
                        log.debug("Existing user found by RC for {}", app);
                    } catch (Exception ex) {
                        log.warn("Couldn't find or set user to application {} by RC: {}", app, ex);
                    }
                    // associate existing user with the identity used on registration form
                    if (app.getUser() != null) {
                        PerunBl perunBl = (PerunBl) session.getPerun();
                        ExtSource es = perunBl.getExtSourcesManager().checkOrCreateExtSource(session, app.getExtSourceName(), app.getExtSourceType());
                        UserExtSource ues = new UserExtSource(es, app.getExtSourceLoa(), app.getCreatedBy());
                        try {
                            ues = perunBl.getUsersManagerBl().addUserExtSource(session, app.getUser(), ues);
                            log.debug("{} associated with {} from application {}", app.getUser(), ues, app);
                        } catch (UserExtSourceExistsException ex) {
                            // we can ignore, user will be paired with application
                            log.warn("{} already had identity associated from application {}", app.getUser(), app);
                        }
                        try {
                            Member member = ((PerunBl) session.getPerun()).getMembersManagerBl().getMemberByUser(session, app.getVo(), app.getUser());
                            // user is already a member, switch application type
                            if (Application.AppType.INITIAL.equals(app.getType())) {
                                app.setType(Application.AppType.EXTENSION);
                                registrar.updateApplicationType(session, app);
                                log.debug("Updating application type to EXTENSION since we matched user which is VO member!");
                            }
                        } catch (MemberNotExistsException e) {
                        // OK state
                        }
                    }
                }
                break;
            }
        }
    }
    return app;
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) User(cz.metacentrum.perun.core.api.User) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) ParseException(java.text.ParseException) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) CantBeApprovedException(cz.metacentrum.perun.registrar.exceptions.CantBeApprovedException)

Aggregations

RegistrarException (cz.metacentrum.perun.registrar.exceptions.RegistrarException)6 MailSentForApplication (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)3 MessagingException (javax.mail.MessagingException)3 ExtSource (cz.metacentrum.perun.core.api.ExtSource)2 Member (cz.metacentrum.perun.core.api.Member)2 User (cz.metacentrum.perun.core.api.User)2 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)2 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)2 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)2 Application (cz.metacentrum.perun.registrar.model.Application)2 ApplicationFormItemData (cz.metacentrum.perun.registrar.model.ApplicationFormItemData)2 ParseException (java.text.ParseException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MailException (org.springframework.mail.MailException)2 AuditEvent (cz.metacentrum.perun.audit.events.AuditEvent)1 InvitationSentEvent (cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent)1 Attribute (cz.metacentrum.perun.core.api.Attribute)1 Vo (cz.metacentrum.perun.core.api.Vo)1 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)1