From 237168f2eb5e414f7ff32af11f8e88def52a1b5d Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 2 Feb 2019 11:56:30 +0200 Subject: [PATCH] Refactored SecurityTable#addNewUser to a transaction --- .../command/commands/RegisterCommand.java | 3 +- .../RegisterWebUserTransaction.java | 50 +++++++++++++++++++ .../plan/db/sql/tables/SecurityTable.java | 19 ------- .../databases/operation/SaveOperations.java | 4 -- .../databases/sql/operation/SQLSaveOps.java | 6 --- .../com/djrapitops/plan/db/CommonDBTest.java | 5 +- .../webserver/HTTPSWebServerAuthTest.java | 5 +- 7 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/RegisterWebUserTransaction.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/command/commands/RegisterCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/command/commands/RegisterCommand.java index c60158aff..323382414 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/command/commands/RegisterCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/command/commands/RegisterCommand.java @@ -19,6 +19,7 @@ package com.djrapitops.plan.command.commands; import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.access.queries.OptionalFetchQueries; +import com.djrapitops.plan.db.access.transactions.RegisterWebUserTransaction; import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.lang.CmdHelpLang; @@ -153,7 +154,7 @@ public class RegisterCommand extends CommandNode { sender.sendMessage(locale.getString(CommandLang.FAIL_WEB_USER_EXISTS)); return; } - database.save().webUser(webUser); + database.executeTransaction(new RegisterWebUserTransaction(webUser)); sender.sendMessage(locale.getString(CommandLang.WEB_USER_REGISTER_SUCCESS, userName)); logger.info(locale.getString(CommandLang.WEB_USER_REGISTER_NOTIFY, userName, webUser.getPermLevel())); } catch (Exception e) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/RegisterWebUserTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/RegisterWebUserTransaction.java new file mode 100644 index 000000000..26b56e768 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/RegisterWebUserTransaction.java @@ -0,0 +1,50 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.db.access.transactions; + +import com.djrapitops.plan.data.WebUser; +import com.djrapitops.plan.db.access.ExecStatement; +import com.djrapitops.plan.db.sql.tables.SecurityTable; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * Transaction to save a new Plan {@link WebUser} to the database. + * + * @author Rsl1122 + */ +public class RegisterWebUserTransaction extends Transaction { + + private WebUser webUser; + + public RegisterWebUserTransaction(WebUser webUser) { + this.webUser = webUser; + } + + @Override + protected void performOperations() { + execute(new ExecStatement(SecurityTable.INSERT_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, webUser.getName()); + statement.setString(2, webUser.getSaltedPassHash()); + statement.setInt(3, webUser.getPermLevel()); + } + }); + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SecurityTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SecurityTable.java index 655cdb028..c1eb0f09a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SecurityTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SecurityTable.java @@ -19,14 +19,10 @@ package com.djrapitops.plan.db.sql.tables; 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.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.Insert; import com.djrapitops.plan.db.sql.parsing.Sql; -import java.sql.PreparedStatement; -import java.sql.SQLException; - /** * Table that is in charge of storing WebUser data. *

@@ -56,19 +52,4 @@ public class SecurityTable extends Table { .column(PERMISSION_LEVEL, Sql.INT).notNull() .toString(); } - - public void addNewUser(WebUser info) { - addNewUser(info.getName(), info.getSaltedPassHash(), info.getPermLevel()); - } - - public void addNewUser(String user, String saltPassHash, int permLevel) { - execute(new ExecStatement(INSERT_STATEMENT) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, user); - statement.setString(2, saltPassHash); - statement.setInt(3, permLevel); - } - }); - } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java index 33d0e8e75..75a48a266 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java @@ -16,7 +16,6 @@ */ package com.djrapitops.plan.system.database.databases.operation; -import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.container.GeoInfo; import com.djrapitops.plan.data.container.Session; import com.djrapitops.plan.data.container.TPS; @@ -88,9 +87,6 @@ public interface SaveOperations { @Deprecated void serverInfoForThisServer(Server server); - @Deprecated - void webUser(WebUser webUser); - @Deprecated void setAsUninstalled(UUID serverUUID); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java index 6843587d0..28b0b55fa 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java @@ -16,7 +16,6 @@ */ package com.djrapitops.plan.system.database.databases.sql.operation; -import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.container.*; import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.db.SQLDB; @@ -167,11 +166,6 @@ public class SQLSaveOps extends SQLOps implements SaveOperations { serverTable.saveCurrentServerInfo(server); } - @Override - public void webUser(WebUser webUser) { - securityTable.addNewUser(webUser); - } - @Override public void setAsUninstalled(UUID serverUUID) { serverTable.setAsUninstalled(serverUUID); diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java index 05748a3c2..f20cd1753 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java @@ -267,7 +267,7 @@ public abstract class CommonDBTest { @Test public void webUserIsRegistered() throws DBInitException { WebUser expected = new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0); - db.getSecurityTable().addNewUser(expected); + db.executeTransaction(new RegisterWebUserTransaction(expected)); commitTest(); Optional found = db.query(OptionalFetchQueries.fetchWebUser(TestConstants.PLAYER_ONE_NAME)); @@ -567,7 +567,8 @@ public abstract class CommonDBTest { Collections.singletonList(new DateObj<>(System.currentTimeMillis(), r.nextInt()))) ); - securityTable.addNewUser(new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0)); + WebUser webUser = new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0); + db.executeTransaction(new RegisterWebUserTransaction(webUser)); } void saveGeoInfo(UUID uuid, GeoInfo geoInfo) { diff --git a/Plan/common/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java b/Plan/common/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java index 482f3d9b7..2821cdbf7 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java @@ -18,6 +18,7 @@ package com.djrapitops.plan.system.webserver; import com.djrapitops.plan.api.exceptions.connection.*; import com.djrapitops.plan.data.WebUser; +import com.djrapitops.plan.db.access.transactions.RegisterWebUserTransaction; import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.paths.WebserverSettings; @@ -78,8 +79,8 @@ public class HTTPSWebServerAuthTest { system.enable(); - system.getDatabaseSystem().getDatabase().save() - .webUser(new WebUser("test", PassEncryptUtil.createHash("testPass"), 0)); + WebUser webUser = new WebUser("test", PassEncryptUtil.createHash("testPass"), 0); + system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(webUser)); } @AfterClass