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.
170 lines
9.3 KiB
170 lines
9.3 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Domain;
|
|
using Infrastructure.Email;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Security;
|
|
using Infrastructure.UI;
|
|
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 System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IoT.UI.Shard
|
|
{
|
|
public class IoTServiceStartup : BaseStartup
|
|
{
|
|
public IoTServiceStartup(IConfiguration configuration, IHostingEnvironment env) : base(configuration, env)
|
|
{
|
|
}
|
|
|
|
public override void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddTransient<IEmailSender, EmptyEmailSender>();
|
|
base.ConfigureServices(services);
|
|
}
|
|
|
|
public override void ConfigureOptions(IServiceCollection services)
|
|
{
|
|
services.ConfigureOptions(new FileConfigureOptions(_env, new List<string> { "IoT.Shared" }));
|
|
}
|
|
|
|
public override void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
base.Configure(app, env, loggerFactory);
|
|
}
|
|
|
|
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.UserName).IsUnique();
|
|
modelBuilder.Entity<User>().HasIndex(o => o.Email).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<Category>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<Node>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<Node>().Property(o => o.Name).IsRequired();
|
|
modelBuilder.Entity<Node>().Property(o => o.Number).IsRequired();
|
|
modelBuilder.Entity<DeviceInfo>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<Api>().HasOne(o => o.Info).WithMany(o => o.Apis).HasForeignKey(o => o.InfoId);
|
|
modelBuilder.Entity<Device>().HasOne(o => o.Category).WithMany(o => o.Devices).HasForeignKey(o => o.CategoryId);
|
|
modelBuilder.Entity<Device>().HasOne(o => o.Info).WithMany(o => o.Devices).HasForeignKey(o => o.InfoId);
|
|
modelBuilder.Entity<Device>().HasOne(o => o.Node).WithMany(o => o.Devices).HasForeignKey(o => o.NodeId).OnDelete(DeleteBehavior.Cascade);
|
|
modelBuilder.Entity<Data>().HasOne(o => o.Device).WithMany(o => o.Data).HasForeignKey(o => o.DeviceId);
|
|
modelBuilder.Entity<Parameter>().HasOne(o => o.Api).WithMany(o => o.Parameters).HasForeignKey(o => o.ApiId);
|
|
modelBuilder.Entity<Scene>().HasOne(o => o.Node).WithMany(o => o.Scenes).HasForeignKey(o => o.NodeId);
|
|
modelBuilder.Entity<Command>().HasOne(o => o.Scene).WithMany(o => o.Commands).HasForeignKey(o => o.SceneId);
|
|
modelBuilder.Entity<Command>().HasOne(o => o.Api).WithMany(o => o.Commands).HasForeignKey(o => o.ApiId);
|
|
modelBuilder.Entity<Command>().HasOne(o => o.Device).WithMany(o => o.Commands).HasForeignKey(o => o.DeviceId);
|
|
}
|
|
|
|
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", "Setting" };
|
|
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),
|
|
Email = "test@test.com",
|
|
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
|
|
});
|
|
dbContext.SaveChanges();
|
|
dbContext.Set<Category>().Add(new Category { Number = "00", Name = "网关" });
|
|
dbContext.Set<Category>().Add(new Category { Number = "10", Name = "安防" });
|
|
dbContext.Set<Category>().Add(new Category { Number = "20", Name = "电器" });
|
|
dbContext.Set<Category>().Add(new Category { Number = "30", Name = "照明" });
|
|
dbContext.Set<Category>().Add(new Category { Number = "40", Name = "监测" });
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
} |