|
|
|
@ -1,5 +1,6 @@
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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)); |
|
|
|
|
var map = new HashMap<>(); |
|
|
|
|
if (ml.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)); |
|
|
|
|
} |
|
|
|
|
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())); |
|
|
|
|
} |
|
|
|
|
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 listDetail(MailingList list, User user) { |
|
|
|
|
if (list == null) return Map.of(ERROR,t("Mailinglist {} unknown",list.email())); |
|
|
|
|
var map = new HashMap<>(); |
|
|
|
|
if (list.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true); |
|
|
|
|
return map; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
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())); |
|
|
|
|
} |
|
|
|
|
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 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 { |
|
|
|
|
MailingList.load(listEmail).test(user); |
|
|
|
|
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); |
|
|
|
|