add exception to datasource method signature.

This commit is contained in:
DNx5 2015-11-03 12:38:24 +07:00
parent 1562cb7615
commit ce432aa25a
10 changed files with 154 additions and 163 deletions

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.command.executable.authme;
import java.util.List;
import fr.xephi.authme.ConsoleLogger;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -79,11 +80,20 @@ public class AccountsCommand extends ExecutableCommand {
});
return true;
} else {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
List<String> accountList;
try {
accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.writeStackTrace(e);
m.send(sender, "error");
return;
}
StringBuilder message = new StringBuilder("[AuthMe] ");
List<String> accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
if (accountList == null || accountList.isEmpty()) {
sender.sendMessage("[AuthMe] This IP does not exist in the database");
return;

View File

@ -1,27 +1,26 @@
package fr.xephi.authme.command.executable.email;
import java.util.Arrays;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Arrays;
public class ChangeEmailCommand extends ExecutableCommand {
/**
* Execute the command.
*
* @param sender The command sender.
* @param sender The command sender.
* @param commandReference The command reference.
* @param commandArguments The command arguments.
*
* @return True if the command was executed successfully, false otherwise.
*/
@Override
@ -37,7 +36,7 @@ public class ChangeEmailCommand extends ExecutableCommand {
String playerMailNew = commandArguments.get(1);
// Make sure the current command executor is a player
if(!(sender instanceof Player)) {
if (!(sender instanceof Player)) {
return true;
}
@ -46,44 +45,51 @@ public class ChangeEmailCommand extends ExecutableCommand {
final String playerName = player.getName();
// Command logic
if (Settings.getmaxRegPerEmail > 0) {
if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMailNew).size() >= Settings.getmaxRegPerEmail) {
m.send(player, "max_reg");
return true;
}
}
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName);
if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) {
m.send(player, "usage_email_add");
return true;
}
if (!playerMailOld.equals(auth.getEmail())) {
m.send(player, "old_email_invalid");
return true;
}
if (!Settings.isEmailCorrect(playerMailNew)) {
m.send(player, "new_email_invalid");
return true;
}
auth.setEmail(playerMailNew);
if (!plugin.database.updateEmail(auth)) {
m.send(player, "error");
return true;
}
PlayerCache.getInstance().updatePlayer(auth);
m.send(player, "email_changed");
player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail());
} else if (PlayerCache.getInstance().isAuthenticated(playerName)) {
m.send(player, "email_confirm");
} else {
if (!plugin.database.isAuthAvailable(playerName)) {
m.send(player, "login_msg");
} else {
m.send(player, "reg_email_msg");
}
}
return true;
try {
if (Settings.getmaxRegPerEmail > 0) {
if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMailNew).size() >= Settings.getmaxRegPerEmail) {
m.send(player, "max_reg");
return true;
}
}
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName);
if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) {
m.send(player, "usage_email_add");
return true;
}
if (!playerMailOld.equals(auth.getEmail())) {
m.send(player, "old_email_invalid");
return true;
}
if (!Settings.isEmailCorrect(playerMailNew)) {
m.send(player, "new_email_invalid");
return true;
}
auth.setEmail(playerMailNew);
if (!plugin.database.updateEmail(auth)) {
m.send(player, "error");
return true;
}
PlayerCache.getInstance().updatePlayer(auth);
m.send(player, "email_changed");
player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail());
} else if (PlayerCache.getInstance().isAuthenticated(playerName)) {
m.send(player, "email_confirm");
} else {
if (!plugin.database.isAuthAvailable(playerName)) {
m.send(player, "login_msg");
} else {
m.send(player, "reg_email_msg");
}
}
return true;
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.writeStackTrace(e);
m.send(sender, "error");
return false;
}
}
}

View File

