Add IP address tracking for form submissions
Included IP and timestamp columns in the database schema to track form submissions. Updated methods to handle and store the IP information upon form submission. Adjusted related test cases to validate the new implementation.
This commit is contained in:
parent
fcda4c9e53
commit
ab74360571
|
|
@ -4,8 +4,11 @@
|
|||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Improve email handling with Mailable interface Added a new Mailable interface for extracting email-related data like receiver and subject. Updated StaffAppFormData and ContactFormData to implement this interface, and refactored MailForm to utilize these details. This enhances flexibility and decouples email details from form classes.">
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" afterDir="false" />
|
||||
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Update `toHtml()` method in `Form.java` Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments.">
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/controlers/FormRequestHandler.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/controlers/FormRequestHandler.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/database/Database.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/database/Database.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/StoreFormQuery.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/StoreFormQuery.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/test/java/TestForm.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/TestForm.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
|
@ -392,7 +395,7 @@
|
|||
<workItem from="1723242314809" duration="9799000" />
|
||||
<workItem from="1723307861750" duration="2179000" />
|
||||
<workItem from="1723392202182" duration="258000" />
|
||||
<workItem from="1723392471077" duration="905000" />
|
||||
<workItem from="1723392471077" duration="1301000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="Initial commit for site for forms">
|
||||
<option name="closed" value="true" />
|
||||
|
|
@ -674,7 +677,15 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1723393276632</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="36" />
|
||||
<task id="LOCAL-00036" summary="Update `toHtml()` method in `Form.java` Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments.">
|
||||
<option name="closed" value="true" />
|
||||
<created>1723393478027</created>
|
||||
<option name="number" value="00036" />
|
||||
<option name="presentableId" value="LOCAL-00036" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1723393478027</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="37" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
|
|
@ -692,7 +703,6 @@
|
|||
</option>
|
||||
</component>
|
||||
<component name="VcsManagerConfiguration">
|
||||
<MESSAGE value="Enhance logging for mail verification details Updated logging to provide more detailed information about user credentials and email actions. This helps in debugging and monitoring mail verification processes more effectively. Adjusted trace messages to include username and password for better traceability." />
|
||||
<MESSAGE value="Update URI to point to Discord bot endpoint Changed the URI in VerifyController to use the Discord bot's endpoint for form submission. This adjustment is part of the process to route form data correctly through the specified internal service." />
|
||||
<MESSAGE value="Rename StoreFormQuery package to forms.form Updated the package name for consistency and better categorization. Adjusted imports in related test files to reflect the new package structure." />
|
||||
<MESSAGE value="Refactor HTML generation in ContactFormData Extract the HTML generation logic to a reusable method in the Form class. This change reduces code duplication and enhances maintainability by centralizing the table generation functionality." />
|
||||
|
|
@ -717,7 +727,8 @@
|
|||
<MESSAGE value="Add JSON response handling for form activity check Refactor `FormActiveController` to return JSON responses using `BooleanResponse`. Introduce exception handling for JSON processing errors and update `FormActiveData` to allow object initialization without parameters." />
|
||||
<MESSAGE value="Add branch condition to Discord notification stage The Discord notification stage now runs only on 'main' or 'master' branches. This helps to avoid unnecessary notifications for feature or bugfix branches." />
|
||||
<MESSAGE value="Improve email handling with Mailable interface Added a new Mailable interface for extracting email-related data like receiver and subject. Updated StaffAppFormData and ContactFormData to implement this interface, and refactored MailForm to utilize these details. This enhances flexibility and decouples email details from form classes." />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Improve email handling with Mailable interface Added a new Mailable interface for extracting email-related data like receiver and subject. Updated StaffAppFormData and ContactFormData to implement this interface, and refactored MailForm to utilize these details. This enhances flexibility and decouples email details from form classes." />
|
||||
<MESSAGE value="Update `toHtml()` method in `Form.java` Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments." />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Update `toHtml()` method in `Form.java` Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments." />
|
||||
</component>
|
||||
<component name="XSLT-Support.FileAssociations.UIState">
|
||||
<expand />
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ public class FormRequestHandler {
|
|||
private static final Logger logger = LoggerFactory.getLogger(FormRequestHandler.class);
|
||||
|
||||
public static CompletableFuture<ResponseEntity<String>> handleRequestWithVerifyMail(Form form, String ip) {
|
||||
CompletableFuture<Integer> storeFormForVerificationCode = new StoreFormQuery().storeFormForVerificationCode(form.getSender(), form);
|
||||
CompletableFuture<Integer> storeFormForVerificationCode = new StoreFormQuery().storeFormForVerificationCode(form.getSender(), form, ip);
|
||||
return storeFormForVerificationCode.thenCompose(code -> Verify.verifyEmail(ip, form.getSender(), code, form).thenApply(verificationResult -> {
|
||||
if (verificationResult == VerificationResult.VERIFICATION_SENT) {
|
||||
//TODO if this is ok tell the user they have x min to verify if they fail to do so they have to remake the form
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import java.sql.SQLException;
|
|||
|
||||
public class Database {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Database.class); //TODO can be replaced with library and that one db file that creates the db
|
||||
private static final Logger logger = LoggerFactory.getLogger(Database.class);
|
||||
|
||||
public static void createTables() {
|
||||
String[] createTables = {
|
||||
|
|
@ -28,6 +28,8 @@ public class Database {
|
|||
creation_date BIGINT NOT NULL,
|
||||
form_json TEXT NOT NULL,
|
||||
form_class VARCHAR(64) NOT NULL,
|
||||
ip VARCHAR(45) not null,
|
||||
time TIMESTAMP default current_timestamp() not null,
|
||||
PRIMARY KEY(formId)
|
||||
)
|
||||
""",
|
||||
|
|
|
|||
|
|
@ -21,29 +21,30 @@ public class StoreFormQuery {
|
|||
return 100000 + random.nextInt(900000);
|
||||
}
|
||||
|
||||
private Optional<Long> insertForm(Connection connection, Form form) {
|
||||
String insertForm = "INSERT INTO form (creation_date, form_json, form_class) VALUES (?, ?, ?)";
|
||||
private Optional<Long> insertForm(Connection connection, Form form, String ip) {
|
||||
String insertForm = "INSERT INTO form (creation_date, form_json, form_class, ip) VALUES (?, ?, ?, ?)";
|
||||
try (PreparedStatement stmt = connection.prepareStatement(insertForm, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setLong(1, Instant.now().toEpochMilli());
|
||||
stmt.setString(2, form.toJsonString());
|
||||
stmt.setString(3, form.getClass().getSimpleName());
|
||||
stmt.setString(4, ip);
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
if (affectedRows == 0) {
|
||||
logger.error("No rows affected during insert of form: " + form);
|
||||
logger.error("No rows affected during insert of form: {}", form);
|
||||
return Optional.empty();
|
||||
}
|
||||
ResultSet generatedKeys = stmt.getGeneratedKeys();
|
||||
if (generatedKeys.next()) {
|
||||
return Optional.of(generatedKeys.getLong(1));
|
||||
} else {
|
||||
logger.error("No primary key generated when inserting form " + form);
|
||||
logger.error("No primary key generated when inserting form {}", form);
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed insert form query for: " + form, e);
|
||||
logger.error("Failed insert form query for: {}", form, e);
|
||||
return Optional.empty();
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Invalid class for JSON, failed insert form query for: " + form, e);
|
||||
logger.error("Invalid class for JSON, failed insert form query for: {}", form, e);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
|
@ -58,17 +59,17 @@ public class StoreFormQuery {
|
|||
stmt.executeUpdate();
|
||||
return Optional.of(verificationCode);
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to insert verification code for form with id: " + formId);
|
||||
logger.error("Failed to insert verification code for form with id: {}", formId);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Integer> storeFormForVerificationCode(String eMail, Form form) {
|
||||
public CompletableFuture<Integer> storeFormForVerificationCode(String eMail, Form form, String ip) {
|
||||
logger.debug("storeFormForVerificationCode");
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
logger.trace("Connection: {}", connection);
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
Optional<Long> optionalFormId = insertForm(connection, form);
|
||||
Optional<Long> optionalFormId = insertForm(connection, form, ip);
|
||||
logger.trace("Form id: {}", optionalFormId);
|
||||
if (optionalFormId.isEmpty()) {
|
||||
throw new RuntimeException("Failed to store form");
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ public class TestForm {
|
|||
Assertions.assertDoesNotThrow(() -> DatabaseConnection.initialize());
|
||||
StoreFormQuery storeFormQuery = new StoreFormQuery();
|
||||
ContactFormData contactFormData = new ContactFormData("akastijn", "akastijn@alttd.com", "This is a test question.");
|
||||
storeFormQuery.storeFormForVerificationCode("akastijn@alttd.com", contactFormData).
|
||||
storeFormQuery.storeFormForVerificationCode("akastijn@alttd.com", contactFormData, "0.0.0.0").
|
||||
thenAccept(code -> {
|
||||
TestForm.code = code;
|
||||
Assertions.assertTrue(code > 999 && code < 10000);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user