Use more specific query to get logged in players without email
- Reduces the amount of data returned from the DB and the work required to build objects
This commit is contained in:
+4
-13
@@ -2,7 +2,6 @@ package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
@@ -65,16 +64,13 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
|
||||
* @param sender the sender initiating the password change
|
||||
*/
|
||||
private void changePassword(String nameLowercase, String password, CommandSender sender) {
|
||||
PlayerAuth auth = getAuth(nameLowercase);
|
||||
if (auth == null) {
|
||||
if (!isNameRegistered(nameLowercase)) {
|
||||
commonService.send(sender, MessageKey.UNKNOWN_USER);
|
||||
return;
|
||||
}
|
||||
|
||||
HashedPassword hashedPassword = passwordSecurity.computeHash(password, nameLowercase);
|
||||
auth.setPassword(hashedPassword);
|
||||
|
||||
if (dataSource.updatePassword(auth)) {
|
||||
if (dataSource.updatePassword(nameLowercase, hashedPassword)) {
|
||||
commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
|
||||
ConsoleLogger.info(sender.getName() + " changed password of " + nameLowercase);
|
||||
} else {
|
||||
@@ -82,12 +78,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerAuth getAuth(String nameLowercase) {
|
||||
if (playerCache.isAuthenticated(nameLowercase)) {
|
||||
return playerCache.getAuth(nameLowercase);
|
||||
} else if (dataSource.isAuthAvailable(nameLowercase)) {
|
||||
return dataSource.getAuth(nameLowercase);
|
||||
}
|
||||
return null;
|
||||
private boolean isNameRegistered(String nameLowercase) {
|
||||
return playerCache.isAuthenticated(nameLowercase) || dataSource.isAuthAvailable(nameLowercase);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,6 @@ class DataStatistics implements DebugSection {
|
||||
|
||||
private void outputDatabaseStats(CommandSender sender) {
|
||||
sender.sendMessage("Total players in DB: " + dataSource.getAccountsRegistered());
|
||||
sender.sendMessage("Total marked as logged in in DB: " + dataSource.getLoggedPlayers().size());
|
||||
if (dataSource instanceof CacheDataSource) {
|
||||
CacheDataSource cacheDataSource = (CacheDataSource) this.dataSource;
|
||||
sender.sendMessage("Cached PlayerAuth objects: " + cacheDataSource.getCachedAuths().size());
|
||||
|
||||
@@ -11,14 +11,15 @@ import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CacheDataSource implements DataSource {
|
||||
|
||||
@@ -240,7 +241,10 @@ public class CacheDataSource implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getLoggedPlayers() {
|
||||
return new ArrayList<>(playerCache.getCache().values());
|
||||
public List<String> getLoggedPlayersWithEmptyMail() {
|
||||
return playerCache.getCache().values().stream()
|
||||
.filter(auth -> Utils.isEmailEmpty(auth.getEmail()))
|
||||
.map(PlayerAuth::getRealName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,11 +167,11 @@ public interface DataSource extends Reloadable {
|
||||
void purgeLogged();
|
||||
|
||||
/**
|
||||
* Return all players which are logged in.
|
||||
* Return all players which are logged in and whose email is not set.
|
||||
*
|
||||
* @return All logged in players
|
||||
* @return logged in players with no email
|
||||
*/
|
||||
List<PlayerAuth> getLoggedPlayers();
|
||||
List<String> getLoggedPlayersWithEmptyMail();
|
||||
|
||||
/**
|
||||
* Return the number of registered accounts.
|
||||
|
||||
@@ -423,7 +423,7 @@ public class FlatFile implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getLoggedPlayers() {
|
||||
public List<String> getLoggedPlayersWithEmptyMail() {
|
||||
throw new UnsupportedOperationException("Flat file no longer supported");
|
||||
}
|
||||
|
||||
|
||||
@@ -927,33 +927,20 @@ public class MySQL implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getLoggedPlayers() {
|
||||
List<PlayerAuth> auths = new ArrayList<>();
|
||||
String sql = "SELECT * FROM " + tableName + " WHERE " + col.IS_LOGGED + "=1;";
|
||||
public List<String> getLoggedPlayersWithEmptyMail() {
|
||||
List<String> players = new ArrayList<>();
|
||||
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
|
||||
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
PlayerAuth pAuth = buildAuthFromResultSet(rs);
|
||||
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
|
||||
try (PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;")) {
|
||||
int id = rs.getInt(col.ID);
|
||||
pst.setInt(1, id);
|
||||
ResultSet rs2 = pst.executeQuery();
|
||||
if (rs2.next()) {
|
||||
Blob blob = rs2.getBlob("data");
|
||||
byte[] bytes = blob.getBytes(1, (int) blob.length());
|
||||
pAuth.setPassword(new HashedPassword(XfBCrypt.getHashFromBlob(bytes)));
|
||||
}
|
||||
rs2.close();
|
||||
}
|
||||
}
|
||||
auths.add(pAuth);
|
||||
players.add(rs.getString(1));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return auths;
|
||||
return players;
|
||||
}
|
||||
|
||||
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
||||
|
||||
@@ -591,18 +591,18 @@ public class SQLite implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getLoggedPlayers() {
|
||||
List<PlayerAuth> auths = new ArrayList<>();
|
||||
String sql = "SELECT * FROM " + tableName + " WHERE " + col.IS_LOGGED + "=1;";
|
||||
try (PreparedStatement pst = con.prepareStatement(sql); ResultSet rs = pst.executeQuery()) {
|
||||
public List<String> getLoggedPlayersWithEmptyMail() {
|
||||
List<String> players = new ArrayList<>();
|
||||
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
|
||||
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
|
||||
try (Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
PlayerAuth auth = buildAuthFromResultSet(rs);
|
||||
auths.add(auth);
|
||||
players.add(rs.getString(1));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return auths;
|
||||
return players;
|
||||
}
|
||||
|
||||
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
||||
|
||||
@@ -3,7 +3,6 @@ package fr.xephi.authme.initialization;
|
||||
import ch.jalu.injector.exceptions.InjectorReflectionException;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.message.Messages;
|
||||
@@ -18,7 +17,6 @@ import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bstats.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -110,13 +108,10 @@ public class OnStartupTasks {
|
||||
bukkitService.runTaskTimerAsynchronously(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (PlayerAuth auth : dataSource.getLoggedPlayers()) {
|
||||
String email = auth.getEmail();
|
||||
if (Utils.isEmailEmpty(email)) {
|
||||
Player player = bukkitService.getPlayerExact(auth.getRealName());
|
||||
if (player != null) {
|
||||
messages.send(player, MessageKey.ADD_EMAIL_MESSAGE);
|
||||
}
|
||||
for (String playerWithoutMail : dataSource.getLoggedPlayersWithEmptyMail()) {
|
||||
Player player = bukkitService.getPlayerExact(playerWithoutMail);
|
||||
if (player != null) {
|
||||
messages.send(player, MessageKey.ADD_EMAIL_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user