Search in sources :

Example 76 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class EPShareListController method sendInvitation.

/**
 * sends a link to the map to permitted users by email
 *
 * @param ureq
 * @param wrapper
 */
private void sendInvitation(UserRequest ureq, EPSharePolicyWrapper wrapper) {
    EPMapPolicy.Type shareType = wrapper.getType();
    List<Identity> identitiesToMail = new ArrayList<Identity>();
    Invitation invitation = null;
    if (shareType.equals(EPMapPolicy.Type.allusers)) {
        return;
    } else if (shareType.equals(EPMapPolicy.Type.invitation)) {
        invitation = wrapper.getInvitation();
    } else if (shareType.equals(EPMapPolicy.Type.group)) {
        List<BusinessGroup> groups = wrapper.getGroups();
        List<Identity> members = businessGroupService.getMembers(groups, GroupRoles.coach.name(), GroupRoles.participant.name());
        identitiesToMail.addAll(members);
    } else if (shareType.equals(EPMapPolicy.Type.user)) {
        identitiesToMail = wrapper.getIdentities();
    }
    wrapper.setInvitationSend(true);
    ContactList contactList = null;
    if (identitiesToMail.size() == 1) {
        contactList = new ContactList(identitiesToMail.get(0).getUser().getProperty(UserConstants.EMAIL, ureq.getLocale()));
    } else {
        contactList = new ContactList(translate("map.share.invitation.mail.list"));
    }
    contactList.addAllIdentites(identitiesToMail);
    String busLink = "";
    if (invitation != null) {
        contactList.add(invitation.getMail());
        busLink = getInvitationLink(invitation, map);
    } else {
        BusinessControlFactory bCF = BusinessControlFactory.getInstance();
        ContextEntry mapCE = bCF.createContextEntry(map.getOlatResource());
        ArrayList<ContextEntry> cEList = new ArrayList<ContextEntry>();
        cEList.add(mapCE);
        busLink = bCF.getAsURIString(cEList, true);
    }
    boolean success = false;
    try {
        String first = getIdentity().getUser().getProperty(UserConstants.FIRSTNAME, null);
        String last = getIdentity().getUser().getProperty(UserConstants.LASTNAME, null);
        String sender = first + " " + last;
        String[] bodyArgs = new String[] { busLink, sender };
        MailContext context = new MailContextImpl(map.getOlatResource(), null, getWindowControl().getBusinessControl().getAsString());
        MailBundle bundle = new MailBundle();
        bundle.setContext(context);
        bundle.setFrom(WebappHelper.getMailConfig("mailReplyTo"));
        bundle.setContactList(contactList);
        bundle.setContent(translate("map.share.invitation.mail.subject"), translate("map.share.invitation.mail.body", bodyArgs));
        MailerResult result = mailManager.sendMessage(bundle);
        success = result.isSuccessful();
    } catch (Exception e) {
        logError("Error on sending invitation mail to contactlist, invalid address.", e);
    }
    if (success) {
        showInfo("map.share.invitation.mail.success");
    } else {
        showError("map.share.invitation.mail.failure");
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) BusinessGroup(org.olat.group.BusinessGroup) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) EPMapPolicy(org.olat.portfolio.manager.EPMapPolicy) ArrayList(java.util.ArrayList) Invitation(org.olat.basesecurity.Invitation) ContactList(org.olat.core.util.mail.ContactList) ContextEntry(org.olat.core.id.context.ContextEntry) Type(org.olat.portfolio.manager.EPMapPolicy.Type) BusinessControlFactory(org.olat.core.id.context.BusinessControlFactory) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 77 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class RegistrationController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
 */
