SHA256
59 lines
3.1 KiB
Java
59 lines
3.1 KiB
Java
package shine.db.dao;
|
|
|
|
import shine.db.DbController;
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
/** Единая авторитетная проекция: FREE_CERT не притворяется Solana PDA. */
|
|
public final class RegistrationIdentityDAO {
|
|
public record ResolvedIdentity(String fullLogin, String rootKey, String blockchainKey,
|
|
String clientKey, String registrationSource, String registrationStatus,
|
|
String namespace, String accessServerLogin, boolean hasSolanaPda) {
|
|
public boolean isFree() { return "FREE_CERT".equals(registrationStatus); }
|
|
}
|
|
private static final RegistrationIdentityDAO INSTANCE = new RegistrationIdentityDAO();
|
|
public static RegistrationIdentityDAO getInstance() { return INSTANCE; }
|
|
private RegistrationIdentityDAO() {}
|
|
|
|
public ResolvedIdentity get(String login) throws SQLException {
|
|
try (Connection c = DbController.getInstance().getConnection()) { return get(c, login); }
|
|
}
|
|
public ResolvedIdentity get(Connection c, String login) throws SQLException {
|
|
if (login == null || login.isBlank()) return null;
|
|
try (PreparedStatement ps = c.prepareStatement("""
|
|
SELECT i.login, i.root_key, i.blockchain_key, i.client_key,
|
|
i.registration_source, i.registration_status, i.namespace,
|
|
i.solana_pda_address, n.default_server_login,
|
|
s.server_login AS actual_access_server
|
|
FROM user_identity_current i
|
|
LEFT JOIN shine_namespace_current n ON n.namespace=i.namespace
|
|
LEFT JOIN user_access_servers_current s ON s.user_login=i.login
|
|
WHERE i.normalized_login=LOWER(BTRIM(?))
|
|
ORDER BY s.server_login ASC
|
|
LIMIT 1
|
|
""")) {
|
|
ps.setString(1, login);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
if (!rs.next()) return null;
|
|
boolean hasPda = rs.getString("solana_pda_address") != null;
|
|
String status = rs.getString("registration_status");
|
|
String server = "FREE_CERT".equals(status) ? rs.getString("default_server_login")
|
|
: rs.getString("actual_access_server");
|
|
if ("FREE_CERT".equals(status) && hasPda)
|
|
throw new SQLException("Inconsistent free user: Solana PDA must be absent");
|
|
if (!"FREE_CERT".equals(status) && !hasPda)
|
|
throw new SQLException("Inconsistent paid user: Solana PDA is missing");
|
|
return new ResolvedIdentity(rs.getString("login"), rs.getString("root_key"),
|
|
rs.getString("blockchain_key"), rs.getString("client_key"),
|
|
rs.getString("registration_source"), status, rs.getString("namespace"), server, hasPda);
|
|
}
|
|
}
|
|
}
|
|
public boolean isFreeCertificate(String login) throws SQLException {
|
|
ResolvedIdentity identity = get(login);
|
|
return identity != null && identity.isFree();
|
|
}
|
|
}
|