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,8 +2,8 @@
using BootstrapAdmin.Web.Core;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
{
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
class GroupService : BaseDatabase, IGroup
{
/// <summary>
@ -82,4 +82,3 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services
return ret;
}
}
}

View File

@ -3,8 +3,8 @@ using BootstrapAdmin.Web.Core;
using Longbow.Security.Cryptography;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
{
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
class UserService : BaseDatabase, IUser
{
/// <summary>
@ -94,5 +94,36 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services
}
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,8 +1,7 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapBlazor.Components;
namespace BootstrapAdmin.Web.Core
{
namespace BootstrapAdmin.Web.Core;
/// <summary>
///
/// </summary>
@ -32,16 +31,32 @@ namespace BootstrapAdmin.Web.Core
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="groupId"></param>
/// <returns></returns>
List<string> GetUsersByGroupId(string? id);
List<string> GetUsersByGroupId(string? groupId);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="groupId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
bool SaveUsersByGroupId(string? id, IEnumerable<string> userIds);
bool SaveUsersByGroupId(string? groupId, IEnumerable<string> userIds);
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
List<string> GetUsersByRoleId(string? roleId);
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
bool SaveUsersByRoleId(string? roleId, IEnumerable<string> userIds);
/// <summary>
/// 获得所有用户
@ -57,4 +72,3 @@ namespace BootstrapAdmin.Web.Core
/// <returns></returns>
bool Authenticate(string userName, string password);
}
}

View File

@ -1,10 +1,9 @@
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
{
[Inject]
@ -19,14 +18,20 @@ namespace BootstrapAdmin.Web.Pages.Admin
[NotNull]
private IGroup? GroupService { get; set; }
[Inject]
[NotNull]
private IUser? UserService { get; set; }
private async Task OnAssignmentUsers(Role role)
{
var option = new DialogOption()
{
Title = $"分配用户 - {role}",
};
var users = UserService.GetAll().ToSelectedItemList();
var values = UserService.GetUsersByRoleId(role.Id);
await DialogService.Show(option);
await DialogService.ShowAssignmentDialog($"分配部门 - {role.RoleName}", users, values, () =>
{
var ret = UserService.SaveUsersByRoleId(role.Id, values);
return Task.FromResult(ret);
}, ToastService);
}
private async Task OnAssignmentGroups(Role role)
@ -62,4 +67,3 @@ namespace BootstrapAdmin.Web.Pages.Admin
}
}
}

View File

@ -2,8 +2,8 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions;
namespace BootstrapAdmin.Web.Pages.Admin
{
namespace BootstrapAdmin.Web.Pages.Admin;
/// <summary>
///
/// </summary>
@ -49,4 +49,3 @@ namespace BootstrapAdmin.Web.Pages.Admin
}, ToastService);
}
}
}