Refactored SecurityTable#getWebUser into a query

- Removes CheckOperations#doesWebUserExists
- Removed usages of FetchOperations#getWebUser
- Split WebUser functionality test into 3 tests
This commit is contained in:
Rsl1122 2019-01-25 20:13:37 +02:00
parent 46a41de81d
commit 0ae37f017a
12 changed files with 58 additions and 62 deletions

View File

@ -125,7 +125,7 @@ public class AnalyzeCommand extends CommandNode {
private void sendWebUserNotificationIfNecessary(Sender sender) {
if (webServer.isAuthRequired() &&
CommandUtils.isPlayer(sender) &&
!dbSystem.getDatabase().check().doesWebUserExists(sender.getName())) {
!dbSystem.getDatabase().query(OptionalFetchQueries.webUser(sender.getName())).isPresent()) {
sender.sendMessage("§e" + locale.getString(CommandLang.NO_WEB_USER_NOTIFY));
}
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.command.commands;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
import com.djrapitops.plan.system.locale.Locale;
@ -118,7 +119,7 @@ public class InspectCommand extends CommandNode {
private void checkWebUserAndNotify(Sender sender) {
if (CommandUtils.isPlayer(sender) && webServer.isAuthRequired()) {
boolean senderHasWebUser = dbSystem.getDatabase().check().doesWebUserExists(sender.getName());
boolean senderHasWebUser = dbSystem.getDatabase().query(OptionalFetchQueries.webUser(sender.getName())).isPresent();
if (!senderHasWebUser) {
sender.sendMessage("§e" + locale.getString(CommandLang.NO_WEB_USER_NOTIFY));

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.command.commands;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
@ -147,7 +148,7 @@ public class RegisterCommand extends CommandNode {
String userName = webUser.getName();
try {
Database database = dbSystem.getDatabase();
boolean userExists = database.check().doesWebUserExists(userName);
boolean userExists = database.query(OptionalFetchQueries.webUser(userName)).isPresent();
if (userExists) {
sender.sendMessage(locale.getString(CommandLang.FAIL_WEB_USER_EXISTS));
return;

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.command.commands.webuser;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
@ -35,6 +36,7 @@ import com.djrapitops.plugin.utilities.Verify;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Optional;
/**
* Subcommand for checking WebUser permission level.
@ -77,11 +79,12 @@ public class WebCheckCommand extends CommandNode {
processing.submitNonCritical(() -> {
try {
Database db = dbSystem.getDatabase();
if (!db.check().doesWebUserExists(user)) {
Optional<WebUser> found = db.query(OptionalFetchQueries.webUser(user));
if (!found.isPresent()) {
sender.sendMessage(locale.getString(CommandLang.FAIL_WEB_USER_NOT_EXISTS));
return;
}
WebUser info = db.fetch().getWebUser(user);
WebUser info = found.get();
sender.sendMessage(locale.getString(CommandLang.WEB_USER_LIST, info.getName(), info.getPermLevel()));
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);

View File

@ -16,7 +16,9 @@
*/
package com.djrapitops.plan.command.commands.webuser;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
@ -34,6 +36,7 @@ import com.djrapitops.plugin.utilities.Verify;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Optional;
/**
* Subcommand for deleting a WebUser.
@ -76,7 +79,8 @@ public class WebDeleteCommand extends CommandNode {
processing.submitNonCritical(() -> {
try {
Database db = dbSystem.getDatabase();
if (!db.check().doesWebUserExists(user)) {
Optional<WebUser> found = db.query(OptionalFetchQueries.webUser(user));
if (!found.isPresent()) {
sender.sendMessage("§c[Plan] User Doesn't exist.");
return;
}

View File

@ -16,8 +16,10 @@
*/
package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.SecurityTable;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import com.djrapitops.plan.system.info.server.Server;
@ -96,4 +98,25 @@ public class OptionalFetchQueries {
};
}
public static Query<Optional<WebUser>> webUser(String called) {
String sql = "SELECT * FROM " + SecurityTable.TABLE_NAME +
" WHERE " + SecurityTable.USERNAME + "=? LIMIT 1";
return new QueryStatement<Optional<WebUser>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, called);
}
@Override
public Optional<WebUser> processResults(ResultSet set) throws SQLException {
if (set.next()) {
String saltedPassHash = set.getString(SecurityTable.SALT_PASSWORD_HASH);
int permissionLevel = set.getInt(SecurityTable.PERMISSION_LEVEL);
return Optional.of(new WebUser(called, saltedPassHash, permissionLevel));
}
return Optional.empty();
}
};
}
}

View File

@ -20,14 +20,11 @@ import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Insert;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
@ -71,27 +68,6 @@ public class SecurityTable extends Table {
});
}
public WebUser getWebUser(String user) {
String sql = Select.all(tableName).where(USERNAME + "=?").toString();
return query(new QueryStatement<WebUser>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, user);
}
@Override
public WebUser processResults(ResultSet set) throws SQLException {
if (set.next()) {
String saltedPassHash = set.getString(SALT_PASSWORD_HASH);
int permissionLevel = set.getInt(PERMISSION_LEVEL);
return new WebUser(user, saltedPassHash, permissionLevel);
}
return null;
}
});
}
public void addNewUser(WebUser info) {
addNewUser(info.getName(), info.getSaltedPassHash(), info.getPermLevel());
}
@ -106,8 +82,4 @@ public class SecurityTable extends Table {
}
});
}
public boolean userExists(String user) {
return getWebUser(user) != null;
}
}

