use of cz.metacentrum.perun.registrar.model.ApplicationMail in project perun by CESNET.
the class MailManagerImpl method getApplicationMails.
@Override
public List<ApplicationMail> getApplicationMails(PerunSession sess, ApplicationForm form) throws PerunException {
List<ApplicationMail> mails = new ArrayList<ApplicationMail>();
mails = jdbc.query(MAILS_SELECT_BY_FORM_ID, new RowMapper<ApplicationMail>() {
@Override
public ApplicationMail mapRow(ResultSet rs, int arg1) throws SQLException {
return new ApplicationMail(rs.getInt("id"), AppType.valueOf(rs.getString("app_type")), rs.getInt("form_id"), MailType.valueOf(rs.getString("mail_type")), rs.getBoolean("send"));
}
}, form.getId());
for (ApplicationMail mail : mails) {
List<MailText> texts = new ArrayList<MailText>();
texts = jdbc.query(MAIL_TEXTS_SELECT_BY_MAIL_ID, new RowMapper<MailText>() {
@Override
public MailText mapRow(ResultSet rs, int arg1) throws SQLException {
return new MailText(new Locale(rs.getString("locale")), rs.getString("subject"), rs.getString("text"));
}
}, mail.getId());
for (MailText text : texts) {
// fil localized messages
mail.getMessage().put(text.getLocale(), text);
}
}
return mails;
}
use of cz.metacentrum.perun.registrar.model.ApplicationMail in project perun by CESNET.
the class MailManagerImpl method sendMessage.
@Override
public void sendMessage(PerunSession sess, Application app, MailType mailType, String reason) throws PerunException {
// authz
if (app.getGroup() == null) {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, app.getVo())) {
throw new PrivilegeException(sess, "sendMessage");
}
} else {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, app.getVo()) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, app.getGroup())) {
throw new PrivilegeException(sess, "sendMessage");
}
}
if (MailType.USER_INVITE.equals(mailType)) {
throw new RegistrarException("USER_INVITE notification can't be sent this way. Use sendInvitation() instead.");
}
ApplicationForm form;
if (app.getGroup() == null) {
form = registrarManager.getFormForVo(app.getVo());
} else {
form = registrarManager.getFormForGroup(app.getGroup());
}
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.");
}
if (MailType.APP_CREATED_USER.equals(mailType) || MailType.APP_CREATED_VO_ADMIN.equals(mailType)) {
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.");
}
} else if (MailType.MAIL_VALIDATION.equals(mailType)) {
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.");
}
} else if (MailType.APP_APPROVED_USER.equals(mailType)) {
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.");
}
} else if (MailType.APP_REJECTED_USER.equals(mailType)) {
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.");
}
}
} else {
// perun admin can always sent any message with exception of USER_INVITE
sendMessage(app, mailType, reason, null);
}
perun.getAuditer().log(sess, "Mail of Type: {} sent for Application: {}", mailType, app.getId());
}
use of cz.metacentrum.perun.registrar.model.ApplicationMail 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 {
if (group == null) {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, vo) && !AuthzResolver.isAuthorized(sess, Role.TOPGROUPCREATOR, vo)) {
throw new PrivilegeException(sess, "sendInvitation");
}
} else {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, vo) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, group)) {
throw new PrivilegeException(sess, "sendInvitation");
}
}
if (email == null || email.isEmpty())
throw new RegistrarException("You must provide non-empty email of person you are inviting.");
// get form
ApplicationForm form;
if (group != null) {
form = registrarManager.getFormForGroup(group);
} else {
form = registrarManager.getFormForVo(vo);
}
// get mail definition
ApplicationMail mail = getMailByParams(form.getId(), AppType.INITIAL, MailType.USER_INVITE);
if (mail == null) {
throw new RegistrarException("You don't have invitation e-mail template defined.");
} else if (mail.getSend() == false) {
throw new RegistrarException("Sending of invitations is disabled.");
}
if (language == null) {
language = "en";
if (group == null) {
try {
Attribute a = attrManager.getAttribute(registrarSession, vo, URN_VO_LANGUAGE_EMAIL);
if (a != null && a.getValue() != null) {
language = BeansUtils.attributeValueToString(a);
}
} catch (Exception ex) {
log.error("[MAIL MANAGER] Exception thrown when getting preferred language of notification for VO={}: {}", vo, ex);
}
} else {
try {
Attribute a = attrManager.getAttribute(registrarSession, group, URN_GROUP_LANGUAGE_EMAIL);
if (a != null && a.getValue() != null) {
language = BeansUtils.attributeValueToString(a);
}
} catch (Exception ex) {
log.error("[MAIL MANAGER] Exception thrown when getting preferred language of notification for Group={}: {}", group, ex);
}
}
}
// get language
Locale lang = new Locale(language);
// get localized subject and text
MailText mt = mail.getMessage(lang);
String mailText = "";
String mailSubject = "";
if (mt.getText() != null && !mt.getText().isEmpty()) {
mailText = mt.getText();
}
if (mt.getSubject() != null && !mt.getSubject().isEmpty()) {
mailSubject = mt.getSubject();
}
SimpleMailMessage message = new SimpleMailMessage();
// fake app to get "from" address
Application app = new Application();
app.setVo(vo);
app.setGroup(group);
// get from
setFromMailAddress(message, app);
message.setTo(email);
mailText = substituteCommonStringsForInvite(vo, group, null, name, mailText);
mailSubject = substituteCommonStringsForInvite(vo, group, null, name, mailSubject);
message.setSubject(mailSubject);
message.setText(mailText);
try {
mailSender.send(message);
log.info("[MAIL MANAGER] Sending mail: USER_INVITE to: {} / " + app.getVo() + " / " + app.getGroup(), message.getTo());
} catch (MailException ex) {
log.error("[MAIL MANAGER] Sending mail: USER_INVITE failed because of exception: {}", ex);
throw new RegistrarException("Unable to send e-mail.", ex);
}
}
use of cz.metacentrum.perun.registrar.model.ApplicationMail in project perun by CESNET.
the class MailManagerImpl method getMailById.
@Override
public ApplicationMail getMailById(PerunSession sess, Integer id) throws InternalErrorException, PrivilegeException {
// TODO authz
ApplicationMail mail = null;
// get mail def
try {
List<ApplicationMail> mails = jdbc.query("select id,app_type,form_id,mail_type,send from application_mails where id=?", new RowMapper<ApplicationMail>() {
@Override
public ApplicationMail mapRow(ResultSet rs, int arg1) throws SQLException {
return new ApplicationMail(rs.getInt("id"), AppType.valueOf(rs.getString("app_type")), rs.getInt("form_id"), MailType.valueOf(rs.getString("mail_type")), rs.getBoolean("send"));
}
}, id);
// set
if (mails.size() != 1) {
log.error("[MAIL MANAGER] Wrong number of mail definitions returned by unique params, expected 1 but was: " + mails.size());
throw new InternalErrorException("Wrong number of mail definitions returned by unique params, expected 1 but was: " + mails.size());
}
mail = mails.get(0);
} catch (EmptyResultDataAccessException ex) {
throw new InternalErrorException("Mail definition with ID=" + id + " doesn't exists.");
}
List<MailText> texts = new ArrayList<MailText>();
try {
texts = jdbc.query(MAIL_TEXTS_SELECT_BY_MAIL_ID, new RowMapper<MailText>() {
@Override
public MailText mapRow(ResultSet rs, int arg1) throws SQLException {
return new MailText(new Locale(rs.getString("locale")), rs.getString("subject"), rs.getString("text"));
}
}, mail.getId());
} catch (EmptyResultDataAccessException ex) {
// if no texts it's error
log.error("[MAIL MANAGER] Mail do not contains any text message: {}", ex);
return mail;
}
for (MailText text : texts) {
// fill localized messages
mail.getMessage().put(text.getLocale(), text);
}
return mail;
}
use of cz.metacentrum.perun.registrar.model.ApplicationMail in project perun by CESNET.
the class MailManagerImpl method copyMailsFromGroupToGroup.
@Override
public void copyMailsFromGroupToGroup(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)) {
throw new PrivilegeException(sess, "copyMailsFromGroupToGroup");
}
if (!AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, toGroup) && !AuthzResolver.isAuthorized(sess, Role.VOADMIN, toGroup)) {
throw new PrivilegeException(sess, "copyMailsFromGroupToGroup");
}
ApplicationForm formFrom = registrarManager.getFormForGroup(fromGroup);
ApplicationForm formTo = registrarManager.getFormForGroup(toGroup);
List<ApplicationMail> mails = getApplicationMails(sess, formFrom);
for (ApplicationMail mail : mails) {
// to start transaction
try {
registrarManager.getMailManager().addMail(sess, formTo, mail);
} catch (DuplicateKeyException ex) {
log.info("[MAIL MANAGER] Mail notification of type {} skipped while copying (was already present).", mail.getMailType() + "/" + mail.getAppType());
}
}
}
Aggregations