@Override
public void event(UserRequest ureq, Controller source, Event event) {
    if (source == emailSendForm) {
        if (event == Event.DONE_EVENT) {
            // form
            // validation
            // was ok
            wizInfoController.setCurStep(2);
            // Email requested for tempkey
            // save the fields somewhere
            String email = emailSendForm.getEmailAddress();
            myContent.contextPut("email", email);
            myContent.contextPut("text", translate("step2.reg.text", email));
            // ef.setVisible(false);
            regarea.setVisible(false);
            // get remote address
            String ip = ureq.getHttpReq().getRemoteAddr();
            String serverpath = Settings.getServerContextPathURI();
            String today = DateFormat.getDateInstance(DateFormat.LONG, ureq.getLocale()).format(new Date());
            String[] whereFromAttrs = new String[] { serverpath, today, ip };
            boolean isMailSent = false;
            if (registrationManager.isRegistrationPending(email) || UserManager.getInstance().isEmailAllowed(email)) {
                TemporaryKey tk = null;
                if (userModule.isEmailUnique()) {
                    tk = registrationManager.loadTemporaryKeyByEmail(email);
                }
                if (tk == null)
                    tk = registrationManager.loadOrCreateTemporaryKeyByEmail(email, ip, RegistrationManager.REGISTRATION);
                myContent.contextPut("regKey", tk.getRegistrationKey());
                String link = serverpath + "/dmz/registration/index.html?key=" + tk.getRegistrationKey() + "&language=" + i18nModule.getLocaleKey(ureq.getLocale());
                String[] bodyAttrs = new String[] { // 0
                serverpath, // 1
                tk.getRegistrationKey(), // 2
                i18nModule.getLocaleKey(ureq.getLocale()), // 3
                "<a href=\"" + link + "\">" + link + "</a>" };
                String body = translate("reg.body", bodyAttrs);
                boolean htmlBody = StringHelper.isHtml(body);
                if (!htmlBody) {
                    body += SEPARATOR + translate("reg.wherefrom", whereFromAttrs);
                }
                try {
                    MailBundle bundle = new MailBundle();
                    bundle.setTo(email);
                    bundle.setContent(translate("reg.subject"), body);
                    MailerResult result = mailManager.sendExternMessage(bundle, null, htmlBody);
                    if (result.isSuccessful()) {
                        isMailSent = true;
                    }
                } catch (Exception e) {
                // nothing to do, emailSent flag is false, errors will be reported to user
                }
            } else {
                // if users with this email address exists, they are informed.
                List<Identity> identities = UserManager.getInstance().findIdentitiesByEmail(Collections.singletonList(email));
                for (Identity identity : identities) {
                    String body = translate("login.body", identity.getName()) + SEPARATOR + translate("reg.wherefrom", whereFromAttrs);
                    try {
                        MailBundle bundle = new MailBundle();
                        bundle.setTo(email);
                        bundle.setContent(translate("login.subject"), body);
                        MailerResult result = mailManager.sendExternMessage(bundle, null, true);
                        if (result.isSuccessful()) {
                            isMailSent = true;
                        }
                    } catch (Exception e) {
                    // nothing to do, emailSent flag is false, errors will be reported to user
                    }
                }
            }
            if (isMailSent) {
                showInfo("email.sent");
            } else {
                showError("email.notsent");
            }
        } else if (event == Event.CANCELLED_EVENT) {
            fireEvent(ureq, Event.CANCELLED_EVENT);
        }
    } else if (source == langChooserController) {
        if (event == Event.DONE_EVENT) {
            wizInfoController.setCurStep(2);
            createEmailForm(ureq);
            ureq.getUserSession().removeEntry(LocaleNegotiator.NEGOTIATED_LOCALE);
        } else if (event == Event.CANCELLED_EVENT) {
            ureq.getDispatchResult().setResultingMediaResource(new RedirectMediaResource(Settings.getServerContextPathURI()));
        } else if (event instanceof LanguageChangedEvent) {
            LanguageChangedEvent lcev = (LanguageChangedEvent) event;
            setLocale(lcev.getNewLocale(), true);
            myContent.contextPut("text", translate("select.language.description"));
        }
    } else if (source == registrationForm) {
        // Userdata entered
        if (event == Event.DONE_EVENT) {
            String lang = registrationForm.getLangKey();
            // change language if different then current language
            if (!lang.equals(i18nModule.getLocaleKey(ureq.getLocale()))) {
                Locale loc = i18nManager.getLocaleOrDefault(lang);
                ureq.getUserSession().setLocale(loc);
                getTranslator().setLocale(loc);
            }
            wizInfoController.setCurStep(4);
            myContent.contextPut("pwdhelp", "");
            myContent.contextPut("loginhelp", "");
            myContent.contextPut("text", translate("step4.reg.text"));
            removeAsListenerAndDispose(disclaimerController);
            disclaimerController = new DisclaimerController(ureq, getWindowControl());
            listenTo(disclaimerController);
            regarea.setContent(disclaimerController.getInitialComponent());
        } else if (event == Event.CANCELLED_EVENT) {
            ureq.getDispatchResult().setResultingMediaResource(new RedirectMediaResource(Settings.getServerContextPathURI()));
        }
    } else if (source == disclaimerController) {
        if (event == Event.DONE_EVENT) {
            // finalize the registration by creating the user
            Identity persitedIdentity = createNewUserAfterRegistration();
            // display step5
            displayFinalStep(persitedIdentity);
        } else if (event == Event.CANCELLED_EVENT) {
            ureq.getDispatchResult().setResultingMediaResource(new RedirectMediaResource(Settings.getServerContextPathURI()));
        }
    }
}
Also used : Locale(java.util.Locale) MailerResult(org.olat.core.util.mail.MailerResult) LanguageChangedEvent(org.olat.core.commons.chiefcontrollers.LanguageChangedEvent) Date(java.util.Date) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) MailBundle(org.olat.core.util.mail.MailBundle) Identity(org.olat.core.id.Identity)

Example 78 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

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 79 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class MailManagerTest method testGetEmailByMetaId.