View File

@ -26,9 +26,6 @@ public interface CheckOperations {
@Deprecated
boolean isPlayerRegistered(UUID player, UUID server);
@Deprecated
boolean doesWebUserExists(String username);
@Deprecated
boolean isPlayerRegisteredOnThisServer(UUID player);

View File

@ -42,11 +42,6 @@ public class SQLCheckOps extends SQLOps implements CheckOperations {
return userInfoTable.isRegisteredOnThisServer(player);
}
@Override
public boolean doesWebUserExists(String username) {
return securityTable.userExists(username);
}
@Override
public boolean isServerInDatabase(UUID serverUUID) {
return serverTable.getServerID(serverUUID).isPresent();

View File

@ -338,7 +338,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public WebUser getWebUser(String username) {
return securityTable.getWebUser(username);
return db.query(OptionalFetchQueries.webUser(username)).orElse(null);
}
@Override

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.api.exceptions.WebUserAuthException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.utilities.Base64Util;
import com.djrapitops.plan.utilities.PassEncryptUtil;
@ -55,11 +56,8 @@ public class BasicAuthentication implements Authentication {
String passwordRaw = userInfo[1];
try {
if (!database.check().doesWebUserExists(user)) {
throw new WebUserAuthException(FailReason.USER_DOES_NOT_EXIST, user);
}
WebUser webUser = database.fetch().getWebUser(user);
WebUser webUser = database.query(OptionalFetchQueries.webUser(user))
.orElseThrow(() -> new WebUserAuthException(FailReason.USER_DOES_NOT_EXIST, user));
boolean correctPass = PassEncryptUtil.verifyPassword(passwordRaw, webUser.getSaltedPassHash());
if (!correctPass) {

View File

@ -36,6 +36,7 @@ import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.queries.LargeStoreQueries;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.db.tasks.CreateIndexTask;
import com.djrapitops.plan.system.PlanSystem;
@ -304,26 +305,27 @@ public abstract class CommonDBTest {
}
@Test
public void testSecurityTable() throws DBInitException {
SecurityTable securityTable = db.getSecurityTable();
public void webUserIsRegistered() throws DBInitException {
WebUser expected = new WebUser("Test", "RandomGarbageBlah", 0);
securityTable.addNewUser(expected);
db.getSecurityTable().addNewUser(expected);
commitTest();
assertTrue(securityTable.userExists("Test"));
WebUser test = securityTable.getWebUser("Test");
assertEquals(expected, test);
assertFalse(securityTable.userExists("NotExist"));
assertNull(securityTable.getWebUser("NotExist"));
Optional<WebUser> found = db.query(OptionalFetchQueries.webUser("Test"));
assertTrue(found.isPresent());
assertEquals(expected, found.get());
}
@Test
public void multipleWebUsersAreFetchedAppropriately() throws DBInitException {
webUserIsRegistered();
assertEquals(1, db.query(LargeFetchQueries.fetchAllPlanWebUsers()).size());
}
securityTable.removeUser("Test");
assertFalse(securityTable.userExists("Test"));
assertNull(securityTable.getWebUser("Test"));
assertEquals(0, db.query(LargeFetchQueries.fetchAllPlanWebUsers()).size());
@Test
public void webUserIsRemoved() throws DBInitException {
webUserIsRegistered();
db.getSecurityTable().removeUser("Test");
assertFalse(db.query(OptionalFetchQueries.webUser("Test")).isPresent());
}
@Test