mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-10-31 07:39:42 +01:00
- Create command under /authme debug that allows to change the 'nullable' status of MySQL columns (currently last date and email only) - We need to offer a default value for forum integrations that have a NOT NULL email column. Offering a command avoids us from force-migrating existing databases while still offering migrations in both directions - Change in default value handling: lack of values are not handled by setting default values to the PlayerAuth anymore, and reading a default value from the database into a PlayerAuth will be translated into null by the PlayerAuth builder - When a new database is created, email and lastlogin are now nullable and lack a default a value Open points: - Finish MySqlDefaultChangerTest - Revise purging logic (#792) - Allow to have more columns nullable (#814)
This commit is contained in:
parent
718c38aa24
commit
1df5308e56
@ -177,7 +177,7 @@ public class AuthMeApi {
|
||||
if (auth == null) {
|
||||
auth = dataSource.getAuth(playerName);
|
||||
}
|
||||
if (auth != null) {
|
||||
if (auth != null && auth.getLastLogin() != null) {
|
||||
return new Date(auth.getLastLogin());
|
||||
}
|
||||
return null;
|
||||
|
@ -34,17 +34,23 @@ public class LastLoginCommand implements ExecutableCommand {
|
||||
}
|
||||
|
||||
// Get the last login date
|
||||
final long lastLogin = auth.getLastLogin();
|
||||
final Long lastLogin = auth.getLastLogin();
|
||||
final String lastLoginDate = lastLogin == null ? "never" : new Date(lastLogin).toString();
|
||||
|
||||
// Show the player status
|
||||
sender.sendMessage("[AuthMe] " + playerName + " last login: " + lastLoginDate);
|
||||
if (lastLogin != null) {
|
||||
sender.sendMessage("[AuthMe] The player " + playerName + " last logged in "
|
||||
+ createLastLoginIntervalMessage(lastLogin) + " ago");
|
||||
}
|
||||
sender.sendMessage("[AuthMe] Last player's IP: " + auth.getLastIp());
|
||||
}
|
||||
|
||||
private static String createLastLoginIntervalMessage(long lastLogin) {
|
||||
final long diff = System.currentTimeMillis() - lastLogin;
|
||||
final String lastLoginMessage = (int) (diff / 86400000) + " days "
|
||||
return (int) (diff / 86400000) + " days "
|
||||
+ (int) (diff / 3600000 % 24) + " hours "
|
||||
+ (int) (diff / 60000 % 60) + " mins "
|
||||
+ (int) (diff / 1000 % 60) + " secs";
|
||||
Date date = new Date(lastLogin);
|
||||
|
||||
// Show the player status
|
||||
sender.sendMessage("[AuthMe] " + playerName + " last login: " + date.toString());
|
||||
sender.sendMessage("[AuthMe] The player " + playerName + " last logged in " + lastLoginMessage + " ago.");
|
||||
sender.sendMessage("[AuthMe] Last Player's IP: " + auth.getLastIp());
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public class DebugCommand implements ExecutableCommand {
|
||||
private static final Set<Class<? extends DebugSection>> SECTION_CLASSES = ImmutableSet.of(
|
||||
PermissionGroups.class, DataStatistics.class, CountryLookup.class, PlayerAuthViewer.class, InputValidator.class,
|
||||
LimboPlayerViewer.class, CountryLookup.class, HasPermissionChecker.class, TestEmailSender.class,
|
||||
SpawnLocationViewer.class);
|
||||
SpawnLocationViewer.class, MySqlDefaultChanger.class);
|
||||
|
||||
@Inject
|
||||
private Factory<DebugSection> debugSectionFactory;
|
||||
|
@ -0,0 +1,282 @@
|
||||
package fr.xephi.authme.command.executable.authme.debug;
|
||||
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.datasource.CacheDataSource;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.datasource.MySQL;
|
||||
import fr.xephi.authme.permission.DebugSectionPermissions;
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.data.auth.PlayerAuth.DB_EMAIL_DEFAULT;
|
||||
import static fr.xephi.authme.data.auth.PlayerAuth.DB_LAST_LOGIN_DEFAULT;
|
||||
import static java.lang.String.format;
|
||||
|
||||
/**
|
||||
* Convenience command to add or remove the default value of a column and its nullable status
|
||||
* in the MySQL data source.
|
||||
*/
|
||||
class MySqlDefaultChanger implements DebugSection {
|
||||
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
@Inject
|
||||
private DataSource dataSource;
|
||||
|
||||
private MySQL mySql;
|
||||
|
||||
@PostConstruct
|
||||
void setMySqlField() {
|
||||
DataSource dataSource = unwrapSourceFromCacheDataSource(this.dataSource);
|
||||
if (dataSource instanceof MySQL) {
|
||||
this.mySql = (MySQL) dataSource;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "mysqldef";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Add or remove the default value of a column for MySQL";
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionNode getRequiredPermission() {
|
||||
return DebugSectionPermissions.MYSQL_DEFAULT_CHANGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, List<String> arguments) {
|
||||
if (mySql == null) {
|
||||
sender.sendMessage("Defaults can be changed for the MySQL data source only.");
|
||||
return;
|
||||
}
|
||||
|
||||
Operation operation = matchToEnum(arguments, 0, Operation.class);
|
||||
Columns column = matchToEnum(arguments, 1, Columns.class);
|
||||
if (operation == null || column == null) {
|
||||
displayUsageHints(sender);
|
||||
} else {
|
||||
try (Connection con = getConnection(mySql)) {
|
||||
switch (operation) {
|
||||
case ADD:
|
||||
changeColumnToNotNullWithDefault(sender, column, con);
|
||||
break;
|
||||
case REMOVE:
|
||||
removeNotNullAndDefault(sender, column, con);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Unknown operation '" + operation + "'");
|
||||
}
|
||||
} catch (SQLException | IllegalStateException e) {
|
||||
ConsoleLogger.logException("Failed to perform MySQL default altering operation:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void changeColumnToNotNullWithDefault(CommandSender sender, Columns column,
|
||||
Connection con) throws SQLException {
|
||||
final String tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
||||
final String columnName = settings.getProperty(column.columnName);
|
||||
|
||||
// Replace NULLs with future default value
|
||||
String sql = format("UPDATE %s SET %s = ? WHERE %s IS NULL;", tableName, columnName, columnName);
|
||||
int updatedRows;
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setObject(1, column.defaultValue);
|
||||
updatedRows = pst.executeUpdate();
|
||||
}
|
||||
sender.sendMessage("Replaced NULLs with default value ('" + column.defaultValue
|
||||
+ "'), modifying " + updatedRows + " entries");
|
||||
|
||||
// Change column definition to NOT NULL version
|
||||
try (Statement st = con.createStatement()) {
|
||||
st.execute(format("ALTER TABLE %s MODIFY %s %s", tableName, columnName, column.notNullDefinition));
|
||||
sender.sendMessage("Changed column '" + columnName + "' to have NOT NULL constraint");
|
||||
}
|
||||
|
||||
// Log success message
|
||||
ConsoleLogger.info("Changed MySQL column '" + columnName + "' to be NOT NULL, as initiated by '"
|
||||
+ sender.getName() + "'");
|
||||
}
|
||||
|
||||
private void removeNotNullAndDefault(CommandSender sender, Columns column, Connection con) throws SQLException {
|
||||
final String tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
||||
final String columnName = settings.getProperty(column.columnName);
|
||||
|
||||
// Change column definition to nullable version
|
||||
try (Statement st = con.createStatement()) {
|
||||
st.execute(format("ALTER TABLE %s MODIFY %s %s", tableName, columnName, column.nullableDefinition));
|
||||
sender.sendMessage("Changed column '" + columnName + "' to allow nulls");
|
||||
}
|
||||
|
||||
// Replace old default value with NULL
|
||||
String sql = format("UPDATE %s SET %s = NULL WHERE %s = ?;", tableName, columnName, columnName);
|
||||
int updatedRows;
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setObject(1, column.defaultValue);
|
||||
updatedRows = pst.executeUpdate();
|
||||
}
|
||||
sender.sendMessage("Replaced default value ('" + column.defaultValue
|
||||
+ "') to be NULL, modifying " + updatedRows + " entries");
|
||||
|
||||
// Log success message
|
||||
ConsoleLogger.info("Changed MySQL column '" + columnName + "' to allow NULL, as initiated by '"
|
||||
+ sender.getName() + "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays sample commands and the list of columns that can be changed.
|
||||
*
|
||||
* @param sender the sender issuing the command
|
||||
*/
|
||||
private void displayUsageHints(CommandSender sender) {
|
||||
sender.sendMessage("Adds or removes a NOT NULL constraint for a column.");
|
||||
sender.sendMessage(" Only available for MySQL.");
|
||||
if (mySql == null) {
|
||||
sender.sendMessage("You are currently not using MySQL!");
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage("Examples: add a NOT NULL constraint with");
|
||||
sender.sendMessage(" /authme debug mysqldef add <column>");
|
||||
sender.sendMessage("Remove a NOT NULL constraint with");
|
||||
sender.sendMessage(" /authme debug mysqldef remove <column>");
|
||||
|
||||
// Note ljacqu 20171015: Intentionally avoid green & red as to avoid suggesting that one state is good or bad
|
||||
sender.sendMessage("Available columns: " + constructColoredColumnList());
|
||||
sender.sendMessage(" where " + ChatColor.DARK_AQUA + "blue " + ChatColor.RESET
|
||||
+ "is currently not-null, and " + ChatColor.GOLD + "gold " + ChatColor.RESET + "is null");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list of {@link Columns} we can toggle, colored by their current not-null status
|
||||
*/
|
||||
private String constructColoredColumnList() {
|
||||
try (Connection con = getConnection(mySql)) {
|
||||
final DatabaseMetaData metaData = con.getMetaData();
|
||||
final String tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
||||
|
||||
List<String> formattedColumns = new ArrayList<>(Columns.values().length);
|
||||
for (Columns col : Columns.values()) {
|
||||
boolean isNotNull = isNotNullColumn(metaData, tableName, settings.getProperty(col.columnName));
|
||||
String formattedColumn = (isNotNull ? ChatColor.DARK_AQUA : ChatColor.GOLD) + col.name().toLowerCase();
|
||||
formattedColumns.add(formattedColumn);
|
||||
}
|
||||
return String.join(ChatColor.RESET + ", ", formattedColumns);
|
||||
} catch (SQLException e) {
|
||||
ConsoleLogger.logException("Failed to construct column list:", e);
|
||||
return ChatColor.RED + "An error occurred! Please see the console for details.";
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNotNullColumn(DatabaseMetaData metaData, String tableName,
|
||||
String columnName) throws SQLException {
|
||||
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
|
||||
if (!rs.next()) {
|
||||
throw new IllegalStateException("Did not find meta data for column '" + columnName
|
||||
+ "' while migrating not-null columns (this should never happen!)");
|
||||
}
|
||||
|
||||
int nullableCode = rs.getInt("NULLABLE");
|
||||
if (nullableCode == DatabaseMetaData.columnNoNulls) {
|
||||
return true;
|
||||
} else if (nullableCode == DatabaseMetaData.columnNullableUnknown) {
|
||||
ConsoleLogger.warning("Unknown nullable status for column '" + columnName + "'");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
Connection getConnection(MySQL mySql) {
|
||||
try {
|
||||
Method method = MySQL.class.getDeclaredMethod("getConnection");
|
||||
method.setAccessible(true);
|
||||
return (Connection) method.invoke(mySql);
|
||||
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
throw new IllegalStateException("Could not get MySQL connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps the "cache data source" and returns the underlying source. Returns the
|
||||
* same as the input argument otherwise.
|
||||
*
|
||||
* @param dataSource the data source to unwrap if applicable
|
||||
* @return the non-cache data source
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static DataSource unwrapSourceFromCacheDataSource(DataSource dataSource) {
|
||||
if (dataSource instanceof CacheDataSource) {
|
||||
try {
|
||||
Field source = CacheDataSource.class.getDeclaredField("source");
|
||||
source.setAccessible(true);
|
||||
return (DataSource) source.get(dataSource);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
ConsoleLogger.logException("Could not get source of CacheDataSource:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private static <E extends Enum<E>> E matchToEnum(List<String> arguments, int index, Class<E> clazz) {
|
||||
if (arguments.size() <= index) {
|
||||
return null;
|
||||
}
|
||||
String str = arguments.get(index);
|
||||
return Arrays.stream(clazz.getEnumConstants())
|
||||
.filter(e -> e.name().equalsIgnoreCase(str))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private enum Operation {
|
||||
ADD, REMOVE
|
||||
}
|
||||
|
||||
enum Columns {
|
||||
|
||||
LASTLOGIN(DatabaseSettings.MYSQL_COL_LASTLOGIN,
|
||||
"BIGINT", "BIGINT NOT NULL DEFAULT 0", DB_LAST_LOGIN_DEFAULT),
|
||||
|
||||
EMAIL(DatabaseSettings.MYSQL_COL_EMAIL,
|
||||
"VARCHAR(255)", "VARCHAR(255) NOT NULL DEFAULT 'your@email.com'", DB_EMAIL_DEFAULT);
|
||||
|
||||
final Property<String> columnName;
|
||||
final String nullableDefinition;
|
||||
final String notNullDefinition;
|
||||
final Object defaultValue;
|
||||
|
||||
Columns(Property<String> columnName, String nullableDefinition, String notNullDefinition, Object defaultValue) {
|
||||
this.columnName = columnName;
|
||||
this.nullableDefinition = nullableDefinition;
|
||||
this.notNullDefinition = notNullDefinition;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
*/
|
||||
public class PlayerAuth {
|
||||
|
||||
/** Default email used in the database if the email column is defined to be NOT NULL. */
|
||||
public static final String DB_EMAIL_DEFAULT = "your@email.com";
|
||||
/** Default last login value used in the database if the last login column is NOT NULL. */
|
||||
public static final long DB_LAST_LOGIN_DEFAULT = 0;
|
||||
|
||||
/** The player's name in lowercase, e.g. "xephi". */
|
||||
private String nickname;
|
||||
/** The player's name in the correct casing, e.g. "Xephi". */
|
||||
@ -22,7 +27,7 @@ public class PlayerAuth {
|
||||
private String email;
|
||||
private String lastIp;
|
||||
private int groupId;
|
||||
private long lastLogin;
|
||||
private Long lastLogin;
|
||||
private String registrationIp;
|
||||
private long registrationDate;
|
||||
// Fields storing the player's quit location
|
||||
@ -117,7 +122,7 @@ public class PlayerAuth {
|
||||
this.lastIp = lastIp;
|
||||
}
|
||||
|
||||
public long getLastLogin() {
|
||||
public Long getLastLogin() {
|
||||
return lastLogin;
|
||||
}
|
||||
|
||||
@ -191,7 +196,7 @@ public class PlayerAuth {
|
||||
private String lastIp;
|
||||
private String email;
|
||||
private int groupId = -1;
|
||||
private long lastLogin = System.currentTimeMillis();
|
||||
private Long lastLogin;
|
||||
private String registrationIp;
|
||||
private Long registrationDate;
|
||||
|
||||
@ -212,10 +217,10 @@ public class PlayerAuth {
|
||||
auth.nickname = checkNotNull(name).toLowerCase();
|
||||
auth.realName = firstNonNull(realName, "Player");
|
||||
auth.password = firstNonNull(password, new HashedPassword(""));
|
||||
auth.email = firstNonNull(email, "your@email.com");
|
||||
auth.email = DB_EMAIL_DEFAULT.equals(email) ? null : email;
|
||||
auth.lastIp = firstNonNull(lastIp, "127.0.0.1");
|
||||
auth.groupId = groupId;
|
||||
auth.lastLogin = lastLogin;
|
||||
auth.lastLogin = isEqualTo(lastLogin, DB_LAST_LOGIN_DEFAULT) ? null : lastLogin;
|
||||
auth.registrationIp = registrationIp;
|
||||
auth.registrationDate = registrationDate == null ? System.currentTimeMillis() : registrationDate;
|
||||
|
||||
@ -228,6 +233,10 @@ public class PlayerAuth {
|
||||
return auth;
|
||||
}
|
||||
|
||||
private static boolean isEqualTo(Long value, long defaultValue) {
|
||||
return value != null && defaultValue == value;
|
||||
}
|
||||
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
@ -298,7 +307,7 @@ public class PlayerAuth {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder lastLogin(long lastLogin) {
|
||||
public Builder lastLogin(Long lastLogin) {
|
||||
this.lastLogin = lastLogin;
|
||||
return this;
|
||||
}
|
||||
|
@ -394,7 +394,7 @@ public class FlatFile implements DataSource {
|
||||
.name(args[0]).realName(args[0]).password(args[1], null);
|
||||
|
||||
if (args.length >= 3) builder.lastIp(args[2]);
|
||||
if (args.length >= 4) builder.lastLogin(Long.parseLong(args[3]));
|
||||
if (args.length >= 4) builder.lastLogin(parseNullableLong(args[3]));
|
||||
if (args.length >= 7) {
|
||||
builder.locX(Double.parseDouble(args[4]))
|
||||
.locY(Double.parseDouble(args[5]))
|
||||
@ -406,4 +406,8 @@ public class FlatFile implements DataSource {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Long parseNullableLong(String str) {
|
||||
return "null".equals(str) ? null : Long.parseLong(str);
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
|
||||
|
||||
public class MySQL implements DataSource {
|
||||
@ -196,14 +197,14 @@ public class MySQL implements DataSource {
|
||||
|
||||
if (isColumnMissing(md, col.LAST_LOGIN)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;");
|
||||
+ " ADD COLUMN " + col.LAST_LOGIN + " BIGINT;");
|
||||
} else {
|
||||
migrateLastLoginColumn(con, md);
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.REGISTRATION_DATE)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.REGISTRATION_DATE + " BIGINT;");
|
||||
+ " ADD COLUMN " + col.REGISTRATION_DATE + " BIGINT NOT NULL;");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.REGISTRATION_IP)) {
|
||||
@ -240,7 +241,7 @@ public class MySQL implements DataSource {
|
||||
|
||||
if (isColumnMissing(md, col.EMAIL)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com' AFTER " + col.LASTLOC_WORLD);
|
||||
+ col.EMAIL + " VARCHAR(255);");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.IS_LOGGED)) {
|
||||
@ -394,7 +395,7 @@ public class MySQL implements DataSource {
|
||||
+ col.LAST_IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, auth.getLastIp());
|
||||
pst.setLong(2, auth.getLastLogin());
|
||||
pst.setObject(2, auth.getLastLogin());
|
||||
pst.setString(3, auth.getRealName());
|
||||
pst.setString(4, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
@ -673,7 +674,7 @@ public class MySQL implements DataSource {
|
||||
.name(row.getString(col.NAME))
|
||||
.realName(row.getString(col.REAL_NAME))
|
||||
.password(row.getString(col.PASSWORD), salt)
|
||||
.lastLogin(row.getLong(col.LAST_LOGIN))
|
||||
.lastLogin(getNullableLong(row, col.LAST_LOGIN))
|
||||
.lastIp(row.getString(col.LAST_IP))
|
||||
.email(row.getString(col.EMAIL))
|
||||
.registrationDate(row.getLong(col.REGISTRATION_DATE))
|
||||
|
@ -21,6 +21,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
|
||||
|
||||
/**
|
||||
@ -140,7 +141,7 @@ public class SQLite implements DataSource {
|
||||
|
||||
if (isColumnMissing(md, col.EMAIL)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com';");
|
||||
+ " ADD COLUMN " + col.EMAIL + " VARCHAR(255);");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.IS_LOGGED)) {
|
||||
@ -295,7 +296,7 @@ public class SQLite implements DataSource {
|
||||
+ col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)){
|
||||
pst.setString(1, auth.getLastIp());
|
||||
pst.setLong(2, auth.getLastLogin());
|
||||
pst.setObject(2, auth.getLastLogin());
|
||||
pst.setString(3, auth.getRealName());
|
||||
pst.setString(4, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
@ -574,7 +575,7 @@ public class SQLite implements DataSource {
|
||||
.email(row.getString(col.EMAIL))
|
||||
.realName(row.getString(col.REAL_NAME))
|
||||
.password(row.getString(col.PASSWORD), salt)
|
||||
.lastLogin(row.getLong(col.LAST_LOGIN))
|
||||
.lastLogin(getNullableLong(row, col.LAST_LOGIN))
|
||||
.lastIp(row.getString(col.LAST_IP))
|
||||
.registrationDate(row.getLong(col.REGISTRATION_DATE))
|
||||
.registrationIp(row.getString(col.REGISTRATION_IP))
|
||||
|
@ -29,6 +29,9 @@ public enum DebugSectionPermissions implements PermissionNode {
|
||||
/** Permission to view data from the database. */
|
||||
PLAYER_AUTH_VIEWER("authme.debug.db"),
|
||||
|
||||
/** Permission to change nullable status of MySQL columns. */
|
||||
MYSQL_DEFAULT_CHANGER("authme.debug.mysqldef"),
|
||||
|
||||
/** Permission to view spawn information. */
|
||||
SPAWN_LOCATION("authme.debug.spawn"),
|
||||
|
||||
|
@ -177,6 +177,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
|
||||
});
|
||||
}
|
||||
|
||||
// TODO #792: lastlogin date might be null (not updating now because of has_session branch changes)
|
||||
|
||||
private boolean canResumeSession(Player player) {
|
||||
final String name = player.getName();
|
||||
if (database.isLogged(name)) {
|
||||
|
@ -156,7 +156,7 @@ permissions:
|
||||
description: Permission node to bypass AntiBot protection.
|
||||
default: op
|
||||
authme.bypasscountrycheck:
|
||||
description: Permission to use to see own other accounts.
|
||||
description: Permission to bypass the GeoIp country code check.
|
||||
default: false
|
||||
authme.bypassforcesurvival:
|
||||
description: Permission for users to bypass force-survival mode.
|
||||
@ -173,6 +173,7 @@ permissions:
|
||||
authme.debug.group: true
|
||||
authme.debug.limbo: true
|
||||
authme.debug.mail: true
|
||||
authme.debug.mysqldef: true
|
||||
authme.debug.perm: true
|
||||
authme.debug.spawn: true
|
||||
authme.debug.stats: true
|
||||
@ -195,6 +196,9 @@ permissions:
|
||||
authme.debug.mail:
|
||||
description: Permission to use the test email sender.
|
||||
default: op
|
||||
authme.debug.mysqldef:
|
||||
description: Permission to change nullable status of MySQL columns.
|
||||
default: op
|
||||
authme.debug.perm:
|
||||
description: Permission to use the permission checker.
|
||||
default: op
|
||||
|
@ -162,20 +162,36 @@ public class AuthMeApiTest {
|
||||
public void shouldGetLastLogin() {
|
||||
// given
|
||||
String name = "David";
|
||||
Player player = mockPlayerWithName(name);
|
||||
PlayerAuth auth = PlayerAuth.builder().name(name)
|
||||
.lastLogin(1501597979)
|
||||
.lastLogin(1501597979L)
|
||||
.build();
|
||||
given(playerCache.getAuth(name)).willReturn(auth);
|
||||
|
||||
// when
|
||||
Date result = api.getLastLogin(player.getName());
|
||||
Date result = api.getLastLogin(name);
|
||||
|
||||
// then
|
||||
assertThat(result, not(nullValue()));
|
||||
assertThat(result, equalTo(new Date(1501597979)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNullLastLogin() {
|
||||
// given
|
||||
String name = "John";
|
||||
PlayerAuth auth = PlayerAuth.builder().name(name)
|
||||
.lastLogin(null)
|
||||
.build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
|
||||
// when
|
||||
Date result = api.getLastLogin(name);
|
||||
|
||||
// then
|
||||
assertThat(result, nullValue());
|
||||
verify(dataSource).getAuth(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForUnavailablePlayer() {
|
||||
// given
|
||||
|
@ -111,4 +111,25 @@ public class LastLoginCommandTest {
|
||||
assertThat(captor.getAllValues().get(2), containsString("123.45.66.77"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNullLastLoginDate() {
|
||||
// given
|
||||
String name = "player";
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastIp("123.45.67.89")
|
||||
.build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(name));
|
||||
|
||||
// then
|
||||
verify(dataSource).getAuth(name);
|
||||
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
|
||||
verify(sender, times(2)).sendMessage(captor.capture());
|
||||
assertThat(captor.getAllValues().get(0), allOf(containsString(name), containsString("never")));
|
||||
assertThat(captor.getAllValues().get(1), containsString("123.45.67.89"));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,69 @@
|
||||
package fr.xephi.authme.command.executable.authme.debug;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Consistency test for {@link MySqlDefaultChanger.Columns} enum.
|
||||
*/
|
||||
public class MySqlDefaultChangerColumnsTest {
|
||||
|
||||
@Test
|
||||
public void shouldAllHaveDifferentNameProperty() {
|
||||
// given
|
||||
Set<String> properties = new HashSet<>();
|
||||
|
||||
// when / then
|
||||
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
|
||||
if (!properties.add(col.columnName.getPath())) {
|
||||
fail("Column '" + col + "' has a column name property path that was already encountered: "
|
||||
+ col.columnName.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveMatchingNullableAndNotNullDefinition() {
|
||||
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
|
||||
verifyHasCorrespondingColumnDefinitions(col);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveMatchingDefaultValueInNotNullDefinition() {
|
||||
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
|
||||
verifyHasSameDefaultValueInNotNullDefinition(col);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyHasCorrespondingColumnDefinitions(MySqlDefaultChanger.Columns column) {
|
||||
// given / when
|
||||
String nullable = column.nullableDefinition;
|
||||
String notNull = column.notNullDefinition;
|
||||
|
||||
// then
|
||||
String expectedNotNull = nullable + " NOT NULL DEFAULT ";
|
||||
assertThat(column.name(), notNull.startsWith(expectedNotNull), equalTo(true));
|
||||
// Check that `notNull` length is bigger because we expect a value after DEFAULT
|
||||
assertThat(column.name(), notNull.length() > expectedNotNull.length(), equalTo(true));
|
||||
}
|
||||
|
||||
private void verifyHasSameDefaultValueInNotNullDefinition(MySqlDefaultChanger.Columns column) {
|
||||
// given / when
|
||||
String notNull = column.notNullDefinition;
|
||||
Object defaultValue = column.defaultValue;
|
||||
|
||||
// then
|
||||
String defaultValueAsString = String.valueOf(defaultValue);
|
||||
if (!notNull.endsWith("DEFAULT " + defaultValueAsString)
|
||||
&& !notNull.endsWith("DEFAULT '" + defaultValueAsString + "'")) {
|
||||
fail("Expected '" + column + "' not-null definition to contain DEFAULT " + defaultValueAsString);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package fr.xephi.authme.command.executable.authme.debug;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.CacheDataSource;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link MySqlDefaultChanger}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MySqlDefaultChangerTest {
|
||||
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
@Test
|
||||
public void shouldReturnSameDataSourceInstance() {
|
||||
// given
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
|
||||
// when
|
||||
DataSource result = MySqlDefaultChanger.unwrapSourceFromCacheDataSource(dataSource);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(dataSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUnwrapCacheDataSource() {
|
||||
// given
|
||||
DataSource source = mock(DataSource.class);
|
||||
PlayerCache playerCache = mock(PlayerCache.class);
|
||||
CacheDataSource cacheDataSource = new CacheDataSource(source, playerCache);
|
||||
|
||||
// when
|
||||
DataSource result = MySqlDefaultChanger.unwrapSourceFromCacheDataSource(cacheDataSource);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(source));
|
||||
}
|
||||
|
||||
// TODO #792: Add more tests
|
||||
|
||||
private MySqlDefaultChanger createDefaultChanger(DataSource dataSource) {
|
||||
MySqlDefaultChanger defaultChanger = new MySqlDefaultChanger();
|
||||
ReflectionTestUtils.setField(defaultChanger, "dataSource", dataSource);
|
||||
ReflectionTestUtils.setField(defaultChanger, "settings", settings);
|
||||
return defaultChanger;
|
||||
}
|
||||
}
|
63
src/test/java/fr/xephi/authme/data/auth/PlayerAuthTest.java
Normal file
63
src/test/java/fr/xephi/authme/data/auth/PlayerAuthTest.java
Normal file
@ -0,0 +1,63 @@
|
||||
package fr.xephi.authme.data.auth;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Test for {@link PlayerAuth} and its builder.
|
||||
*/
|
||||
public class PlayerAuthTest {
|
||||
|
||||
@Test
|
||||
public void shouldRemoveDatabaseDefaults() {
|
||||
// given / when
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name("Bobby")
|
||||
.lastLogin(0L)
|
||||
.email("your@email.com")
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertThat(auth.getNickname(), equalTo("bobby"));
|
||||
assertThat(auth.getLastLogin(), nullValue());
|
||||
assertThat(auth.getEmail(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForMissingName() {
|
||||
try {
|
||||
// given / when
|
||||
PlayerAuth.builder()
|
||||
.email("test@example.org")
|
||||
.groupId(3)
|
||||
.build();
|
||||
|
||||
// then
|
||||
fail("Expected exception to be thrown");
|
||||
} catch (NullPointerException e) {
|
||||
// all good
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreatePlayerAuthWithNullValues() {
|
||||
// given / when
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name("Charlie")
|
||||
.email(null)
|
||||
.lastLogin(null)
|
||||
.groupId(19)
|
||||
.locPitch(123.004f)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertThat(auth.getEmail(), nullValue());
|
||||
assertThat(auth.getLastLogin(), nullValue());
|
||||
assertThat(auth.getGroupId(), equalTo(19));
|
||||
assertThat(auth.getPitch(), equalTo(123.004f));
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package fr.xephi.authme.datasource;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -96,7 +97,7 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
// then
|
||||
assertThat(invalidAuth, nullValue());
|
||||
|
||||
assertThat(bobbyAuth, hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89"));
|
||||
assertThat(bobbyAuth, hasAuthBasicData("bobby", "Bobby", null, "123.45.67.89"));
|
||||
assertThat(bobbyAuth, hasAuthLocation(1.05, 2.1, 4.2, "world", -0.44f, 2.77f));
|
||||
assertThat(bobbyAuth, hasRegistrationInfo("127.0.4.22", 1436778723L));
|
||||
assertThat(bobbyAuth.getLastLogin(), equalTo(1449136800L));
|
||||
@ -142,9 +143,9 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
// then
|
||||
assertThat(response, equalTo(true));
|
||||
assertThat(authList, hasSize(2));
|
||||
assertThat(authList, hasItem(hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89")));
|
||||
assertThat(authList, hasItem(hasAuthBasicData("bobby", "Bobby", null, "123.45.67.89")));
|
||||
assertThat(newAuthList, hasSize(3));
|
||||
assertThat(newAuthList, hasItem(hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89")));
|
||||
assertThat(newAuthList, hasItem(hasAuthBasicData("bobby", "Bobby", null, "123.45.67.89")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -222,7 +223,7 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
// then
|
||||
assertThat(response, equalTo(true));
|
||||
PlayerAuth result = dataSource.getAuth("bobby");
|
||||
assertThat(result, hasAuthBasicData("bobby", "BOBBY", "your@email.com", "12.12.12.12"));
|
||||
assertThat(result, hasAuthBasicData("bobby", "BOBBY", null, "12.12.12.12"));
|
||||
assertThat(result.getLastLogin(), equalTo(123L));
|
||||
}
|
||||
|
||||
@ -327,10 +328,11 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
|
||||
// then
|
||||
assertThat(response1 && response2, equalTo(true));
|
||||
assertThat(dataSource.getAuth("bobby"), hasAuthBasicData("bobby", "BOBBY", "your@email.com", "123.45.67.89"));
|
||||
assertThat(dataSource.getAuth("bobby"), hasAuthBasicData("bobby", "BOBBY", null, "123.45.67.89"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // TODO #792: Fix purging logic
|
||||
public void shouldGetRecordsToPurge() {
|
||||
// given
|
||||
DataSource dataSource = getDataSource();
|
||||
|
@ -61,13 +61,13 @@ public class FlatFileIntegrationTest {
|
||||
|
||||
// then
|
||||
assertThat(authList, hasSize(7));
|
||||
assertThat(getName("bobby", authList), hasAuthBasicData("bobby", "bobby", "your@email.com", "123.45.67.89"));
|
||||
assertThat(getName("bobby", authList), hasAuthBasicData("bobby", "bobby", null, "123.45.67.89"));
|
||||
assertThat(getName("bobby", authList), hasAuthLocation(1.05, 2.1, 4.2, "world", 0, 0));
|
||||
assertThat(getName("bobby", authList).getPassword(), equalToHash("$SHA$11aa0706173d7272$dbba966"));
|
||||
assertThat(getName("twofields", authList), hasAuthBasicData("twofields", "twofields", "your@email.com", "127.0.0.1"));
|
||||
assertThat(getName("twofields", authList), hasAuthBasicData("twofields", "twofields", null, "127.0.0.1"));
|
||||
assertThat(getName("twofields", authList).getPassword(), equalToHash("hash1234"));
|
||||
assertThat(getName("threefields", authList), hasAuthBasicData("threefields", "threefields", "your@email.com", "33.33.33.33"));
|
||||
assertThat(getName("fourfields", authList), hasAuthBasicData("fourfields", "fourfields", "your@email.com", "4.4.4.4"));
|
||||
assertThat(getName("threefields", authList), hasAuthBasicData("threefields", "threefields", null, "33.33.33.33"));
|
||||
assertThat(getName("fourfields", authList), hasAuthBasicData("fourfields", "fourfields", null, "4.4.4.4"));
|
||||
assertThat(getName("fourfields", authList).getLastLogin(), equalTo(404040404L));
|
||||
assertThat(getName("sevenfields", authList), hasAuthLocation(7.7, 14.14, 21.21, "world", 0, 0));
|
||||
assertThat(getName("eightfields", authList), hasAuthLocation(8.8, 17.6, 26.4, "eightworld", 0, 0));
|
||||
|
@ -63,11 +63,11 @@ public class ForceFlatToSqliteTest {
|
||||
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource, times(7)).saveAuth(authCaptor.capture());
|
||||
List<PlayerAuth> auths = authCaptor.getAllValues();
|
||||
assertThat(auths, hasItem(hasAuthBasicData("bobby", "Player", "your@email.com", "123.45.67.89")));
|
||||
assertThat(auths, hasItem(hasAuthBasicData("bobby", "Player", null, "123.45.67.89")));
|
||||
assertThat(auths, hasItem(hasAuthLocation(1.05, 2.1, 4.2, "world", 0, 0)));
|
||||
assertThat(auths, hasItem(hasAuthBasicData("user", "Player", "user@example.org", "34.56.78.90")));
|
||||
assertThat(auths, hasItem(hasAuthLocation(124.1, 76.3, -127.8, "nether", 0, 0)));
|
||||
assertThat(auths, hasItem(hasAuthBasicData("eightfields", "Player", "your@email.com", "6.6.6.66")));
|
||||
assertThat(auths, hasItem(hasAuthBasicData("eightfields", "Player", null, "6.6.6.66")));
|
||||
assertThat(auths, hasItem(hasAuthLocation(8.8, 17.6, 26.4, "eightworld", 0, 0)));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user