|
|
|
@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
|
|
|
|
|
package de.srsoftware.widerhall.web; |
|
|
|
|
|
|
|
|
|
import de.srsoftware.widerhall.Configuration; |
|
|
|
|
import de.srsoftware.widerhall.Constants; |
|
|
|
|
import de.srsoftware.widerhall.Util; |
|
|
|
|
import de.srsoftware.widerhall.data.ListMember; |
|
|
|
|
import de.srsoftware.widerhall.data.MailingList; |
|
|
|
@ -30,6 +31,7 @@ public class Web extends HttpServlet {
@@ -30,6 +31,7 @@ public class Web extends HttpServlet {
|
|
|
|
|
private static final String ADD_LIST = "add_list"; |
|
|
|
|
private static final String CONFIRM = "confirm"; |
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Web.class); |
|
|
|
|
private static final String ADMIN = "admin"; |
|
|
|
|
private static final String LOGIN = "login"; |
|
|
|
|
private static final String LOGOUT = "logout"; |
|
|
|
|
private static final String REGISTER = "register"; |
|
|
|
@ -58,80 +60,84 @@ public class Web extends HttpServlet {
@@ -58,80 +60,84 @@ public class Web extends HttpServlet {
|
|
|
|
|
private String addList(HttpServletRequest req, HttpServletResponse resp) { |
|
|
|
|
|
|
|
|
|
var o = req.getSession().getAttribute(USER); |
|
|
|
|
if (o instanceof User user){ |
|
|
|
|
var data = new HashMap<String,Object>(); |
|
|
|
|
data.put(USER,user); |
|
|
|
|
|
|
|
|
|
var name = req.getParameter(NAME); |
|
|
|
|
data.put(NAME,name); |
|
|
|
|
|
|
|
|
|
var email = req.getParameter(EMAIL); |
|
|
|
|
data.put(EMAIL,email); |
|
|
|
|
|
|
|
|
|
var imapHost = req.getParameter(IMAP_HOST); |
|
|
|
|
data.put(IMAP_HOST,imapHost); |
|
|
|
|
var imapUser = req.getParameter(IMAP_USER); |
|
|
|
|
data.put(IMAP_USER,imapUser); |
|
|
|
|
var imapPass = req.getParameter(IMAP_PASS); |
|
|
|
|
var smtpHost = req.getParameter(SMTP_HOST); |
|
|
|
|
data.put(SMTP_HOST,smtpHost); |
|
|
|
|
var smtpUser = req.getParameter(SMTP_USER); |
|
|
|
|
data.put(SMTP_USER,smtpUser); |
|
|
|
|
var smtpPass = req.getParameter(SMTP_PASS); |
|
|
|
|
|
|
|
|
|
Integer imapPort = 993; |
|
|
|
|
data.put(IMAP_PORT,imapPort); |
|
|
|
|
|
|
|
|
|
Integer smtpPort = 465; |
|
|
|
|
data.put(SMTP_PORT,smtpPort); |
|
|
|
|
|
|
|
|
|
if (name == null || name.isBlank() || email == null || email.isBlank()) { |
|
|
|
|
data.put(ERROR,"List name and address are required!"); |
|
|
|
|
return loadTemplate(ADD_LIST,data,resp); |
|
|
|
|
} |
|
|
|
|
if (!(o instanceof User user)) { |
|
|
|
|
return redirectTo(LOGIN,resp); |
|
|
|
|
} |
|
|
|
|
var data = new HashMap<String, Object>(); |
|
|
|
|
data.put(USER, user); |
|
|
|
|
|
|
|
|
|
if (!Util.isEmail(email)){ |
|
|
|
|
data.put(ERROR,t("List email ({}) is not a valid email address!",email)); |
|
|
|
|
return loadTemplate(ADD_LIST,data,resp); |
|
|
|
|
} |
|
|
|
|
if (!user.hashPermission(User.PERMISSION_CREATE_LISTS)){ |
|
|
|
|
data.put(ERROR,t("You are not allowed to create new mailing lists!")); |
|
|
|
|
return loadTemplate(ADMIN,data,resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) { |
|
|
|
|
data.put(ERROR,"IMAP credentials are required!"); |
|
|
|
|
return loadTemplate(ADD_LIST,data,resp); |
|
|
|
|
} |
|
|
|
|
var name = req.getParameter(NAME); |
|
|
|
|
data.put(NAME, name); |
|
|
|
|
|
|
|
|
|
var email = req.getParameter(EMAIL); |
|
|
|
|
data.put(EMAIL, email); |
|
|
|
|
|
|
|
|
|
var imapHost = req.getParameter(IMAP_HOST); |
|
|
|
|
data.put(IMAP_HOST, imapHost); |
|
|
|
|
var imapUser = req.getParameter(IMAP_USER); |
|
|
|
|
data.put(IMAP_USER, imapUser); |
|
|
|
|
var imapPass = req.getParameter(IMAP_PASS); |
|
|
|
|
var smtpHost = req.getParameter(SMTP_HOST); |
|
|
|
|
data.put(SMTP_HOST, smtpHost); |
|
|
|
|
var smtpUser = req.getParameter(SMTP_USER); |
|
|
|
|
data.put(SMTP_USER, smtpUser); |
|
|
|
|
var smtpPass = req.getParameter(SMTP_PASS); |
|
|
|
|
|
|
|
|
|
Integer imapPort = 993; |
|
|
|
|
data.put(IMAP_PORT, imapPort); |
|
|
|
|
|
|
|
|
|
Integer smtpPort = 465; |
|
|
|
|
data.put(SMTP_PORT, smtpPort); |
|
|
|
|
|
|
|
|
|
if (name == null || name.isBlank() || email == null || email.isBlank()) { |
|
|
|
|
data.put(ERROR, "List name and address are required!"); |
|
|
|
|
return loadTemplate(ADD_LIST, data, resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
imapPort = Integer.parseInt(req.getParameter(IMAP_PORT)); |
|
|
|
|
data.put(IMAP_PORT,imapPort); |
|
|
|
|
} catch (NumberFormatException nfe){ |
|
|
|
|
data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(IMAP_PORT))); |
|
|
|
|
return loadTemplate(ADD_LIST,data,resp); |
|
|
|
|
} |
|
|
|
|
if (!Util.isEmail(email)) { |
|
|
|
|
data.put(ERROR, t("List email ({}) is not a valid email address!", email)); |
|
|
|
|
return loadTemplate(ADD_LIST, data, resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (smtpHost == null || smtpHost.isBlank() || smtpUser == null || smtpUser.isBlank() || smtpPass == null || smtpPass.isBlank()) { |
|
|
|
|
data.put(ERROR,"SMTP credentials are required!"); |
|
|
|
|
return loadTemplate(ADD_LIST,data,resp); |
|
|
|
|
} |
|
|
|
|
if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) { |
|
|
|
|
data.put(ERROR, "IMAP credentials are required!"); |
|
|
|
|
return loadTemplate(ADD_LIST, data, resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
smtpPort = Integer.parseInt(req.getParameter(SMTP_PORT)); |
|
|
|
|
data.put(SMTP_PORT,smtpPort); |
|
|
|
|
} catch (NumberFormatException nfe){ |
|
|
|
|
data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(SMTP_PORT))); |
|
|
|
|
return loadTemplate(ADD_LIST,data,resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
var list = MailingList.create(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass); |
|
|
|
|
ListMember.create(list,user,ListMember.STATE_OWNER); |
|
|
|
|
return redirectTo(INDEX,resp); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
return t("Failed to create list '{}': {}",name,e.getMessage()); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
imapPort = Integer.parseInt(req.getParameter(IMAP_PORT)); |
|
|
|
|
data.put(IMAP_PORT, imapPort); |
|
|
|
|
} catch (NumberFormatException nfe) { |
|
|
|
|
data.put(ERROR, t("'{}' is not a proper port number!", req.getParameter(IMAP_PORT))); |
|
|
|
|
return loadTemplate(ADD_LIST, data, resp); |
|
|
|
|
} |
|
|
|
|
return redirectTo(LOGIN,resp); |
|
|
|
|
|
|
|
|
|
if (smtpHost == null || smtpHost.isBlank() || smtpUser == null || smtpUser.isBlank() || smtpPass == null || smtpPass.isBlank()) { |
|
|
|
|
data.put(ERROR, "SMTP credentials are required!"); |
|
|
|
|
return loadTemplate(ADD_LIST, data, resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
smtpPort = Integer.parseInt(req.getParameter(SMTP_PORT)); |
|
|
|
|
data.put(SMTP_PORT, smtpPort); |
|
|
|
|
} catch (NumberFormatException nfe) { |
|
|
|
|
data.put(ERROR, t("'{}' is not a proper port number!", req.getParameter(SMTP_PORT))); |
|
|
|
|
return loadTemplate(ADD_LIST, data, resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
var list = MailingList.create(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass); |
|
|
|
|
ListMember.create(list, user, ListMember.STATE_OWNER); |
|
|
|
|
return redirectTo(INDEX, resp); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
return t("Failed to create list '{}': {}", name, e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String confirm(HttpServletRequest req, HttpServletResponse resp) { |
|
|
|
@ -181,8 +187,9 @@ public class Web extends HttpServlet {
@@ -181,8 +187,9 @@ public class Web extends HttpServlet {
|
|
|
|
|
var path = req.getPathInfo(); |
|
|
|
|
path = (path == null || path.equals("/")) ? INDEX : path.substring(1); |
|
|
|
|
String notes = null; |
|
|
|
|
var list = req.getParameter(LIST); |
|
|
|
|
if (list != null && !list.isBlank()) data.put(LIST,list); |
|
|
|
|
var listEmail = req.getParameter(LIST); |
|
|
|
|
var list = MailingList.load(listEmail); |
|
|
|
|
if (list != null) data.put(LIST,list.minimalMap()); |
|
|
|
|
switch (path){ |
|
|
|
|
case CONFIRM: |
|
|
|
|
return confirm(req,resp); |
|
|
|
@ -195,12 +202,11 @@ public class Web extends HttpServlet {
@@ -195,12 +202,11 @@ public class Web extends HttpServlet {
|
|
|
|
|
case UNSUBSCRIBE: |
|
|
|
|
return loadTemplate(path,data,resp); |
|
|
|
|
case SUBSCRIBE: |
|
|
|
|
// TODO check permission
|
|
|
|
|
if (MailingList.isOpen(list)) { |
|
|
|
|
data.put(LIST, list); |
|
|
|
|
if (list.isOpenFor(user)) { |
|
|
|
|
data.put(LIST,listEmail); |
|
|
|
|
return loadTemplate(path, data, resp); |
|
|
|
|
} |
|
|
|
|
return t("You are not allowed to subscribe to '{}'!",list); |
|
|
|
|
return t("You are not allowed to subscribe to '{}'!",list.email()); |
|
|
|
|
case "js": |
|
|
|
|
resp.setContentType("text/javascript"); |
|
|
|
|
return loadTemplate(path,data,resp); |
|
|
|
@ -326,12 +332,9 @@ public class Web extends HttpServlet {
@@ -326,12 +332,9 @@ public class Web extends HttpServlet {
|
|
|
|
|
if (!pass.equals(pass_repeat)) return loadTemplate(REGISTER,Map.of(ERROR,"Passwords do not match!",NAME,name,EMAIL,email),resp); |
|
|
|
|
if (Util.simplePassword(pass)) return loadTemplate(REGISTER,Map.of(ERROR,"Password to short or to simple!",NAME,name,EMAIL,email),resp); |
|
|
|
|
|
|
|
|
|
var firstUser = false; |
|
|
|
|
try { |
|
|
|
|
if (User.noUsers()) { // we are registering the first user, which is forced to be „Admin“
|
|
|
|
|
name = ADMIN; |
|
|
|
|
} else { |
|
|
|
|
if (ADMIN.equals(name)) return loadTemplate(REGISTER,Map.of(ERROR,t("Name must not be „{}“",ADMIN),NAME,name,EMAIL,email),resp); |
|
|
|
|
} |
|
|
|
|
firstUser = User.noUsers(); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
return t("Failed to access user database: {}",e.getMessage()); |
|
|
|
|
} |
|
|
|
@ -339,9 +342,11 @@ public class Web extends HttpServlet {
@@ -339,9 +342,11 @@ public class Web extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
var user = User.create(email, name, pass); |
|
|
|
|
if (firstUser) user.addPermission(User.PERMISSION_ADMIN|User.PERMISSION_CREATE_LISTS); |
|
|
|
|
req.getSession().setAttribute("user",user); |
|
|
|
|
return redirectTo(INDEX,resp); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.warn("Failed to create new user:",e); |
|
|
|
|
return t("Failed to create new user: {}",e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -388,6 +393,11 @@ public class Web extends HttpServlet {
@@ -388,6 +393,11 @@ public class Web extends HttpServlet {
|
|
|
|
|
} |
|
|
|
|
data.put(USER,user.safeMap()); |
|
|
|
|
|
|
|
|
|
if (!list.isOpenFor(user)){ |
|
|
|
|
data.put(ERROR,t("You are not allowed to join {}!",list.email())); |
|
|
|
|
return loadTemplate(SUBSCRIBE,data,resp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
list.requestSubscription(user,skipConfirmation); |
|
|
|
|
data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",user.email(),list.email())); |
|
|
|
@ -454,6 +464,4 @@ public class Web extends HttpServlet {
@@ -454,6 +464,4 @@ public class Web extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|