diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs index 039db795..e6e8af40 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs @@ -2,84 +2,83 @@ using BootstrapAdmin.Web.Core; using PetaPoco; -namespace BootstrapAdmin.DataAccess.PetaPoco.Services +namespace BootstrapAdmin.DataAccess.PetaPoco.Services; + +class GroupService : BaseDatabase, IGroup { - class GroupService : BaseDatabase, IGroup + /// + /// + /// + /// + public GroupService(IDatabase db) => Database = db; + + /// + /// + /// + /// + public List GetAll() => Database.Fetch(); + + /// + /// + /// + /// + /// + public List GetGroupsByUserId(string? userId) => Database.Fetch("select GroupID from UserGroup where UserID = @0", userId); + + /// + /// + /// + /// + /// + /// + public bool SaveGroupsByUserId(string? userId, IEnumerable groupIds) { - /// - /// - /// - /// - public GroupService(IDatabase db) => Database = db; - - /// - /// - /// - /// - public List GetAll() => Database.Fetch(); - - /// - /// - /// - /// - /// - public List GetGroupsByUserId(string? userId) => Database.Fetch("select GroupID from UserGroup where UserID = @0", userId); - - /// - /// - /// - /// - /// - /// - public bool SaveGroupsByUserId(string? userId, IEnumerable groupIds) + var ret = false; + try { - var ret = false; - try - { - Database.BeginTransaction(); - Database.Execute("delete from UserGroup where UserID = @0", userId); - Database.InsertBatch("UserGroup", groupIds.Select(g => new { GroupID = g, UserID = userId })); - Database.CompleteTransaction(); - ret = true; - } - catch (Exception) - { - Database.AbortTransaction(); - throw; - } - return ret; + Database.BeginTransaction(); + Database.Execute("delete from UserGroup where UserID = @0", userId); + Database.InsertBatch("UserGroup", groupIds.Select(g => new { GroupID = g, UserID = userId })); + Database.CompleteTransaction(); + ret = true; } - - /// - /// - /// - /// - /// - public List GetGroupsByRoleId(string? roleId) => Database.Fetch("select GroupID from RoleGroup where RoleGroup = @0", roleId); - - /// - /// - /// - /// - /// - /// - public bool SaveGroupsByRoleId(string? roleId, IEnumerable groupIds) + catch (Exception) { - var ret = false; - try - { - Database.BeginTransaction(); - Database.Execute("delete from RoleGroup where RoleGroup = @0", roleId); - Database.InsertBatch("RoleGroup", groupIds.Select(g => new { GroupID = g, RoleID = roleId })); - Database.CompleteTransaction(); - ret = true; - } - catch (Exception) - { - Database.AbortTransaction(); - throw; - } - return ret; + Database.AbortTransaction(); + throw; } + return ret; + } + + /// + /// + /// + /// + /// + public List GetGroupsByRoleId(string? roleId) => Database.Fetch("select GroupID from RoleGroup where RoleGroup = @0", roleId); + + /// + /// + /// + /// + /// + /// + public bool SaveGroupsByRoleId(string? roleId, IEnumerable groupIds) + { + var ret = false; + try + { + Database.BeginTransaction(); + Database.Execute("delete from RoleGroup where RoleGroup = @0", roleId); + Database.InsertBatch("RoleGroup", groupIds.Select(g => new { GroupID = g, RoleID = roleId })); + Database.CompleteTransaction(); + ret = true; + } + catch (Exception) + { + Database.AbortTransaction(); + throw; + } + return ret; } } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs index c572bd96..d26e301d 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs @@ -3,96 +3,127 @@ using BootstrapAdmin.Web.Core; using Longbow.Security.Cryptography; using PetaPoco; -namespace BootstrapAdmin.DataAccess.PetaPoco.Services +namespace BootstrapAdmin.DataAccess.PetaPoco.Services; + +class UserService : BaseDatabase, IUser { - class UserService : BaseDatabase, IUser + /// + /// + /// + /// + public UserService(IDatabase db) => Database = db; + + /// + /// + /// + /// + public List GetAll() => Database.Fetch(); + + /// + /// + /// + /// + /// + /// + /// + public bool Authenticate(string userName, string password) { - /// - /// - /// - /// - public UserService(IDatabase db) => Database = db; + var user = Database.SingleOrDefault("select DisplayName, Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName); - /// - /// - /// - /// - public List GetAll() => Database.Fetch(); - - /// - /// - /// - /// - /// - /// - /// - public bool Authenticate(string userName, string password) + var isAuth = false; + if (user != null && !string.IsNullOrEmpty(user.PassSalt)) { - var user = Database.SingleOrDefault("select DisplayName, Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName); - - var isAuth = false; - if (user != null && !string.IsNullOrEmpty(user.PassSalt)) - { - isAuth = user.Password == LgbCryptography.ComputeHash(password, user.PassSalt); - } - return isAuth; + isAuth = user.Password == LgbCryptography.ComputeHash(password, user.PassSalt); } + return isAuth; + } - /// - /// - /// - /// - /// - public string? GetDisplayName(string? userName) => string.IsNullOrEmpty(userName) ? "" : Database.ExecuteScalar("select DisplayName from Users where UserName = @0", userName); + /// + /// + /// + /// + /// + public string? GetDisplayName(string? userName) => string.IsNullOrEmpty(userName) ? "" : Database.ExecuteScalar("select DisplayName from Users where UserName = @0", userName); - /// - /// - /// - /// - /// - /// - 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 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); + /// + /// + /// + /// + /// + /// + 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); - /// - /// - /// - /// - /// - /// - public List GetUsersByGroupId(string? id) => Database.Fetch("select UserID from UserGroup where GroupID = @0", id); + /// + /// + /// + /// + /// + /// + public List GetUsersByGroupId(string? id) => Database.Fetch("select UserID from UserGroup where GroupID = @0", id); - /// - /// - /// - /// - /// - /// - /// - public bool SaveUsersByGroupId(string? id, IEnumerable userIds) + /// + /// + /// + /// + /// + /// + /// + public bool SaveUsersByGroupId(string? id, IEnumerable userIds) + { + var ret = false; + try { - var ret = false; - try - { - Database.BeginTransaction(); - Database.Execute("delete from UserGroup where GroupId = @0", id); - Database.InsertBatch("UserGroup", userIds.Select(g => new { UserID = g, GroupID = id })); - Database.CompleteTransaction(); - ret = true; - } - catch (Exception) - { - Database.AbortTransaction(); - throw; - } - return ret; + Database.BeginTransaction(); + Database.Execute("delete from UserGroup where GroupId = @0", id); + Database.InsertBatch("UserGroup", userIds.Select(g => new { UserID = g, GroupID = id })); + Database.CompleteTransaction(); + ret = true; } + catch (Exception) + { + Database.AbortTransaction(); + throw; + } + return ret; + } + + /// + /// + /// + /// + /// + public List GetUsersByRoleId(string? roleId) => Database.Fetch("select UserID from UserRole where RoleID = @0", roleId); + + /// + /// + /// + /// + /// + /// + public bool SaveUsersByRoleId(string? roleId, IEnumerable userIds) + { + var ret = false; + try + { + Database.BeginTransaction(); + Database.Execute("delete from UserRole where RoleID = @0", roleId); + Database.InsertBatch("UserRole", userIds.Select(g => new { UserID = g, RoleID = roleId })); + Database.CompleteTransaction(); + ret = true; + } + catch (Exception) + { + Database.AbortTransaction(); + throw; + } + return ret; } } diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs index 022f98b1..badbd53a 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs @@ -1,60 +1,74 @@ using BootstrapAdmin.DataAccess.Models; -using BootstrapBlazor.Components; -namespace BootstrapAdmin.Web.Core +namespace BootstrapAdmin.Web.Core; + +/// +/// +/// +public interface IUser { /// /// /// - public interface IUser - { - /// - /// - /// - /// - /// - string? GetDisplayName(string? userName); + /// + /// + string? GetDisplayName(string? userName); - /// - /// 通过用户名获取角色列表 - /// - /// - /// - List GetRoles(string userName); + /// + /// 通过用户名获取角色列表 + /// + /// + /// + List GetRoles(string userName); - /// - /// 通过用户名获得授权 App 集合 - /// - /// - /// - List GetApps(string userName); + /// + /// 通过用户名获得授权 App 集合 + /// + /// + /// + List GetApps(string userName); - /// - /// - /// - /// - /// - List GetUsersByGroupId(string? id); + /// + /// + /// + /// + /// + List GetUsersByGroupId(string? groupId); - /// - /// - /// - /// - /// - bool SaveUsersByGroupId(string? id, IEnumerable userIds); + /// + /// + /// + /// + /// + /// + bool SaveUsersByGroupId(string? groupId, IEnumerable userIds); - /// - /// 获得所有用户 - /// - /// - List GetAll(); + /// + /// + /// + /// + /// + List GetUsersByRoleId(string? roleId); - /// - /// 认证方法 - /// - /// - /// - /// - bool Authenticate(string userName, string password); - } + /// + /// + /// + /// + /// + /// + bool SaveUsersByRoleId(string? roleId, IEnumerable userIds); + + /// + /// 获得所有用户 + /// + /// + List GetAll(); + + /// + /// 认证方法 + /// + /// + /// + /// + bool Authenticate(string userName, string password); } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Roles.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Roles.razor.cs index 52e34231..7a5f037c 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Roles.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Roles.razor.cs @@ -1,65 +1,69 @@ using BootstrapAdmin.DataAccess.Models; -using BootstrapAdmin.Web.Components; using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Extensions; -namespace BootstrapAdmin.Web.Pages.Admin +namespace BootstrapAdmin.Web.Pages.Admin; + +public partial class Roles { - public partial class Roles + [Inject] + [NotNull] + private DialogService? DialogService { get; set; } + + [Inject] + [NotNull] + private ToastService? ToastService { get; set; } + + [Inject] + [NotNull] + private IGroup? GroupService { get; set; } + + [Inject] + [NotNull] + private IUser? UserService { get; set; } + + private async Task OnAssignmentUsers(Role role) { - [Inject] - [NotNull] - private DialogService? DialogService { get; set; } + var users = UserService.GetAll().ToSelectedItemList(); + var values = UserService.GetUsersByRoleId(role.Id); - [Inject] - [NotNull] - private ToastService? ToastService { get; set; } - - [Inject] - [NotNull] - private IGroup? GroupService { get; set; } - - private async Task OnAssignmentUsers(Role role) + await DialogService.ShowAssignmentDialog($"分配部门 - {role.RoleName}", users, values, () => { - var option = new DialogOption() - { - Title = $"分配用户 - {role}", - }; - - await DialogService.Show(option); - } - - private async Task OnAssignmentGroups(Role role) - { - var groups = GroupService.GetAll().ToSelectedItemList(); - var values = GroupService.GetGroupsByRoleId(role.Id); - - await DialogService.ShowAssignmentDialog($"分配部门 - {role.RoleName}", groups, values, () => - { - var ret = GroupService.SaveGroupsByRoleId(role.Id, values); - return Task.FromResult(ret); - }, ToastService); - } - - private async Task OnAssignmentMenus(Role role) - { - var option = new DialogOption() - { - Title = $"分配菜单 - {role}", - }; - - await DialogService.Show(option); - } - - private async Task OnAssignmentApps(Role role) - { - var option = new DialogOption() - { - Title = $"分配应用 - {role}", - }; - - await DialogService.Show(option); - } - + var ret = UserService.SaveUsersByRoleId(role.Id, values); + return Task.FromResult(ret); + }, ToastService); } + + private async Task OnAssignmentGroups(Role role) + { + var groups = GroupService.GetAll().ToSelectedItemList(); + var values = GroupService.GetGroupsByRoleId(role.Id); + + await DialogService.ShowAssignmentDialog($"分配部门 - {role.RoleName}", groups, values, () => + { + var ret = GroupService.SaveGroupsByRoleId(role.Id, values); + return Task.FromResult(ret); + }, ToastService); + } + + private async Task OnAssignmentMenus(Role role) + { + var option = new DialogOption() + { + Title = $"分配菜单 - {role}", + }; + + await DialogService.Show(option); + } + + private async Task OnAssignmentApps(Role role) + { + var option = new DialogOption() + { + Title = $"分配应用 - {role}", + }; + + await DialogService.Show(option); + } + } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs index f3e641cb..a679a8de 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs @@ -2,51 +2,50 @@ using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Extensions; -namespace BootstrapAdmin.Web.Pages.Admin +namespace BootstrapAdmin.Web.Pages.Admin; + +/// +/// +/// +public partial class Users { - /// - /// - /// - public partial class Users + [Inject] + [NotNull] + private DialogService? DialogService { get; set; } + + [Inject] + [NotNull] + private ToastService? ToastService { get; set; } + + [Inject] + [NotNull] + private IGroup? GroupService { get; set; } + + [Inject] + [NotNull] + private IRole? RoleService { get; set; } + + private async Task OnAssignmentGroups(User user) { - [Inject] - [NotNull] - private DialogService? DialogService { get; set; } + var groups = GroupService.GetAll().ToSelectedItemList(); + var values = GroupService.GetGroupsByUserId(user.Id); - [Inject] - [NotNull] - private ToastService? ToastService { get; set; } - - [Inject] - [NotNull] - private IGroup? GroupService { get; set; } - - [Inject] - [NotNull] - private IRole? RoleService { get; set; } - - private async Task OnAssignmentGroups(User user) + await DialogService.ShowAssignmentDialog($"分配部门 - {user}", groups, values, () => { - var groups = GroupService.GetAll().ToSelectedItemList(); - var values = GroupService.GetGroupsByUserId(user.Id); + var ret = GroupService.SaveGroupsByUserId(user.Id, values); + return Task.FromResult(ret); + }, ToastService); + } - await DialogService.ShowAssignmentDialog($"分配部门 - {user}", groups, values, () => - { - var ret = GroupService.SaveGroupsByUserId(user.Id, values); - return Task.FromResult(ret); - }, ToastService); - } + private async Task OnAssignmentRoles(User user) + { + var groups = RoleService.GetAll().ToSelectedItemList(); + var values = RoleService.GetRolesByUserId(user.Id); - private async Task OnAssignmentRoles(User user) + await DialogService.ShowAssignmentDialog($"分配角色 - {user}", groups, values, () => { - var groups = RoleService.GetAll().ToSelectedItemList(); - var values = RoleService.GetRolesByUserId(user.Id); - - await DialogService.ShowAssignmentDialog($"分配角色 - {user}", groups, values, () => - { - var ret = RoleService.SaveRolesByUserId(user.Id, values); - return Task.FromResult(ret); - }, ToastService); - } + var ret = RoleService.SaveRolesByUserId(user.Id, values); + return Task.FromResult(ret); + }, ToastService); } }