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.
iot/projects/IoTCenter/DbConfig.cs

79 lines
3.3 KiB

using Application.Domain.Entities;
using Infrastructure.Application.Entites.Settings;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Security;
using IoT.Shared;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace IoTCenter
{
public class DbConfig : IDbConfig
{
private readonly IEncryptionService _encryptionService;
public DbConfig(IEncryptionService encryptionService)
{
this._encryptionService = encryptionService;
}
public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
public void OnModelCreating(ModelBuilder modelBuilder)
{
IoTSharedDbConfig.OnModelCreating(modelBuilder);
}
public void Seed(DbContext db)
{
IoTSharedDbConfig.Seed(db);
var saRole = new Role { Name = "超级管理员", IsReadOnly = true };
var adminRole = new Role { Name = "管理员", IsReadOnly = true };
//var skips = new string[] { "添加节点", "删除节点", "添加设备", "删除设备" };
foreach (var item in db.Set<Permission>())
{
saRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
if (!item.Name.Contains("删除"))// && !skips.Contains(item.Name))
{
adminRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
}
}
var securityStam = "123456";
db.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 } }
});
db.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 } }
});
db.SaveChanges();
//
var set = db.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 });
set.Add(new Setting { Name = "iosAppUrl", Value = "itms-services://?action=download-manifest&url=https://iot.edusoa.com/IoTCenter/Info.plist", Type = SettingType.Text });
//
var macAddress = Helper.Instance.GetMacAddress();
set.Add(new Setting { Name = "sn", Value = macAddress, Type = SettingType.Text });
set.Add(new Setting { Name = "id", Value = macAddress.Md5(), Type = SettingType.Text });
set.Add(new Setting { Name = "code", Value = "根据id生成的授权码", Type = SettingType.Text });
db.SaveChanges();
}
}
}