#1255 Simplify MySQL data source extensions
- Mostly oving the logic of getting the ID from the DB to the extensions superclass
This commit is contained in:
parent
cbc794ba20
commit
8eceaa8cbb
@ -85,6 +85,7 @@ public class MySQL implements DataSource {
|
|||||||
* Retrieves various settings.
|
* Retrieves various settings.
|
||||||
*
|
*
|
||||||
* @param settings the settings to read properties from
|
* @param settings the settings to read properties from
|
||||||
|
* @param extensionsFactory factory to create the MySQL extension
|
||||||
*/
|
*/
|
||||||
private void setParameters(Settings settings, MySqlExtensionsFactory extensionsFactory) {
|
private void setParameters(Settings settings, MySqlExtensionsFactory extensionsFactory) {
|
||||||
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST);
|
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST);
|
||||||
@ -435,8 +436,8 @@ public class MySQL implements DataSource {
|
|||||||
@Override
|
@Override
|
||||||
public boolean updateQuitLoc(PlayerAuth auth) {
|
public boolean updateQuitLoc(PlayerAuth auth) {
|
||||||
String sql = "UPDATE " + tableName
|
String sql = "UPDATE " + tableName
|
||||||
+ " SET " + col.LASTLOC_X + " =?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, " + col.LASTLOC_WORLD + "=?, "
|
+ " SET " + col.LASTLOC_X + " =?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, "
|
||||||
+ col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=?"
|
+ col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=?"
|
||||||
+ " WHERE " + col.NAME + "=?;";
|
+ " WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setDouble(1, auth.getQuitLocX());
|
pst.setDouble(1, auth.getQuitLocX());
|
||||||
|
|||||||
@ -3,12 +3,10 @@ package fr.xephi.authme.datasource.mysqlextensions;
|
|||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.datasource.Columns;
|
import fr.xephi.authme.datasource.Columns;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|
||||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,53 +14,42 @@ import java.sql.SQLException;
|
|||||||
*/
|
*/
|
||||||
class Ipb4Extension extends MySqlExtension {
|
class Ipb4Extension extends MySqlExtension {
|
||||||
|
|
||||||
private final Columns col;
|
|
||||||
private final String tableName;
|
|
||||||
private final String ipbPrefix;
|
private final String ipbPrefix;
|
||||||
private final int ipbGroup;
|
private final int ipbGroup;
|
||||||
|
|
||||||
Ipb4Extension(Settings settings, Columns col) {
|
Ipb4Extension(Settings settings, Columns col) {
|
||||||
this.col = col;
|
super(settings, col);
|
||||||
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
|
||||||
this.ipbPrefix = settings.getProperty(HooksSettings.IPB_TABLE_PREFIX);
|
this.ipbPrefix = settings.getProperty(HooksSettings.IPB_TABLE_PREFIX);
|
||||||
this.ipbGroup = settings.getProperty(HooksSettings.IPB_ACTIVATED_GROUP_ID);
|
this.ipbGroup = settings.getProperty(HooksSettings.IPB_ACTIVATED_GROUP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
||||||
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
// Update player group in core_members
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
String sql = "UPDATE " + ipbPrefix + tableName
|
||||||
pst.setString(1, auth.getNickname());
|
+ " SET " + tableName + ".member_group_id=? WHERE " + col.NAME + "=?;";
|
||||||
try (ResultSet rs = pst.executeQuery()) {
|
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
||||||
if (rs.next()) {
|
pst2.setInt(1, ipbGroup);
|
||||||
// Update player group in core_members
|
pst2.setString(2, auth.getNickname());
|
||||||
sql = "UPDATE " + ipbPrefix + tableName
|
pst2.executeUpdate();
|
||||||
+ " SET " + tableName + ".member_group_id=? WHERE " + col.NAME + "=?;";
|
}
|
||||||
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
// Get current time without ms
|
||||||
pst2.setInt(1, ipbGroup);
|
long time = System.currentTimeMillis() / 1000;
|
||||||
pst2.setString(2, auth.getNickname());
|
// update joined date
|
||||||
pst2.executeUpdate();
|
sql = "UPDATE " + ipbPrefix + tableName
|
||||||
}
|
+ " SET " + tableName + ".joined=? WHERE " + col.NAME + "=?;";
|
||||||
// Get current time without ms
|
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
||||||
long time = System.currentTimeMillis() / 1000;
|
pst2.setLong(1, time);
|
||||||
// update joined date
|
pst2.setString(2, auth.getNickname());
|
||||||
sql = "UPDATE " + ipbPrefix + tableName
|
pst2.executeUpdate();
|
||||||
+ " SET " + tableName + ".joined=? WHERE " + col.NAME + "=?;";
|
}
|
||||||
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
// Update last_visit
|
||||||
pst2.setLong(1, time);
|
sql = "UPDATE " + ipbPrefix + tableName
|
||||||
pst2.setString(2, auth.getNickname());
|
+ " SET " + tableName + ".last_visit=? WHERE " + col.NAME + "=?;";
|
||||||
pst2.executeUpdate();
|
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
||||||
}
|
pst2.setLong(1, time);
|
||||||
// Update last_visit
|
pst2.setString(2, auth.getNickname());
|
||||||
sql = "UPDATE " + ipbPrefix + tableName
|
pst2.executeUpdate();
|
||||||
+ " SET " + tableName + ".last_visit=? WHERE " + col.NAME + "=?;";
|
|
||||||
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
|
||||||
pst2.setLong(1, time);
|
|
||||||
pst2.setString(2, auth.getNickname());
|
|
||||||
pst2.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,16 @@
|
|||||||
package fr.xephi.authme.datasource.mysqlextensions;
|
package fr.xephi.authme.datasource.mysqlextensions;
|
||||||
|
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||||
|
import fr.xephi.authme.datasource.Columns;
|
||||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||||
|
import fr.xephi.authme.settings.Settings;
|
||||||
|
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension for the MySQL data source for forums. For certain password hashes (e.g. phpBB), we want
|
* Extension for the MySQL data source for forums. For certain password hashes (e.g. phpBB), we want
|
||||||
@ -12,6 +18,14 @@ import java.sql.SQLException;
|
|||||||
*/
|
*/
|
||||||
public abstract class MySqlExtension {
|
public abstract class MySqlExtension {
|
||||||
|
|
||||||
|
protected final Columns col;
|
||||||
|
protected final String tableName;
|
||||||
|
|
||||||
|
MySqlExtension(Settings settings, Columns col) {
|
||||||
|
this.col = col;
|
||||||
|
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs additional actions when a new player is saved.
|
* Performs additional actions when a new player is saved.
|
||||||
*
|
*
|
||||||
@ -59,4 +73,24 @@ public abstract class MySqlExtension {
|
|||||||
// extend for custom behavior
|
// extend for custom behavior
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the database ID of the given name from the database.
|
||||||
|
*
|
||||||
|
* @param name the name to get the ID for
|
||||||
|
* @param con connection to the sql table
|
||||||
|
* @return id of the playerAuth, or empty OptionalInt if the name is not registered
|
||||||
|
* @throws SQLException .
|
||||||
|
*/
|
||||||
|
protected OptionalInt retrieveIdFromTable(String name, Connection con) throws SQLException {
|
||||||
|
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
|
pst.setString(1, name);
|
||||||
|
try (ResultSet rs = pst.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return OptionalInt.of(rs.getInt(col.ID));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return OptionalInt.empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,12 @@ public class MySqlExtensionsFactory {
|
|||||||
@Inject
|
@Inject
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link MySqlExtension} object according to the configured hash algorithm.
|
||||||
|
*
|
||||||
|
* @param columnsConfig the columns configuration
|
||||||
|
* @return the extension the MySQL data source should use
|
||||||
|
*/
|
||||||
public MySqlExtension buildExtension(Columns columnsConfig) {
|
public MySqlExtension buildExtension(Columns columnsConfig) {
|
||||||
HashAlgorithm hash = settings.getProperty(SecuritySettings.PASSWORD_HASH);
|
HashAlgorithm hash = settings.getProperty(SecuritySettings.PASSWORD_HASH);
|
||||||
switch (hash) {
|
switch (hash) {
|
||||||
@ -27,7 +33,7 @@ public class MySqlExtensionsFactory {
|
|||||||
case XFBCRYPT:
|
case XFBCRYPT:
|
||||||
return new XfBcryptExtension(settings, columnsConfig);
|
return new XfBcryptExtension(settings, columnsConfig);
|
||||||
default:
|
default:
|
||||||
return new NoOpExtension();
|
return new NoOpExtension(settings, columnsConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,14 @@
|
|||||||
package fr.xephi.authme.datasource.mysqlextensions;
|
package fr.xephi.authme.datasource.mysqlextensions;
|
||||||
|
|
||||||
|
import fr.xephi.authme.datasource.Columns;
|
||||||
|
import fr.xephi.authme.settings.Settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension implementation that does not do anything.
|
* Extension implementation that does not do anything.
|
||||||
*/
|
*/
|
||||||
class NoOpExtension extends MySqlExtension {
|
class NoOpExtension extends MySqlExtension {
|
||||||
|
|
||||||
|
NoOpExtension(Settings settings, Columns col) {
|
||||||
|
super(settings, col);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,42 +3,32 @@ package fr.xephi.authme.datasource.mysqlextensions;
|
|||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.datasource.Columns;
|
import fr.xephi.authme.datasource.Columns;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|
||||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extensions for phpBB when MySQL is used as data source.
|
* Extensions for phpBB when MySQL is used as data source.
|
||||||
*/
|
*/
|
||||||
class PhpBbExtension extends MySqlExtension {
|
class PhpBbExtension extends MySqlExtension {
|
||||||
|
|
||||||
private final Columns col;
|
|
||||||
private final String tableName;
|
|
||||||
private final String phpBbPrefix;
|
private final String phpBbPrefix;
|
||||||
private final int phpBbGroup;
|
private final int phpBbGroup;
|
||||||
|
|
||||||
PhpBbExtension(Settings settings, Columns col) {
|
PhpBbExtension(Settings settings, Columns col) {
|
||||||
this.col = col;
|
super(settings, col);
|
||||||
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
|
||||||
this.phpBbPrefix = settings.getProperty(HooksSettings.PHPBB_TABLE_PREFIX);
|
this.phpBbPrefix = settings.getProperty(HooksSettings.PHPBB_TABLE_PREFIX);
|
||||||
this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID);
|
this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
||||||
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
OptionalInt authId = retrieveIdFromTable(auth.getNickname(), con);
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
if (authId.isPresent()) {
|
||||||
pst.setString(1, auth.getNickname());
|
updateSpecificsOnSave(authId.getAsInt(), auth.getNickname(), con);
|
||||||
try (ResultSet rs = pst.executeQuery()) {
|
|
||||||
if (rs.next()) {
|
|
||||||
int id = rs.getInt(col.ID);
|
|
||||||
updateSpecificsOnSave(id, auth.getNickname(), con);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,105 +3,100 @@ package fr.xephi.authme.datasource.mysqlextensions;
|
|||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.datasource.Columns;
|
import fr.xephi.authme.datasource.Columns;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|
||||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MySQL extensions for Wordpress.
|
* MySQL extensions for Wordpress.
|
||||||
*/
|
*/
|
||||||
class WordpressExtension extends MySqlExtension {
|
class WordpressExtension extends MySqlExtension {
|
||||||
|
|
||||||
private final Columns col;
|
|
||||||
private final String tableName;
|
|
||||||
private final String wordpressPrefix;
|
private final String wordpressPrefix;
|
||||||
|
|
||||||
WordpressExtension(Settings settings, Columns col) {
|
WordpressExtension(Settings settings, Columns col) {
|
||||||
this.col = col;
|
super(settings, col);
|
||||||
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
|
||||||
this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX);
|
this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
||||||
try (PreparedStatement pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;")) {
|
OptionalInt authId = retrieveIdFromTable(auth.getNickname(), con);
|
||||||
pst.setString(1, auth.getNickname());
|
if (authId.isPresent()) {
|
||||||
try (ResultSet rs = pst.executeQuery()) {
|
saveSpecifics(auth, authId.getAsInt(), con);
|
||||||
if (rs.next()) {
|
}
|
||||||
int id = rs.getInt(col.ID);
|
}
|
||||||
String sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)";
|
|
||||||
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
|
|
||||||
// First Name
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "first_name");
|
|
||||||
pst2.setString(3, "");
|
|
||||||
pst2.addBatch();
|
|
||||||
// Last Name
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "last_name");
|
|
||||||
pst2.setString(3, "");
|
|
||||||
pst2.addBatch();
|
|
||||||
// Nick Name
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "nickname");
|
|
||||||
pst2.setString(3, auth.getNickname());
|
|
||||||
pst2.addBatch();
|
|
||||||
// Description
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "description");
|
|
||||||
pst2.setString(3, "");
|
|
||||||
pst2.addBatch();
|
|
||||||
// Rich_Editing
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "rich_editing");
|
|
||||||
pst2.setString(3, "true");
|
|
||||||
pst2.addBatch();
|
|
||||||
// Comments_Shortcuts
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "comment_shortcuts");
|
|
||||||
pst2.setString(3, "false");
|
|
||||||
pst2.addBatch();
|
|
||||||
// admin_color
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "admin_color");
|
|
||||||
pst2.setString(3, "fresh");
|
|
||||||
pst2.addBatch();
|
|
||||||
// use_ssl
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "use_ssl");
|
|
||||||
pst2.setString(3, "0");
|
|
||||||
pst2.addBatch();
|
|
||||||
// show_admin_bar_front
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "show_admin_bar_front");
|
|
||||||
pst2.setString(3, "true");
|
|
||||||
pst2.addBatch();
|
|
||||||
// wp_capabilities
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, wordpressPrefix + "capabilities");
|
|
||||||
pst2.setString(3, "a:1:{s:10:\"subscriber\";b:1;}");
|
|
||||||
pst2.addBatch();
|
|
||||||
// wp_user_level
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, wordpressPrefix + "user_level");
|
|
||||||
pst2.setString(3, "0");
|
|
||||||
pst2.addBatch();
|
|
||||||
// default_password_nag
|
|
||||||
pst2.setInt(1, id);
|
|
||||||
pst2.setString(2, "default_password_nag");
|
|
||||||
pst2.setString(3, "");
|
|
||||||
pst2.addBatch();
|
|
||||||
|
|
||||||
// Execute queries
|
private void saveSpecifics(PlayerAuth auth, int id, Connection con) throws SQLException {
|
||||||
pst2.executeBatch();
|
String sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)";
|
||||||
pst2.clearBatch();
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
}
|
// First Name
|
||||||
}
|
pst.setInt(1, id);
|
||||||
}
|
pst.setString(2, "first_name");
|
||||||
|
pst.setString(3, "");
|
||||||
|
pst.addBatch();
|
||||||
|
// Last Name
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "last_name");
|
||||||
|
pst.setString(3, "");
|
||||||
|
pst.addBatch();
|
||||||
|
// Nick Name
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "nickname");
|
||||||
|
pst.setString(3, auth.getNickname());
|
||||||
|
pst.addBatch();
|
||||||
|
// Description
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "description");
|
||||||
|
pst.setString(3, "");
|
||||||
|
pst.addBatch();
|
||||||
|
// Rich_Editing
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "rich_editing");
|
||||||
|
pst.setString(3, "true");
|
||||||
|
pst.addBatch();
|
||||||
|
// Comments_Shortcuts
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "comment_shortcuts");
|
||||||
|
pst.setString(3, "false");
|
||||||
|
pst.addBatch();
|
||||||
|
// admin_color
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "admin_color");
|
||||||
|
pst.setString(3, "fresh");
|
||||||
|
pst.addBatch();
|
||||||
|
// use_ssl
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "use_ssl");
|
||||||
|
pst.setString(3, "0");
|
||||||
|
pst.addBatch();
|
||||||
|
// show_admin_bar_front
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "show_admin_bar_front");
|
||||||
|
pst.setString(3, "true");
|
||||||
|
pst.addBatch();
|
||||||
|
// wp_capabilities
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, wordpressPrefix + "capabilities");
|
||||||
|
pst.setString(3, "a:1:{s:10:\"subscriber\";b:1;}");
|
||||||
|
pst.addBatch();
|
||||||
|
// wp_user_level
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, wordpressPrefix + "user_level");
|
||||||
|
pst.setString(3, "0");
|
||||||
|
pst.addBatch();
|
||||||
|
// default_password_nag
|
||||||
|
pst.setInt(1, id);
|
||||||
|
pst.setString(2, "default_password_nag");
|
||||||
|
pst.setString(3, "");
|
||||||
|
pst.addBatch();
|
||||||
|
|
||||||
|
// Execute queries
|
||||||
|
pst.executeBatch();
|
||||||
|
pst.clearBatch();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import fr.xephi.authme.datasource.Columns;
|
|||||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||||
import fr.xephi.authme.security.crypts.XfBCrypt;
|
import fr.xephi.authme.security.crypts.XfBCrypt;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|
||||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||||
|
|
||||||
import java.sql.Blob;
|
import java.sql.Blob;
|
||||||
@ -13,35 +12,27 @@ import java.sql.Connection;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension for XFBCRYPT.
|
* Extension for XFBCRYPT.
|
||||||
*/
|
*/
|
||||||
class XfBcryptExtension extends MySqlExtension {
|
class XfBcryptExtension extends MySqlExtension {
|
||||||
|
|
||||||
private final Columns col;
|
|
||||||
private final String tableName;
|
|
||||||
private final String xfPrefix;
|
private final String xfPrefix;
|
||||||
private final int xfGroup;
|
private final int xfGroup;
|
||||||
|
|
||||||
XfBcryptExtension(Settings settings, Columns col) {
|
XfBcryptExtension(Settings settings, Columns col) {
|
||||||
this.col = col;
|
super(settings, col);
|
||||||
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
|
||||||
this.xfPrefix = settings.getProperty(HooksSettings.XF_TABLE_PREFIX);
|
this.xfPrefix = settings.getProperty(HooksSettings.XF_TABLE_PREFIX);
|
||||||
this.xfGroup = settings.getProperty(HooksSettings.XF_ACTIVATED_GROUP_ID);
|
this.xfGroup = settings.getProperty(HooksSettings.XF_ACTIVATED_GROUP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
public void saveAuth(PlayerAuth auth, Connection con) throws SQLException {
|
||||||
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
OptionalInt authId = retrieveIdFromTable(auth.getNickname(), con);
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
if (authId.isPresent()) {
|
||||||
pst.setString(1, auth.getNickname());
|
updateXenforoTablesOnSave(auth, authId.getAsInt(), con);
|
||||||
try (ResultSet rs = pst.executeQuery()) {
|
|
||||||
if (rs.next()) {
|
|
||||||
int id = rs.getInt(col.ID);
|
|
||||||
updateXenforoTablesOnSave(auth, id, con);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,50 +101,39 @@ class XfBcryptExtension extends MySqlExtension {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void changePassword(String user, HashedPassword password, Connection con) throws SQLException {
|
public void changePassword(String user, HashedPassword password, Connection con) throws SQLException {
|
||||||
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
OptionalInt authId = retrieveIdFromTable(user, con);
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
if (authId.isPresent()) {
|
||||||
pst.setString(1, user);
|
final int id = authId.getAsInt();
|
||||||
try (ResultSet rs = pst.executeQuery()) {
|
// Insert password in the correct table
|
||||||
if (rs.next()) {
|
String sql = "UPDATE " + xfPrefix + "user_authenticate SET data=? WHERE " + col.ID + "=?;";
|
||||||
int id = rs.getInt(col.ID);
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
// Insert password in the correct table
|
String serializedHash = XfBCrypt.serializeHash(password.getHash());
|
||||||
// TODO #1255: Close these statements with try-catch
|
byte[] bytes = serializedHash.getBytes();
|
||||||
sql = "UPDATE " + xfPrefix + "user_authenticate SET data=? WHERE " + col.ID + "=?;";
|
Blob blob = con.createBlob();
|
||||||
PreparedStatement pst2 = con.prepareStatement(sql);
|
blob.setBytes(1, bytes);
|
||||||
String serializedHash = XfBCrypt.serializeHash(password.getHash());
|
pst.setBlob(1, blob);
|
||||||
byte[] bytes = serializedHash.getBytes();
|
pst.setInt(2, id);
|
||||||
Blob blob = con.createBlob();
|
pst.executeUpdate();
|
||||||
blob.setBytes(1, bytes);
|
}
|
||||||
pst2.setBlob(1, blob);
|
|
||||||
pst2.setInt(2, id);
|
// ...
|
||||||
pst2.executeUpdate();
|
sql = "UPDATE " + xfPrefix + "user_authenticate SET scheme_class=? WHERE " + col.ID + "=?;";
|
||||||
pst2.close();
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
// ...
|
pst.setString(1, XfBCrypt.SCHEME_CLASS);
|
||||||
sql = "UPDATE " + xfPrefix + "user_authenticate SET scheme_class=? WHERE " + col.ID + "=?;";
|
pst.setInt(2, id);
|
||||||
pst2 = con.prepareStatement(sql);
|
pst.executeUpdate();
|
||||||
pst2.setString(1, XfBCrypt.SCHEME_CLASS);
|
|
||||||
pst2.setInt(2, id);
|
|
||||||
pst2.executeUpdate();
|
|
||||||
pst2.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeAuth(String user, Connection con) throws SQLException {
|
public void removeAuth(String user, Connection con) throws SQLException {
|
||||||
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
OptionalInt authId = retrieveIdFromTable(user, con);
|
||||||
try (PreparedStatement xfSelect = con.prepareStatement(sql)) {
|
if (authId.isPresent()) {
|
||||||
xfSelect.setString(1, user);
|
String sql = "DELETE FROM " + xfPrefix + "user_authenticate WHERE " + col.ID + "=?;";
|
||||||
try (ResultSet rs = xfSelect.executeQuery()) {
|
try (PreparedStatement xfDelete = con.prepareStatement(sql)) {
|
||||||
if (rs.next()) {
|
xfDelete.setInt(1, authId.getAsInt());
|
||||||
int id = rs.getInt(col.ID);
|
xfDelete.executeUpdate();
|
||||||
sql = "DELETE FROM " + xfPrefix + "user_authenticate WHERE " + col.ID + "=?;";
|
|
||||||
try (PreparedStatement xfDelete = con.prepareStatement(sql)) {
|
|
||||||
xfDelete.setInt(1, id);
|
|
||||||
xfDelete.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableList;
|
|||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import fr.xephi.authme.datasource.Columns;
|
import fr.xephi.authme.datasource.Columns;
|
||||||
|
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||||
import fr.xephi.authme.initialization.HasCleanup;
|
import fr.xephi.authme.initialization.HasCleanup;
|
||||||
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
||||||
import fr.xephi.authme.security.crypts.Whirlpool;
|
import fr.xephi.authme.security.crypts.Whirlpool;
|
||||||
@ -55,6 +56,7 @@ public class ClassesConsistencyTest {
|
|||||||
/** Classes excluded from the field visibility test. */
|
/** Classes excluded from the field visibility test. */
|
||||||
private static final Set<Class<?>> CLASSES_EXCLUDED_FROM_VISIBILITY_TEST = ImmutableSet.of(
|
private static final Set<Class<?>> CLASSES_EXCLUDED_FROM_VISIBILITY_TEST = ImmutableSet.of(
|
||||||
Whirlpool.class, // not our implementation, so we don't touch it
|
Whirlpool.class, // not our implementation, so we don't touch it
|
||||||
|
MySqlExtension.class, // has immutable protected fields used by all children
|
||||||
Columns.class // uses non-static String constants, which is safe
|
Columns.class // uses non-static String constants, which is safe
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user