feat: 增加角色对用户授权功能

This commit is contained in:
Argo-Tianyi 2021-12-21 12:02:04 +08:00
parent f2095cac40
commit 570775ad1b
5 changed files with 340 additions and 293 deletions

View File

@ -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
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public GroupService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Group> GetAll() => Database.Fetch<Group>();
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public List<string> GetGroupsByUserId(string? userId) => Database.Fetch<string>("select GroupID from UserGroup where UserID = @0", userId);
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByUserId(string? userId, IEnumerable<string> groupIds)
{
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public GroupService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Group> GetAll() => Database.Fetch<Group>();
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public List<string> GetGroupsByUserId(string? userId) => Database.Fetch<string>("select GroupID from UserGroup where UserID = @0", userId);
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByUserId(string? userId, IEnumerable<string> 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;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public List<string> GetGroupsByRoleId(string? roleId) => Database.Fetch<string>("select GroupID from RoleGroup where RoleGroup = @0", roleId);
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByRoleId(string? roleId, IEnumerable<string> 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;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public List<string> GetGroupsByRoleId(string? roleId) => Database.Fetch<string>("select GroupID from RoleGroup where RoleGroup = @0", roleId);
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByRoleId(string? roleId, IEnumerable<string> 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;
}
}

View File

@ -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
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public UserService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<User> GetAll() => Database.Fetch<User>();
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Authenticate(string userName, string password)
{
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public UserService(IDatabase db) => Database = db;
var user = Database.SingleOrDefault<User>("select DisplayName, Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName);
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<User> GetAll() => Database.Fetch<User>();
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Authenticate(string userName, string password)
var isAuth = false;
if (user != null && !string.IsNullOrEmpty(user.PassSalt))
{
var user = Database.SingleOrDefault<User>("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;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public string? GetDisplayName(string? userName) => string.IsNullOrEmpty(userName) ? "" : Database.ExecuteScalar<string>("select DisplayName from Users where UserName = @0", userName);
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public string? GetDisplayName(string? userName) => string.IsNullOrEmpty(userName) ? "" : Database.ExecuteScalar<string>("select DisplayName from Users where UserName = @0", userName);
/// <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> 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);
/// <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);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetUsersByGroupId(string? id) => Database.Fetch<string>("select UserID from UserGroup where GroupID = @0", id);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetUsersByGroupId(string? id) => Database.Fetch<string>("select UserID from UserGroup where GroupID = @0", id);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="userIds"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveUsersByGroupId(string? id, IEnumerable<string> userIds)
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="userIds"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveUsersByGroupId(string? id, IEnumerable<string> 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;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public List<string> GetUsersByRoleId(string? roleId) => Database.Fetch<string>("select UserID from UserRole where RoleID = @0", roleId);
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
public bool SaveUsersByRoleId(string? roleId, IEnumerable<string> 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;
}
}

View File

@ -1,60 +1,74 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapBlazor.Components;
namespace BootstrapAdmin.Web.Core
namespace BootstrapAdmin.Web.Core;
/// <summary>
///
/// </summary>
public interface IUser
{
/// <summary>
///
/// </summary>
public interface IUser
{
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
string? GetDisplayName(string? userName);
/// <param name="userName"></param>
/// <returns></returns>
string? GetDisplayName(string? userName);
/// <summary>
/// 通过用户名获取角色列表
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
List<string> GetRoles(string userName);
/// <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);
/// <summary>
/// 通过用户名获得授权 App 集合
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
List<string> GetApps(string userName);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
List<string> GetUsersByGroupId(string? id);
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
List<string> GetUsersByGroupId(string? groupId);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
bool SaveUsersByGroupId(string? id, IEnumerable<string> userIds);
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
bool SaveUsersByGroupId(string? groupId, IEnumerable<string> userIds);
/// <summary>
/// 获得所有用户
/// </summary>
/// <returns></returns>
List<User> GetAll();
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
List<string> GetUsersByRoleId(string? roleId);
/// <summary>
/// 认证方法
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
bool Authenticate(string userName, string password);
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
bool SaveUsersByRoleId(string? roleId, IEnumerable<string> userIds);
/// <summary>
/// 获得所有用户
/// </summary>
/// <returns></returns>
List<User> GetAll();
/// <summary>
/// 认证方法
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
bool Authenticate(string userName, string password);
}

View File

@ -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);
}
}

View File

@ -2,51 +2,50 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions;
namespace BootstrapAdmin.Web.Pages.Admin
namespace BootstrapAdmin.Web.Pages.Admin;
/// <summary>
///
/// </summary>
public partial class Users
{
/// <summary>
///
/// </summary>
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);
}
}