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.
226 lines
11 KiB
226 lines
11 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Application.Entites.Settings;
|
|
using Infrastructure.Email;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Security;
|
|
using Infrastructure.Sms;
|
|
using Infrastructure.Web;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Raven.Embedded;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace UserCenter
|
|
{
|
|
public class Startup : BaseStartup
|
|
{
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env)
|
|
{
|
|
EmbeddedServer.Instance.StartServer();
|
|
}
|
|
|
|
public override void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddTransient<IEmailSender, EmptyEmailSender>();
|
|
services.AddTransient<ISmsSender, EmptySmsSender>();
|
|
services.AddTransient<IRoleService, RoleService>();
|
|
//services.AddSingleton<FaceRecognitionService>();
|
|
base.ConfigureServices(services);
|
|
}
|
|
|
|
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
base.Configure(app, env, loggerFactory);
|
|
//app.ApplicationServices.GetRequiredService<FaceRecognitionService>();
|
|
}
|
|
|
|
public override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<Setting>();
|
|
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 CreateDatabase(IServiceProvider services)
|
|
{
|
|
if (this.env.IsDevelopment())
|
|
{
|
|
base.CreateDatabase(services);
|
|
}
|
|
}
|
|
|
|
public override void Seed(DbContext dbContext, IServiceProvider serviceProvider, IConfiguration configuration)
|
|
{
|
|
var set = dbContext.Set<Setting>();
|
|
set.Add(new Setting { Name = "name", Value = "物联中心", Type = SettingType.Text });
|
|
set.Add(new Setting { Name = "logo", Value = "/images/logo.png", Type = SettingType.ImageUrl });
|
|
set.Add(new Setting { Name = "copyright", Value = "Copyright © {0} Company. All rights reserved", Type = SettingType.Html });
|
|
dbContext.SaveChanges();
|
|
//
|
|
dbContext.Set<PermissionCategory>().Add(new PermissionCategory
|
|
{
|
|
Name = "配置",
|
|
Number = "EFConfigurationValue",
|
|
Permissions = new List<Permission> {
|
|
new Permission { Name = "查看配置", Number = "Read-EFConfigurationValue" },
|
|
new Permission { Name = "添加配置", Number = "Add-EFConfigurationValue" },
|
|
new Permission { Name = "修改配置", Number = "Edit-EFConfigurationValue" },
|
|
new Permission { Name = "删除配置", Number = "Delete-EFConfigurationValue" }
|
|
}
|
|
});
|
|
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
|
|
};
|
|
category.Permissions.Add(new Permission { Name = $"查看{name}", Number = $"Read-{number}" });
|
|
category.Permissions.Add(new Permission { Name = $"添加{name}", Number = $"Add-{number}" });
|
|
category.Permissions.Add(new Permission { Name = $"修改{name}", Number = $"Edit-{number}" });
|
|
category.Permissions.Add(new Permission { Name = $"删除{name}", Number = $"Delete-{number}" });
|
|
dbContext.Set<PermissionCategory>().Add(category);
|
|
}
|
|
dbContext.SaveChanges();
|
|
|
|
var saRole = new Role { Name = "超级管理员", IsReadOnly = true };
|
|
var adminRole = new Role { Name = "管理员", IsReadOnly = true };
|
|
|
|
foreach (var item in dbContext.Set<Permission>())
|
|
{
|
|
saRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
|
|
if (!item.Name.Contains("删除"))
|
|
{
|
|
adminRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
|
|
}
|
|
}
|
|
|
|
var encryptionService = serviceProvider.GetService<IEncryptionService>();
|
|
var securityStam = "123456";
|
|
dbContext.Set<User>().Add(new User
|
|
{
|
|
UserName = "super",
|
|
SecurityStamp = securityStam,
|
|
PasswordHash = encryptionService.CreatePasswordHash("123456", securityStam),
|
|
PasswordConfirmed = true,
|
|
Email = "sa@test.com",
|
|
EmailConfirmed = true,
|
|
PhoneNumber = "13000000000",
|
|
PhoneNumberConfirmed = true,
|
|
NickName = "超级管理员",
|
|
FaceImage = "/face/sa.jpg",
|
|
UserRoles = new List<UserRole> { new UserRole { Role = saRole } }
|
|
});
|
|
dbContext.Set<User>().Add(new User
|
|
{
|
|
UserName = "admin",
|
|
SecurityStamp = securityStam,
|
|
PasswordHash = encryptionService.CreatePasswordHash("123456", securityStam),
|
|
PasswordConfirmed = true,
|
|
Email = "admin@test.com",
|
|
EmailConfirmed = true,
|
|
PhoneNumber = "13000000001",
|
|
PhoneNumberConfirmed = true,
|
|
NickName = "管理员",
|
|
FaceImage = "/face/admin.jpg",
|
|
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
|
|
});
|
|
dbContext.SaveChanges();
|
|
var host = "localhost";
|
|
dbContext.Set<Site>().Add(new Site
|
|
{
|
|
Icon = "/images/iotcenter.png",
|
|
Name = "物联网平台",
|
|
Description = "智能设备管控中心",
|
|
Home = $"http://{host}/IoTCenter/",
|
|
Login = $"http://{host}/IoTCenter/Account/JsonpLogin",
|
|
Logout = $"http://{host}/IoTCenter/Account/JsonpLogout",
|
|
Key = "123456"
|
|
});
|
|
dbContext.Set<Site>().Add(new Site
|
|
{
|
|
Icon = "/images/studycenter.png",
|
|
Name = "学习平台",
|
|
Description = "资源库、在线学习、网络课程中心",
|
|
Home = $"http://{host}:8012/",
|
|
Login = $"http://{host}:8012/Account/JsonpLogin",
|
|
Logout = $"http://{host}:8012/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();
|
|
}
|
|
}
|
|
} |