From 9a046cf799b4cfb361125037f0aa80c6a2425f2a Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Thu, 16 Apr 2020 08:48:17 +0800
Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=94=A8=E6=88=B7?=
=?UTF-8?q?=E8=A7=92=E8=89=B2=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FAuth.Database/DbContextInit.cs | 93 +++++++++++++++++++++++++++++++++
FAuth/Startup.cs | 7 +++
2 files changed, 100 insertions(+)
create mode 100644 FAuth.Database/DbContextInit.cs
diff --git a/FAuth.Database/DbContextInit.cs b/FAuth.Database/DbContextInit.cs
new file mode 100644
index 0000000..7977a03
--- /dev/null
+++ b/FAuth.Database/DbContextInit.cs
@@ -0,0 +1,93 @@
+using System.Linq;
+using System.Threading.Tasks;
+using FAuth.DataBase.Tables;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Internal;
+
+namespace FAuth.DataBase
+{
+ ///
+ /// 数据库数据初始化
+ ///
+ public static class DbContextInit
+ {
+ ///
+ /// 是否首次执行
+ ///
+ private static bool _isFirstRun = true;
+
+ ///
+ /// 初始化数据库
+ ///
+ /// 数据库上下文
+ ///
+ public static async Task InitSync(this FAuthDb db) {
+ if(!_isFirstRun)
+ return;
+ _isFirstRun = false;
+ db.Database.EnsureCreated();
+ int appId = 0, rgId = 0, rId = 0, uId = 0;
+ if(!db.Apps.Any()) {
+ var nApp = new Apps {
+ Name = "FAuth",
+ Description = "Falcon统一登录认证服务器",
+ };
+ db.Entry(nApp).State = EntityState.Added;
+ await db.SaveChangesAsync();
+ appId = nApp.Id;
+ }
+
+ if(!db.RoleGroups.Any()) {
+ var rg = new RoleGroup {
+ Name = "系统组",
+ Description = "系统相关角色组",
+ };
+ db.Entry(rg).State = EntityState.Added;
+ await db.SaveChangesAsync();
+ rgId = rg.Id;
+ }
+
+ if(appId != 0 && rgId != 0) {
+ db.App_RoleGroups.Add(new App_RoleGroup {
+ AppId = appId,
+ RoleGroupId = rgId,
+ });
+ await db.SaveChangesAsync();
+ }
+ if(!db.Roles.Any()) {
+ var nRole = new Roles {
+ Name = "Admin",
+ Description = "系统管理员",
+ };
+ db.Roles.Add(nRole);
+ await db.SaveChangesAsync();
+ rId = nRole.Id;
+ }
+ if(rgId != 0 && rId != 0) {
+ db.RoleGroup_Roles.Add(new RoleGroup_Role {
+ RoleGroupId = rgId,
+ RoleId = rId,
+ });
+ await db.SaveChangesAsync();
+ }
+ if(!db.Users.Any()) {
+ var nUser = new FUser {
+ Name = "系统管理员",
+ Password = "admin",
+ Status = 0,
+ UserName = "admin",
+ };
+ db.Users.Add(nUser);
+ await db.SaveChangesAsync();
+ uId = nUser.Id;
+ }
+ if(rId != 0 && uId != 0) {
+ db.Role_Users.Add(new Role_User {
+ RoleId = rId,
+ UserId = uId,
+ });
+ await db.SaveChangesAsync();
+ }
+ }
+ }
+}
diff --git a/FAuth/Startup.cs b/FAuth/Startup.cs
index f07148e..affb1b9 100644
--- a/FAuth/Startup.cs
+++ b/FAuth/Startup.cs
@@ -14,6 +14,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
+using Microsoft.OpenApi.Writers;
namespace FAuth
{
@@ -81,6 +82,12 @@ namespace FAuth
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
+
+ using(var socpe=app.ApplicationServices.CreateScope()) {
+ var db = socpe.ServiceProvider.GetService();
+ db.InitSync().Wait();
+ }
+
app.UseHttpsRedirection();
app.UseStaticFiles();