Search in sources :

Example 6 with IdentityImpl

use of org.olat.basesecurity.IdentityImpl in project OpenOLAT by OpenOLAT.

the class EdubaseManagerImplTest method getUserIdShouldConcatIssuerAndIdentityName.

@Test
public void getUserIdShouldConcatIssuerAndIdentityName() {
    String userName = "user";
    IdentityImpl identity = new IdentityImpl();
    identity.setName(userName);
    String providerName = "OLAT";
    String issuerIdentifier = "https://issuer.abc";
    when(identityEnvironmentMock.getIdentity()).thenReturn(identity);
    when(identityEnvironmentMock.getAttributes().get(AuthHelper.ATTRIBUTE_AUTHPROVIDER)).thenReturn(providerName);
    when(authenticationProviderMock.getIssuerIdentifier(identityEnvironmentMock)).thenReturn(issuerIdentifier);
    when(loginModulMock.getAuthenticationProvider(providerName)).thenReturn(authenticationProviderMock);
    String userID = sut.getUserId(identityEnvironmentMock);
    String expectedUserId = issuerIdentifier.concat("#").concat(userName);
    assertThat(userID).isEqualTo(expectedUserId);
}
Also used : IdentityImpl(org.olat.basesecurity.IdentityImpl) Test(org.junit.Test)

Example 7 with IdentityImpl

use of org.olat.basesecurity.IdentityImpl in project OpenOLAT by OpenOLAT.

the class MailManagerImpl method saveDBMessage.

