#792 Include registration date into purging logic
- Take MAX(reg_date, login_date) as timestamp to compare against - Remove the second "all" parameter to include entries with 0 registration date -> we expect registration date to always be set to the current date, so the parameter becomes obsolete
This commit is contained in:
@@ -320,7 +320,6 @@ public class CommandInitializer {
|
||||
.description("Purge old data")
|
||||
.detailedDescription("Purge old AuthMeReloaded data longer than the specified number of days ago.")
|
||||
.withArgument("days", "Number of days", false)
|
||||
.withArgument("all", "Add 'all' at the end to also purge players with lastlogin = 0", true)
|
||||
.permission(AdminPermission.PURGE)
|
||||
.executableCommand(PurgeCommand.class)
|
||||
.register();
|
||||
|
||||
@@ -41,24 +41,12 @@ public class PurgeCommand implements ExecutableCommand {
|
||||
return;
|
||||
}
|
||||
|
||||
// If second param is available, check that it is equal to "all"
|
||||
boolean includeLastLoginZeroEntries = false;
|
||||
if (arguments.size() >= 2) {
|
||||
if ("all".equals(arguments.get(1))) {
|
||||
includeLastLoginZeroEntries = true;
|
||||
} else {
|
||||
sender.sendMessage("Purge process aborted; use '/authme purge " + days + " all' "
|
||||
+ "to include users with lastlogin = 0");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a calender instance to determine the date
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.add(Calendar.DATE, -days);
|
||||
long until = calendar.getTimeInMillis();
|
||||
|
||||
// Run the purge
|
||||
purgeService.runPurge(sender, until, includeLastLoginZeroEntries);
|
||||
purgeService.runPurge(sender, until);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,8 +137,8 @@ public class CacheDataSource implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
|
||||
return source.getRecordsToPurge(until, includeEntriesWithLastLoginZero);
|
||||
public Set<String> getRecordsToPurge(long until) {
|
||||
return source.getRecordsToPurge(until);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -74,11 +74,9 @@ public interface DataSource extends Reloadable {
|
||||
* Get all records in the database whose last login was before the given time.
|
||||
*
|
||||
* @param until The minimum last login
|
||||
* @param includeEntriesWithLastLoginZero Whether entries with lastlogin = 0 should be included or not,
|
||||
* see <a href="https://github.com/AuthMe/AuthMeReloaded/issues/886">issue #886</a>
|
||||
* @return The account names selected to purge
|
||||
*/
|
||||
Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero);
|
||||
Set<String> getRecordsToPurge(long until);
|
||||
|
||||
/**
|
||||
* Purge the given players from the database.
|
||||
|
||||
@@ -189,7 +189,7 @@ public class FlatFile implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
|
||||
public Set<String> getRecordsToPurge(long until) {
|
||||
throw new UnsupportedOperationException("Flat file no longer supported");
|
||||
}
|
||||
|
||||
|
||||
@@ -407,12 +407,12 @@ public class MySQL implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
|
||||
public Set<String> getRecordsToPurge(long until) {
|
||||
Set<String> list = new HashSet<>();
|
||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
|
||||
if (!includeEntriesWithLastLoginZero) {
|
||||
select += " AND " + col.LAST_LOGIN + " <> 0";
|
||||
}
|
||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE GREATEST("
|
||||
+ " COALESCE(" + col.LAST_LOGIN + ", 0),"
|
||||
+ " COALESCE(" + col.REGISTRATION_DATE + ", 0)"
|
||||
+ ") < ?;";
|
||||
try (Connection con = getConnection();
|
||||
PreparedStatement selectPst = con.prepareStatement(select)) {
|
||||
selectPst.setLong(1, until);
|
||||
|
||||
@@ -308,12 +308,12 @@ public class SQLite implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
|
||||
public Set<String> getRecordsToPurge(long until) {
|
||||
Set<String> list = new HashSet<>();
|
||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
|
||||
if (!includeEntriesWithLastLoginZero) {
|
||||
select += " AND " + col.LAST_LOGIN + " <> 0";
|
||||
}
|
||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE MAX("
|
||||
+ " COALESCE(" + col.LAST_LOGIN + ", 0),"
|
||||
+ " COALESCE(" + col.REGISTRATION_DATE + ", 0)"
|
||||
+ ") < ?;";
|
||||
try (PreparedStatement selectPst = con.prepareStatement(select)) {
|
||||
selectPst.setLong(1, until);
|
||||
try (ResultSet rs = selectPst.executeQuery()) {
|
||||
|
||||
@@ -76,7 +76,7 @@ public class RakamakConverter implements Converter {
|
||||
for (Entry<String, HashedPassword> m : playerPassword.entrySet()) {
|
||||
String playerName = m.getKey();
|
||||
HashedPassword psw = playerPassword.get(playerName);
|
||||
String ip = useIp ? playerIp.get(playerName) : "127.0.0.1";
|
||||
String ip = playerIp.get(playerName);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(playerName)
|
||||
.realName(playerName)
|
||||
@@ -86,7 +86,7 @@ public class RakamakConverter implements Converter {
|
||||
database.saveAuth(auth);
|
||||
database.updateSession(auth);
|
||||
}
|
||||
Utils.logAndSendMessage(sender, "Rakamak database has been imported correctly");
|
||||
Utils.logAndSendMessage(sender, "Rakamak database has been imported successfully");
|
||||
} catch (IOException ex) {
|
||||
ConsoleLogger.logException("Can't open the rakamak database file! Does it exist?", ex);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class PurgeService {
|
||||
calendar.add(Calendar.DATE, -daysBeforePurge);
|
||||
long until = calendar.getTimeInMillis();
|
||||
|
||||
runPurge(null, until, false);
|
||||
runPurge(null, until);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,11 +69,10 @@ public class PurgeService {
|
||||
*
|
||||
* @param sender Sender running the command
|
||||
* @param until The last login threshold in milliseconds
|
||||
* @param includeEntriesWithLastLoginZero True to also purge players with lastlogin = 0, false otherwise
|
||||
*/
|
||||
public void runPurge(CommandSender sender, long until, boolean includeEntriesWithLastLoginZero) {
|
||||
public void runPurge(CommandSender sender, long until) {
|
||||
//todo: note this should may run async because it may executes a SQL-Query
|
||||
Set<String> toPurge = dataSource.getRecordsToPurge(until, includeEntriesWithLastLoginZero);
|
||||
Set<String> toPurge = dataSource.getRecordsToPurge(until);
|
||||
if (Utils.isCollectionEmpty(toPurge)) {
|
||||
logAndSendMessage(sender, "No players to purge");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user