From 5f5df6f868df6a073c8f8cfe63d91b9c87dfad81 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 1 Mar 2020 13:06:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E6=BC=94?= =?UTF-8?q?=E7=A4=BA=E6=A8=A1=E5=BC=8F=E6=8E=88=E6=9D=83=E7=A0=81=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/SettingsController.cs | 2 +- .../Pages/Views/Components/SettingsBase.cs | 2 +- .../Bootstrap.Admin/wwwroot/js/settings.js | 2 +- .../Bootstrap.DataAccess/Helper/DictHelper.cs | 17 +++++++++++++++-- .../Bootstrap.Admin/Api/SettingsTest.cs | 4 ++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/SettingsController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/SettingsController.cs index 2a58618c..63444be5 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/SettingsController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/SettingsController.cs @@ -33,7 +33,7 @@ namespace Bootstrap.Admin.Controllers.Api [HttpPost("{id}")] public bool Post(string id, [FromBody]BootstrapDict dict) => id switch { - "Demo" => DictHelper.UpdateSystemModel(dict.Code == "1", dict.Name, dict.Category), + "Demo" => DictHelper.UpdateSystemModel(dict.Code == "1", dict.Name), "AppPath" => DictHelper.SaveAppSettings(dict), _ => false }; diff --git a/src/admin/Bootstrap.Admin/Pages/Views/Components/SettingsBase.cs b/src/admin/Bootstrap.Admin/Pages/Views/Components/SettingsBase.cs index cbe59645..11a9786e 100644 --- a/src/admin/Bootstrap.Admin/Pages/Views/Components/SettingsBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Views/Components/SettingsBase.cs @@ -244,7 +244,7 @@ namespace Bootstrap.Admin.Pages.Views.Admin.Components /// protected async System.Threading.Tasks.Task SaveSystemModel() { - var ret = DictHelper.UpdateSystemModel(Model.EnableDemo, Model.AuthKey, "/oEQLKLccvHA+MsDwCwmgaKddR0IEcOy9KgBmFsHXRs="); + var ret = DictHelper.UpdateSystemModel(Model.EnableDemo, Model.AuthKey); ShowMessage("保存演示系统设置", ret); if (ret) { diff --git a/src/admin/Bootstrap.Admin/wwwroot/js/settings.js b/src/admin/Bootstrap.Admin/wwwroot/js/settings.js index d1653447..840e1cc2 100644 --- a/src/admin/Bootstrap.Admin/wwwroot/js/settings.js +++ b/src/admin/Bootstrap.Admin/wwwroot/js/settings.js @@ -144,7 +144,7 @@ $(function () { var demo = $('#demo').prop('checked') ? "1" : "0"; var authKey = $('#authKey').val(); $.bc({ - url: Settings.url + '/Demo', data: { category: "/oEQLKLccvHA+MsDwCwmgaKddR0IEcOy9KgBmFsHXRs=", name: authKey, code: demo }, title: '演示系统设置', method: "post", + url: Settings.url + '/Demo', data: { name: authKey, code: demo }, title: '演示系统设置', method: "post", callback: function (result) { if (result) { window.setTimeout(function () { window.location.reload(true); }, 1000); diff --git a/src/admin/Bootstrap.DataAccess/Helper/DictHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/DictHelper.cs index 88058305..f3d7ef1f 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/DictHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/DictHelper.cs @@ -1,6 +1,7 @@ using Bootstrap.Security; using Bootstrap.Security.DataAccess; using Longbow.Cache; +using Longbow.Security.Cryptography; using Longbow.Web; using System; using System.Collections.Generic; @@ -287,18 +288,30 @@ namespace Bootstrap.DataAccess /// 设置 系统是否为演示系统 默认为 false 不是演示系统 /// /// - public static bool UpdateSystemModel(bool isDemo, string authKey, string result) + public static bool UpdateSystemModel(bool isDemo, string authKey) { var ret = false; // 检查授权码 // 请求者提供 秘钥与结果 服务器端通过算法比对结果 - if (Longbow.Security.Cryptography.LgbCryptography.ComputeHash(authKey, "l9w+7loytBzNHYkKjGzpWzbhYpU7kWZenT1OeZxkor28wQJQ") == result) + if (LgbCryptography.ComputeHash(authKey, RetrieveAuthorSalt()) == RetrieveAuthorHash()) { ret = DbContextManager.Create()?.UpdateSystemModel(isDemo) ?? false; } return ret; } + /// + /// 获得 字典表中配置的授权盐值 + /// + /// + public static string RetrieveAuthorSalt() => RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "授权盐值")?.Code ?? ""; + + /// + /// 获得 字典表中配置的哈希值 + /// + /// + public static string RetrieveAuthorHash() => RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "哈希结果")?.Code ?? ""; + /// /// 获得验证码图床地址 /// diff --git a/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs index dd318195..89078fe8 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs @@ -48,10 +48,10 @@ namespace Bootstrap.Admin.Api public async void Post_Id_Ok() { // Demo - var resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "UnitTest", Code = "0", Category = "/oEQLKLccvHA+MsDwCwmgaKddR0IEcOy9KgBmFsHXRs=" }); + var resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "UnitTest", Code = "0" }); Assert.False(resp); - resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "123456", Code = "0", Category = "BPtCMhaFngsDb6NsbnnSgQUn4Ej6PnsvaUV3bChYei8=" }); + resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "123789", Code = "0" }); Assert.True(resp); // AppPath