protected DBMail saveDBMessage(MailContext context, Identity fromId, String from, Identity toId, String to, Identity cc, List<ContactList> bccLists, String metaId, MailContent content, MailerResult result) {
    try {
        DBMailImpl mail = new DBMailImpl();
        if (result == null) {
            result = new MailerResult();
        }
        boolean makeRealMail = makeRealMail(toId, cc, bccLists);
        Address fromAddress = null;
        List<Address> toAddress = new ArrayList<Address>();
        List<Address> ccAddress = new ArrayList<Address>();
        List<Address> bccAddress = new ArrayList<Address>();
        if (fromId != null) {
            DBMailRecipient fromRecipient = new DBMailRecipient();
            fromRecipient.setRecipient(fromId);
            if (StringHelper.containsNonWhitespace(from)) {
                fromRecipient.setEmailAddress(from);
                fromAddress = createFromAddress(from, result);
            } else {
                fromAddress = createFromAddress(fromId, result);
            }
            fromRecipient.setVisible(Boolean.TRUE);
            fromRecipient.setMarked(Boolean.FALSE);
            fromRecipient.setDeleted(Boolean.FALSE);
            mail.setFrom(fromRecipient);
        } else {
            if (!StringHelper.containsNonWhitespace(from)) {
                from = WebappHelper.getMailConfig("mailFrom");
            }
            DBMailRecipient fromRecipient = new DBMailRecipient();
            fromRecipient.setEmailAddress(from);
            fromRecipient.setVisible(Boolean.TRUE);
            fromRecipient.setMarked(Boolean.FALSE);
            // marked as delted as nobody can read it
            fromRecipient.setDeleted(Boolean.TRUE);
            mail.setFrom(fromRecipient);
            fromAddress = createFromAddress(from, result);
        }
        if (result.getReturnCode() != MailerResult.OK) {
            return null;
        }
        mail.setMetaId(metaId);
        String subject = content.getSubject();
        if (subject != null && subject.length() > 500) {
            log.warn("Cut a too long subkect in name. Size: " + subject.length(), null);
            subject = subject.substring(0, 500);
        }
        mail.setSubject(subject);
        String body = content.getBody();
        if (body != null && body.length() > 16777210) {
            log.warn("Cut a too long body in mail. Size: " + body.length(), null);
            body = body.substring(0, 16000000);
        }
        mail.setBody(body);
        mail.setLastModified(new Date());
        if (context != null) {
            OLATResourceable ores = context.getOLATResourceable();
            if (ores != null) {
                String resName = ores.getResourceableTypeName();
                if (resName != null && resName.length() > 50) {
                    log.warn("Cut a too long resourceable type name in mail context: " + resName, null);
                    resName = resName.substring(0, 49);
                }
                mail.getContext().setResName(ores.getResourceableTypeName());
                mail.getContext().setResId(ores.getResourceableId());
            }
            String resSubPath = context.getResSubPath();
            if (resSubPath != null && resSubPath.length() > 2000) {
                log.warn("Cut a too long resSubPath in mail context: " + resSubPath, null);
                resSubPath = resSubPath.substring(0, 2000);
            }
            mail.getContext().setResSubPath(resSubPath);
            String businessPath = context.getBusinessPath();
            if (businessPath != null && businessPath.length() > 2000) {
                log.warn("Cut a too long resSubPath in mail context: " + businessPath, null);
                businessPath = businessPath.substring(0, 2000);
            }
            mail.getContext().setBusinessPath(businessPath);
        }
        // add to
        DBMailRecipient recipientTo = null;
        if (toId != null) {
            recipientTo = new DBMailRecipient();
            if (toId instanceof IdentityImpl) {
                recipientTo.setRecipient(toId);
            } else {
                to = toId.getUser().getProperty(UserConstants.EMAIL, null);
            }
            if (StringHelper.containsNonWhitespace(to)) {
                recipientTo.setEmailAddress(to);
            }
            recipientTo.setVisible(Boolean.TRUE);
            recipientTo.setDeleted(Boolean.FALSE);
            recipientTo.setMarked(Boolean.FALSE);
            recipientTo.setRead(Boolean.FALSE);
        } else if (StringHelper.containsNonWhitespace(to)) {
            recipientTo = new DBMailRecipient();
            recipientTo.setEmailAddress(to);
            recipientTo.setVisible(Boolean.TRUE);
            recipientTo.setDeleted(Boolean.TRUE);
            recipientTo.setMarked(Boolean.FALSE);
            recipientTo.setRead(Boolean.FALSE);
        }
        if (recipientTo != null) {
            mail.getRecipients().add(recipientTo);
            createAddress(toAddress, recipientTo, true, result, true);
        }
        if (makeRealMail && StringHelper.containsNonWhitespace(to)) {
            createAddress(toAddress, to);
        }
        if (cc != null) {
            DBMailRecipient recipient = new DBMailRecipient();
            if (cc instanceof IdentityImpl) {
                recipient.setRecipient(cc);
            } else {
                recipient.setEmailAddress(cc.getUser().getProperty(UserConstants.EMAIL, null));
            }
            recipient.setVisible(Boolean.TRUE);
            recipient.setDeleted(Boolean.FALSE);
            recipient.setMarked(Boolean.FALSE);
            recipient.setRead(Boolean.FALSE);
            mail.getRecipients().add(recipient);
            createAddress(ccAddress, recipient, false, result, true);
        }
        // add bcc recipients
        appendRecipients(mail, bccLists, toAddress, bccAddress, false, makeRealMail, result);
        dbInstance.getCurrentEntityManager().persist(mail);
        // save attachments
        List<File> attachments = content.getAttachments();
        if (attachments != null && !attachments.isEmpty()) {
            for (File attachment : attachments) {
                FileInputStream in = null;
                try {
                    DBMailAttachment data = new DBMailAttachment();
                    data.setSize(attachment.length());
                    data.setName(attachment.getName());
                    long checksum = FileUtils.checksum(attachment, new Adler32()).getValue();
                    data.setChecksum(new Long(checksum));
                    data.setMimetype(WebappHelper.getMimeType(attachment.getName()));
                    in = new FileInputStream(attachment);
                    String path = saveAttachmentToStorage(data.getName(), data.getMimetype(), checksum, attachment.length(), in);
                    data.setPath(path);
                    data.setMail(mail);
                    dbInstance.getCurrentEntityManager().persist(data);
                } catch (FileNotFoundException e) {
                    log.error("File attachment not found: " + attachment, e);
                } catch (IOException e) {
                    log.error("Error with file attachment: " + attachment, e);
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
        if (makeRealMail) {
            // check that we send an email to someone
            if (!toAddress.isEmpty() || !ccAddress.isEmpty() || !bccAddress.isEmpty()) {
                sendRealMessage(fromAddress, toAddress, ccAddress, bccAddress, subject, body, attachments, result);
                if (result != null && !result.isSuccessful()) {
                    handleErrors(result, fromId, toId, cc, bccLists);
                }
            }
        }
        // update subscription
        for (DBMailRecipient recipient : mail.getRecipients()) {
            if (recipient.getRecipient() != null) {
                subscribe(recipient.getRecipient());
            }
        }
        SubscriptionContext subContext = getSubscriptionContext();
        notificationsManager.markPublisherNews(subContext, null, false);
        return mail;
    } catch (AddressException e) {
        log.error("Cannot send e-mail: ", e);
        result.setReturnCode(MailerResult.RECIPIENT_ADDRESS_ERROR);
        return null;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) MailerResult(org.olat.core.util.mail.MailerResult) OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) DBMailImpl(org.olat.core.util.mail.model.DBMailImpl) IdentityImpl(org.olat.basesecurity.IdentityImpl) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) AddressException(javax.mail.internet.AddressException) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) File(java.io.File)

Example 8 with IdentityImpl

use of org.olat.basesecurity.IdentityImpl in project openolat by klemens.

the class UserManagerImpl method updateUserFromIdentity.

@Override
public boolean updateUserFromIdentity(Identity identity) {
    try {
        String fullName = getUserDisplayName(identity);
        updateUsernameCache(identity.getKey(), identity.getName(), fullName);
    } catch (Exception e) {
        logWarn("Error update usernames cache", e);
    }
    User user = updateUser(identity.getUser());
    ((IdentityImpl) identity).setUser(user);
    return true;
}
Also used : User(org.olat.core.id.User) IdentityImpl(org.olat.basesecurity.IdentityImpl) AssertException(org.olat.core.logging.AssertException)

Example 9 with IdentityImpl

use of org.olat.basesecurity.IdentityImpl in project openolat by klemens.

the class MailManagerImpl method saveDBMessage.

protected DBMail saveDBMessage(MailContext context, Identity fromId, String from, Identity toId, String to, Identity cc, List<ContactList> bccLists, String metaId, MailContent content, MailerResult result) {
    try {
        DBMailImpl mail = new DBMailImpl();
        if (result == null) {
            result = new MailerResult();
        }
        boolean makeRealMail = makeRealMail(toId, cc, bccLists);
        Address fromAddress = null;
        List<Address> toAddress = new ArrayList<Address>();
        List<Address> ccAddress = new ArrayList<Address>();
        List<Address> bccAddress = new ArrayList<Address>();
        if (fromId != null) {
            DBMailRecipient fromRecipient = new DBMailRecipient();
            fromRecipient.setRecipient(fromId);
            if (StringHelper.containsNonWhitespace(from)) {
                fromRecipient.setEmailAddress(from);
                fromAddress = createFromAddress(from, result);
            } else {
                fromAddress = createFromAddress(fromId, result);
            }
            fromRecipient.setVisible(Boolean.TRUE);
            fromRecipient.setMarked(Boolean.FALSE);
            fromRecipient.setDeleted(Boolean.FALSE);
            mail.setFrom(fromRecipient);
        } else {
            if (!StringHelper.containsNonWhitespace(from)) {
                from = WebappHelper.getMailConfig("mailFrom");
            }
            DBMailRecipient fromRecipient = new DBMailRecipient();
            fromRecipient.setEmailAddress(from);
            fromRecipient.setVisible(Boolean.TRUE);
            fromRecipient.setMarked(Boolean.FALSE);
            // marked as delted as nobody can read it
            fromRecipient.setDeleted(Boolean.TRUE);
            mail.setFrom(fromRecipient);
            fromAddress = createFromAddress(from, result);
        }
        if (result.getReturnCode() != MailerResult.OK) {
            return null;
        }
        mail.setMetaId(metaId);
        String subject = content.getSubject();
        if (subject != null && subject.length() > 500) {
            log.warn("Cut a too long subkect in name. Size: " + subject.length(), null);
            subject = subject.substring(0, 500);
        }
        mail.setSubject(subject);
        String body = content.getBody();
        if (body != null && body.length() > 16777210) {
            log.warn("Cut a too long body in mail. Size: " + body.length(), null);
            body = body.substring(0, 16000000);
        }
        mail.setBody(body);
        mail.setLastModified(new Date());
        if (context != null) {
            OLATResourceable ores = context.getOLATResourceable();
            if (ores != null) {
                String resName = ores.getResourceableTypeName();
                if (resName != null && resName.length() > 50) {
                    log.warn("Cut a too long resourceable type name in mail context: " + resName, null);
                    resName = resName.substring(0, 49);
                }
                mail.getContext().setResName(ores.getResourceableTypeName());
                mail.getContext().setResId(ores.getResourceableId());
            }
            String resSubPath = context.getResSubPath();
            if (resSubPath != null && resSubPath.length() > 2000) {
                log.warn("Cut a too long resSubPath in mail context: " + resSubPath, null);
                resSubPath = resSubPath.substring(0, 2000);
            }
            mail.getContext().setResSubPath(resSubPath);
            String businessPath = context.getBusinessPath();
            if (businessPath != null && businessPath.length() > 2000) {
                log.warn("Cut a too long resSubPath in mail context: " + businessPath, null);
                businessPath = businessPath.substring(0, 2000);
            }
            mail.getContext().setBusinessPath(businessPath);
        }
        // add to
        DBMailRecipient recipientTo = null;
        if (toId != null) {
            recipientTo = new DBMailRecipient();
            if (toId instanceof IdentityImpl) {
                recipientTo.setRecipient(toId);
            } else {
                to = toId.getUser().getProperty(UserConstants.EMAIL, null);
            }
            if (StringHelper.containsNonWhitespace(to)) {
                recipientTo.setEmailAddress(to);
            }
            recipientTo.setVisible(Boolean.TRUE);
            recipientTo.setDeleted(Boolean.FALSE);
            recipientTo.setMarked(Boolean.FALSE);
            recipientTo.setRead(Boolean.FALSE);
        } else if (StringHelper.containsNonWhitespace(to)) {
            recipientTo = new DBMailRecipient();
            recipientTo.setEmailAddress(to);
            recipientTo.setVisible(Boolean.TRUE);
            recipientTo.setDeleted(Boolean.TRUE);
            recipientTo.setMarked(Boolean.FALSE);
            recipientTo.setRead(Boolean.FALSE);
        }
        if (recipientTo != null) {
            mail.getRecipients().add(recipientTo);
            createAddress(toAddress, recipientTo, true, result, true);
        }
        if (makeRealMail && StringHelper.containsNonWhitespace(to)) {
            createAddress(toAddress, to);
        }
        if (cc != null) {
            DBMailRecipient recipient = new DBMailRecipient();
            if (cc instanceof IdentityImpl) {
                recipient.setRecipient(cc);
            } else {
                recipient.setEmailAddress(cc.getUser().getProperty(UserConstants.EMAIL, null));
            }
            recipient.setVisible(Boolean.TRUE);
            recipient.setDeleted(Boolean.FALSE);
            recipient.setMarked(Boolean.FALSE);
            recipient.setRead(Boolean.FALSE);
            mail.getRecipients().add(recipient);
            createAddress(ccAddress, recipient, false, result, true);
        }
        // add bcc recipients
        appendRecipients(mail, bccLists, toAddress, bccAddress, false, makeRealMail, result);
        dbInstance.getCurrentEntityManager().persist(mail);
        // save attachments
        List<File> attachments = content.getAttachments();
        if (attachments != null && !attachments.isEmpty()) {
            for (File attachment : attachments) {
                FileInputStream in = null;
                try {
                    DBMailAttachment data = new DBMailAttachment();
                    data.setSize(attachment.length());
                    data.setName(attachment.getName());
                    long checksum = FileUtils.checksum(attachment, new Adler32()).getValue();
                    data.setChecksum(new Long(checksum));
                    data.setMimetype(WebappHelper.getMimeType(attachment.getName()));
                    in = new FileInputStream(attachment);
                    String path = saveAttachmentToStorage(data.getName(), data.getMimetype(), checksum, attachment.length(), in);
                    data.setPath(path);
                    data.setMail(mail);
                    dbInstance.getCurrentEntityManager().persist(data);
                } catch (FileNotFoundException e) {
                    log.error("File attachment not found: " + attachment, e);
                } catch (IOException e) {
                    log.error("Error with file attachment: " + attachment, e);
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
        if (makeRealMail) {
            // check that we send an email to someone
            if (!toAddress.isEmpty() || !ccAddress.isEmpty() || !bccAddress.isEmpty()) {
                sendRealMessage(fromAddress, toAddress, ccAddress, bccAddress, subject, body, attachments, result);
                if (result != null && !result.isSuccessful()) {
                    handleErrors(result, fromId, toId, cc, bccLists);
                }
            }
        }
        // update subscription
        for (DBMailRecipient recipient : mail.getRecipients()) {
            if (recipient.getRecipient() != null) {
                subscribe(recipient.getRecipient());
            }
        }
        SubscriptionContext subContext = getSubscriptionContext();
        notificationsManager.markPublisherNews(subContext, null, false);
        return mail;
    } catch (AddressException e) {
        log.error("Cannot send e-mail: ", e);
        result.setReturnCode(MailerResult.RECIPIENT_ADDRESS_ERROR);
        return null;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) MailerResult(org.olat.core.util.mail.MailerResult) OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) DBMailImpl(org.olat.core.util.mail.model.DBMailImpl) IdentityImpl(org.olat.basesecurity.IdentityImpl) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) AddressException(javax.mail.internet.AddressException) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) File(java.io.File)

Example 10 with IdentityImpl

use of org.olat.basesecurity.IdentityImpl in project openolat by klemens.

the class MailManagerImpl method appendRecipients.

private void appendRecipients(DBMailImpl mail, List<ContactList> ccLists, List<Address> toAddress, List<Address> ccAddress, boolean visible, boolean makeRealMail, MailerResult result) throws AddressException {
    // append cc/bcc recipients
    if (ccLists != null && !ccLists.isEmpty()) {
        for (ContactList contactList : ccLists) {
            if (makeRealMail && StringHelper.containsNonWhitespace(contactList.getName())) {
                Address[] groupAddress = InternetAddress.parse(contactList.getRFC2822Name() + ";");
                if (groupAddress != null && groupAddress.length > 0) {
                    for (Address groupAdd : groupAddress) {
                        toAddress.add(groupAdd);
                    }
                }
            }
            for (String email : contactList.getStringEmails().values()) {
                DBMailRecipient recipient = new DBMailRecipient();
                recipient.setEmailAddress(email);
                recipient.setGroup(contactList.getName());
                recipient.setVisible(visible);
                recipient.setDeleted(Boolean.FALSE);
                recipient.setMarked(Boolean.FALSE);
                recipient.setRead(Boolean.FALSE);
                mail.getRecipients().add(recipient);
                if (makeRealMail) {
                    createAddress(ccAddress, recipient, false, result, false);
                }
            }
            for (Identity identityEmail : contactList.getIdentiEmails().values()) {
                DBMailRecipient recipient = new DBMailRecipient();
                if (identityEmail instanceof IdentityImpl) {
                    recipient.setRecipient(identityEmail);
                } else {
                    recipient.setEmailAddress(identityEmail.getUser().getProperty(UserConstants.EMAIL, null));
                }
                recipient.setGroup(contactList.getName());
                recipient.setVisible(visible);
                recipient.setDeleted(Boolean.FALSE);
                recipient.setMarked(Boolean.FALSE);
                recipient.setRead(Boolean.FALSE);
                mail.getRecipients().add(recipient);
                if (makeRealMail) {
                    createAddress(ccAddress, recipient, false, result, false);
                }
            }
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) IdentityImpl(org.olat.basesecurity.IdentityImpl) ContactList(org.olat.core.util.mail.ContactList) Identity(org.olat.core.id.Identity) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient)

Aggregations

IdentityImpl (org.olat.basesecurity.IdentityImpl)10 Address (javax.mail.Address)4 InternetAddress (javax.mail.internet.InternetAddress)4 DBMailRecipient (org.olat.core.util.mail.model.DBMailRecipient)4 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Adler32 (java.util.zip.Adler32)2 AddressException (javax.mail.internet.AddressException)2 Before (org.junit.Before)2 Test (org.junit.Test)2 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)2 Identity (org.olat.core.id.Identity)2 OLATResourceable (org.olat.core.id.OLATResourceable)2 User (org.olat.core.id.User)2 AssertException (org.olat.core.logging.AssertException)2