mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-27 02:21:30 +01:00
Refactored SecurityTable#getUsers to a query
This commit is contained in:
parent
ad6cb62e61
commit
2256f4d718
@ -17,6 +17,7 @@
|
||||
package com.djrapitops.plan.command.commands.webuser;
|
||||
|
||||
import com.djrapitops.plan.data.WebUser;
|
||||
import com.djrapitops.plan.db.sql.queries.batch.LargeFetchQueries;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
|
||||
@ -24,7 +25,6 @@ import com.djrapitops.plan.system.locale.lang.CommandLang;
|
||||
import com.djrapitops.plan.system.locale.lang.ManageLang;
|
||||
import com.djrapitops.plan.system.processing.Processing;
|
||||
import com.djrapitops.plan.system.settings.Permissions;
|
||||
import com.djrapitops.plan.utilities.comparators.WebUserComparator;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
import com.djrapitops.plugin.command.Sender;
|
||||
@ -69,8 +69,7 @@ public class WebListUsersCommand extends CommandNode {
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
processing.submitNonCritical(() -> {
|
||||
try {
|
||||
List<WebUser> users = dbSystem.getDatabase().fetch().getWebUsers();
|
||||
users.sort(new WebUserComparator());
|
||||
List<WebUser> users = dbSystem.getDatabase().query(LargeFetchQueries.fetchAllPlanWebUsers());
|
||||
sender.sendMessage(locale.getString(CommandLang.HEADER_WEB_USERS, users.size()));
|
||||
for (WebUser user : users) {
|
||||
sender.sendMessage(locale.getString(CommandLang.WEB_USER_LIST, user.getName(), user.getPermLevel()));
|
||||
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.db.sql.queries.batch;
|
||||
|
||||
import com.djrapitops.plan.data.WebUser;
|
||||
import com.djrapitops.plan.data.container.GeoInfo;
|
||||
import com.djrapitops.plan.data.container.Ping;
|
||||
import com.djrapitops.plan.data.container.PlayerKill;
|
||||
@ -256,4 +257,27 @@ public class LargeFetchQueries {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Query database for all Plan WebUsers.
|
||||
*
|
||||
* @return Set of Plan WebUsers.
|
||||
*/
|
||||
public static Query<List<WebUser>> fetchAllPlanWebUsers() {
|
||||
String sql = "SELECT * FROM " + SecurityTable.TABLE_NAME + " ORDER BY " + SecurityTable.Col.PERMISSION_LEVEL + " ASC";
|
||||
|
||||
return new QueryAllStatement<List<WebUser>>(sql, 5000) {
|
||||
@Override
|
||||
public List<WebUser> processResults(ResultSet set) throws SQLException {
|
||||
List<WebUser> list = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String user = set.getString(SecurityTable.Col.USERNAME.get());
|
||||
String saltedPassHash = set.getString(SecurityTable.Col.SALT_PASSWORD_HASH.get());
|
||||
int permissionLevel = set.getInt(SecurityTable.Col.PERMISSION_LEVEL.get());
|
||||
WebUser info = new WebUser(user, saltedPassHash, permissionLevel);
|
||||
list.add(info);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -20,7 +20,6 @@ import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.data.WebUser;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
import com.djrapitops.plan.db.access.QueryAllStatement;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.parsing.*;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
@ -28,7 +27,6 @@ import com.djrapitops.plugin.utilities.Verify;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -43,8 +41,10 @@ import java.util.List;
|
||||
*/
|
||||
public class SecurityTable extends Table {
|
||||
|
||||
public static final String TABLE_NAME = "plan_security";
|
||||
|
||||
public SecurityTable(SQLDB db) {
|
||||
super("plan_security", db);
|
||||
super(TABLE_NAME, db);
|
||||
insertStatement = Insert.values(tableName,
|
||||
Col.USERNAME,
|
||||
Col.SALT_PASSWORD_HASH,
|
||||
@ -114,25 +114,6 @@ public class SecurityTable extends Table {
|
||||
return getWebUser(user) != null;
|
||||
}
|
||||
|
||||
public List<WebUser> getUsers() {
|
||||
String sql = Select.all(tableName).toString();
|
||||
|
||||
return query(new QueryAllStatement<List<WebUser>>(sql, 5000) {
|
||||
@Override
|
||||
public List<WebUser> processResults(ResultSet set) throws SQLException {
|
||||
List<WebUser> list = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String user = set.getString(Col.USERNAME.get());
|
||||
String saltedPassHash = set.getString(Col.SALT_PASSWORD_HASH.get());
|
||||
int permissionLevel = set.getInt(Col.PERMISSION_LEVEL.get());
|
||||
WebUser info = new WebUser(user, saltedPassHash, permissionLevel);
|
||||
list.add(info);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
USERNAME("username"),
|
||||
SALT_PASSWORD_HASH("salted_pass_hash"),
|
||||
|
@ -129,7 +129,7 @@ public class BatchOperationTable extends Table {
|
||||
if (toDB.equals(this)) {
|
||||
return;
|
||||
}
|
||||
toDB.db.getSecurityTable().addUsers(db.getSecurityTable().getUsers());
|
||||
toDB.db.getSecurityTable().addUsers(db.query(LargeFetchQueries.fetchAllPlanWebUsers()));
|
||||
}
|
||||
|
||||
public void copyServers(BatchOperationTable toDB) {
|
||||
|
@ -469,7 +469,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
|
||||
|
||||
@Override
|
||||
public List<WebUser> getWebUsers() {
|
||||
return securityTable.getUsers();
|
||||
return new ArrayList<>(db.query(LargeFetchQueries.fetchAllPlanWebUsers()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -312,13 +312,13 @@ public abstract class CommonDBTest {
|
||||
assertFalse(securityTable.userExists("NotExist"));
|
||||
assertNull(securityTable.getWebUser("NotExist"));
|
||||
|
||||
assertEquals(1, securityTable.getUsers().size());
|
||||
assertEquals(1, db.query(LargeFetchQueries.fetchAllPlanWebUsers()).size());
|
||||
|
||||
securityTable.removeUser("Test");
|
||||
assertFalse(securityTable.userExists("Test"));
|
||||
assertNull(securityTable.getWebUser("Test"));
|
||||
|
||||
assertEquals(0, securityTable.getUsers().size());
|
||||
assertEquals(0, db.query(LargeFetchQueries.fetchAllPlanWebUsers()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -574,7 +574,7 @@ public abstract class CommonDBTest {
|
||||
assertTrue(tpsTable.getTPSData().isEmpty());
|
||||
assertTrue(db.getServerTable().getBukkitServers().isEmpty());
|
||||
assertTrue(db.query(LargeFetchQueries.fetchAllPingData()).isEmpty());
|
||||
assertTrue(securityTable.getUsers().isEmpty());
|
||||
assertTrue(db.query(LargeFetchQueries.fetchAllPlanWebUsers()).isEmpty());
|
||||
}
|
||||
|
||||
private void saveAllData(SQLDB database) throws NoSuchAlgorithmException {
|
||||
@ -737,7 +737,7 @@ public abstract class CommonDBTest {
|
||||
assertFalse(backup.getWorldTable().getAllWorlds().isEmpty());
|
||||
assertFalse(tpsTable.getTPSData().isEmpty());
|
||||
assertFalse(backup.getServerTable().getBukkitServers().isEmpty());
|
||||
assertFalse(securityTable.getUsers().isEmpty());
|
||||
assertFalse(db.query(LargeFetchQueries.fetchAllPlanWebUsers()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user