From 33054356d0af44dc9e7876ca6c0c41e165c9ee47 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 14 Dec 2021 16:09:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=9F=BA=E7=B1=BB=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ServicesExtensions.cs | 2 +- .../Services/DictsService.cs | 2 +- .../Services/NavigationsService.cs | 2 +- .../Extensions/ServicesExtensions.cs | 5 ++-- .../Services/Database.cs | 13 ++++++++ .../Services/DictsService.cs | 10 +++++-- .../Services/NavigationsService.cs | 8 ++--- .../Services/UsersService.cs | 30 +++++++++++++++++++ .../admin/BootstrapAdmin.Web.Core/IDicts.cs | 2 +- .../BootstrapAdmin.Web.Core/INavigations.cs | 2 +- .../admin/BootstrapAdmin.Web.Core/IUsers.cs | 22 ++++++++++++++ .../Services/AdminService.cs | 15 ++++++++-- .../Services/BootstrapAppContext.cs | 2 +- .../Shared/MainLayout.razor.cs | 2 +- 14 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/Database.cs create mode 100644 src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs create mode 100644 src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Extensions/ServicesExtensions.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Extensions/ServicesExtensions.cs index 9e31a096..8c315cd9 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Extensions/ServicesExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Extensions/ServicesExtensions.cs @@ -1,6 +1,6 @@ using BootstrapAdmin.DataAccess.EFCore; using BootstrapAdmin.DataAccess.EFCore.Services; -using BootstrapAdmin.DataAccess.Core; +using BootstrapAdmin.Web.Core; using BootstrapBlazor.Components; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs index 790d0b39..9ec8d028 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs @@ -1,4 +1,4 @@ -using BootstrapAdmin.DataAccess.Core; +using BootstrapAdmin.Web.Core; namespace BootstrapAdmin.DataAccess.EFCore.Services { diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/NavigationsService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/NavigationsService.cs index 094fc3e9..a9ba3ef2 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/NavigationsService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/NavigationsService.cs @@ -1,4 +1,4 @@ -using BootstrapAdmin.DataAccess.Core; +using BootstrapAdmin.Web.Core; using BootstrapAdmin.DataAccess.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs index ff0a358d..e6ba0df3 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs @@ -1,6 +1,6 @@ -using BootstrapAdmin.DataAccess.Core; -using BootstrapAdmin.DataAccess.PetaPoco; +using BootstrapAdmin.DataAccess.PetaPoco; using BootstrapAdmin.DataAccess.PetaPoco.Services; +using BootstrapAdmin.Web.Core; using BootstrapBlazor.Components; using BootstrapBlazor.DataAcces.PetaPoco.Services; using Microsoft.Extensions.Configuration; @@ -35,6 +35,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); return services; } } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/Database.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/Database.cs new file mode 100644 index 00000000..22db9234 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/Database.cs @@ -0,0 +1,13 @@ +using PetaPoco; + +namespace BootstrapAdmin.DataAccess.PetaPoco.Services +{ + abstract class BaseDatabase + { + /// + /// + /// + [NotNull] + protected IDatabase? Database { get; set; } + } +} diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs index 7452a965..d72cf8cb 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs @@ -1,8 +1,14 @@ -using BootstrapAdmin.DataAccess.Core; +using BootstrapAdmin.Web.Core; +using PetaPoco; namespace BootstrapAdmin.DataAccess.PetaPoco.Services { - class DictsService : IDicts + class DictsService : BaseDatabase, IDicts { + /// + /// + /// + /// + public DictsService(IDatabase db) => Database = db; } } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/NavigationsService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/NavigationsService.cs index fecb7529..a25ebf2e 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/NavigationsService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/NavigationsService.cs @@ -1,5 +1,5 @@ -using BootstrapAdmin.DataAccess.Core; -using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.Web.Core; using PetaPoco; namespace BootstrapAdmin.DataAccess.PetaPoco.Services @@ -7,10 +7,8 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services /// /// /// - class NavigationsService : INavigations + class NavigationsService : BaseDatabase, INavigations { - private IDatabase Database { get; } - /// /// /// diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs new file mode 100644 index 00000000..6ddbf9cd --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs @@ -0,0 +1,30 @@ +using BootstrapAdmin.Web.Core; +using PetaPoco; + +namespace BootstrapAdmin.DataAccess.PetaPoco.Services +{ + class UsersService : BaseDatabase, IUsers + { + /// + /// + /// + /// + public UsersService(IDatabase db) => Database = db; + + /// + /// + /// + /// + /// + /// + public List GetApps(string userName) => Database.Fetch($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators"); + + /// + /// + /// + /// + /// + /// + public List GetRoles(string userName) => Database.Fetch($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @0 union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName=@0", userName); + } +} diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IDicts.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IDicts.cs index c44cb0de..c8ce28ed 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IDicts.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IDicts.cs @@ -1,4 +1,4 @@ -namespace BootstrapAdmin.DataAccess.Core +namespace BootstrapAdmin.Web.Core { /// /// Dict 字典表接口 diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/INavigations.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/INavigations.cs index 2270ddc7..d2e7dca2 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/INavigations.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/INavigations.cs @@ -1,6 +1,6 @@ using BootstrapAdmin.DataAccess.Models; -namespace BootstrapAdmin.DataAccess.Core +namespace BootstrapAdmin.Web.Core { /// /// diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs new file mode 100644 index 00000000..f9fdadf8 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs @@ -0,0 +1,22 @@ +namespace BootstrapAdmin.Web.Core +{ + /// + /// + /// + public interface IUsers + { + /// + /// 通过用户名获取角色列表 + /// + /// + /// + List GetRoles(string userName); + + /// + /// 通过用户名获得授权 App 集合 + /// + /// + /// + List GetApps(string userName); + } +} diff --git a/src/blazor/admin/BootstrapAdmin.Web/Services/AdminService.cs b/src/blazor/admin/BootstrapAdmin.Web/Services/AdminService.cs index 071d37ef..5b01fae1 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Services/AdminService.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Services/AdminService.cs @@ -1,21 +1,30 @@ using Bootstrap.Security.Blazor; +using BootstrapAdmin.Web.Core; namespace BootstrapAdmin.Web.Services { class AdminService : IBootstrapAdminService { + private IUsers User { get; set; } + + /// + /// + /// + /// + public AdminService(IUsers user) => User = user; + /// /// 通过当前登录用户名获取角色集合方法 /// /// /// - public List GetRolesByUserName(string userName) => new List() { "Administrators" }; + public List GetRoles(string userName) => User.GetRoles(userName); /// - /// + /// 通过当前登录用户名获取授权 App 集合方法 /// /// /// - public List GetAppsByUserName(string userName) => new List() { "BA" }; + public List GetApps(string userName) => User.GetApps(userName); } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs b/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs index 7e6d438b..2f4042ce 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs @@ -16,7 +16,7 @@ /// public BootstrapAppContext(IConfiguration configuration) { - AppId = configuration.GetValue("AppId", "demo"); + AppId = configuration.GetValue("AppId", "BA"); } } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs index aa0838e3..d80eb101 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs @@ -1,4 +1,4 @@ -using BootstrapAdmin.DataAccess.Core; +using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Extensions; namespace BootstrapAdmin.Web.Shared