From cc29d8b628e7b33e0d7bb286fbaf72774a3cd35a Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sat, 20 Feb 2016 06:47:59 +0700 Subject: [PATCH] Fix duplicate entry error, #528 --- .../authme/datasource/CacheDataSource.java | 9 +++++++++ .../fr/xephi/authme/datasource/DataSource.java | 18 +++++++++++++++++- .../fr/xephi/authme/datasource/FlatFile.java | 5 +++++ .../java/fr/xephi/authme/datasource/MySQL.java | 15 +++++++++++++++ .../fr/xephi/authme/datasource/SQLite.java | 14 ++++++++++++++ .../process/login/AsynchronousLogin.java | 6 +++--- 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 57fb7fe5..4d0d4436 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -216,6 +216,15 @@ public class CacheDataSource implements DataSource { return result; } + @Override + public boolean updateIp(String user, String ip) { + boolean result = source.updateIp(user, ip); + if (result) { + cachedAuths.refresh(user); + } + return result; + } + @Override public List getAllAuths() { return source.getAllAuths(); diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index 8a22ffdd..43693313 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -14,6 +14,7 @@ public interface DataSource { * Return whether there is a record for the given username. * * @param user The username to look up + * * @return True if there is a record, false otherwise */ boolean isAuthAvailable(String user); @@ -22,6 +23,7 @@ public interface DataSource { * Return the hashed password of the player. * * @param user The user whose password should be retrieve + * * @return The password hash of the player */ HashedPassword getPassword(String user); @@ -30,6 +32,7 @@ public interface DataSource { * Retrieve the entire PlayerAuth object associated with the username. * * @param user The user to retrieve + * * @return The PlayerAuth object for the given username */ PlayerAuth getAuth(String user); @@ -38,6 +41,7 @@ public interface DataSource { * Save a new PlayerAuth object. * * @param auth The new PlayerAuth to persist + * * @return True upon success, false upon failure */ boolean saveAuth(PlayerAuth auth); @@ -46,6 +50,7 @@ public interface DataSource { * Update the session of a record (IP, last login, real name). * * @param auth The PlayerAuth object to update in the database + * * @return True upon success, false upon failure */ boolean updateSession(PlayerAuth auth); @@ -54,6 +59,7 @@ public interface DataSource { * Update the password of the given PlayerAuth object. * * @param auth The PlayerAuth whose password should be updated + * * @return True upon success, false upon failure */ boolean updatePassword(PlayerAuth auth); @@ -61,8 +67,9 @@ public interface DataSource { /** * Update the password of the given player. * - * @param user The user whose password should be updated + * @param user The user whose password should be updated * @param password The new password + * * @return True upon success, false upon failure */ boolean updatePassword(String user, HashedPassword password); @@ -72,6 +79,7 @@ public interface DataSource { * the given time. * * @param until The minimum last login + * * @return The account names that have been removed */ List autoPurgeDatabase(long until); @@ -80,6 +88,7 @@ public interface DataSource { * Remove a user record from the database. * * @param user The user to remove + * * @return True upon success, false upon failure */ boolean removeAuth(String user); @@ -88,6 +97,7 @@ public interface DataSource { * Update the quit location of a PlayerAuth. * * @param auth The entry whose quit location should be updated + * * @return True upon success, false upon failure */ boolean updateQuitLoc(PlayerAuth auth); @@ -96,6 +106,7 @@ public interface DataSource { * Return all usernames associated with the given IP address. * * @param ip The IP address to look up + * * @return Usernames associated with the given IP address */ List getAllAuthsByIp(String ip); @@ -104,6 +115,7 @@ public interface DataSource { * Return all usernames associated with the given email address. * * @param email The email address to look up + * * @return Users using the given email address */ List getAllAuthsByEmail(String email); @@ -112,6 +124,7 @@ public interface DataSource { * Update the email of the PlayerAuth in the data source. * * @param auth The PlayerAuth whose email should be updated + * * @return True upon success, false upon failure */ boolean updateEmail(PlayerAuth auth); @@ -178,6 +191,9 @@ public interface DataSource { void updateName(String oldOne, String newOne); boolean updateRealName(String user, String realName); + + boolean updateIp(String user, String ip); + /** * Method getAllAuths. * diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index 8fce0f8f..9bab76e1 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -615,6 +615,11 @@ public class FlatFile implements DataSource { return false; } + @Override + public boolean updateIp(String user, String ip) { + throw new UnsupportedOperationException("Flat file no longer supported"); + } + @Override public List getAllAuths() { BufferedReader br = null; diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 78ec0327..a429d181 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -846,6 +846,21 @@ public class MySQL implements DataSource { return false; } + @Override + public boolean updateIp(String user, String ip) { + try (Connection con = getConnection()) { + String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setString(1, ip); + pst.setString(2, user); + pst.executeUpdate(); + return true; + } catch (SQLException ex) { + logSqlException(ex); + } + return false; + } + @Override public List getAllAuths() { List auths = new ArrayList<>(); diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index 86a6d115..081e386c 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -548,6 +548,20 @@ public class SQLite implements DataSource { return false; } + @Override + public boolean updateIp(String user, String ip) { + String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;"; + try(PreparedStatement pst = con.prepareStatement(sql)) { + pst.setString(1, ip); + pst.setString(2, user); + pst.executeUpdate(); + return true; + } catch (SQLException ex) { + logSqlException(ex); + } + return false; + } + @Override public List getAllAuths() { List auths = new ArrayList<>(); diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index a20227e5..5a4cf425 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -7,12 +7,12 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent; +import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.output.Messages; import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.PlayerPermission; import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.security.RandomString; -import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -142,7 +142,7 @@ public class AsynchronousLogin { if (pAuth.getIp().equals("127.0.0.1") && !pAuth.getIp().equals(ip)) { pAuth.setIp(ip); - database.saveAuth(pAuth); + database.updateIp(pAuth.getNickname(), ip); } String email = pAuth.getEmail();