package scripts.qa; import java.io.File; import java.nio.file.Files; import java.sql.*; import java.util.*; public class InitQaUsers { public static void main(String[] args) throws Exception { Class.forName("com.kingbase8.Driver"); File yml = new File("RuoYi-Vue/ruoyi-admin/src/main/resources/application-druid.yml"); String pass = ""; for (String line : Files.readAllLines(yml.toPath())) { if (line.trim().startsWith("password:")) pass = line.trim().substring("password:".length()).trim(); } String dbUrl = "jdbc:kingbase8://127.0.0.1:4321/railwaysystem_integrated_qa_20260916?useUnicode=true&characterEncoding=utf8"; try (Connection conn = DriverManager.getConnection(dbUrl, "kingbase", pass)) { conn.setAutoCommit(false); // 1. Get admin password hash String adminHash = null; Long adminDeptId = 103L; try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT password, dept_id FROM public.sys_user WHERE user_id = 1")) { if (rs.next()) { adminHash = rs.getString(1); adminDeptId = rs.getLong(2); } } if (adminHash == null) { throw new IllegalStateException("admin user not found in public.sys_user!"); } // Define QA accounts and roles // Format: username, nick_name, roles Object[][] qaUsers = new Object[][]{ {"qa_train_admin", "培训管理员", new Long[]{3L}}, {"qa_teacher_a", "教师A", new Long[]{4L}}, {"qa_teacher_b", "教师B", new Long[]{4L}}, {"qa_student_a", "学员A", new Long[]{5L}}, {"qa_student_b", "学员B", new Long[]{5L}}, {"qa_admin_teacher", "双重角色", new Long[]{1L, 4L}}, {"qa_role_nobody", "无业务身份", new Long[]{4L}} }; for (Object[] u : qaUsers) { String username = (String) u[0]; String nick = (String) u[1]; Long[] roles = (Long[]) u[2]; // Delete existing if any Long userId = null; try (PreparedStatement ps = conn.prepareStatement("SELECT user_id FROM public.sys_user WHERE user_name = ?")) { ps.setString(1, username); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) userId = rs.getLong(1); } } if (userId != null) { try (PreparedStatement ps = conn.prepareStatement("DELETE FROM public.sys_user_role WHERE user_id = ?")) { ps.setLong(1, userId); ps.executeUpdate(); } try (PreparedStatement ps = conn.prepareStatement("DELETE FROM public.sys_user WHERE user_id = ?")) { ps.setLong(1, userId); ps.executeUpdate(); } } // Insert user String insertSql = "INSERT INTO public.sys_user (dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, create_by, create_time, remark) " + "VALUES (?, ?, ?, '00', ?, ?, '0', '', ?, '0', '0', 'system', now(), 'QA Acceptance User') RETURNING user_id"; try (PreparedStatement ps = conn.prepareStatement(insertSql)) { ps.setLong(1, adminDeptId != null ? adminDeptId : 103L); ps.setString(2, username); ps.setString(3, nick); ps.setString(4, username + "@qa.example.com"); ps.setString(5, "1380000" + String.format("%04d", new Random().nextInt(10000))); ps.setString(6, adminHash); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) userId = rs.getLong(1); } } // Assign roles for (Long rId : roles) { try (PreparedStatement ps = conn.prepareStatement("INSERT INTO public.sys_user_role (user_id, role_id) VALUES (?, ?)")) { ps.setLong(1, userId); ps.setLong(2, rId); ps.executeUpdate(); } } System.out.println("Initialized QA User: " + username + " (userId=" + userId + ", roles=" + Arrays.toString(roles) + ")"); } // Ensure role_menu has essential perms for train_admin, train_teacher, train_student // role 3 (train_admin): ensure all train:* menu perms // role 4 (train_teacher): ensure 2239 (train:me:teacher), 2202, 2215, 2223, 2171, 2182 // role 5 (train_student): ensure 2240 (train:me:student), 2215, 2223 Long[][] extraRoleMenus = new Long[][]{ {4L, 2239L}, // teacher me {4L, 2223L}, // lesson query {4L, 2219L}, // plan query {5L, 2240L}, // student me {5L, 2223L}, // lesson query }; for (Long[] rm : extraRoleMenus) { try (PreparedStatement ps = conn.prepareStatement("INSERT INTO public.sys_role_menu (role_id, menu_id) VALUES (?, ?) ON CONFLICT DO NOTHING")) { ps.setLong(1, rm[0]); ps.setLong(2, rm[1]); ps.executeUpdate(); } } conn.commit(); System.out.println("QA Users & Role Mappings successfully provisioned."); } } }