diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/ActiveCookieStore.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/ActiveCookieStore.java index f65398641..781c5c42a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/ActiveCookieStore.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/ActiveCookieStore.java @@ -128,12 +128,17 @@ public class ActiveCookieStore implements SubSystem { Optional foundUser = checkCookie(cookie); if (foundUser.isPresent()) { USERS_BY_COOKIE.remove(cookie); - deleteCookie(foundUser.get().getUsername()); + deleteCookieByUser(foundUser.get().getUsername()); + deleteCookie(cookie); } } - private void deleteCookie(String username) { - dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookie(username)); + private void deleteCookie(String cookie) { + dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookie(cookie)); + } + + private void deleteCookieByUser(String username) { + dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookieByUser(username)); } public void removeAll() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/CookieTable.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/CookieTable.java index 589a24973..102f57595 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/CookieTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/CookieTable.java @@ -20,6 +20,7 @@ import com.djrapitops.plan.storage.database.DBType; import com.djrapitops.plan.storage.database.sql.building.CreateTableBuilder; import com.djrapitops.plan.storage.database.sql.building.Sql; +import static com.djrapitops.plan.storage.database.sql.building.Sql.DELETE_FROM; import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE; /** @@ -40,13 +41,16 @@ public class CookieTable { COOKIE + ',' + EXPIRES + ") VALUES (?,?,?)"; - public static final String DELETE_BY_USER_STATEMENT = "DELETE FROM " + TABLE_NAME + + public static final String DELETE_BY_COOKIE_STATEMENT = DELETE_FROM + TABLE_NAME + + WHERE + COOKIE + "=?"; + + public static final String DELETE_BY_USER_STATEMENT = DELETE_FROM + TABLE_NAME + WHERE + WEB_USERNAME + "=?"; - public static final String DELETE_OLDER_STATEMENT = "DELETE FROM " + TABLE_NAME + + public static final String DELETE_OLDER_STATEMENT = DELETE_FROM + TABLE_NAME + WHERE + EXPIRES + "<=?"; - public static final String DELETE_ALL_STATEMENT = "DELETE FROM " + TABLE_NAME; + public static final String DELETE_ALL_STATEMENT = DELETE_FROM + TABLE_NAME; private CookieTable() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/CookieChangeTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/CookieChangeTransaction.java index f3416ec45..db8cd309c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/CookieChangeTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/CookieChangeTransaction.java @@ -39,23 +39,41 @@ public class CookieChangeTransaction extends Transaction { return new CookieChangeTransaction(username, cookie, expires); } - public static CookieChangeTransaction removeCookie(String username) { + public static CookieChangeTransaction removeCookieByUser(String username) { return new CookieChangeTransaction(username, null, null); } + public static CookieChangeTransaction removeCookie(String cookie) { + return new CookieChangeTransaction(null, cookie, null); + } + public static CookieChangeTransaction removeAll() { return new CookieChangeTransaction(null, null, null); } @Override protected void performOperations() { - if (username == null) { + if (username == null && cookie == null) { execute(new ExecStatement(CookieTable.DELETE_ALL_STATEMENT) { @Override public void prepare(PreparedStatement statement) { // No parameters } }); + } else if (username == null) { + execute(new ExecStatement(CookieTable.DELETE_BY_COOKIE_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, cookie); + } + }); + // Perform cleanup at the same time + execute(new ExecStatement(CookieTable.DELETE_OLDER_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setLong(1, System.currentTimeMillis()); + } + }); } else if (cookie == null) { execute(new ExecStatement(CookieTable.DELETE_BY_USER_STATEMENT) { @Override