@ -194,7 +194,7 @@ public class AdminCommand implements CommandExecutor {
return true;
} else {
final String[] arguments = args;
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
StringBuilder message = new StringBuilder("[AuthMe] ");
@ -202,7 +202,15 @@ public class AdminCommand implements CommandExecutor {
sender.sendMessage("[AuthMe] Please put a valid IP");
return;
}
List<String> accountList = plugin.database.getAllAuthsByIp(arguments[1]);
List<String> accountList = null;
try {
accountList = plugin.database.getAllAuthsByIp(arguments[1]);
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.writeStackTrace(e);
m.send(sender, "error");
return;
}
if (accountList == null || accountList.isEmpty()) {
sender.sendMessage("[AuthMe] This IP does not exist in the database");
return;

View File

@ -1,18 +1,18 @@
package fr.xephi.authme.datasource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CacheDataSource implements DataSource {
@ -281,25 +281,21 @@ public class CacheDataSource implements DataSource {
}
@Override
public synchronized List<String> getAllAuthsByIp(String ip) {
List<String> result = new ArrayList<>();
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
PlayerAuth p = stringPlayerAuthEntry.getValue();
if (p.getIp().equals(ip))
result.add(p.getNickname());
}
return result;
public synchronized List<String> getAllAuthsByIp(final String ip) throws Exception {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return source.getAllAuthsByIp(ip);
}
}).get();
}
@Override
public synchronized List<String> getAllAuthsByEmail(String email) {
List<String> result = new ArrayList<>();
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
PlayerAuth p = stringPlayerAuthEntry.getValue();
if (p.getEmail().equals(email))
result.add(p.getNickname());
}
return result;
public synchronized List<String> getAllAuthsByEmail(final String email) throws Exception {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return source.getAllAuthsByEmail(email);
}
}).get();
}
@Override

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.datasource;
import java.sql.SQLException;
import java.util.List;
import fr.xephi.authme.cache.auth.PlayerAuth;
@ -35,9 +36,9 @@ public interface DataSource {
List<String> getAllAuthsByName(PlayerAuth auth);
List<String> getAllAuthsByIp(String ip);
List<String> getAllAuthsByIp(String ip) throws Exception;
List<String> getAllAuthsByEmail(String email);
List<String> getAllAuthsByEmail(String email) throws Exception;
boolean updateEmail(PlayerAuth auth);

View File

@ -751,6 +751,7 @@ public class MySQL implements DataSource {
o.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
}
@ -808,14 +809,13 @@ public class MySQL implements DataSource {
}
@Override
public synchronized List<String> getAllAuthsByEmail(String email) {
Connection con = null;
public synchronized List<String> getAllAuthsByEmail(String email) throws SQLException {
final Connection con = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countEmail = new ArrayList<>();
try {
if ((con = getConnection()) == null)
return countEmail;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
pst.setString(1, email);
rs = pst.executeQuery();
@ -823,9 +823,6 @@ public class MySQL implements DataSource {
countEmail.add(rs.getString(columnName));
}
return countEmail;
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
return new ArrayList<>();
} finally {
close(rs);
close(pst);

View File

@ -472,13 +472,12 @@ public class SQLite_HIKARI implements DataSource {
}
@Override
public List<String> getAllAuthsByIp(String ip) {
Connection con = null;
public List<String> getAllAuthsByIp(String ip) throws SQLException {
final Connection con = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countIp = new ArrayList<>();
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
pst.setString(1, ip);
rs = pst.executeQuery();
@ -486,11 +485,6 @@ public class SQLite_HIKARI implements DataSource {
countIp.add(rs.getString(columnName));
}
return countIp;
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
return new ArrayList<>();
} catch (NullPointerException npe) {
return new ArrayList<>();
} finally {
close(rs);
close(pst);
@ -499,13 +493,12 @@ public class SQLite_HIKARI implements DataSource {
}
@Override
public List<String> getAllAuthsByEmail(String email) {
Connection con = null;
public List<String> getAllAuthsByEmail(String email) throws SQLException {
final Connection con = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countEmail = new ArrayList<>();
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
pst.setString(1, email);
rs = pst.executeQuery();
@ -513,11 +506,6 @@ public class SQLite_HIKARI implements DataSource {
countEmail.add(rs.getString(columnName));
}
return countEmail;
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
return new ArrayList<>();
} catch (NullPointerException npe) {
return new ArrayList<>();
} finally {
close(rs);
close(pst);

View File

@ -9,7 +9,7 @@ import fr.xephi.authme.process.join.AsyncronousJoin;
import fr.xephi.authme.process.login.AsyncronousLogin;
import fr.xephi.authme.process.logout.AsyncronousLogout;
import fr.xephi.authme.process.quit.AsyncronousQuit;
import fr.xephi.authme.process.register.AsyncronousRegister;
import fr.xephi.authme.process.register.AsyncRegister;
import fr.xephi.authme.process.unregister.AsyncronousUnregister;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings;
@ -46,7 +46,7 @@ public class Management {
@Override
public void run() {
new AsyncronousRegister(player, password, email, plugin, plugin.database).process();
new AsyncRegister(player, password, email, plugin, plugin.database).process();
}
});
}

View File

@ -1,10 +1,5 @@
package fr.xephi.authme.process.register;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
@ -13,95 +8,90 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import org.bukkit.entity.Player;
public class AsyncronousRegister {
import java.security.NoSuchAlgorithmException;
import java.util.Date;
public class AsyncRegister {
protected Player player;
protected String name;
protected String password;
protected String email = "";
protected boolean allowRegister;
private AuthMe plugin;
private DataSource database;
private Messages m = Messages.getInstance();
public AsyncronousRegister(Player player, String password, String email,
AuthMe plugin, DataSource data) {
public AsyncRegister(Player player, String password, String email,
AuthMe plugin, DataSource data) {
this.player = player;
this.password = password;
name = player.getName().toLowerCase();
this.email = email;
this.plugin = plugin;
this.database = data;
this.allowRegister = true;
}
protected String getIp() {
return plugin.getIP(player);
}
protected void preRegister() {
protected boolean preRegisterCheck() throws Exception {
String lowpass = password.toLowerCase();
if (PlayerCache.getInstance().isAuthenticated(name)) {
m.send(player, "logged_in");
allowRegister = false;
}
else if (!Settings.isRegistrationEnabled) {
return false;
} else if (!Settings.isRegistrationEnabled) {
m.send(player, "reg_disabled");
allowRegister = false;
}
else if (lowpass.contains("delete") || lowpass.contains("where") || lowpass.contains("insert") || lowpass.contains("modify") || lowpass.contains("from") || lowpass.contains("select") || lowpass.contains(";") || lowpass.contains("null") || !lowpass.matches(Settings.getPassRegex)) {
return false;
} else if (lowpass.contains("delete") || lowpass.contains("where") || lowpass.contains("insert") || lowpass.contains("modify") || lowpass.contains("from") || lowpass.contains("select") || lowpass.contains(";") || lowpass.contains("null") || !lowpass.matches(Settings.getPassRegex)) {
m.send(player, "password_error");
allowRegister = false;
}
else if (lowpass.equalsIgnoreCase(player.getName())) {
return false;
} else if (lowpass.equalsIgnoreCase(player.getName())) {
m.send(player, "password_error_nick");
allowRegister = false;
}
else if (password.length() < Settings.getPasswordMinLen || password.length() > Settings.passwordMaxLength) {
return false;
} else if (password.length() < Settings.getPasswordMinLen || password.length() > Settings.passwordMaxLength) {
m.send(player, "pass_len");
allowRegister = false;
}
else if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(password.toLowerCase())) {
return false;
} else if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(password.toLowerCase())) {
m.send(player, "password_error_unsafe");
allowRegister = false;
return false;
} else if (database.isAuthAvailable(name)) {
m.send(player, "user_regged");
allowRegister = false;
}
else if (Settings.getmaxRegPerIp > 0) {
return false;
} else if (Settings.getmaxRegPerIp > 0) {
if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByIp(getIp()).size() >= Settings.getmaxRegPerIp && !getIp().equalsIgnoreCase("127.0.0.1") && !getIp().equalsIgnoreCase("localhost")) {
m.send(player, "max_reg");
allowRegister = false;
return false;
}
}
return true;
}
public void process() {
preRegister();
if (!allowRegister)
return;
if (!email.isEmpty() && !email.equals("")) {
if (Settings.getmaxRegPerEmail > 0) {
if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByEmail(email).size() >= Settings.getmaxRegPerEmail) {
m.send(player, "max_reg");
return;
try {
if (!preRegisterCheck())
return;
if (!email.isEmpty() && !email.equals("")) {
if (Settings.getmaxRegPerEmail > 0) {
if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByEmail(email).size() >= Settings.getmaxRegPerEmail) {
m.send(player, "max_reg");
return;
}
}
emailRegister();
return;
}
emailRegister();
return;
passwordRegister();
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.writeStackTrace(e);
m.send(player, "error");
}
passwordRegister();
}
protected void emailRegister() {
protected void emailRegister() throws Exception {
if (Settings.getmaxRegPerEmail > 0) {
if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByEmail(email).size() >= Settings.getmaxRegPerEmail) {
m.send(player, "max_reg");
@ -109,14 +99,8 @@ public class AsyncronousRegister {
}
}
PlayerAuth auth;
try {
final String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, password, name);
auth = new PlayerAuth(name, hashnew, getIp(), 0, (int) player.getLocation().getX(), (int) player.getLocation().getY(), (int) player.getLocation().getZ(), player.getLocation().getWorld().getName(), email, player.getName());
} catch (NoSuchAlgorithmException e) {
ConsoleLogger.showError(e.getMessage());
m.send(player, "error");
return;
}
final String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, password, name);
auth = new PlayerAuth(name, hashnew, getIp(), 0, (int) player.getLocation().getX(), (int) player.getLocation().getY(), (int) player.getLocation().getZ(), player.getLocation().getWorld().getName(), email, player.getName());
if (PasswordSecurity.userSalt.containsKey(name)) {
auth.setSalt(PasswordSecurity.userSalt.get(name));
}
@ -124,8 +108,9 @@ public class AsyncronousRegister {
database.updateEmail(auth);
database.updateSession(auth);
plugin.mail.main(auth, password);
ProcessSyncronousEmailRegister syncronous = new ProcessSyncronousEmailRegister(player, plugin);
ProcessSyncEmailRegister syncronous = new ProcessSyncEmailRegister(player, plugin);
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, syncronous);
}
protected void passwordRegister() {

View File

@ -14,14 +14,14 @@ import fr.xephi.authme.task.MessageTask;
import fr.xephi.authme.task.TimeoutTask;
import fr.xephi.authme.util.Utils;
public class ProcessSyncronousEmailRegister implements Runnable {
public class ProcessSyncEmailRegister implements Runnable {
protected Player player;
protected String name;
private AuthMe plugin;
private Messages m = Messages.getInstance();
public ProcessSyncronousEmailRegister(Player player, AuthMe plugin) {
public ProcessSyncEmailRegister(Player player, AuthMe plugin) {
this.player = player;
this.name = player.getName().toLowerCase();
this.plugin = plugin;