refactoring code
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.widerhall.web;
|
||||
|
||||
import de.srsoftware.widerhall.Util;
|
||||
import de.srsoftware.widerhall.data.ListMember;
|
||||
import de.srsoftware.widerhall.data.MailingList;
|
||||
import de.srsoftware.widerhall.data.User;
|
||||
@@ -85,12 +86,25 @@ public class Rest extends HttpServlet {
|
||||
return Map.of(SUCCESS,"Updated user permissions");
|
||||
}
|
||||
|
||||
private Map enableList(MailingList list, User user, boolean enable) {
|
||||
if (list == null) return Map.of(ERROR,"no list email provided!");
|
||||
if (!list.mayBeAlteredBy(user)) Map.of(ERROR,t("You are not allowed to edit '{}'",list.email()));
|
||||
try {
|
||||
list.enable(enable);
|
||||
return Map.of(SUCCESS,t("Mailing list '{}' was {}!",list.email(),enable ? "enabled" : "disabled"));
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to enable/disable mailing list: ",e);
|
||||
return Map.of(ERROR,t("Failed to update list '{}'",list.email()));
|
||||
}
|
||||
}
|
||||
|
||||
public String handleGet(HttpServletRequest req, HttpServletResponse resp){
|
||||
Object o = req.getSession().getAttribute(USER);
|
||||
var user = Util.getUser(req);
|
||||
var path = Util.getPath(req);
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
if (o instanceof User user){
|
||||
|
||||
if (user != null){
|
||||
json.put(USER,user.safeMap());
|
||||
switch (path) {
|
||||
case USER_LIST:
|
||||
@@ -130,38 +144,38 @@ public class Rest extends HttpServlet {
|
||||
}
|
||||
|
||||
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){
|
||||
var user = Util.getUser(req);
|
||||
var path = Util.getPath(req);
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
if (user != null){
|
||||
json.put(USER,user.safeMap());
|
||||
|
||||
var listEmail = req.getParameter(LIST);
|
||||
var list = Util.getMailingList(req);
|
||||
var userEmail = req.getParameter(EMAIL);
|
||||
var permissions = req.getParameter(PERMISSIONS);
|
||||
switch (path) {
|
||||
case LIST_DETAIL:
|
||||
json.putAll(listDetail(listEmail,user));
|
||||
json.putAll(listDetail(list,user));
|
||||
break;
|
||||
case LIST_DISABLE:
|
||||
json.putAll(enableList(listEmail,user,false));
|
||||
json.putAll(enableList(list,user,false));
|
||||
break;
|
||||
case LIST_ENABLE:
|
||||
json.putAll(enableList(listEmail,user,true));
|
||||
json.putAll(enableList(list,user,true));
|
||||
break;
|
||||
case LIST_HIDE:
|
||||
json.putAll(hideList(listEmail,user,true));
|
||||
json.putAll(hideList(list,user,true));
|
||||
break;
|
||||
case LIST_MEMBERS:
|
||||
json.putAll(listMembers(listEmail,user));
|
||||
json.putAll(listMembers(list,user));
|
||||
break;
|
||||
case LIST_SHOW:
|
||||
json.putAll(hideList(listEmail,user,false));
|
||||
json.putAll(hideList(list,user,false));
|
||||
break;
|
||||
case LIST_TEST:
|
||||
json.putAll(testList(listEmail,user));
|
||||
json.putAll(testList(list,user));
|
||||
break;
|
||||
case USER_ADD_PERMISSION:
|
||||
if (user.hashPermission(User.PERMISSION_ADMIN)){
|
||||
@@ -189,69 +203,45 @@ public class Rest extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
private Map listDetail(String listEmail, User user) {
|
||||
var ml = MailingList.load(listEmail);
|
||||
if (ml == null) return Map.of(ERROR,t("Mailinglist {} unknown",listEmail));
|
||||
private Map<String, String> hideList(MailingList list, User user, boolean hide) {
|
||||
if (list == null) return Map.of(ERROR,"no list email provided!");
|
||||
if (!list.mayBeAlteredBy(user)) Map.of(ERROR,t("You are not allowed to edit '{}'",list.email()));
|
||||
try {
|
||||
list.hide(hide);
|
||||
return Map.of(SUCCESS,t("Mailing list '{}' was {}!",list.email(),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 '{}'",list.email()));
|
||||
}
|
||||
}
|
||||
|
||||
private Map listDetail(MailingList list, User user) {
|
||||
if (list == null) return Map.of(ERROR,t("Mailinglist {} unknown",list.email()));
|
||||
var map = new HashMap<>();
|
||||
if (ml.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true);
|
||||
if (list.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true);
|
||||
return map;
|
||||
}
|
||||
|
||||
private Map<String, Object> listMembers(String listEmail, User user) {
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)) {
|
||||
try {
|
||||
var members = ListMember.of(listEmail)
|
||||
.entrySet()
|
||||
.stream()
|
||||
.map(entry -> Map.of(
|
||||
EMAIL,entry.getKey().email(),
|
||||
NAME,entry.getKey().name(),
|
||||
STATE,ListMember.stateText(entry.getValue())
|
||||
))
|
||||
.toList();
|
||||
return Map.of(MEMBERS,members);
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to load member list: ",e);
|
||||
return Map.of("error",t("Failed to load member list '{}'",listEmail));
|
||||
}
|
||||
}
|
||||
return Map.of("error",t("You are not allowed to list members '{}'",listEmail));
|
||||
}
|
||||
|
||||
private Map enableList(String listEmail, User user, boolean enable) {
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
|
||||
try {
|
||||
MailingList.load(listEmail).enable(enable);
|
||||
return Map.of(SUCCESS,t("Mailing list '{}' was {}!",listEmail,enable ? "enabled" : "disabled"));
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to enable/disable mailing list: ",e);
|
||||
return Map.of(ERROR,t("Failed to update list '{}'",listEmail));
|
||||
}
|
||||
}
|
||||
return Map.of(ERROR,t("You are not allowed to edit '{}'",listEmail));
|
||||
}
|
||||
|
||||
private Map<String, String> hideList(String listEmail, User user, boolean hide) {
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
|
||||
try {
|
||||
MailingList.load(listEmail).hide(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));
|
||||
}
|
||||
|
||||
}
|
||||
return Map.of(ERROR,t("You are not allowed to edit '{}'",listEmail));
|
||||
}
|
||||
|
||||
private Map testList(String listEmail, User user) {
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
private Map<String, Object> listMembers(MailingList list, User user) {
|
||||
if (list == null) return Map.of(ERROR,"no list email provided!");
|
||||
if (!list.membersMayBeListedBy(user)) Map.of(ERROR,t("You are not allowed to list members of '{}'",list.email()));
|
||||
try {
|
||||
MailingList.load(listEmail).test(user);
|
||||
var members = list.members()
|
||||
.stream()
|
||||
.map(ListMember::safeMap)
|
||||
.toList();
|
||||
return Map.of(MEMBERS,members);
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to load member list: ",e);
|
||||
return Map.of("error",t("Failed to load member list '{}'",list.email()));
|
||||
}
|
||||
}
|
||||
|
||||
private Map testList(MailingList list, User user) {
|
||||
if (list == null) return Map.of(ERROR,"no list email provided!");
|
||||
if (!list.mayBeTestedBy(user)) Map.of(ERROR,t("You are not allowed to test '{}'",list.email()));
|
||||
try {
|
||||
list.test(user);
|
||||
return Map.of(SUCCESS,t("Sent test email to {}",user.email()));
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Failed to send test email",e);
|
||||
|
||||
@@ -25,6 +25,7 @@ public class Web extends TemplateServlet {
|
||||
private static final String CONFIRM = "confirm";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Web.class);
|
||||
private static final String ADMIN = "admin";
|
||||
private static final String INSPECT = "inspect";
|
||||
private static final String LOGIN = "login";
|
||||
private static final String LOGOUT = "logout";
|
||||
private static final String REGISTER = "register";
|
||||
@@ -245,11 +246,15 @@ public class Web extends TemplateServlet {
|
||||
}
|
||||
|
||||
private String handlePost(HttpServletRequest req, HttpServletResponse resp) {
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
final var user = Util.getUser(req);
|
||||
final var path = Util.getPath(req);
|
||||
final var list = Util.getMailingList(req);
|
||||
|
||||
switch (path){
|
||||
case ADD_LIST:
|
||||
return addList(req,resp);
|
||||
case INSPECT:
|
||||
return inspect(req,resp);
|
||||
case LOGIN:
|
||||
return handleLogin(req,resp);
|
||||
case REGISTER:
|
||||
@@ -263,8 +268,21 @@ public class Web extends TemplateServlet {
|
||||
return t("No handler for path {}!",path);
|
||||
}
|
||||
|
||||
private String inspect(HttpServletRequest req, HttpServletResponse resp) {
|
||||
var o = req.getSession().getAttribute(USER);
|
||||
if (!(o instanceof User user)) {
|
||||
return redirectTo(LOGIN,resp);
|
||||
}
|
||||
var post = req.getParameterMap();
|
||||
var listEmail = req.getParameter(LIST);
|
||||
var list = MailingList.load(listEmail);
|
||||
if (list == null) return t("{} is does not denote a valid list",listEmail);
|
||||
if (!list.mayBeAlteredBy(user)) {
|
||||
|
||||
|
||||
}
|
||||
LOG.debug("POST: {}",post);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private String redirectTo(String page, HttpServletResponse resp) {
|
||||
|
||||
Reference in New Issue
Block a user