first use of e-mail sending

This commit is contained in:
Axel Uhl committed 2014-10-17 15:45:44 +02:00
1 parent 10eac4ec6b
commit 64dfd37932
5 files changed
+36 -16

No files matched your search

@@ -34,6 +34,7 @@ import com.sap.sse.security.Social;
import com.sap.sse.security.shared.Account;
import com.sap.sse.security.shared.Account.AccountType;
import com.sap.sse.security.shared.DefaultRoles;
import com.sap.sse.security.shared.MailException;
import com.sap.sse.security.shared.SocialUserAccount;
import com.sap.sse.security.shared.User;
import com.sap.sse.security.shared.UserManagementException;
@@ -139,15 +140,16 @@ public class UserManagementServiceImpl extends RemoteServiceServlet implements U
@Override
public void updateSimpleUserPassword(String username, String oldPassword, String newPassword) throws UserManagementException {
public void updateSimpleUserPassword(String username, String oldPassword, String newPassword) throws UserManagementException, MailException {
final Subject subject = SecurityUtils.getSubject();
if (subject.hasRole(DefaultRoles.ADMIN.getRolename()) || username.equals(SessionUtils.loadUsername())) {
getSecurityService().updateSimpleUserPassword(username, oldPassword, newPassword);
getSecurityService().sendMail(username, "Password Changed", "Somebody changed your password. If that wasn't you, I'd be worried...");
} else {
throw new UserManagementException(UserManagementException.INVALID_CREDENTIALS);
}
}
@Override
public void updateSimpleUserEmail(String username, String newEmail) throws UserManagementException {
final Subject subject = SecurityUtils.getSubject();
@@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.Map;
import com.google.gwt.user.client.rpc.RemoteService;
import com.sap.sse.security.shared.MailException;
import com.sap.sse.security.shared.UserManagementException;
import com.sap.sse.security.ui.oauth.client.CredentialDTO;
import com.sap.sse.security.ui.oauth.shared.OAuthException;
@@ -19,7 +20,7 @@ public interface UserManagementService extends RemoteService {
UserDTO createSimpleUser(String name, String email, String password) throws UserManagementException;
void updateSimpleUserPassword(String name, String oldPassword, String newPassword) throws UserManagementException;
void updateSimpleUserPassword(String name, String oldPassword, String newPassword) throws UserManagementException, MailException;
void updateSimpleUserEmail(String username, String newEmail) throws UserManagementException;
@@ -4,13 +4,12 @@ import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletContext;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
import com.sap.sse.security.shared.MailException;
import com.sap.sse.security.shared.SocialUserAccount;
import com.sap.sse.security.shared.User;
import com.sap.sse.security.shared.UserManagementException;
@@ -66,6 +65,6 @@ public interface SecurityService {
CacheManager getCacheManager();
void sendMail(String username, String subject, String body) throws AddressException, MessagingException;
void sendMail(String username, String subject, String body) throws MailException;
}
@@ -15,7 +15,6 @@ import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.Filter;
@@ -63,6 +62,7 @@ import org.scribe.oauth.OAuthService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sap.sse.security.shared.Account.AccountType;
import com.sap.sse.security.shared.DefaultRoles;
import com.sap.sse.security.shared.MailException;
import com.sap.sse.security.shared.SocialUserAccount;
import com.sap.sse.security.shared.User;
import com.sap.sse.security.shared.UserManagementException;
@@ -134,21 +134,26 @@ public class SecurityServiceImpl extends RemoteServiceServlet implements Securit
}
@Override
public void sendMail(String username, String subject, String body) throws AddressException, MessagingException {
public void sendMail(String username, String subject, String body) throws MailException {
final User user = getUserByName(username);
if (user != null) {
final String toAddress = user.getEmail();
if (toAddress != null) {
Session session = Session.getInstance(this.mailProperties, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("root@sapsailing.com"));
msg.setSubject(subject);
msg.setContent(body, "text/plain");
msg.addRecipient(RecipientType.TO, new InternetAddress(toAddress.trim()));
Transport ts = session.getTransport();
ts.connect();
ts.sendMessage(msg, msg.getRecipients(RecipientType.TO));
ts.close();
try {
msg.setFrom(new InternetAddress(mailProperties.getProperty("mail.from", "root@sapsailing.com")));
msg.setSubject(subject);
msg.setContent(body, "text/plain");
msg.addRecipient(RecipientType.TO, new InternetAddress(toAddress.trim()));
Transport ts = session.getTransport();
ts.connect();
ts.sendMessage(msg, msg.getRecipients(RecipientType.TO));
ts.close();
} catch (MessagingException e) {
logger.log(Level.SEVERE, "Error trying to send mail to user "+username+" with e-mail address "+toAddress, e);
throw new MailException(e.getMessage());
}
}
}
}
@@ -0,0 +1,13 @@
package com.sap.sse.security.shared;
import java.io.Serializable;
public class MailException extends Exception implements Serializable {
private static final long serialVersionUID = 1L;
public MailException() {}
public MailException(String message) {
super(message);
}
}