From 1282064565204b5adcf8ccd0c1831d0aad159228 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 16 Apr 2022 18:09:13 +0200 Subject: [PATCH] working on subscription --- .../srsoftware/widerhall/data/ListMember.java | 1 + .../de/srsoftware/widerhall/data/User.java | 17 +++++++-- .../java/de/srsoftware/widerhall/web/Web.java | 38 ++++++++++++++++++- static/templates/index.st | 1 - static/templates/js.st | 10 +++-- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/srsoftware/widerhall/data/ListMember.java b/src/main/java/de/srsoftware/widerhall/data/ListMember.java index 0b225e1..74ed7e8 100644 --- a/src/main/java/de/srsoftware/widerhall/data/ListMember.java +++ b/src/main/java/de/srsoftware/widerhall/data/ListMember.java @@ -15,6 +15,7 @@ public class ListMember { private static final Logger LOG = LoggerFactory.getLogger(ListMember.class); public static final String TABLE_NAME = "ListMembers"; public static final int STATE_OWNER = 1; + public static final int STATE_SUBSCRIBER = 2; private static final String LIST_EMAIL = "list_email"; private static final String USER_EMAIL = "user_email"; private static final String STATE = "state"; diff --git a/src/main/java/de/srsoftware/widerhall/data/User.java b/src/main/java/de/srsoftware/widerhall/data/User.java index 6f02199..620a40b 100644 --- a/src/main/java/de/srsoftware/widerhall/data/User.java +++ b/src/main/java/de/srsoftware/widerhall/data/User.java @@ -9,6 +9,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDate; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,8 +29,12 @@ public class User { } public static User create(String email, String name, String password) throws SQLException { - var salt = Util.sha256(email + name + LocalDate.now()); - var hashedPass = Util.sha256(password+salt); + String salt = null; + String hashedPass = null; + if (password != null) { + salt = Util.sha256(email + name + LocalDate.now()); + hashedPass = Util.sha256(password + salt); + } return new User(email,name,salt,hashedPass).save(); } @@ -102,6 +107,7 @@ public class User { private boolean matching(String password) { + if (hashedPass == null && password == null) return true; return hashedPass.equals(Util.sha256(password+salt)); } @@ -126,8 +132,13 @@ public class User { } private User save() throws SQLException { + var values = new HashMap(); + values.put(EMAIL,email); + values.put(NAME,name); + if (salt != null) values.put(SALT,salt); + if (hashedPass != null) values.put(HASHED_PASS,hashedPass); Database.open().insertInto(TABLE_NAME) - .values(Map.of(EMAIL,email,NAME,name,SALT,salt,HASHED_PASS,hashedPass)) + .values(values) .run(); return this; } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 421e0bf..928d689 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.security.InvalidKeyException; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; @@ -40,6 +41,7 @@ public class Web extends HttpServlet { private static final String SMTP_PORT = "smtp_port"; private static final String SMTP_USER = "smtp_user"; private static final String SMTP_PASS = "smtp_pass"; + private static final int PRIMARY_KEY_CONSTRAINT = 19; private final String baseDir; private STGroup templates; private static final String WEB_ROOT = "/web"; @@ -312,7 +314,6 @@ public class Web extends HttpServlet { var data = new HashMap(); data.put(NAME,name); data.put(EMAIL,email); - data.put(PASSWORD,pass); data.put(LIST,list); if (list == null || list.isBlank()){ data.put(ERROR,"No list provided by form data!"); @@ -323,6 +324,39 @@ public class Web extends HttpServlet { data.put(ERROR,"Name and email are required fields for list subscription!"); return loadTemplate(SUBSCRIBE,data,resp); } - return "not implemented"; + if (pass != null && pass.isBlank()) pass = null; + + try { + 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 + User.load(email,pass); + // success → subscribe + } catch (InvalidKeyException | SQLException e) { + // invalid credentials + data.put(ERROR,t("'{}' already in database, but with different password!",email)); + return loadTemplate(SUBSCRIBE,data,resp); + } + } + try { + ListMember.create(list,email,ListMember.STATE_SUBSCRIBER); + data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",email,list)); + return loadTemplate(INDEX,data,resp); + } catch (SQLException e) { + LOG.debug("List subscription failed: ",e); + data.put(ERROR,t("Subscription failed: {}",e.getMessage())); + return loadTemplate(SUBSCRIBE,data,resp); + } + } + + private SQLException getCausingException(SQLException sqle) { + Throwable cause = sqle.getCause(); + while (cause instanceof SQLException){ + sqle = (SQLException) cause; + cause = sqle.getCause(); + } + return sqle; } } diff --git a/static/templates/index.st b/static/templates/index.st index c88b4b3..e989c69 100644 --- a/static/templates/index.st +++ b/static/templates/index.st @@ -11,7 +11,6 @@ «userinfo()» «messages()»

Widerhall Index page

- «messages()» «listlist()» \ No newline at end of file diff --git a/static/templates/js.st b/static/templates/js.st index 6d11308..87ef385 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -43,9 +43,13 @@ function showListAdminList(data){ let list = data.lists[i]; let row = $(''); let addr = list.email.prefix+'@'+list.email.domain; - - $('').text(list.name).appendTo(row); - $('').text(addr).appendTo(row); + let inspect = 'inspect?list='+addr; + let td = $(''); + $('',{href:inspect}).text(list.name).appendTo(td); + td.appendTo(row); + td = $(''); + $('',{href:inspect}).text(addr).appendTo(td) + td.appendTo(row); $('').text(list.state).appendTo(row); let select = $('