@Test
public void testGetEmailByMetaId() {
    // send a mail
    String metaId = UUID.randomUUID().toString();
    Identity fromId = JunitTestHelper.createAndPersistIdentityAsUser("mail-5-" + UUID.randomUUID().toString());
    Identity toId = JunitTestHelper.createAndPersistIdentityAsUser("mail-6-" + UUID.randomUUID().toString());
    dbInstance.commitAndCloseSession();
    MailBundle bundle = new MailBundle();
    bundle.setFromId(fromId);
    bundle.setToId(toId);
    bundle.setMetaId(metaId);
    bundle.setContent("Hello meta ID", "Meta ID");
    MailerResult result = mailManager.sendMessage(bundle);
    Assert.assertNotNull(result);
    Assert.assertEquals(MailerResult.OK, result.getReturnCode());
    dbInstance.commitAndCloseSession();
    // retrieve the inbox of toId
    List<DBMailLight> mails = mailManager.getEmailsByMetaId(metaId);
    Assert.assertNotNull(mails);
    Assert.assertEquals(1, mails.size());
    DBMailLight mail = mails.get(0);
    Assert.assertNotNull(mail);
    Assert.assertEquals("Hello meta ID", mail.getSubject());
}
Also used : MailerResult(org.olat.core.util.mail.MailerResult) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle) DBMailLight(org.olat.core.util.mail.model.DBMailLight) Test(org.junit.Test)

Example 80 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class MailManagerTest method testDeleteUserData_separatedMail.

@Test
public void testDeleteUserData_separatedMail() {
    // send a mail as separated e-mails to three ids
    String metaId = UUID.randomUUID().toString();
    Identity fromId = JunitTestHelper.createAndPersistIdentityAsUser("mail-7-" + UUID.randomUUID().toString());
    Identity toId_1 = JunitTestHelper.createAndPersistIdentityAsUser("mail-8-" + UUID.randomUUID().toString());
    Identity toId_2 = JunitTestHelper.createAndPersistIdentityAsUser("mail-9-" + UUID.randomUUID().toString());
    Identity toId_3 = JunitTestHelper.createAndPersistIdentityAsUser("mail-10-" + UUID.randomUUID().toString());
    MailBundle bundle_1 = new MailBundle();
    bundle_1.setFromId(fromId);
    bundle_1.setToId(toId_1);
    bundle_1.setMetaId(metaId);
    bundle_1.setContent("Hello ccList", "Content of ccList");
    MailerResult result1 = mailManager.sendMessage(bundle_1);
    Assert.assertNotNull(result1);
    Assert.assertEquals(MailerResult.OK, result1.getReturnCode());
    MailBundle bundle_2 = new MailBundle();
    bundle_2.setFromId(fromId);
    bundle_2.setToId(toId_2);
    bundle_2.setMetaId(metaId);
    bundle_2.setContent("Hello ccList", "Content of ccList");
    MailerResult result2 = mailManager.sendMessage(bundle_2);
    Assert.assertNotNull(result2);
    Assert.assertEquals(MailerResult.OK, result2.getReturnCode());
    MailBundle bundle_3 = new MailBundle();
    bundle_3.setFromId(fromId);
    bundle_3.setToId(toId_3);
    bundle_3.setMetaId(metaId);
    bundle_3.setContent("Hello ccList", "Content of ccList");
    MailerResult result3 = mailManager.sendMessage(bundle_3);
    Assert.assertNotNull(result3);
    Assert.assertEquals(MailerResult.OK, result3.getReturnCode());
    dbInstance.commitAndCloseSession();
    // delete the 4 users datas
    mailBoxExtension.deleteUserData(fromId, "lalala", null);
    mailBoxExtension.deleteUserData(toId_1, "lalala", null);
    mailBoxExtension.deleteUserData(toId_2, "lalala", null);
    mailBoxExtension.deleteUserData(toId_3, "lalala", null);
    dbInstance.commitAndCloseSession();
    // check mail by meta id
    List<DBMailLight> deletedMails = mailManager.getEmailsByMetaId(metaId);
    Assert.assertNotNull(deletedMails);
    Assert.assertTrue(deletedMails.isEmpty());
}
Also used : MailerResult(org.olat.core.util.mail.MailerResult) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle) DBMailLight(org.olat.core.util.mail.model.DBMailLight) Test(org.junit.Test)

Aggregations

MailBundle (org.olat.core.util.mail.MailBundle)126 MailerResult (org.olat.core.util.mail.MailerResult)100 Identity (org.olat.core.id.Identity)72 MailContextImpl (org.olat.core.util.mail.MailContextImpl)72 MailContext (org.olat.core.util.mail.MailContext)70 MailTemplate (org.olat.core.util.mail.MailTemplate)32 Translator (org.olat.core.gui.translator.Translator)28 ContactList (org.olat.core.util.mail.ContactList)28 Locale (java.util.Locale)20 File (java.io.File)18 Date (java.util.Date)16 Test (org.junit.Test)14 MailManager (org.olat.core.util.mail.MailManager)14 DBMailLight (org.olat.core.util.mail.model.DBMailLight)12 ArrayList (java.util.ArrayList)10 Property (org.olat.properties.Property)7 ContextEntry (org.olat.core.id.context.ContextEntry)6 AssertException (org.olat.core.logging.AssertException)6 TemporaryKey (org.olat.registration.TemporaryKey)6 UserManager (org.olat.user.UserManager)6