253 lines
20 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AmtScanner.Api.Models;
using Microsoft.EntityFrameworkCore;
namespace AmtScanner.Api.Data;
/// <summary>
/// 数据库种子数据
/// </summary>
public static class DbSeeder
{
/// <summary>
/// 初始化种子数据
/// </summary>
public static async Task SeedAsync(AppDbContext context)
{
await SeedRolesAsync(context);
await SeedUsersAsync(context);
await SeedMenusAsync(context);
await SeedRoleMenusAsync(context);
}
private static async Task SeedRolesAsync(AppDbContext context)
{
if (await context.Roles.AnyAsync()) return;
var roles = new List<Role>
{
new() { RoleName = "超级管理员", RoleCode = "R_SUPER", Description = "拥有所有权限", Enabled = true },
new() { RoleName = "管理员", RoleCode = "R_ADMIN", Description = "系统管理员", Enabled = true },
new() { RoleName = "普通用户", RoleCode = "R_USER", Description = "普通用户", Enabled = true }
};
context.Roles.AddRange(roles);
await context.SaveChangesAsync();
Console.WriteLine("✅ 默认角色已创建");
}
private static async Task SeedUsersAsync(AppDbContext context)
{
if (await context.Users.AnyAsync()) return;
var users = new List<User>
{
new()
{
UserName = "Super",
PasswordHash = BCrypt.Net.BCrypt.HashPassword("123456"),
NickName = "超级管理员",
Email = "super@example.com",
Status = "1",
Gender = "1"
},
new()
{
UserName = "Admin",
PasswordHash = BCrypt.Net.BCrypt.HashPassword("123456"),
NickName = "管理员",
Email = "admin@example.com",
Status = "1",
Gender = "1"
},
new()
{
UserName = "User",
PasswordHash = BCrypt.Net.BCrypt.HashPassword("123456"),
NickName = "普通用户",
Email = "user@example.com",
Status = "1",
Gender = "2"
}
};
context.Users.AddRange(users);
await context.SaveChangesAsync();
var superRole = await context.Roles.FirstAsync(r => r.RoleCode == "R_SUPER");
var adminRole = await context.Roles.FirstAsync(r => r.RoleCode == "R_ADMIN");
var userRole = await context.Roles.FirstAsync(r => r.RoleCode == "R_USER");
var superUser = await context.Users.FirstAsync(u => u.UserName == "Super");
var adminUser = await context.Users.FirstAsync(u => u.UserName == "Admin");
var normalUser = await context.Users.FirstAsync(u => u.UserName == "User");
var userRoles = new List<UserRole>
{
new() { UserId = superUser.Id, RoleId = superRole.Id },
new() { UserId = adminUser.Id, RoleId = adminRole.Id },
new() { UserId = normalUser.Id, RoleId = userRole.Id }
};
context.UserRoles.AddRange(userRoles);
await context.SaveChangesAsync();
Console.WriteLine("✅ 默认用户已创建: Super/Admin/User (密码: 123456)");
}
private static async Task SeedMenusAsync(AppDbContext context)
{
if (await context.Menus.AnyAsync()) return;
var menus = new List<Menu>
{
// ========== 首页 ==========
new() { Id = 1, Name = "Home", Path = "/home", Component = "/index/index", Title = "首页", Icon = "ri:home-line", Sort = 1, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 2, ParentId = 1, Name = "Overview", Path = "index", Component = "/home/index", Title = "系统概览", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
// ========== 机房管理 ==========
new() { Id = 10, Name = "RoomManage", Path = "/room", Component = "/index/index", Title = "机房管理", Icon = "ri:building-line", Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 11, ParentId = 10, Name = "RoomList", Path = "list", Component = "/room/list", Title = "机房列表", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 12, ParentId = 10, Name = "RoomDetail", Path = "detail", Component = "/index/index", Title = "机房详情", Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 13, ParentId = 12, Name = "RoomOverview", Path = "overview", Component = "/room/detail/overview", Title = "机房概览", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 14, ParentId = 12, Name = "RoomDevices", Path = "devices", Component = "/room/detail/devices", Title = "设备列表", KeepAlive = true, Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 15, ParentId = 12, Name = "RoomScreenWall", Path = "screen-wall", Component = "/room/detail/screen-wall", Title = "屏幕监控墙", KeepAlive = true, Sort = 3, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 16, ParentId = 12, Name = "RoomBatchControl", Path = "batch-control", Component = "/room/detail/batch-control", Title = "批量控制", KeepAlive = true, Sort = 4, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 17, ParentId = 12, Name = "RoomClassMode", Path = "class-mode", Component = "/room/detail/class-mode", Title = "上课模式", KeepAlive = true, Sort = 5, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 18, ParentId = 12, Name = "RoomClassRecords", Path = "class-records", Component = "/room/detail/class-records", Title = "课堂记录", KeepAlive = true, Sort = 6, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
// ========== 设备管理 ==========
new() { Id = 20, Name = "DeviceManage", Path = "/device", Component = "/index/index", Title = "设备管理", Icon = "ri:computer-line", Sort = 3, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 21, ParentId = 20, Name = "DeviceList", Path = "list", Component = "/device/list", Title = "设备列表", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 22, ParentId = 20, Name = "DeviceDetail", Path = "detail", Component = "/index/index", Title = "设备详情", IsHide = true, Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 23, ParentId = 22, Name = "DeviceInfo", Path = "info", Component = "/device/detail/info", Title = "基本信息", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 24, ParentId = 22, Name = "DeviceStatus", Path = "status", Component = "/device/detail/status", Title = "状态监控", KeepAlive = true, Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 25, ParentId = 22, Name = "DeviceSystem", Path = "system", Component = "/device/detail/system", Title = "系统管理", KeepAlive = true, Sort = 3, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 26, ParentId = 22, Name = "DeviceRemote", Path = "remote", Component = "/device/detail/remote", Title = "远程控制", KeepAlive = true, Sort = 4, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 27, ParentId = 22, Name = "DeviceScreen", Path = "screen", Component = "/device/detail/screen", Title = "屏幕监控", KeepAlive = true, Sort = 5, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 28, ParentId = 22, Name = "DeviceLogs", Path = "logs", Component = "/device/detail/logs", Title = "操作日志", KeepAlive = true, Sort = 6, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
// ========== 课堂控制 ==========
new() { Id = 30, Name = "ClassroomControl", Path = "/classroom", Component = "/index/index", Title = "课堂控制", Icon = "ri:presentation-line", Sort = 4, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 31, ParentId = 30, Name = "CurrentClass", Path = "current", Component = "/index/index", Title = "当前课堂", Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 32, ParentId = 31, Name = "TeacherScreen", Path = "teacher-screen", Component = "/classroom/current/teacher-screen", Title = "教师投屏", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 33, ParentId = 31, Name = "StudentScreens", Path = "student-screens", Component = "/classroom/current/student-screens", Title = "学生屏幕列表", KeepAlive = true, Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 34, ParentId = 31, Name = "RemoteAssist", Path = "remote-assist", Component = "/classroom/current/remote-assist", Title = "远程协助", KeepAlive = true, Sort = 3, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 35, ParentId = 31, Name = "LockScreen", Path = "lock-screen", Component = "/classroom/current/lock-screen", Title = "锁屏 / 解锁", KeepAlive = true, Sort = 4, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 36, ParentId = 31, Name = "Broadcast", Path = "broadcast", Component = "/classroom/current/broadcast", Title = "广播指令", KeepAlive = true, Sort = 5, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
// ========== 远程使用 ==========
new() { Id = 40, Name = "RemoteUse", Path = "/remote-use", Component = "/index/index", Title = "远程使用", Icon = "ri:remote-control-line", Sort = 5, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 41, ParentId = 40, Name = "MyRemoteDevices", Path = "my-devices", Component = "/remote-use/my-devices", Title = "我的远程设备", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 42, ParentId = 40, Name = "WindowsRdp", Path = "windows-rdp", Component = "/remote-use/windows-rdp", Title = "Windows 远程桌面", KeepAlive = true, Sort = 2, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 43, ParentId = 40, Name = "LinuxTerminal", Path = "linux-terminal", Component = "/remote-use/linux-terminal", Title = "Linux 远程终端", KeepAlive = true, Sort = 3, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 44, ParentId = 40, Name = "WebKvm", Path = "web-kvm", Component = "/remote-use/web-kvm", Title = "Web KVMAMT", KeepAlive = true, Sort = 4, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
// ========== 告警与日志 ==========
new() { Id = 50, Name = "AlertLog", Path = "/alert", Component = "/index/index", Title = "告警与日志", Icon = "ri:alarm-warning-line", Sort = 6, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 51, ParentId = 50, Name = "AlertCenter", Path = "center", Component = "/alert/center", Title = "告警中心", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 52, ParentId = 50, Name = "OperationAudit", Path = "audit", Component = "/alert/audit", Title = "操作审计", KeepAlive = true, Sort = 2, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 53, ParentId = 50, Name = "LoginRecords", Path = "login-records", Component = "/alert/login-records", Title = "登录记录", KeepAlive = true, Sort = 3, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
new() { Id = 54, ParentId = 50, Name = "SecurityEvents", Path = "security-events", Component = "/alert/security-events", Title = "安全事件", KeepAlive = true, Sort = 4, Roles = "R_SUPER,R_ADMIN", IsSystem = true },
// ========== 系统管理(超级管理员) ==========
new() { Id = 60, Name = "Admin", Path = "/admin", Component = "/index/index", Title = "系统管理", Icon = "ri:settings-3-line", Sort = 7, Roles = "R_SUPER", IsSystem = true },
// 设备接管
new() { Id = 61, ParentId = 60, Name = "DeviceTakeover", Path = "device-takeover", Component = "/index/index", Title = "设备接管", Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 62, ParentId = 61, Name = "AutoDiscover", Path = "auto-discover", Component = "/admin/device-takeover/auto-discover", Title = "自动发现AMT 扫描)", KeepAlive = true, Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 63, ParentId = 61, Name = "ManualImport", Path = "manual-import", Component = "/admin/device-takeover/manual-import", Title = "手动导入", KeepAlive = true, Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 64, ParentId = 61, Name = "BatchTakeover", Path = "batch-takeover", Component = "/admin/device-takeover/batch-takeover", Title = "批量接管", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
// 凭据管理
new() { Id = 65, ParentId = 60, Name = "CredentialsManage", Path = "credentials", Component = "/index/index", Title = "凭据管理", Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 66, ParentId = 65, Name = "AmtCredentials", Path = "amt", Component = "/admin/credentials/amt", Title = "AMT 凭据", KeepAlive = true, Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 67, ParentId = 65, Name = "WindowsCredentials", Path = "windows", Component = "/admin/credentials/windows", Title = "Windows 凭据", KeepAlive = true, Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 68, ParentId = 65, Name = "LinuxCredentials", Path = "linux", Component = "/admin/credentials/linux", Title = "Linux 凭据", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
// AMT 网络管理
new() { Id = 69, ParentId = 60, Name = "AmtNetwork", Path = "amt-network", Component = "/index/index", Title = "AMT 网络管理", Sort = 3, Roles = "R_SUPER", IsSystem = true },
new() { Id = 70, ParentId = 69, Name = "AmtIpConfig", Path = "ip-config", Component = "/admin/amt-network/ip-config", Title = "IP 配置", KeepAlive = true, Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 71, ParentId = 69, Name = "ProvisionStatus", Path = "provision-status", Component = "/admin/amt-network/provision-status", Title = "Provision 状态", KeepAlive = true, Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 72, ParentId = 69, Name = "CertInfo", Path = "cert-info", Component = "/admin/amt-network/cert-info", Title = "证书信息", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
// AMT 电源操作
new() { Id = 85, ParentId = 60, Name = "AmtPower", Path = "amt-power", Component = "/admin/amt-power/index", Title = "AMT 电源操作", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
// 用户与权限
new() { Id = 73, ParentId = 60, Name = "UserPermission", Path = "user-permission", Component = "/index/index", Title = "用户与权限", Sort = 4, Roles = "R_SUPER", IsSystem = true },
new() { Id = 74, ParentId = 73, Name = "UserManage", Path = "users", Component = "/admin/user-permission/users", Title = "用户管理", KeepAlive = true, Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 75, ParentId = 73, Name = "RoleManage", Path = "roles", Component = "/admin/user-permission/roles", Title = "角色管理", KeepAlive = true, Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 76, ParentId = 73, Name = "PermissionPolicies", Path = "policies", Component = "/admin/user-permission/policies", Title = "权限策略", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
// Agent 管理
new() { Id = 77, ParentId = 60, Name = "AgentManage", Path = "agent", Component = "/index/index", Title = "Agent 管理", Sort = 5, Roles = "R_SUPER", IsSystem = true },
new() { Id = 78, ParentId = 77, Name = "AgentDevices", Path = "devices", Component = "/admin/agent-devices/index", Title = "Agent 设备", KeepAlive = true, Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 79, ParentId = 77, Name = "AgentVersion", Path = "version", Component = "/admin/agent/version", Title = "Agent 版本", KeepAlive = true, Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 80, ParentId = 77, Name = "AgentHeartbeat", Path = "heartbeat", Component = "/admin/agent/heartbeat", Title = "心跳状态", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
new() { Id = 86, ParentId = 77, Name = "AgentWatchdog", Path = "watchdog", Component = "/admin/agent/watchdog", Title = "看门狗策略", KeepAlive = true, Sort = 4, Roles = "R_SUPER", IsSystem = true },
// 系统设置
new() { Id = 81, ParentId = 60, Name = "SystemSettings", Path = "settings", Component = "/index/index", Title = "系统设置", Sort = 6, Roles = "R_SUPER", IsSystem = true },
new() { Id = 82, ParentId = 81, Name = "HeartbeatCycle", Path = "heartbeat-cycle", Component = "/admin/settings/heartbeat-cycle", Title = "心跳周期", KeepAlive = true, Sort = 1, Roles = "R_SUPER", IsSystem = true },
new() { Id = 83, ParentId = 81, Name = "DualSystemPolicy", Path = "dual-system", Component = "/admin/settings/dual-system", Title = "双系统策略", KeepAlive = true, Sort = 2, Roles = "R_SUPER", IsSystem = true },
new() { Id = 84, ParentId = 81, Name = "SecurityPolicy", Path = "security", Component = "/admin/settings/security", Title = "安全策略", KeepAlive = true, Sort = 3, Roles = "R_SUPER", IsSystem = true },
// ========== 帮助与支持 ==========
new() { Id = 90, Name = "Help", Path = "/help", Component = "/index/index", Title = "帮助与支持", Icon = "ri:question-line", Sort = 8, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 91, ParentId = 90, Name = "TeacherGuide", Path = "teacher-guide", Component = "/help/teacher-guide", Title = "教师使用指南", KeepAlive = true, Sort = 1, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 92, ParentId = 90, Name = "StudentGuide", Path = "student-guide", Component = "/help/student-guide", Title = "学生使用说明", KeepAlive = true, Sort = 2, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
new() { Id = 93, ParentId = 90, Name = "SystemIntro", Path = "system-intro", Component = "/help/system-intro", Title = "系统介绍", KeepAlive = true, Sort = 3, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true },
// ========== 用户中心(隐藏菜单) ==========
new() { Id = 99, Name = "UserCenter", Path = "/user-center", Component = "/system/user-center", Title = "个人中心", IsHide = true, KeepAlive = true, Sort = 99, Roles = "R_SUPER,R_ADMIN,R_USER", IsSystem = true }
};
foreach (var menu in menus)
{
context.Menus.Add(menu);
}
await context.SaveChangesAsync();
Console.WriteLine("✅ 默认菜单已创建");
}
private static async Task SeedRoleMenusAsync(AppDbContext context)
{
if (await context.RoleMenus.AnyAsync()) return;
var superRole = await context.Roles.FirstAsync(r => r.RoleCode == "R_SUPER");
var adminRole = await context.Roles.FirstAsync(r => r.RoleCode == "R_ADMIN");
var userRole = await context.Roles.FirstAsync(r => r.RoleCode == "R_USER");
var allMenuIds = await context.Menus.Select(m => m.Id).ToListAsync();
var adminMenuIds = await context.Menus
.Where(m => m.Roles != null && (m.Roles.Contains("R_ADMIN") || m.Roles.Contains("R_USER")))
.Select(m => m.Id).ToListAsync();
var userMenuIds = await context.Menus
.Where(m => m.Roles != null && m.Roles.Contains("R_USER"))
.Select(m => m.Id).ToListAsync();
var roleMenus = new List<RoleMenu>();
// 超级管理员拥有所有菜单
foreach (var menuId in allMenuIds)
{
roleMenus.Add(new RoleMenu { RoleId = superRole.Id, MenuId = menuId });
}
// 管理员菜单
foreach (var menuId in adminMenuIds)
{
roleMenus.Add(new RoleMenu { RoleId = adminRole.Id, MenuId = menuId });
}
// 普通用户菜单
foreach (var menuId in userMenuIds)
{
roleMenus.Add(new RoleMenu { RoleId = userRole.Id, MenuId = menuId });
}
context.RoleMenus.AddRange(roleMenus);
await context.SaveChangesAsync();
Console.WriteLine("✅ 角色菜单权限已分配");
}
}