working on subscription
This commit is contained in:
@@ -15,6 +15,7 @@ public class ListMember {
|
|||||||
private static final Logger LOG = LoggerFactory.getLogger(ListMember.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ListMember.class);
|
||||||
public static final String TABLE_NAME = "ListMembers";
|
public static final String TABLE_NAME = "ListMembers";
|
||||||
public static final int STATE_OWNER = 1;
|
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 LIST_EMAIL = "list_email";
|
||||||
private static final String USER_EMAIL = "user_email";
|
private static final String USER_EMAIL = "user_email";
|
||||||
private static final String STATE = "state";
|
private static final String STATE = "state";
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -28,8 +29,12 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static User create(String email, String name, String password) throws SQLException {
|
public static User create(String email, String name, String password) throws SQLException {
|
||||||
var salt = Util.sha256(email + name + LocalDate.now());
|
String salt = null;
|
||||||
var hashedPass = Util.sha256(password+salt);
|
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();
|
return new User(email,name,salt,hashedPass).save();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +107,7 @@ public class User {
|
|||||||
|
|
||||||
|
|
||||||
private boolean matching(String password) {
|
private boolean matching(String password) {
|
||||||
|
if (hashedPass == null && password == null) return true;
|
||||||
return hashedPass.equals(Util.sha256(password+salt));
|
return hashedPass.equals(Util.sha256(password+salt));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,8 +132,13 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private User save() throws SQLException {
|
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)
|
Database.open().insertInto(TABLE_NAME)
|
||||||
.values(Map.of(EMAIL,email,NAME,name,SALT,salt,HASHED_PASS,hashedPass))
|
.values(values)
|
||||||
.run();
|
.run();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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_PORT = "smtp_port";
|
||||||
private static final String SMTP_USER = "smtp_user";
|
private static final String SMTP_USER = "smtp_user";
|
||||||
private static final String SMTP_PASS = "smtp_pass";
|
private static final String SMTP_PASS = "smtp_pass";
|
||||||
|
private static final int PRIMARY_KEY_CONSTRAINT = 19;
|
||||||
private final String baseDir;
|
private final String baseDir;
|
||||||
private STGroup templates;
|
private STGroup templates;
|
||||||
private static final String WEB_ROOT = "/web";
|
private static final String WEB_ROOT = "/web";
|
||||||
@@ -312,7 +314,6 @@ public class Web extends HttpServlet {
|
|||||||
var data = new HashMap<String,String>();
|
var data = new HashMap<String,String>();
|
||||||
data.put(NAME,name);
|
data.put(NAME,name);
|
||||||
data.put(EMAIL,email);
|
data.put(EMAIL,email);
|
||||||
data.put(PASSWORD,pass);
|
|
||||||
data.put(LIST,list);
|
data.put(LIST,list);
|
||||||
if (list == null || list.isBlank()){
|
if (list == null || list.isBlank()){
|
||||||
data.put(ERROR,"No list provided by form data!");
|
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!");
|
data.put(ERROR,"Name and email are required fields for list subscription!");
|
||||||
return loadTemplate(SUBSCRIBE,data,resp);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
«userinfo()»
|
«userinfo()»
|
||||||
«messages()»
|
«messages()»
|
||||||
<h1>Widerhall Index page</h1>
|
<h1>Widerhall Index page</h1>
|
||||||
«messages()»
|
|
||||||
«listlist()»
|
«listlist()»
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -43,9 +43,13 @@ function showListAdminList(data){
|
|||||||
let list = data.lists[i];
|
let list = data.lists[i];
|
||||||
let row = $('<tr/>');
|
let row = $('<tr/>');
|
||||||
let addr = list.email.prefix+'@'+list.email.domain;
|
let addr = list.email.prefix+'@'+list.email.domain;
|
||||||
|
let inspect = 'inspect?list='+addr;
|
||||||
$('<td/>').text(list.name).appendTo(row);
|
let td = $('<td/>');
|
||||||
$('<td/>').text(addr).appendTo(row);
|
$('<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);
|
$('<td/>').text(list.state).appendTo(row);
|
||||||
|
|
||||||
let select = $('<select/>',{name:addr}).change(function () {
|
let select = $('<select/>',{name:addr}).change(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user