Browse Source

working on subscription

drop_old_mail
Stephan Richter 3 years ago
parent
commit
1282064565
  1. 1
      src/main/java/de/srsoftware/widerhall/data/ListMember.java
  2. 17
      src/main/java/de/srsoftware/widerhall/data/User.java
  3. 38
      src/main/java/de/srsoftware/widerhall/web/Web.java
  4. 1
      static/templates/index.st
  5. 10
      static/templates/js.st

1
src/main/java/de/srsoftware/widerhall/data/ListMember.java

@ -15,6 +15,7 @@ public class ListMember { @@ -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";

17
src/main/java/de/srsoftware/widerhall/data/User.java

@ -9,6 +9,7 @@ import java.sql.ResultSet; @@ -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 { @@ -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 { @@ -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 { @@ -126,8 +132,13 @@ public class User {
}
private User save() throws SQLException {
var values = new HashMap<String,Object>();
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;
}

38
src/main/java/de/srsoftware/widerhall/web/Web.java

@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse; @@ -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 { @@ -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 { @@ -312,7 +314,6 @@ public class Web extends HttpServlet {
var data = new HashMap<String,String>();
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 { @@ -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;
}
}

1
static/templates/index.st

@ -11,7 +11,6 @@ @@ -11,7 +11,6 @@
«userinfo()»
«messages()»
<h1>Widerhall Index page</h1>
«messages()»
«listlist()»
</body>
</html>

10
static/templates/js.st

@ -43,9 +43,13 @@ function showListAdminList(data){ @@ -43,9 +43,13 @@ function showListAdminList(data){
let list = data.lists[i];
let row = $('<tr/>');
let addr = list.email.prefix+'@'+list.email.domain;
$('<td/>').text(list.name).appendTo(row);
$('<td/>').text(addr).appendTo(row);
let inspect = 'inspect?list='+addr;
let td = $('<td/>');
$('<a/>',{href:inspect}).text(list.name).appendTo(td);
td.appendTo(row);
td = $('<td/>');
$('<a/>',{href:inspect}).text(addr).appendTo(td)
td.appendTo(row);
$('<td/>').text(list.state).appendTo(row);
let select = $('<select/>',{name:addr}).change(function () {

Loading…
Cancel
Save