You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
11 KiB
228 lines
11 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Domain;
|
|
using Infrastructure.Email;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Security;
|
|
using Infrastructure.Sms;
|
|
using Infrastructure.Web;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Ocelot.DependencyInjection;
|
|
using Ocelot.Middleware;
|
|
using Raven.Embedded;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace UserCenter
|
|
{
|
|
public class Startup : BaseStartup
|
|
{
|
|
public Startup(IConfiguration configuration, IHostingEnvironment env) : base(configuration, env)
|
|
{
|
|
EmbeddedServer.Instance.StartServer();
|
|
}
|
|
|
|
public override void ConfigureServices(IServiceCollection services)
|
|
{
|
|
base.ConfigureServices(services);
|
|
services.AddTransient<IEmailSender, EmptyEmailSender>();
|
|
services.AddTransient<ISmsSender, EmptySmsSender>();
|
|
services.AddOcelot();
|
|
}
|
|
|
|
public override void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
base.Configure(app, env, loggerFactory);
|
|
app.UseOcelot().Wait();
|
|
}
|
|
|
|
public override Task ValidatePrincipal(CookieValidatePrincipalContext arg)
|
|
{
|
|
return Task.Run(() =>
|
|
{
|
|
var userRepo = arg.HttpContext.RequestServices.GetService<IRepository<User>>();
|
|
|
|
var userName = arg.Principal.Identity.Name;
|
|
var userPermissions = userRepo.ReadOnlyTable().Where(o => o.UserName == userName)
|
|
.SelectMany(o => o.UserRoles)
|
|
.Select(o => o.Role)
|
|
.SelectMany(o => o.RolePermissions)
|
|
.Select(o => o.Permission.Number)
|
|
.ToList();
|
|
var currentPermissions = arg.Principal.Claims.Where(o => o.Type == "Role").Select(o => o.Value).ToList();
|
|
if (!currentPermissions.SequenceEqual(userPermissions))
|
|
{
|
|
arg.HttpContext.SignOutAsync();
|
|
arg.HttpContext.SignIn(userName, userPermissions, arg.Properties.IsPersistent);
|
|
}
|
|
});
|
|
}
|
|
|
|
public override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<PermissionCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId).OnDelete(DeleteBehavior.SetNull);
|
|
modelBuilder.Entity<Permission>().HasOne(o => o.Category).WithMany(o => o.Permissions).HasForeignKey(o => o.CategoryId).OnDelete(DeleteBehavior.SetNull);
|
|
modelBuilder.Entity<UserRole>().HasOne(o => o.User).WithMany(o => o.UserRoles).HasForeignKey(o => o.UserId);
|
|
modelBuilder.Entity<UserRole>().HasOne(o => o.Role).WithMany(o => o.UserRoles).HasForeignKey(o => o.RoleId);
|
|
modelBuilder.Entity<RolePermission>().HasOne(o => o.Role).WithMany(o => o.RolePermissions).HasForeignKey(o => o.RoleId);
|
|
modelBuilder.Entity<RolePermission>().HasOne(o => o.Permission).WithMany(o => o.RolePermissions).HasForeignKey(o => o.PermissionId);
|
|
modelBuilder.Entity<User>().HasIndex(o => o.PhoneNumber).IsUnique();
|
|
modelBuilder.Entity<User>().HasIndex(o => o.NickName).IsUnique(); modelBuilder.Entity<Role>().HasIndex(o => o.Name).IsUnique();
|
|
modelBuilder.Entity<PermissionCategory>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<Permission>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<UserRole>().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique();
|
|
modelBuilder.Entity<RolePermission>().HasIndex(o => new { o.RoleId, o.PermissionId }).IsUnique();
|
|
modelBuilder.Entity<Department>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId);
|
|
modelBuilder.Entity<Department>().Property(o => o.Number).IsRequired();
|
|
modelBuilder.Entity<Department>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<UserDepartment>().HasOne(o => o.User).WithMany(o => o.UserDepartments).HasForeignKey(o => o.UserId);
|
|
modelBuilder.Entity<UserDepartment>().HasOne(o => o.Department).WithMany(o => o.UserDepartments).HasForeignKey(o => o.DepartmentId);
|
|
modelBuilder.Entity<UserDepartment>().HasIndex(o => new { o.UserId, o.DepartmentId }).IsUnique();
|
|
//关系
|
|
modelBuilder.Entity<Site>();
|
|
modelBuilder.Entity<Site>().HasIndex(o => o.Name).IsUnique();
|
|
}
|
|
|
|
public override void Seed(DbContext dbContext, IServiceProvider serviceProvider, IConfiguration configuration)
|
|
{
|
|
dbContext.Set<PermissionCategory>().Add(new PermissionCategory
|
|
{
|
|
Name = "配置",
|
|
Number = "Configuration",
|
|
Permissions = new List<Permission> {
|
|
new Permission { Name = "查询配置", Number = "ListConfiguration",DisplayOrder =1 },
|
|
new Permission { Name = "修改配置", Number = "EditConfiguration",DisplayOrder =2 }
|
|
}
|
|
});
|
|
int i = 1;
|
|
var skipReadCollection = new string[] { "Permission" };
|
|
var skipAddCollection = new string[] { "Permission" };
|
|
foreach (var item in dbContext.Model.GetEntityTypes())
|
|
{
|
|
var type = item.ClrType;
|
|
var name = type.GetDisplayName();
|
|
var number = type.Name;
|
|
var category = new PermissionCategory
|
|
{
|
|
Name = name,
|
|
Number = type.Name,
|
|
DisplayOrder = i
|
|
};
|
|
category.Permissions.Add(new Permission { Name = $"查询{name}", Number = $"List{number}", DisplayOrder = 10 * i + 1 });
|
|
if (!skipReadCollection.Contains(type.Name))
|
|
{
|
|
category.Permissions.Add(new Permission { Name = $"查看{name}", Number = $"Read{number}", DisplayOrder = 10 * i + 2 });
|
|
}
|
|
if (!skipAddCollection.Contains(type.Name))
|
|
{
|
|
category.Permissions.Add(new Permission { Name = $"添加{name}", Number = $"Add{number}", DisplayOrder = 10 * i + 3 });
|
|
}
|
|
if (!typeof(IDisableUpdate).IsAssignableFrom(type))
|
|
{
|
|
category.Permissions.Add(new Permission { Name = $"修改{name}", Number = $"Edit{number}", DisplayOrder = 10 * i + 4 });
|
|
}
|
|
if (!typeof(IDisableDelete).IsAssignableFrom(type))
|
|
{
|
|
category.Permissions.Add(new Permission { Name = $"删除{name}", Number = $"Delete{number}", DisplayOrder = 10 * i + 5 });
|
|
}
|
|
dbContext.Set<PermissionCategory>().Add(category);
|
|
i += 1;
|
|
}
|
|
dbContext.SaveChanges();
|
|
|
|
var adminRole = new Role { Name = "管理员", IsReadOnly = true };
|
|
foreach (var item in dbContext.Set<Permission>())
|
|
{
|
|
adminRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
|
|
}
|
|
var encryptionService = serviceProvider.GetService<IEncryptionService>();
|
|
var securityStam = "123456";
|
|
dbContext.Set<User>().Add(new User
|
|
{
|
|
UserName = "admin",
|
|
SecurityStamp = securityStam,
|
|
PasswordHash = encryptionService.CreatePasswordHash("123456", securityStam),
|
|
PasswordConfirmed = true,
|
|
Email = "test@test.com",
|
|
EmailConfirmed = true,
|
|
PhoneNumber = "13000000000",
|
|
PhoneNumberConfirmed = true,
|
|
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
|
|
});
|
|
dbContext.SaveChanges();
|
|
var host = Helper.Instance.GetLocalIP().ToString();
|
|
dbContext.Set<Site>().Add(new Site
|
|
{
|
|
Name = "物联网平台",
|
|
Description = "智能设备管控中心",
|
|
Home = $"http://{host}:8001/",
|
|
Login = $"http://{host}:8001/Account/JsonpLogin",
|
|
Logout = $"http://{host}:8001/Account/JsonpLogout",
|
|
Key = "123456"
|
|
});
|
|
dbContext.Set<Site>().Add(new Site
|
|
{
|
|
Name = "学习平台",
|
|
Description = "资源库、在线学习、网络课程中心",
|
|
Home = $"http://{host}:8082/",
|
|
Login = $"http://{host}:8082/Account/JsonpLogin",
|
|
Logout = $"http://{host}:8082/Account/JsonpLogout",
|
|
Key = "123456"
|
|
});
|
|
dbContext.SaveChanges();
|
|
var department = new Department
|
|
{
|
|
Name = "科学技术大学",
|
|
Number = "0",
|
|
Children = new List<Department>
|
|
{
|
|
new Department{ Name="人事部",Number="1"},
|
|
new Department{ Name="财务部",Number="2"},
|
|
new Department{ Name="理学院",Number="3"},
|
|
new Department{
|
|
Name ="计算机学院",
|
|
Number ="4",
|
|
Children = new List<Department>
|
|
{
|
|
new Department
|
|
{
|
|
Name ="计算机科学与技术专业",
|
|
Number ="5",
|
|
Children = new List<Department>
|
|
{
|
|
new Department
|
|
{
|
|
Name ="2018级",
|
|
Number ="6",
|
|
Children=new List<Department>
|
|
{
|
|
new Department{ Name="1班",Number="7" }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
dbContext.Set<Department>().Add(department);
|
|
dbContext.SaveChanges();
|
|
var departments = dbContext.Set<Department>().ToList();
|
|
foreach (var item in departments)
|
|
{
|
|
item.UpdatePath();
|
|
}
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
} |