Fix duplicate entry error, #528

This commit is contained in:
DNx5 2016-02-20 06:47:59 +07:00
parent 90e0dc1875
commit cc29d8b628
6 changed files with 63 additions and 4 deletions

View File

@ -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<PlayerAuth> getAllAuths() {
return source.getAllAuths();

View File

@ -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<String> 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<String> 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<String> 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.
*

View File

@ -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<PlayerAuth> getAllAuths() {
BufferedReader br = null;

View File

@ -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<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>();

View File

@ -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<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>();

View File

@ -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();