implemented hiding/unhiding of mailing lists on the database side
This commit is contained in:
@@ -14,6 +14,8 @@ import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Application {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@ package de.srsoftware.widerhall;
|
||||
|
||||
public class Constants {
|
||||
public static final String ADMIN = "Admin";
|
||||
public static final String BASE = "base";
|
||||
public static final String BASE_URL = "base_url";
|
||||
public static final String DB = "database";
|
||||
public static final String EMAIL = "email";
|
||||
public static final String ERROR = "error";
|
||||
public static final String HOST = "host";
|
||||
@@ -10,6 +12,7 @@ public class Constants {
|
||||
public static final String INBOX = "inbox";
|
||||
public static final String INDEX = "index";
|
||||
public static final String INT = "INT";
|
||||
public static final String LIST = "list";
|
||||
public static final String NAME = "name";
|
||||
public static final String NOTES = "notes";
|
||||
public static final String PASSWORD = "password";
|
||||
@@ -20,8 +23,6 @@ public class Constants {
|
||||
public static final String VARCHAR = "VARCHAR(255)";
|
||||
|
||||
|
||||
public static final String DB = "database";
|
||||
public static final String BASE = "base";
|
||||
public static final String CONFIG = "configuration";
|
||||
public static final String LOCATIONS = "locations";
|
||||
}
|
||||
|
||||
@@ -80,6 +80,20 @@ public class Database {
|
||||
var marks = String.join(", ",arr);
|
||||
sb.append("(").append(marks).append(")");
|
||||
}
|
||||
if (!where.isEmpty()){
|
||||
var clauses = new ArrayList<String>();
|
||||
sb.append(" WHERE ");
|
||||
|
||||
for (var entry : where.entrySet()){
|
||||
var arr = new String[entry.getValue().size()];
|
||||
Arrays.fill(arr,"?");
|
||||
var marks = String.join(", ",arr);
|
||||
clauses.add("("+entry.getKey()+" IN ("+marks+"))");
|
||||
args.addAll(entry.getValue());
|
||||
}
|
||||
sb.append(String.join(" AND ",clauses));
|
||||
|
||||
}
|
||||
var sql = sb.toString();
|
||||
LOG.debug(sql);
|
||||
try {
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ListMember {
|
||||
Database.open().query(sql.toString()).run();
|
||||
}
|
||||
|
||||
public static List<String> listsOf(User user) {
|
||||
public static List<String> listsOwnedBy(User user) {
|
||||
var list = new ArrayList<String>();
|
||||
try {
|
||||
var rs = Database.open()
|
||||
|
||||
@@ -25,10 +25,10 @@ public class MailingList {
|
||||
private final String name;
|
||||
private final String email;
|
||||
public static final String TABLE_NAME = "Lists";
|
||||
private final String imapPass,smtpPass,imapHost,smtpHost,imapUser,smtpUser;
|
||||
private final int imapPort,smtpPort,state;
|
||||
private final String imapPass, smtpPass, imapHost, smtpHost, imapUser, smtpUser;
|
||||
private final int imapPort, smtpPort, state;
|
||||
|
||||
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state){
|
||||
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state) {
|
||||
this.email = email;
|
||||
this.name = name;
|
||||
this.imapHost = imapHost;
|
||||
@@ -43,7 +43,7 @@ public class MailingList {
|
||||
}
|
||||
|
||||
public static MailingList create(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass) throws SQLException {
|
||||
return new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,ENABLED).save();
|
||||
return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, ENABLED).save();
|
||||
}
|
||||
|
||||
public static void createTable() throws SQLException {
|
||||
@@ -65,18 +65,22 @@ public class MailingList {
|
||||
Database.open().query(sql.toString()).run();
|
||||
}
|
||||
|
||||
public static void hide(String listEmail, boolean hide) throws SQLException {
|
||||
// https://stackoverflow.com/questions/16440831/bitwise-xor-in-sqlite-bitwise-not-not-working-as-i-expect
|
||||
String expression = hide ? "state = (~(state & "+PUBLIC+"))&(state|"+PUBLIC+")" : ("state = state | "+PUBLIC);
|
||||
Database.open().query("UPDATE " + TABLE_NAME + " SET "+expression).where(EMAIL, listEmail).run();
|
||||
}
|
||||
|
||||
public static List<MailingList> listsOf(User user) {
|
||||
|
||||
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user);
|
||||
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOwnedBy(user);
|
||||
var list = new ArrayList<MailingList>();
|
||||
if (keys != null && keys.isEmpty()) return list;
|
||||
try {
|
||||
Database.Request q = Database.open().query("SELECT * FROM " + TABLE_NAME);
|
||||
if (keys != null){
|
||||
if (keys.isEmpty()) return list;
|
||||
q.where(EMAIL,keys);
|
||||
}
|
||||
var rs = q.exec();
|
||||
while (rs.next()){
|
||||
Database.Request query = Database.open().query("SELECT * FROM " + TABLE_NAME);
|
||||
if (keys != null) query.where(EMAIL, keys);
|
||||
var rs = query.exec();
|
||||
while (rs.next()) {
|
||||
var email = rs.getString(EMAIL);
|
||||
var name = rs.getString(NAME);
|
||||
var imapHost = rs.getString(IMAP_HOST);
|
||||
@@ -88,46 +92,60 @@ public class MailingList {
|
||||
var smtpUser = rs.getString(SMTP_USER);
|
||||
var smtpPass = rs.getString(SMTP_PASS);
|
||||
var state = rs.getInt(STATE);
|
||||
list.add(new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,state));
|
||||
list.add(new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, state));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Listing mailing lists failed: ",e);
|
||||
LOG.warn("Listing mailing lists failed: ", e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public Map<String,Object> safeMap() {
|
||||
return Map.of(EMAIL,email,NAME,name,
|
||||
IMAP_HOST,imapHost,IMAP_PORT,imapPort,IMAP_USER,imapUser,
|
||||
SMTP_HOST,smtpHost,SMTP_PORT,smtpPort,SMTP_USER,smtpUser,
|
||||
STATE,stateName(state));
|
||||
public static List<MailingList> openLists() {
|
||||
var list = new ArrayList<MailingList>();
|
||||
try {
|
||||
var rs = Database.open().query("SELECT *, (" + STATE + " & " + PUBLIC + ") as test FROM " + TABLE_NAME).where("test", PUBLIC).exec();
|
||||
while (rs.next()) {
|
||||
var email = rs.getString(EMAIL);
|
||||
var name = rs.getString(NAME);
|
||||
var state = rs.getInt(STATE);
|
||||
list.add(new MailingList(email, name, null, 0, null, null, null, 0, null, null, state));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Listing mailing lists failed: ", e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static String stateName(int state) {
|
||||
switch (state){
|
||||
case ENABLED:
|
||||
return "enabled";
|
||||
default:
|
||||
return "disabled";
|
||||
}
|
||||
public Map<String, Object> safeMap() {
|
||||
return Map.of(EMAIL, email, NAME, name,
|
||||
IMAP_HOST, imapHost, IMAP_PORT, imapPort, IMAP_USER, imapUser,
|
||||
SMTP_HOST, smtpHost, SMTP_PORT, smtpPort, SMTP_USER, smtpUser,
|
||||
STATE, stateString(state));
|
||||
}
|
||||
|
||||
private static String stateString(int state) {
|
||||
var states = new ArrayList<String>();
|
||||
states.add((state & ENABLED) == ENABLED ? "enabled" : "disabled");
|
||||
states.add((state & PUBLIC) == PUBLIC ? "public" : "hidden");
|
||||
return String.join(", ", states);
|
||||
}
|
||||
|
||||
private MailingList save() throws SQLException {
|
||||
Database.open().insertInto(TABLE_NAME)
|
||||
.values(Map.ofEntries(
|
||||
Map.entry(EMAIL,email),
|
||||
Map.entry(NAME,name),
|
||||
Map.entry(IMAP_HOST,imapHost),
|
||||
Map.entry(IMAP_PORT,imapPort),
|
||||
Map.entry(IMAP_USER,imapUser),
|
||||
Map.entry(IMAP_PASS,imapPass),
|
||||
Map.entry(SMTP_HOST,smtpHost),
|
||||
Map.entry(SMTP_PORT,smtpPort),
|
||||
Map.entry(SMTP_USER,smtpUser),
|
||||
Map.entry(SMTP_PASS,smtpPass),
|
||||
Map.entry(STATE,state)))
|
||||
Map.entry(EMAIL, email),
|
||||
Map.entry(NAME, name),
|
||||
Map.entry(IMAP_HOST, imapHost),
|
||||
Map.entry(IMAP_PORT, imapPort),
|
||||
Map.entry(IMAP_USER, imapUser),
|
||||
Map.entry(IMAP_PASS, imapPass),
|
||||
Map.entry(SMTP_HOST, smtpHost),
|
||||
Map.entry(SMTP_PORT, smtpPort),
|
||||
Map.entry(SMTP_USER, smtpUser),
|
||||
Map.entry(SMTP_PASS, smtpPass),
|
||||
Map.entry(STATE, state)))
|
||||
.run();
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.widerhall.web;
|
||||
|
||||
import de.srsoftware.widerhall.data.ListMember;
|
||||
import de.srsoftware.widerhall.data.MailingList;
|
||||
import de.srsoftware.widerhall.data.User;
|
||||
import org.json.simple.JSONObject;
|
||||
@@ -11,7 +12,9 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.widerhall.Constants.*;
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
@@ -19,20 +22,29 @@ import static de.srsoftware.widerhall.Util.t;
|
||||
public class Rest extends HttpServlet {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
|
||||
private static final String LIST_LIST = "list/list";
|
||||
private static final String LIST_HIDE = "list/hide";
|
||||
private static final String LIST_SHOW = "list/show";
|
||||
private static final String USER_LIST = "user/list";
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String error = handleGet(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String error = handlePost(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
public String handleGet(HttpServletRequest req, HttpServletResponse resp){
|
||||
Object o = req.getSession().getAttribute(USER);
|
||||
JSONObject json = new JSONObject();
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
if (o instanceof User user){
|
||||
var path = req.getPathInfo();
|
||||
json.put(USER,user.safeMap());
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
switch (path) {
|
||||
case USER_LIST:
|
||||
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(User::safeMap).toList());
|
||||
@@ -44,6 +56,42 @@ public class Rest extends HttpServlet {
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (path) {
|
||||
case LIST_LIST:
|
||||
json.put("lists", MailingList.openLists().stream().map(MailingList::safeMap).toList());
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,"Not logged in!");
|
||||
}
|
||||
}
|
||||
try {
|
||||
resp.setContentType("application/json");
|
||||
resp.getWriter().println(json.toJSONString());
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return t("Failed to handle request: {}",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public String handlePost(HttpServletRequest req, HttpServletResponse resp){
|
||||
Object o = req.getSession().getAttribute(USER);
|
||||
JSONObject json = new JSONObject();
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
if (o instanceof User user){
|
||||
json.put(USER,user.safeMap());
|
||||
switch (path) {
|
||||
case LIST_HIDE:
|
||||
json.putAll(hideList(req,user,true));
|
||||
break;
|
||||
case LIST_SHOW:
|
||||
json.putAll(hideList(req,user,false));
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
json.put(ERROR,"Not logged in!");
|
||||
}
|
||||
@@ -55,4 +103,22 @@ public class Rest extends HttpServlet {
|
||||
return t("Failed to handle request: {}",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> hideList(HttpServletRequest req, User user, boolean hide) {
|
||||
var listEmail = req.getParameter(LIST);
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
|
||||
try {
|
||||
MailingList.hide(listEmail,hide);
|
||||
return Map.of("success",t("Mailing list '{}' was {}!",listEmail,hide ? "hidden" : "made public"));
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to (un)hide mailing list: ",e);
|
||||
return Map.of("error",t("Failed to update list '{}'",listEmail));
|
||||
}
|
||||
|
||||
} else {
|
||||
return Map.of("error",t("You are not allowed to edit '{}'",listEmail));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
16
src/main/resources/logback.xml
Normal file
16
src/main/resources/logback.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<configuration scan="true" scanPeriod="30 seconds" debug="true">
|
||||
<!-- scan="true" enables automatic updates if config file changes, see http://logback.qos.ch/manual/configuration.html -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5}: %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
<logger name="de.srsoftware" level="DEBUG" />
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user