mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2025-02-14 02:41:53 +01:00
Add optional column for players uuids (#1840)
This commit is contained in:
parent
ae68667f5e
commit
254d4d75a2
@ -5,6 +5,7 @@ import org.bukkit.Location;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
@ -40,6 +41,7 @@ public class PlayerAuth {
|
||||
private String world;
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
private UUID uuid;
|
||||
|
||||
/**
|
||||
* Hidden constructor.
|
||||
@ -169,6 +171,14 @@ public class PlayerAuth {
|
||||
this.totpKey = totpKey;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof PlayerAuth)) {
|
||||
@ -193,7 +203,8 @@ public class PlayerAuth {
|
||||
+ " ! LastLogin : " + lastLogin
|
||||
+ " ! LastPosition : " + x + "," + y + "," + z + "," + world
|
||||
+ " ! Email : " + email
|
||||
+ " ! Password : {" + password.getHash() + ", " + password.getSalt() + "}";
|
||||
+ " ! Password : {" + password.getHash() + ", " + password.getSalt() + "}"
|
||||
+ " ! UUID : " + uuid;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
@ -218,6 +229,7 @@ public class PlayerAuth {
|
||||
private String world;
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
private UUID uuid;
|
||||
|
||||
/**
|
||||
* Creates a PlayerAuth object.
|
||||
@ -243,6 +255,7 @@ public class PlayerAuth {
|
||||
auth.world = Optional.ofNullable(world).orElse("world");
|
||||
auth.yaw = yaw;
|
||||
auth.pitch = pitch;
|
||||
auth.uuid = uuid;
|
||||
return auth;
|
||||
}
|
||||
|
||||
@ -349,5 +362,10 @@ public class PlayerAuth {
|
||||
this.registrationDate = date;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder uuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,8 @@ public abstract class AbstractSqlDataSource implements DataSource {
|
||||
public boolean saveAuth(PlayerAuth auth) {
|
||||
return columnsHandler.insert(auth,
|
||||
AuthMeColumns.NAME, AuthMeColumns.NICK_NAME, AuthMeColumns.PASSWORD, AuthMeColumns.SALT,
|
||||
AuthMeColumns.EMAIL, AuthMeColumns.REGISTRATION_DATE, AuthMeColumns.REGISTRATION_IP);
|
||||
AuthMeColumns.EMAIL, AuthMeColumns.REGISTRATION_DATE, AuthMeColumns.REGISTRATION_IP,
|
||||
AuthMeColumns.UUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,6 +30,7 @@ public final class Columns {
|
||||
public final String HAS_SESSION;
|
||||
public final String REGISTRATION_DATE;
|
||||
public final String REGISTRATION_IP;
|
||||
public final String PLAYER_UUID;
|
||||
|
||||
public Columns(Settings settings) {
|
||||
NAME = settings.getProperty(DatabaseSettings.MYSQL_COL_NAME);
|
||||
@ -52,6 +53,7 @@ public final class Columns {
|
||||
HAS_SESSION = settings.getProperty(DatabaseSettings.MYSQL_COL_HASSESSION);
|
||||
REGISTRATION_DATE = settings.getProperty(DatabaseSettings.MYSQL_COL_REGISTER_DATE);
|
||||
REGISTRATION_IP = settings.getProperty(DatabaseSettings.MYSQL_COL_REGISTER_IP);
|
||||
PLAYER_UUID = settings.getProperty(DatabaseSettings.MYSQL_COL_PLAYER_UUID);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||
import fr.xephi.authme.util.UuidUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
@ -23,6 +24,7 @@ import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
|
||||
@ -264,6 +266,11 @@ public class MySQL extends AbstractSqlDataSource {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(16);");
|
||||
}
|
||||
|
||||
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.PLAYER_UUID + " VARCHAR(36)");
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("MySQL setup finished");
|
||||
}
|
||||
@ -454,6 +461,8 @@ public class MySQL extends AbstractSqlDataSource {
|
||||
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
||||
String salt = col.SALT.isEmpty() ? null : row.getString(col.SALT);
|
||||
int group = col.GROUP.isEmpty() ? -1 : row.getInt(col.GROUP);
|
||||
UUID uuid = col.PLAYER_UUID.isEmpty()
|
||||
? null : UuidUtils.parseUuidSafely(row.getString(col.PLAYER_UUID));
|
||||
return PlayerAuth.builder()
|
||||
.name(row.getString(col.NAME))
|
||||
.realName(row.getString(col.REAL_NAME))
|
||||
@ -471,6 +480,7 @@ public class MySQL extends AbstractSqlDataSource {
|
||||
.locZ(row.getDouble(col.LASTLOC_Z))
|
||||
.locYaw(row.getFloat(col.LASTLOC_YAW))
|
||||
.locPitch(row.getFloat(col.LASTLOC_PITCH))
|
||||
.uuid(uuid)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -241,6 +241,11 @@ public class PostgreSqlDataSource extends AbstractSqlDataSource {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(16);");
|
||||
}
|
||||
|
||||
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.PLAYER_UUID + " VARCHAR(36)");
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("PostgreSQL setup finished");
|
||||
}
|
||||
|
@ -182,6 +182,11 @@ public class SQLite extends AbstractSqlDataSource {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(16);");
|
||||
}
|
||||
|
||||
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.PLAYER_UUID + " VARCHAR(36)");
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("SQLite Setup finished");
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package fr.xephi.authme.datasource.columnshandler;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static fr.xephi.authme.datasource.columnshandler.AuthMeColumnsFactory.ColumnOptions.DEFAULT_FOR_NULL;
|
||||
import static fr.xephi.authme.datasource.columnshandler.AuthMeColumnsFactory.ColumnOptions.OPTIONAL;
|
||||
import static fr.xephi.authme.datasource.columnshandler.AuthMeColumnsFactory.createDouble;
|
||||
@ -46,6 +48,11 @@ public final class AuthMeColumns {
|
||||
public static final PlayerAuthColumn<Long> REGISTRATION_DATE = createLong(
|
||||
DatabaseSettings.MYSQL_COL_REGISTER_DATE, PlayerAuth::getRegistrationDate);
|
||||
|
||||
public static final PlayerAuthColumn<String> UUID = createString(
|
||||
DatabaseSettings.MYSQL_COL_PLAYER_UUID,
|
||||
auth -> ( auth.getUuid() == null ? null : auth.getUuid().toString()),
|
||||
OPTIONAL);
|
||||
|
||||
// --------
|
||||
// Location columns
|
||||
// --------
|
||||
@ -76,7 +83,6 @@ public final class AuthMeColumns {
|
||||
public static final DataSourceColumn<Integer> HAS_SESSION = createInteger(
|
||||
DatabaseSettings.MYSQL_COL_HASSESSION);
|
||||
|
||||
|
||||
private AuthMeColumns() {
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.ConverterSettings;
|
||||
import fr.xephi.authme.util.UuidUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -21,6 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static fr.xephi.authme.util.Utils.logAndSendMessage;
|
||||
|
||||
@ -119,6 +121,7 @@ public class LoginSecurityConverter implements Converter {
|
||||
.map(Timestamp::getTime).orElse(null);
|
||||
long regDate = Optional.ofNullable(resultSet.getDate("registration_date"))
|
||||
.map(Date::getTime).orElse(System.currentTimeMillis());
|
||||
UUID uuid = UuidUtils.parseUuidSafely(resultSet.getString("unique_user_id"));
|
||||
return PlayerAuth.builder()
|
||||
.name(name)
|
||||
.realName(name)
|
||||
@ -132,6 +135,7 @@ public class LoginSecurityConverter implements Converter {
|
||||
.locWorld(resultSet.getString("world"))
|
||||
.locYaw(resultSet.getFloat("yaw"))
|
||||
.locPitch(resultSet.getFloat("pitch"))
|
||||
.uuid(uuid)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,7 @@ final class PlayerAuthBuilderHelper {
|
||||
.email(email)
|
||||
.registrationIp(PlayerUtils.getPlayerIp(player))
|
||||
.registrationDate(System.currentTimeMillis())
|
||||
.uuid(player.getUniqueId())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -129,6 +129,10 @@ public final class DatabaseSettings implements SettingsHolder {
|
||||
public static final Property<String> MYSQL_COL_LASTLOC_PITCH =
|
||||
newProperty("DataSource.mySQLlastlocPitch", "pitch");
|
||||
|
||||
@Comment("Column for storing players uuids (optional)")
|
||||
public static final Property<String> MYSQL_COL_PLAYER_UUID =
|
||||
newProperty( "DataSource.mySQLPlayerUUID", "" );
|
||||
|
||||
@Comment("Column for storing players groups")
|
||||
public static final Property<String> MYSQL_COL_GROUP =
|
||||
newProperty("ExternalBoardOptions.mySQLColumnGroup", "");
|
||||
|
27
src/main/java/fr/xephi/authme/util/UuidUtils.java
Normal file
27
src/main/java/fr/xephi/authme/util/UuidUtils.java
Normal file
@ -0,0 +1,27 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Utility class for various operations on UUID.
|
||||
*/
|
||||
public final class UuidUtils {
|
||||
|
||||
// Utility class
|
||||
private UuidUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given string as an UUID or null
|
||||
*
|
||||
* @param string the uuid to parse
|
||||
* @return parsed UUID if succeed or null
|
||||
*/
|
||||
public static UUID parseUuidSafely(String string) {
|
||||
try {
|
||||
return UUID.fromString(string);
|
||||
} catch (IllegalArgumentException | NullPointerException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user