Refactored SecurityTable#addNewUser to a transaction

This commit is contained in:
Rsl1122 2019-02-02 11:56:30 +02:00
parent 2bfbb60f1e
commit 237168f2eb
7 changed files with 58 additions and 34 deletions

View File

@ -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) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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());
}
});
}
}

View File

@ -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.
* <p>
@ -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);
}
});
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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<WebUser> 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) {

View File

@ -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