use of org.olat.core.util.mail.MailerResult in project openolat by klemens.
the class RegistrationWebService method register.
/**
* Register with the specified email
* @response.representation.200.doc Registration successful
* @response.representation.304.doc Already registered, HTTP-Header location set to redirect
* @response.representation.400.doc Email address not allowed
* @param email The email address
* @param request The HTTP Request
* @return
*/
@PUT
public Response register(@QueryParam("email") String email, @Context HttpServletRequest request) {
if (!CoreSpringFactory.getImpl(RegistrationModule.class).isSelfRegistrationEnabled()) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
ResponseBuilder response;
Locale locale = getLocale(request);
Translator translator = getTranslator(locale);
MailManager mailM = CoreSpringFactory.getImpl(MailManager.class);
UserManager userManager = UserManager.getInstance();
RegistrationManager rm = CoreSpringFactory.getImpl(RegistrationManager.class);
boolean foundUser = userManager.findUniqueIdentityByEmail(email) != null;
boolean noNewUserWithEmail = !userManager.isEmailAllowed(email);
String serverpath = Settings.getServerContextPathURI();
if (foundUser && noNewUserWithEmail) {
// redirect
URI redirectUri = UriBuilder.fromUri(Settings.getServerContextPathURI()).build();
response = Response.ok().status(Status.NOT_MODIFIED).location(redirectUri);
} else if (userManager.isEmailAllowed(email)) {
String ip = request.getRemoteAddr();
TemporaryKey tk = null;
UserModule userModule = CoreSpringFactory.getImpl(UserModule.class);
if (userModule.isEmailUnique()) {
tk = rm.loadTemporaryKeyByEmail(email);
}
if (tk == null) {
tk = rm.loadOrCreateTemporaryKeyByEmail(email, ip, RegistrationManager.REGISTRATION);
}
String today = DateFormat.getDateInstance(DateFormat.LONG, locale).format(new Date());
String[] bodyAttrs = new String[] { serverpath, tk.getRegistrationKey(), CoreSpringFactory.getImpl(I18nModule.class).getLocaleKey(locale) };
String[] whereFromAttrs = new String[] { serverpath, today, ip };
String body = translator.translate("reg.body", bodyAttrs) + SEPARATOR + translator.translate("reg.wherefrom", whereFromAttrs);
try {
MailBundle bundle = new MailBundle();
bundle.setTo(email);
bundle.setContent(translator.translate("reg.subject"), body);
MailerResult result = mailM.sendExternMessage(bundle, null, true);
if (result.isSuccessful()) {
response = Response.ok();
} else {
response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
log.error("", e);
}
} else {
response = Response.serverError().status(Status.BAD_REQUEST);
}
return response.build();
}
use of org.olat.core.util.mail.MailerResult in project openolat by klemens.
the class RepositoryEntryDetailsController method doLeave.
protected void doLeave() {
if (guestOnly)
return;
MailerResult result = new MailerResult();
MailPackage reMailing = new MailPackage(result, getWindowControl().getBusinessControl().getAsString(), true);
LeavingStatusList status = new LeavingStatusList();
// leave course
repositoryManager.leave(getIdentity(), entry, status, reMailing);
// leave groups
businessGroupService.leave(getIdentity(), entry, status, reMailing);
// make sur all changes are committed
DBFactory.getInstance().commit();
if (status.isWarningManagedGroup() || status.isWarningManagedCourse()) {
showWarning("sign.out.warning.managed");
} else if (status.isWarningGroupWithMultipleResources()) {
showWarning("sign.out.warning.mutiple.resources");
} else {
showInfo("sign.out.success", new String[] { entry.getDisplayname() });
}
}
use of org.olat.core.util.mail.MailerResult in project openolat by klemens.
the class UnpublishResourceCallback method execute.
@Override
public Step execute(UserRequest ureq, WindowControl wControl, StepsRunContext runContext) {
MailTemplate mailTemplate = (MailTemplate) runContext.get("mailTemplate");
if (mailTemplate != null) {
List<Identity> ownerList = new ArrayList<Identity>();
// owners
if (repositoryService.hasRole(ureq.getIdentity(), repositoryEntry, GroupRoles.owner.name())) {
ownerList = repositoryService.getMembers(repositoryEntry, GroupRoles.owner.name());
}
String businessPath = wControl.getBusinessControl().getAsString();
MailContext context = new MailContextImpl(businessPath);
String metaId = UUID.randomUUID().toString().replace("-", "");
MailerResult result = new MailerResult();
MailBundle[] bundles = mailManager.makeMailBundles(context, ownerList, mailTemplate, ureq.getIdentity(), metaId, result);
result.append(mailManager.sendMessage(bundles));
if (mailTemplate.getCpfrom()) {
MailBundle ccBundle = mailManager.makeMailBundle(context, ureq.getIdentity(), mailTemplate, ureq.getIdentity(), metaId, result);
result.append(mailManager.sendMessage(ccBundle));
}
StringBuilder errorMessage = new StringBuilder();
StringBuilder warningMessage = new StringBuilder();
MailHelper.appendErrorsAndWarnings(result, errorMessage, warningMessage, ureq.getUserSession().getRoles().isOLATAdmin(), ureq.getLocale());
if (warningMessage.length() > 0) {
wControl.setWarning(warningMessage.toString());
}
if (errorMessage.length() > 0) {
wControl.setError(errorMessage.toString());
}
ownerList.clear();
}
// update status
repositoryEntry = repositoryService.loadByKey(repositoryEntry.getKey());
repositoryEntry.setStatusCode(RepositoryEntryStatus.REPOSITORY_STATUS_CLOSED);
repositoryEntry = DBFactory.getInstance().getCurrentEntityManager().merge(repositoryEntry);
// clean catalog
Object cleanCatalog = runContext.get("cleanCatalog");
if (cleanCatalog != null && Boolean.TRUE.equals(cleanCatalog)) {
CoreSpringFactory.getImpl(CatalogManager.class).resourceableDeleted(repositoryEntry);
}
// clean groups
Object cleanGroups = runContext.get("cleanGroups");
if (cleanGroups != null && Boolean.TRUE.equals(cleanGroups)) {
doCleanGroups(ureq.getIdentity());
}
ThreadLocalUserActivityLogger.log(CourseLoggingAction.COURSE_EDITOR_CLOSE, getClass());
log.audit("Repository entry " + repositoryEntry.getDisplayname() + " ( " + repositoryEntry.getOlatResource() + " ) closed");
return StepsMainRunController.DONE_MODIFIED;
}
use of org.olat.core.util.mail.MailerResult in project openolat by klemens.
the class ReminderServiceImpl method sendReminder.
@Override
public MailerResult sendReminder(Reminder reminder, List<Identity> identitiesToRemind) {
RepositoryEntry entry = reminder.getEntry();
ContactList contactList = new ContactList("Infos");
contactList.addAllIdentites(identitiesToRemind);
MailContext context = new MailContextImpl("[RepositoryEntry:" + entry.getKey() + "]");
Locale locale = I18nModule.getDefaultLocale();
Translator trans = Util.createPackageTranslator(ReminderAdminController.class, locale);
String subject = reminder.getEmailSubject();
String body = reminder.getEmailBody();
if (body.contains("$courseurl")) {
body = body.replace("$courseurl", "<a href=\"$courseurl\">$courseurl</a>");
} else {
body = body + "<p>---<br />" + trans.translate("reminder.from.course", new String[] { "<a href=\"$courseurl\">$coursename</a>" }) + "</p>";
}
String metaId = UUID.randomUUID().toString();
String url = Settings.getServerContextPathURI() + "/url/RepositoryEntry/" + entry.getKey();
MailerResult overviewResult = new MailerResult();
ReminderTemplate template = new ReminderTemplate(subject, body, url, entry, locale);
for (Identity identityToRemind : identitiesToRemind) {
String status;
MailBundle bundle = mailManager.makeMailBundle(context, identityToRemind, template, null, metaId, overviewResult);
MailerResult result = mailManager.sendMessage(bundle);
overviewResult.append(result);
List<Identity> failedIdentities = result.getFailedIdentites();
if (failedIdentities != null && failedIdentities.contains(identityToRemind)) {
status = "error";
} else {
status = "ok";
}
reminderDao.markAsSend(reminder, identityToRemind, status);
}
return overviewResult;
}
use of org.olat.core.util.mail.MailerResult in project openolat by klemens.
the class MailTest method testMailToCcBccForEach.
/**
* Test for the mail template and the context variable methods
*/
@Test
public void testMailToCcBccForEach() {
String subject = "For Each Subject: Hello $firstname $lastname";
String body = "For Each Body: \n\n You ($login) should go to \n\n'$coursename' @ $courseURL$login";
final String coursename = "my course";
final String courseURL = "http://www.mytrashmail.com/myTrashMail_inbox.aspx?email=";
MailTemplate template = new MailTemplate(subject, body, null) {
@Override
public void putVariablesInMailContext(VelocityContext context, Identity identity) {
// Put user variables
User user = identity.getUser();
context.put("firstname", user.getProperty(UserConstants.FIRSTNAME, null));
context.put("lastname", user.getProperty(UserConstants.LASTNAME, null));
context.put("login", identity.getName());
// Put variables from greater context, eg. course id, group name etc.
context.put("coursename", coursename);
context.put("courseURL", courseURL);
}
};
// some recipients data
List<Identity> recipients = new ArrayList<Identity>();
recipients.add(id1);
recipients.add(id2);
recipients.add(id3);
Identity recipientCC = id4;
// tests with / witthout CC and BCC
MailerResult result = new MailerResult();
result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
assertEquals(MailerResult.OK, result.getReturnCode());
result = sendMailAsSeparateMails(null, recipients, recipientCC, template, id6, null);
assertEquals(MailerResult.OK, result.getReturnCode());
result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
assertEquals(MailerResult.OK, result.getReturnCode());
}
Aggregations