SHA256
87 lines
3.0 KiB
Java
87 lines
3.0 KiB
Java
package shine.db.dao;
|
|
|
|
import shine.db.DbController;
|
|
import shine.db.KeyEncodingUtil;
|
|
import shine.db.entities.UserAccessServerRouteEntry;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* DAO локальной routing-проекции access servers пользователей.
|
|
*/
|
|
public final class UserAccessServersCurrentDAO {
|
|
|
|
private static volatile UserAccessServersCurrentDAO instance;
|
|
private final DbController db = DbController.getInstance();
|
|
|
|
private UserAccessServersCurrentDAO() {}
|
|
|
|
public static UserAccessServersCurrentDAO getInstance() {
|
|
if (instance == null) {
|
|
synchronized (UserAccessServersCurrentDAO.class) {
|
|
if (instance == null) instance = new UserAccessServersCurrentDAO();
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public List<UserAccessServerRouteEntry> listByUserLogin(String userLogin) throws SQLException {
|
|
try (Connection c = db.getConnection()) {
|
|
return listByUserLogin(c, userLogin);
|
|
}
|
|
}
|
|
|
|
public List<UserAccessServerRouteEntry> listByUserLogin(Connection c, String userLogin) throws SQLException {
|
|
String sql = """
|
|
SELECT user_login, server_login, server_url, server_client_key
|
|
FROM user_access_servers_current
|
|
WHERE LOWER(user_login) = LOWER(?)
|
|
ORDER BY server_login
|
|
""";
|
|
List<UserAccessServerRouteEntry> result = new ArrayList<>();
|
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, userLogin);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
while (rs.next()) {
|
|
result.add(mapRow(rs));
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<String> listUserLoginsByServerLogin(String serverLogin) throws SQLException {
|
|
String sql = """
|
|
SELECT user_login
|
|
FROM user_access_servers_current
|
|
WHERE LOWER(server_login) = LOWER(?)
|
|
ORDER BY user_login
|
|
""";
|
|
List<String> result = new ArrayList<>();
|
|
try (Connection c = db.getConnection();
|
|
PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, serverLogin);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
while (rs.next()) {
|
|
result.add(rs.getString("user_login"));
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private UserAccessServerRouteEntry mapRow(ResultSet rs) throws SQLException {
|
|
UserAccessServerRouteEntry entry = new UserAccessServerRouteEntry();
|
|
entry.setUserLogin(rs.getString("user_login"));
|
|
entry.setServerLogin(rs.getString("server_login"));
|
|
entry.setServerUrl(rs.getString("server_url"));
|
|
entry.setServerClientKey(KeyEncodingUtil.normalizeKeyToBase64_32(rs.getString("server_client_key")));
|
|
return entry;
|
|
}
|
|
}
|