working on permissions

This commit is contained in:
2022-04-18 16:07:59 +02:00
parent a326004e82
commit 2b9a185bfc
12 changed files with 274 additions and 162 deletions

View File

@@ -13,6 +13,7 @@ import java.util.*;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
import static de.srsoftware.widerhall.data.User.PERMISSION_ADMIN;
public class MailingList {
private static final Logger LOG = LoggerFactory.getLogger(MailingList.class);
@@ -31,7 +32,8 @@ public class MailingList {
private final String email;
public static final String TABLE_NAME = "Lists";
private final String imapPass, imapHost, imapUser;
private final int imapPort, state;
private final int imapPort;
private int state;
private final SmtpClient smtp;
private static final HashMap<String,MailingList> lists = new HashMap<>();
@@ -75,22 +77,26 @@ public class MailingList {
}
public static void enable(String listEmail, boolean enable) throws SQLException {
Database.open()
.update(TABLE_NAME)
.set(STATE,enable ? STATE+" | "+ STATE_ENABLED : Database.xor(STATE,STATE_ENABLED))
.where(EMAIL, listEmail).run();
public void enable(boolean enable) throws SQLException {
state = enable ? state | STATE_ENABLED : state ^ (state & STATE_ENABLED);
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).run();
}
public static void hide(String listEmail, boolean hide) throws SQLException {
Database.open()
.update(TABLE_NAME)
.set(STATE,hide ? STATE+" | "+ STATE_PUBLIC : Database.xor(STATE,STATE_PUBLIC))
.where(EMAIL, listEmail).run();
public void hide(boolean hide) throws SQLException {
state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC;
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).run();
}
public static boolean isOpen(String list) {
return openLists().stream().filter(ml -> ml.email.equals(list)).count() > 0;
public boolean isOpenFor(User user) {
if ((state & STATE_PUBLIC) > 0) return true;
if (user == null) return false;
try {
var member = ListMember.load(this,user);
return member.hasState(ListMember.STATE_OWNER|ListMember.STATE_SUBSCRIBER);
} catch (SQLException e) {
LOG.warn("Was not able to load ListMember: ",e);
return false;
}
}
public static Set<MailingList> editableBy(User user) {
@@ -117,6 +123,7 @@ public class MailingList {
}
public static MailingList load(String listEmail) {
if (listEmail == null) return null;
var ml = lists.get(listEmail);
if (ml == null) try {
var rs = Database.open()
@@ -214,7 +221,7 @@ public class MailingList {
public static Set<MailingList> subscribable(User user) {
try {
if (user == null) return openLists();
if (user.is(ADMIN)) {
if (user.hashPermission(PERMISSION_ADMIN)) {
var rs = Database.open().select(TABLE_NAME).exec();
var result = new HashSet<MailingList>();
while (rs.next()) result.add(MailingList.from(rs));