refactor: 重构服务基类增加用户服务

This commit is contained in:
Argo-Lenovo 2021-12-14 16:09:41 +08:00
parent 0b1a110b22
commit 33054356d0
14 changed files with 98 additions and 19 deletions

View File

@ -1,6 +1,6 @@
using BootstrapAdmin.DataAccess.EFCore; using BootstrapAdmin.DataAccess.EFCore;
using BootstrapAdmin.DataAccess.EFCore.Services; using BootstrapAdmin.DataAccess.EFCore.Services;
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.Web.Core;
using BootstrapBlazor.Components; using BootstrapBlazor.Components;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;

View File

@ -1,4 +1,4 @@
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.Web.Core;
namespace BootstrapAdmin.DataAccess.EFCore.Services namespace BootstrapAdmin.DataAccess.EFCore.Services
{ {

View File

@ -1,4 +1,4 @@
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;

View File

@ -1,6 +1,6 @@
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.DataAccess.PetaPoco;
using BootstrapAdmin.DataAccess.PetaPoco;
using BootstrapAdmin.DataAccess.PetaPoco.Services; using BootstrapAdmin.DataAccess.PetaPoco.Services;
using BootstrapAdmin.Web.Core;
using BootstrapBlazor.Components; using BootstrapBlazor.Components;
using BootstrapBlazor.DataAcces.PetaPoco.Services; using BootstrapBlazor.DataAcces.PetaPoco.Services;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -35,6 +35,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<INavigations, NavigationsService>(); services.AddSingleton<INavigations, NavigationsService>();
services.AddSingleton<IDicts, DictsService>(); services.AddSingleton<IDicts, DictsService>();
services.AddSingleton<IUsers, UsersService>();
return services; return services;
} }
} }

View File

@ -0,0 +1,13 @@
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
{
abstract class BaseDatabase
{
/// <summary>
///
/// </summary>
[NotNull]
protected IDatabase? Database { get; set; }
}
}

View File

@ -1,8 +1,14 @@
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.Web.Core;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services namespace BootstrapAdmin.DataAccess.PetaPoco.Services
{ {
class DictsService : IDicts class DictsService : BaseDatabase, IDicts
{ {
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public DictsService(IDatabase db) => Database = db;
} }
} }

View File

@ -1,5 +1,5 @@
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.Web.Core;
using PetaPoco; using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services namespace BootstrapAdmin.DataAccess.PetaPoco.Services
@ -7,10 +7,8 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
class NavigationsService : INavigations class NavigationsService : BaseDatabase, INavigations
{ {
private IDatabase Database { get; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -0,0 +1,30 @@
using BootstrapAdmin.Web.Core;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
{
class UsersService : BaseDatabase, IUsers
{
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public UsersService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetApps(string userName) => Database.Fetch<string>($"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");
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetRoles(string userName) => Database.Fetch<string>($"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);
}
}

View File

@ -1,4 +1,4 @@
namespace BootstrapAdmin.DataAccess.Core namespace BootstrapAdmin.Web.Core
{ {
/// <summary> /// <summary>
/// Dict 字典表接口 /// Dict 字典表接口

View File

@ -1,6 +1,6 @@
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
namespace BootstrapAdmin.DataAccess.Core namespace BootstrapAdmin.Web.Core
{ {
/// <summary> /// <summary>
/// ///

View File

@ -0,0 +1,22 @@
namespace BootstrapAdmin.Web.Core
{
/// <summary>
///
/// </summary>
public interface IUsers
{
/// <summary>
/// 通过用户名获取角色列表
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
List<string> GetRoles(string userName);
/// <summary>
/// 通过用户名获得授权 App 集合
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
List<string> GetApps(string userName);
}
}

View File

@ -1,21 +1,30 @@
using Bootstrap.Security.Blazor; using Bootstrap.Security.Blazor;
using BootstrapAdmin.Web.Core;
namespace BootstrapAdmin.Web.Services namespace BootstrapAdmin.Web.Services
{ {
class AdminService : IBootstrapAdminService class AdminService : IBootstrapAdminService
{ {
private IUsers User { get; set; }
/// <summary>
///
/// </summary>
/// <param name="user"></param>
public AdminService(IUsers user) => User = user;
/// <summary> /// <summary>
/// 通过当前登录用户名获取角色集合方法 /// 通过当前登录用户名获取角色集合方法
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public List<string> GetRolesByUserName(string userName) => new List<string>() { "Administrators" }; public List<string> GetRoles(string userName) => User.GetRoles(userName);
/// <summary> /// <summary>
/// /// 通过当前登录用户名获取授权 App 集合方法
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public List<string> GetAppsByUserName(string userName) => new List<string>() { "BA" }; public List<string> GetApps(string userName) => User.GetApps(userName);
} }
} }

View File

@ -16,7 +16,7 @@
/// <param name="configuration"></param> /// <param name="configuration"></param>
public BootstrapAppContext(IConfiguration configuration) public BootstrapAppContext(IConfiguration configuration)
{ {
AppId = configuration.GetValue("AppId", "demo"); AppId = configuration.GetValue("AppId", "BA");
} }
} }
} }

View File

@ -1,4 +1,4 @@
using BootstrapAdmin.DataAccess.Core; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions; using BootstrapAdmin.Web.Extensions;
namespace BootstrapAdmin.Web.Shared namespace BootstrapAdmin.Web.Shared