Search in sources :

Example 46 with MailerResult

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();
}
Also used : Locale(java.util.Locale) RestSecurityHelper.getLocale(org.olat.restapi.security.RestSecurityHelper.getLocale) RegistrationManager(org.olat.registration.RegistrationManager) MailerResult(org.olat.core.util.mail.MailerResult) TemporaryKey(org.olat.registration.TemporaryKey) URI(java.net.URI) Date(java.util.Date) Translator(org.olat.core.gui.translator.Translator) UserManager(org.olat.user.UserManager) MailManager(org.olat.core.util.mail.MailManager) UserModule(org.olat.user.UserModule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) MailBundle(org.olat.core.util.mail.MailBundle) PUT(javax.ws.rs.PUT)

Example 47 with MailerResult

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() });
    }
}
Also used : LeavingStatusList(org.olat.repository.LeavingStatusList) MailPackage(org.olat.core.util.mail.MailPackage) MailerResult(org.olat.core.util.mail.MailerResult)

Example 48 with MailerResult

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;
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) ArrayList(java.util.ArrayList) CatalogManager(org.olat.repository.manager.CatalogManager) MailTemplate(org.olat.core.util.mail.MailTemplate) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 49 with MailerResult

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;
}
Also used : Locale(java.util.Locale) MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) Translator(org.olat.core.gui.translator.Translator) MailerResult(org.olat.core.util.mail.MailerResult) RepositoryEntry(org.olat.repository.RepositoryEntry) ContactList(org.olat.core.util.mail.ContactList) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 50 with MailerResult

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());
}
Also used : User(org.olat.core.id.User) MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Aggregations

MailerResult (org.olat.core.util.mail.MailerResult)140 MailBundle (org.olat.core.util.mail.MailBundle)100 Identity (org.olat.core.id.Identity)82 MailContext (org.olat.core.util.mail.MailContext)66 MailContextImpl (org.olat.core.util.mail.MailContextImpl)66 MailTemplate (org.olat.core.util.mail.MailTemplate)46 Test (org.junit.Test)26 File (java.io.File)24 ArrayList (java.util.ArrayList)20 ContactList (org.olat.core.util.mail.ContactList)20 Locale (java.util.Locale)16 Translator (org.olat.core.gui.translator.Translator)16 VelocityContext (org.apache.velocity.VelocityContext)14 DBMailLight (org.olat.core.util.mail.model.DBMailLight)14 Date (java.util.Date)12 MailPackage (org.olat.core.util.mail.MailPackage)12 List (java.util.List)8 User (org.olat.core.id.User)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 Property (org.olat.properties.Property)7