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.
200 lines
11 KiB
200 lines
11 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Email;
|
|
using Infrastructure.Events;
|
|
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.Reflection;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IoT.UI.Shard
|
|
{
|
|
public class IoTServiceStartup : BaseStartup
|
|
{
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
public IoTServiceStartup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env)
|
|
{
|
|
this._env = env;
|
|
}
|
|
|
|
public override void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddTransient<IEmailSender, EmptyEmailSender>();
|
|
Assembly.GetExecutingAssembly()
|
|
.GetTypes()
|
|
.Where(t => t.GetInterfaces().Any(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEventHander<>)))
|
|
.ToList()
|
|
.ForEach(t =>
|
|
{
|
|
services.AddTransient(t.GetInterfaces().Where(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEventHander<>)).First(), t);
|
|
});
|
|
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, IWebHostEnvironment 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)
|
|
{
|
|
if (modelBuilder == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(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<Product>().HasOne(o => o.Category).WithMany(o => o.Products).HasForeignKey(o => o.CategoryId);
|
|
modelBuilder.Entity<Product>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<Api>().HasOne(o => o.Product).WithMany(o => o.Apis).HasForeignKey(o => o.ProductId);
|
|
modelBuilder.Entity<Api>().HasIndex(o => new { o.ProductId, o.Name }).IsUnique();
|
|
modelBuilder.Entity<Node>().HasIndex(o => o.Number).IsUnique();
|
|
modelBuilder.Entity<Device>().HasOne(o => o.Product).WithMany(o => o.Devices).HasForeignKey(o => o.ProductId);
|
|
modelBuilder.Entity<Device>().HasOne(o => o.Node).WithMany(o => o.Devices).HasForeignKey(o => o.NodeId);
|
|
modelBuilder.Entity<Device>().HasIndex(o => o.Number).IsUnique();
|
|
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<LiveRecord>().Property(o => o.DeviceNumber).IsRequired();
|
|
modelBuilder.Entity<Command>().HasOne(o => o.Api).WithMany(o => o.Commands).HasForeignKey(o => o.ApiId);
|
|
modelBuilder.Entity<Scene>().HasOne(o => o.Node).WithMany(o => o.Scenes).HasForeignKey(o => o.NodeId);
|
|
modelBuilder.Entity<IoTTimer>().HasOne(o => o.Node).WithMany(o => o.Timers).HasForeignKey(o => o.NodeId);
|
|
modelBuilder.Entity<IoTTigger>().HasOne(o => o.Node).WithMany(o => o.Tiggers).HasForeignKey(o => o.NodeId);
|
|
modelBuilder.Entity<IoTTigger>().HasOne(o => o.Data).WithMany(o => o.Tiggers).HasForeignKey(o => o.DataId);
|
|
modelBuilder.Entity<SceneCommand>().HasOne(o => o.Scene).WithMany(o => o.SceneCommands).HasForeignKey(o => o.SceneId);
|
|
modelBuilder.Entity<SceneCommand>().HasOne(o => o.Command).WithMany(o => o.SceneCommands).HasForeignKey(o => o.CommandId);
|
|
modelBuilder.Entity<TimerCommand>().HasOne(o => o.Timer).WithMany(o => o.TimerCommands).HasForeignKey(o => o.TimerId);
|
|
modelBuilder.Entity<TimerCommand>().HasOne(o => o.Command).WithMany(o => o.TimerCommands).HasForeignKey(o => o.CommandId);
|
|
modelBuilder.Entity<TiggerCommand>().HasOne(o => o.Tigger).WithMany(o => o.TiggerCommands).HasForeignKey(o => o.TiggerId);
|
|
modelBuilder.Entity<TiggerCommand>().HasOne(o => o.Command).WithMany(o => o.TiggerCommands).HasForeignKey(o => o.CommandId);
|
|
}
|
|
|
|
public override void Seed(DbContext dbContext, IServiceProvider serviceProvider, IConfiguration configuration)
|
|
{
|
|
if (dbContext == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(dbContext));
|
|
}
|
|
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),
|
|
Email = "super@test.com",
|
|
UserRoles = new List<UserRole> { new UserRole { Role = saRole } }
|
|
});
|
|
dbContext.Set<User>().Add(new User
|
|
{
|
|
UserName = "admin",
|
|
SecurityStamp = securityStam,
|
|
PasswordHash = encryptionService.CreatePasswordHash("123456", securityStam),
|
|
Email = "admin@test.com",
|
|
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
|
|
});
|
|
dbContext.SaveChanges();
|
|
|
|
dbContext.Set<Category>().Add(new Category { Id = Guid.Parse("BA92B82B-1E92-428B-92ED-28AD93FB7514"), Number = "00", Name = "网关", Icon = "gateway" });
|
|
dbContext.Set<Category>().Add(new Category { Id = Guid.Parse("8E271914-622C-4B4D-BD33-78993F99BE43"), Number = "10", Name = "安防", Icon = "safe" });
|
|
dbContext.Set<Category>().Add(new Category { Id = Guid.Parse("F510E634-5D1E-4398-A121-6945D43B5A5C"), Number = "20", Name = "电器", Icon = "electric" });
|
|
dbContext.Set<Category>().Add(new Category { Id = Guid.Parse("AC2A427C-173C-4277-B9C5-3B73FFE841C9"), Number = "30", Name = "照明", Icon = "lighting" });
|
|
dbContext.Set<Category>().Add(new Category { Id = Guid.Parse("67FC5B9D-6479-4714-8D07-E24EF0AEB502"), Number = "40", Name = "监测", Icon = "monitor" });
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
} |