using Fengling.AuthService.Models; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace Fengling.AuthService.Data; public static class SeedData { public static async Task Initialize(IServiceProvider serviceProvider) { using var scope = serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); var userManager = scope.ServiceProvider.GetRequiredService>(); var roleManager = scope.ServiceProvider.GetRequiredService>(); await context.Database.EnsureCreatedAsync(); var defaultTenant = await context.Tenants .AsNoTracking() .FirstOrDefaultAsync(t => t.TenantId == "default"); if (defaultTenant == null) { defaultTenant = new Tenant { TenantId = "default", Name = "默认租户", ContactName = "系统管理员", ContactEmail = "admin@fengling.local", ContactPhone = "13800138000", MaxUsers = 1000, Description = "系统默认租户", Status = "active", CreatedAt = DateTime.UtcNow }; context.Tenants.Add(defaultTenant); await context.SaveChangesAsync(); } var adminRole = await roleManager.FindByNameAsync("Admin"); if (adminRole == null) { adminRole = new ApplicationRole { Name = "Admin", DisplayName = "管理员", Description = "System administrator", TenantId = defaultTenant.Id, IsSystem = true, Permissions = new List { "user.manage", "user.view", "role.manage", "role.view", "tenant.manage", "tenant.view", "oauth.manage", "oauth.view", "log.view", "system.config" }, CreatedTime = DateTime.UtcNow }; await roleManager.CreateAsync(adminRole); } var userRole = await roleManager.FindByNameAsync("User"); if (userRole == null) { userRole = new ApplicationRole { Name = "User", DisplayName = "普通用户", Description = "Regular user", TenantId = defaultTenant.Id, IsSystem = true, Permissions = new List { "user.view" }, CreatedTime = DateTime.UtcNow }; await roleManager.CreateAsync(userRole); } var adminUser = await userManager.FindByNameAsync("admin"); if (adminUser == null) { adminUser = new ApplicationUser { UserName = "admin", Email = "admin@fengling.local", RealName = "系统管理员", Phone = "13800138000", TenantInfo = new TenantInfo(defaultTenant.Id, defaultTenant.TenantId, defaultTenant.Name), EmailConfirmed = true, IsDeleted = false, CreatedTime = DateTime.UtcNow }; var result = await userManager.CreateAsync(adminUser, "Admin@123"); if (result.Succeeded) { await userManager.AddToRoleAsync(adminUser, "Admin"); } } var testUser = await userManager.FindByNameAsync("testuser"); if (testUser == null) { testUser = new ApplicationUser { UserName = "testuser", Email = "test@fengling.local", RealName = "测试用户", Phone = "13900139000", TenantInfo = new TenantInfo(defaultTenant.Id, defaultTenant.TenantId, defaultTenant.Name), EmailConfirmed = true, IsDeleted = false, CreatedTime = DateTime.UtcNow }; var result = await userManager.CreateAsync(testUser, "Test@123"); if (result.Succeeded) { await userManager.AddToRoleAsync(testUser, "User"); } } var consoleClient = await context.OAuthApplications .FirstOrDefaultAsync(c => c.ClientId == "fengling-console"); if (consoleClient == null) { consoleClient = new OAuthApplication { ClientId = "fengling-console", ClientSecret = null, DisplayName = "Fengling Console", RedirectUris = new[] { "http://localhost:5777/auth/callback", "https://console.fengling.local/auth/callback" }, PostLogoutRedirectUris = new[] { "http://localhost:5777/", "https://console.fengling.local/" }, Scopes = new[] { "api", "offline_access", "openid", "profile", "email" }, GrantTypes = new[] { "authorization_code", "refresh_token" }, ClientType = "public", ConsentType = "implicit", Status = "active", CreatedAt = DateTime.UtcNow }; context.OAuthApplications.Add(consoleClient); await context.SaveChangesAsync(); } } }