Add email verification functionality, including backend support, email handling, and user interface integration.
This commit is contained in:
@@ -6,7 +6,8 @@ import lombok.Getter;
|
||||
public enum Databases {
|
||||
DEFAULT("web_db"),
|
||||
LUCK_PERMS("luckperms"),
|
||||
LITE_BANS("litebans");
|
||||
LITE_BANS("litebans"),
|
||||
DISCORD("discordLink");
|
||||
|
||||
private final String internalName;
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.altitudeweb.database.discord;
|
||||
|
||||
public record OutputChannel(long guild, String outputType, long channel, String channelType) {
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.alttd.altitudeweb.database.discord;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OutputChannelMapper {
|
||||
@Select("""
|
||||
SELECT guild, output_type, channel, channel_type
|
||||
FROM output_channels
|
||||
WHERE output_type = #{outputType}
|
||||
""")
|
||||
List<OutputChannel> getChannelsWithOutputType(@Param("outputType") String outputType);
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import java.util.UUID;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PrivilegedUser {
|
||||
private int id;
|
||||
private Integer id;
|
||||
private UUID uuid;
|
||||
private List<String> permissions;
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.alttd.altitudeweb.database.web_db.mail;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
public record EmailVerification(
|
||||
UUID id,
|
||||
UUID userUuid,
|
||||
String email,
|
||||
String verificationCode,
|
||||
boolean verified,
|
||||
Instant createdAt,
|
||||
Instant verifiedAt,
|
||||
Instant lastSentAt
|
||||
) {
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.alttd.altitudeweb.database.web_db.mail;
|
||||
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface EmailVerificationMapper {
|
||||
|
||||
@Insert("""
|
||||
INSERT INTO user_emails (id, user_uuid, email, verification_code, verified, created_at, last_sent_at)
|
||||
VALUES (#{id}, #{userUuid}, #{email}, #{verificationCode}, #{verified}, #{createdAt}, #{lastSentAt})
|
||||
""")
|
||||
void insert(EmailVerification emailVerification);
|
||||
|
||||
@Select("""
|
||||
SELECT id, user_uuid AS userUuid, email, verification_code AS verificationCode, verified,
|
||||
created_at AS createdAt, verified_at AS verifiedAt, last_sent_at AS lastSentAt
|
||||
FROM user_emails
|
||||
WHERE user_uuid = #{userUuid} AND email = #{email}
|
||||
""")
|
||||
EmailVerification findByUserAndEmail(@Param("userUuid") UUID userUuid, @Param("email") String email);
|
||||
|
||||
@Select("""
|
||||
SELECT id, user_uuid AS userUuid, email, verification_code AS verificationCode, verified,
|
||||
created_at AS createdAt, verified_at AS verifiedAt, last_sent_at AS lastSentAt
|
||||
FROM user_emails
|
||||
WHERE user_uuid = #{userUuid} AND verification_code = #{code}
|
||||
ORDER BY created_at DESC LIMIT 1
|
||||
""")
|
||||
EmailVerification findByUserAndCode(@Param("userUuid") UUID userUuid, @Param("code") String code);
|
||||
|
||||
@Select("""
|
||||
SELECT id, user_uuid AS userUuid, email, verification_code AS verificationCode, verified,
|
||||
created_at AS createdAt, verified_at AS verifiedAt, last_sent_at AS lastSentAt
|
||||
FROM user_emails
|
||||
WHERE user_uuid = #{userUuid}
|
||||
ORDER BY created_at ASC
|
||||
""")
|
||||
java.util.List<EmailVerification> findAllByUser(@Param("userUuid") UUID userUuid);
|
||||
|
||||
@Update("""
|
||||
UPDATE user_emails SET verified = 1, verified_at = #{verifiedAt}
|
||||
WHERE id = #{id}
|
||||
""")
|
||||
void markVerified(@Param("id") UUID id, @Param("verifiedAt") Instant verifiedAt);
|
||||
|
||||
@Update("""
|
||||
UPDATE user_emails SET verification_code = #{code}, last_sent_at = #{lastSentAt}, verified = 0, verified_at = NULL
|
||||
WHERE id = #{id}
|
||||
""")
|
||||
void updateCodeAndLastSent(@Param("id") UUID id, @Param("code") String code, @Param("lastSentAt") Instant lastSentAt);
|
||||
|
||||
@Delete("""
|
||||
DELETE FROM user_emails WHERE user_uuid = #{userUuid} AND email = #{email}
|
||||
""")
|
||||
void deleteByUserAndEmail(@Param("userUuid") UUID userUuid, @Param("email") String email);
|
||||
}
|
||||
@@ -35,6 +35,7 @@ public class Connection {
|
||||
InitializeWebDb.init();
|
||||
InitializeLiteBans.init();
|
||||
InitializeLuckPerms.init();
|
||||
InitializeDiscord.init();
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.alttd.altitudeweb.setup;
|
||||
|
||||
import com.alttd.altitudeweb.database.Databases;
|
||||
import com.alttd.altitudeweb.database.discord.OutputChannelMapper;
|
||||
import com.alttd.altitudeweb.database.luckperms.TeamMemberMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class InitializeDiscord {
|
||||
|
||||
protected static void init() {
|
||||
log.info("Initializing Discord");
|
||||
Connection.getConnection(Databases.DISCORD, (configuration) -> {
|
||||
configuration.addMapper(OutputChannelMapper.class);
|
||||
}).join();
|
||||
log.debug("Initialized Discord");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ public class InitializeWebDb {
|
||||
configuration.addMapper(KeyPairMapper.class);
|
||||
configuration.addMapper(PrivilegedUserMapper.class);
|
||||
configuration.addMapper(AppealMapper.class);
|
||||
configuration.addMapper(com.alttd.altitudeweb.database.web_db.mail.EmailVerificationMapper.class);
|
||||
}).join()
|
||||
.runQuery(sqlSession -> {
|
||||
createSettingsTable(sqlSession);
|
||||
@@ -29,6 +30,7 @@ public class InitializeWebDb {
|
||||
createPrivilegedUsersTable(sqlSession);
|
||||
createPrivilegesTable(sqlSession);
|
||||
createAppealTable(sqlSession);
|
||||
createUserEmailsTable(sqlSession);
|
||||
});
|
||||
log.debug("Initialized WebDb");
|
||||
}
|
||||
@@ -102,6 +104,27 @@ public class InitializeWebDb {
|
||||
}
|
||||
}
|
||||
|
||||
private static void createUserEmailsTable(@NotNull SqlSession sqlSession) {
|
||||
String query = """
|
||||
CREATE TABLE IF NOT EXISTS user_emails (
|
||||
id UUID NOT NULL DEFAULT (UUID()) PRIMARY KEY,
|
||||
user_uuid UUID NOT NULL,
|
||||
email VARCHAR(255) NOT NULL,
|
||||
verification_code VARCHAR(16) NOT NULL,
|
||||
verified BOOLEAN NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
verified_at TIMESTAMP NULL,
|
||||
last_sent_at TIMESTAMP NULL,
|
||||
FOREIGN KEY (user_uuid) REFERENCES privileged_users(uuid) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
""";
|
||||
try (Statement statement = sqlSession.getConnection().createStatement()) {
|
||||
statement.execute(query);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createAppealTable(@NotNull SqlSession sqlSession) {
|
||||
String query = """
|
||||
CREATE TABLE IF NOT EXISTS appeals (
|
||||
|
||||
Reference in New Issue
Block a user