feat: 更新 IRole 接口方法
This commit is contained in:
parent
30614bd223
commit
6029e86072
|
@ -2,54 +2,83 @@
|
||||||
using BootstrapAdmin.Web.Core;
|
using BootstrapAdmin.Web.Core;
|
||||||
using PetaPoco;
|
using PetaPoco;
|
||||||
|
|
||||||
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
|
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
|
||||||
|
|
||||||
|
class RoleService : BaseDatabase, IRole
|
||||||
{
|
{
|
||||||
class RoleService : BaseDatabase, IRole
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="db"></param>
|
||||||
|
public RoleService(IDatabase db) => Database = db;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public List<Role> GetAll() => Database.Fetch<Role>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="groupId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public List<string> GetRolesByGroupId(string? groupId) => Database.Fetch<string>("select RoleID from RoleGroup where GroupID = @0", groupId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="groupId"></param>
|
||||||
|
/// <param name="roleIds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds)
|
||||||
{
|
{
|
||||||
/// <summary>
|
var ret = false;
|
||||||
///
|
try
|
||||||
/// </summary>
|
|
||||||
/// <param name="db"></param>
|
|
||||||
public RoleService(IDatabase db) => Database = db;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public List<Role> GetAll() => Database.Fetch<Role>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
/// <exception cref="NotImplementedException"></exception>
|
|
||||||
public List<string> GetUsersByRoleId(string? id) => Database.Fetch<string>("select UserID from UserRole where RoleID = @0", id);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <param name="values"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
/// <exception cref="NotImplementedException"></exception>
|
|
||||||
public bool SaveUsersByRoleId(string? id, IEnumerable<string> userIds)
|
|
||||||
{
|
{
|
||||||
var ret = false;
|
Database.BeginTransaction();
|
||||||
try
|
Database.Execute("delete from RoleGroup where GroupID = @0", groupId);
|
||||||
{
|
Database.InsertBatch("RoleGroup", roleIds.Select(g => new { RoleID = g, GroupID = groupId }));
|
||||||
Database.BeginTransaction();
|
Database.CompleteTransaction();
|
||||||
Database.Execute("delete from UserRole where RoleID = @0", id);
|
ret = true;
|
||||||
Database.InsertBatch("UserRole", userIds.Select(g => new { UserID = g, RoleID = id }));
|
|
||||||
Database.CompleteTransaction();
|
|
||||||
ret = true;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
Database.AbortTransaction();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
Database.AbortTransaction();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public List<string> GetRolesByUserId(string? userId) => Database.Fetch<string>("select RoleID from UserRole where UserID = @0", userId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <param name="roleIds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool SaveRolesByUserId(string? userId, IEnumerable<string> roleIds)
|
||||||
|
{
|
||||||
|
var ret = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Database.BeginTransaction();
|
||||||
|
Database.Execute("delete from UserRole where UserID = @0", userId);
|
||||||
|
Database.InsertBatch("UserRole", roleIds.Select(g => new { RoleID = g, UserID = userId }));
|
||||||
|
Database.CompleteTransaction();
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
Database.AbortTransaction();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,15 +16,30 @@ public interface IRole
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="userId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
List<string> GetUsersByRoleId(string? id);
|
List<string> GetRolesByGroupId(string? userId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="groupId"></param>
|
||||||
/// <param name="values"></param>
|
/// <param name="roleIds"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool SaveUsersByRoleId(string? id, IEnumerable<string> values);
|
bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
List<string> GetRolesByUserId(string? groupId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="groupId"></param>
|
||||||
|
/// <param name="roleIds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool SaveRolesByUserId(string? groupId, IEnumerable<string> roleIds);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,18 @@
|
||||||
using BootstrapAdmin.Web.Components;
|
namespace BootstrapAdmin.Web.Pages.Admin;
|
||||||
|
|
||||||
namespace BootstrapAdmin.Web.Pages.Admin
|
public partial class Menus
|
||||||
{
|
{
|
||||||
public partial class Menus
|
[Inject]
|
||||||
|
[NotNull]
|
||||||
|
private DialogService? DialogService { get; set; }
|
||||||
|
|
||||||
|
private async Task OnAssignmentRoles(DataAccess.Models.Navigation menu)
|
||||||
{
|
{
|
||||||
[Inject]
|
var option = new DialogOption()
|
||||||
[NotNull]
|
|
||||||
private DialogService? DialogService { get; set; }
|
|
||||||
|
|
||||||
private async Task OnAssignmentRoles(DataAccess.Models.Navigation menu)
|
|
||||||
{
|
{
|
||||||
var option = new DialogOption()
|
Title = $"分配角色 - {menu}",
|
||||||
{
|
};
|
||||||
Title = $"分配角色 - {menu}",
|
|
||||||
};
|
|
||||||
|
|
||||||
await DialogService.Show(option);
|
await DialogService.Show(option);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using BootstrapAdmin.DataAccess.Models;
|
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
|
||||||
{
|
{
|
||||||
|
@ -19,6 +20,11 @@ namespace BootstrapAdmin.Web.Pages.Admin
|
||||||
[Inject]
|
[Inject]
|
||||||
[NotNull]
|
[NotNull]
|
||||||
private IGroup? GroupService { get; set; }
|
private IGroup? GroupService { get; set; }
|
||||||
|
|
||||||
|
[Inject]
|
||||||
|
[NotNull]
|
||||||
|
private IRole? RoleService { get; set; }
|
||||||
|
|
||||||
private async Task OnAssignmentGroups(User user)
|
private async Task OnAssignmentGroups(User user)
|
||||||
{
|
{
|
||||||
var groups = GroupService.GetAll().ToSelectedItemList();
|
var groups = GroupService.GetAll().ToSelectedItemList();
|
||||||
|
@ -33,12 +39,14 @@ namespace BootstrapAdmin.Web.Pages.Admin
|
||||||
|
|
||||||
private async Task OnAssignmentRoles(User user)
|
private async Task OnAssignmentRoles(User user)
|
||||||
{
|
{
|
||||||
var option = new DialogOption()
|
var groups = RoleService.GetAll().ToSelectedItemList();
|
||||||
{
|
var values = RoleService.GetRolesByUserId(user.Id);
|
||||||
Title = $"分配角色 - {user}",
|
|
||||||
};
|
|
||||||
|
|
||||||
await DialogService.Show(option);
|
await DialogService.ShowAssignmentDialog($"分配角色 - {user}", groups, values, () =>
|
||||||
|
{
|
||||||
|
var ret = RoleService.SaveRolesByUserId(user.Id, values);
|
||||||
|
return Task.FromResult(ret);
|
||||||
|
}, ToastService);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue