added tests, preparing submission confirmation
This commit is contained in:
@@ -116,8 +116,8 @@ public class Web extends HttpServlet {
|
||||
}
|
||||
|
||||
try {
|
||||
MailingList.create(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass);
|
||||
ListMember.create(email,user.email(),ListMember.STATE_OWNER);
|
||||
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());
|
||||
@@ -129,13 +129,13 @@ public class Web extends HttpServlet {
|
||||
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
String error = handleGet(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
String error = handlePost(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
@@ -208,7 +208,7 @@ public class Web extends HttpServlet {
|
||||
if (email == null || pass == null) return loadTemplate("login", Map.of("error",t("Missing username or password!")), resp);
|
||||
if (!Util.isEmail(email)) return loadTemplate("login", Map.of("error",t("'{}' is not a valid email address!",email)), resp);
|
||||
try {
|
||||
var user = User.load(email,pass);
|
||||
var user = User.loadUser(email,pass);
|
||||
req.getSession().setAttribute("user",user);
|
||||
// loading user successfull: goto index
|
||||
resp.sendRedirect(String.join("/",WEB_ROOT,"admin"));
|
||||
@@ -244,7 +244,6 @@ public class Web extends HttpServlet {
|
||||
|
||||
private String loadFile(String filename, HttpServletResponse resp) {
|
||||
var path = String.join(File.separator,baseDir,"static",filename);
|
||||
LOG.debug("loading {}",path);
|
||||
var file = new File(path);
|
||||
if (!file.exists()) return t("File {} does not exist!",filename);
|
||||
try {
|
||||
@@ -324,12 +323,15 @@ public class Web extends HttpServlet {
|
||||
var name = req.getParameter(NAME);
|
||||
var email = req.getParameter(EMAIL);
|
||||
var pass = req.getParameter(PASSWORD);
|
||||
var list = req.getParameter(LIST);
|
||||
var listEmail = req.getParameter(LIST);
|
||||
var data = new HashMap<String,Object>();
|
||||
data.put(NAME,name);
|
||||
data.put(EMAIL,email);
|
||||
data.put(LIST,list);
|
||||
if (list == null || list.isBlank()){
|
||||
data.put(LIST,listEmail);
|
||||
|
||||
var list = MailingList.load(listEmail);
|
||||
|
||||
if (list == null){
|
||||
data.put(ERROR,"No list provided by form data!");
|
||||
return loadTemplate(SUBSCRIBE,data,resp);
|
||||
|
||||
@@ -340,13 +342,14 @@ public class Web extends HttpServlet {
|
||||
}
|
||||
if (pass != null && pass.isBlank()) pass = null;
|
||||
|
||||
User user = null;
|
||||
try {
|
||||
data.put(USER,User.create(email,name,pass).safeMap());
|
||||
user = User.create(email,name,pass);
|
||||
} catch (SQLException sqle) {
|
||||
var cause = getCausingException(sqle);
|
||||
int code = cause.getErrorCode();
|
||||
if (code == PRIMARY_KEY_CONSTRAINT) try {// user already exists
|
||||
data.put(USER,User.load(email,pass).safeMap());
|
||||
user = User.loadUser(email,pass);
|
||||
// success → subscribe
|
||||
} catch (InvalidKeyException | SQLException e) {
|
||||
// invalid credentials
|
||||
@@ -354,9 +357,11 @@ public class Web extends HttpServlet {
|
||||
return loadTemplate(SUBSCRIBE,data,resp);
|
||||
}
|
||||
}
|
||||
data.put(USER,user.safeMap());
|
||||
|
||||
try {
|
||||
ListMember.create(list,email,ListMember.STATE_SUBSCRIBER);
|
||||
data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",email,list));
|
||||
ListMember.create(list,user,ListMember.STATE_SUBSCRIBER);
|
||||
data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",user.email(),list.email()));
|
||||
return loadTemplate(INDEX,data,resp);
|
||||
} catch (SQLException sqle) {
|
||||
LOG.debug("List subscription failed: ",sqle);
|
||||
@@ -376,11 +381,14 @@ public class Web extends HttpServlet {
|
||||
var user = getSessionUser(req);
|
||||
var email = req.getParameter(EMAIL);
|
||||
var pass = req.getParameter(PASSWORD);
|
||||
var list = req.getParameter(LIST);
|
||||
var listEmail = req.getParameter(LIST);
|
||||
data.put(EMAIL,email);
|
||||
data.put(LIST,list);
|
||||
data.put(LIST,listEmail);
|
||||
|
||||
var list = MailingList.load(listEmail);
|
||||
|
||||
if (user != null) data.put(USER,user.safeMap());
|
||||
if (list == null || list.isBlank()){
|
||||
if (list == null){
|
||||
data.put(ERROR,"No list provided by form data!");
|
||||
return loadTemplate(UNSUBSCRIBE,data,resp);
|
||||
|
||||
@@ -393,7 +401,7 @@ public class Web extends HttpServlet {
|
||||
if (pass != null && pass.isBlank()) pass = null;
|
||||
|
||||
try {
|
||||
user = User.load(email,pass);
|
||||
user = User.loadUser(email,pass);
|
||||
req.getSession().setAttribute(USER,user);
|
||||
data.put(USER,user.safeMap());
|
||||
} catch (InvalidKeyException | SQLException e) {
|
||||
@@ -404,10 +412,10 @@ public class Web extends HttpServlet {
|
||||
// if we get here, we should have a valid user
|
||||
try {
|
||||
ListMember.unsubscribe(list,user);
|
||||
data.put(NOTES,t("Sucessfully un-subscribed from '{}'.",list));
|
||||
data.put(NOTES,t("Sucessfully un-subscribed from '{}'.",list.email()));
|
||||
return loadTemplate(INDEX,data,resp);
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Problem during unscubsription of {} from {}:",user.email(),list,e);
|
||||
LOG.warn("Problem during unscubsription of {} from {}:",user.email(),list.email(),e);
|
||||
data.put(ERROR,"Failed to unsubscribe!");
|
||||
return loadTemplate(UNSUBSCRIBE,data,resp);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user