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);
}
}
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()));
}
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);
}
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);
}
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;
}
Aggregations