working on permissions
This commit is contained in:
@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
@@ -92,14 +93,28 @@ public class MailingList {
|
||||
return openLists().stream().filter(ml -> ml.email.equals(list)).count() > 0;
|
||||
}
|
||||
|
||||
public static Set<MailingList> listsOf(User user) {
|
||||
var list = openLists();
|
||||
Set<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOwnedBy(user);
|
||||
if (keys == null || keys.isEmpty()) return list;
|
||||
for (String key : keys) list.add(load(key));
|
||||
public static Set<MailingList> editableBy(User user) {
|
||||
var list = new HashSet<MailingList>();
|
||||
for (String key : ListMember.listsOwnedBy(user)) list.add(load(key));
|
||||
return list;
|
||||
}
|
||||
|
||||
private static MailingList from(ResultSet rs) throws SQLException {
|
||||
String email = rs.getString(EMAIL);
|
||||
var ml = lists.get(email);
|
||||
if (ml == null) lists.put(email,ml = new MailingList(rs.getString(EMAIL),
|
||||
rs.getString(NAME),
|
||||
rs.getString(IMAP_HOST),
|
||||
rs.getInt(IMAP_PORT),
|
||||
rs.getString(IMAP_USER),
|
||||
rs.getString(IMAP_PASS),
|
||||
rs.getString(SMTP_HOST),
|
||||
rs.getInt(SMTP_PORT),
|
||||
rs.getString(SMTP_USER),
|
||||
rs.getString(SMTP_PASS),
|
||||
rs.getInt(STATE)));
|
||||
return ml;
|
||||
}
|
||||
|
||||
public static MailingList load(String listEmail) {
|
||||
var ml = lists.get(listEmail);
|
||||
@@ -108,26 +123,22 @@ public class MailingList {
|
||||
.select(TABLE_NAME)
|
||||
.where(EMAIL,listEmail)
|
||||
.exec();
|
||||
if (rs.next()){
|
||||
ml = new MailingList(rs.getString(EMAIL),
|
||||
rs.getString(NAME),
|
||||
rs.getString(IMAP_HOST),
|
||||
rs.getInt(IMAP_PORT),
|
||||
rs.getString(IMAP_USER),
|
||||
rs.getString(IMAP_PASS),
|
||||
rs.getString(SMTP_HOST),
|
||||
rs.getInt(SMTP_PORT),
|
||||
rs.getString(SMTP_USER),
|
||||
rs.getString(SMTP_PASS),
|
||||
rs.getInt(STATE));
|
||||
lists.put(listEmail,ml);
|
||||
}
|
||||
if (rs.next()) lists.put(listEmail,ml = MailingList.from(rs));
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Failed to load MailingList: ",e);
|
||||
}
|
||||
return ml;
|
||||
}
|
||||
|
||||
public HashMap<String, Object> minimalMap() {
|
||||
var map = new HashMap<String,Object>();
|
||||
String[] parts = email.split("@", 2);
|
||||
map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1]));
|
||||
map.put(NAME, name);
|
||||
map.put(STATE, stateString(state));
|
||||
return map;
|
||||
}
|
||||
|
||||
public String name(){
|
||||
return name;
|
||||
}
|
||||
@@ -151,17 +162,13 @@ public class MailingList {
|
||||
|
||||
|
||||
public Map<String, Object> safeMap() {
|
||||
var map = new HashMap<String,Object>();
|
||||
String[] parts = email.split("@", 2);
|
||||
map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1]));
|
||||
map.put(NAME, name);
|
||||
var map = minimalMap();
|
||||
if (imapHost != null) map.put(IMAP_HOST, imapHost);
|
||||
if (imapPort != 0) map.put(IMAP_PORT, imapPort);
|
||||
if (imapUser != null) map.put(IMAP_USER, imapUser);
|
||||
if (smtp.host() != null) map.put(SMTP_HOST, smtp.host());
|
||||
if (smtp.port() != 0) map.put(SMTP_PORT, smtp.port());
|
||||
if (smtp.username() != null) map.put(SMTP_USER, smtp.username());
|
||||
map.put(STATE, stateString(state));
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -200,6 +207,32 @@ public class MailingList {
|
||||
return String.join(", ", states);
|
||||
}
|
||||
|
||||
public static Set<MailingList> subscribable() {
|
||||
return subscribable(null);
|
||||
}
|
||||
|
||||
public static Set<MailingList> subscribable(User user) {
|
||||
try {
|
||||
if (user == null) return openLists();
|
||||
if (user.is(ADMIN)) {
|
||||
var rs = Database.open().select(TABLE_NAME).exec();
|
||||
var result = new HashSet<MailingList>();
|
||||
while (rs.next()) result.add(MailingList.from(rs));
|
||||
rs.close();
|
||||
return result;
|
||||
}
|
||||
var listEmails = ListMember.listsOwnedBy(user);
|
||||
var rs = Database.open().select(TABLE_NAME).where(EMAIL, listEmails).exec();
|
||||
var result = openLists();
|
||||
while (rs.next()) result.add(MailingList.from(rs));
|
||||
rs.close();
|
||||
return result;
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Failed to read subscribable mailinglists for {}",user,e);
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
|
||||
public void requestSubscription(User user, boolean skipConfirmation) throws SQLException, MessagingException {
|
||||
var state = skipConfirmation ? ListMember.STATE_SUBSCRIBER : ListMember.STATE_AWAITING_CONFIRMATION;
|
||||
var member = ListMember.create(this,user,state);
|
||||
@@ -218,4 +251,6 @@ public class MailingList {
|
||||
var text = t("If you received this mail, the SMTP settings of your mailing list are correct.");
|
||||
smtp.login().send(email(),name(),user.email(),subject,text);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user