feat: 增加菜单授权角色功能

This commit is contained in:
Argo-Tianyi 2021-12-21 12:30:13 +08:00
parent 77f1f2e0d9
commit 0b828ff38c
6 changed files with 63 additions and 14 deletions

View File

@ -81,4 +81,25 @@ class RoleService : BaseDatabase, IRole
} }
return ret; return ret;
} }
public List<string> GetRolesByMenuId(string? menuId) => Database.Fetch<string>("select RoleID from NavigationRole where NavigationID = @0", menuId);
public bool SaveRolesByMenuId(string? menuId, IEnumerable<string> roleIds)
{
var ret = false;
try
{
Database.BeginTransaction();
Database.Execute("delete from NavigationRole where NavigationID = @0", menuId);
Database.InsertBatch("NavigationRole", roleIds.Select(g => new { RoleID = g, NavigationID = menuId }));
Database.CompleteTransaction();
ret = true;
}
catch (Exception)
{
Database.AbortTransaction();
throw;
}
return ret;
}
} }

View File

@ -31,7 +31,7 @@ public interface IRole
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="userId"></param> /// <param name="groupId"></param>
/// <returns></returns> /// <returns></returns>
List<string> GetRolesByUserId(string? groupId); List<string> GetRolesByUserId(string? groupId);
@ -42,4 +42,19 @@ public interface IRole
/// <param name="roleIds"></param> /// <param name="roleIds"></param>
/// <returns></returns> /// <returns></returns>
bool SaveRolesByUserId(string? groupId, IEnumerable<string> roleIds); bool SaveRolesByUserId(string? groupId, IEnumerable<string> roleIds);
/// <summary>
///
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
List<string> GetRolesByMenuId(string? menuId);
/// <summary>
///
/// </summary>
/// <param name="menuId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
bool SaveRolesByMenuId(string? menuId, IEnumerable<string> roleIds);
} }

View File

@ -27,7 +27,7 @@ public partial class Groups
var users = UserService.GetAll().ToSelectedItemList(); var users = UserService.GetAll().ToSelectedItemList();
var values = UserService.GetUsersByGroupId(group.Id); var values = UserService.GetUsersByGroupId(group.Id);
await DialogService.ShowAssignmentDialog($"分配用户 - {group}", users, values, () => await DialogService.ShowAssignmentDialog($"分配用户 - 当前部门: {group}", users, values, () =>
{ {
var ret = UserService.SaveUsersByGroupId(group.Id, values); var ret = UserService.SaveUsersByGroupId(group.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);
@ -39,7 +39,7 @@ public partial class Groups
var users = RoleService.GetAll().ToSelectedItemList(); var users = RoleService.GetAll().ToSelectedItemList();
var values = RoleService.GetRolesByGroupId(group.Id); var values = RoleService.GetRolesByGroupId(group.Id);
await DialogService.ShowAssignmentDialog($"分配角色 - {group}", users, values, () => await DialogService.ShowAssignmentDialog($"分配角色 - 当前部门: {group}", users, values, () =>
{ {
var ret = RoleService.SaveRolesByGroupId(group.Id, values); var ret = RoleService.SaveRolesByGroupId(group.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);

View File

@ -1,4 +1,7 @@
namespace BootstrapAdmin.Web.Pages.Admin; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions;
namespace BootstrapAdmin.Web.Pages.Admin;
public partial class Menus public partial class Menus
{ {
@ -6,13 +9,23 @@ public partial class Menus
[NotNull] [NotNull]
private DialogService? DialogService { get; set; } private DialogService? DialogService { get; set; }
[Inject]
[NotNull]
private ToastService? ToastService { get; set; }
[Inject]
[NotNull]
private IRole? RoleService { get; set; }
private async Task OnAssignmentRoles(DataAccess.Models.Navigation menu) private async Task OnAssignmentRoles(DataAccess.Models.Navigation menu)
{ {
var option = new DialogOption() var roles = RoleService.GetAll().ToSelectedItemList();
{ var values = RoleService.GetRolesByMenuId(menu.Id);
Title = $"分配角色 - {menu}",
};
await DialogService.Show(option); await DialogService.ShowAssignmentDialog($"分配角色 - 当前菜单: {menu.Name}", roles, values, () =>
{
var ret = RoleService.SaveRolesByMenuId(menu.Id, values);
return Task.FromResult(ret);
}, ToastService);
} }
} }

View File

@ -31,7 +31,7 @@ public partial class Roles
var users = UserService.GetAll().ToSelectedItemList(); var users = UserService.GetAll().ToSelectedItemList();
var values = UserService.GetUsersByRoleId(role.Id); var values = UserService.GetUsersByRoleId(role.Id);
await DialogService.ShowAssignmentDialog($"分配用户 - {role.RoleName}", users, values, () => await DialogService.ShowAssignmentDialog($"分配用户 - 当前角色: {role.RoleName}", users, values, () =>
{ {
var ret = UserService.SaveUsersByRoleId(role.Id, values); var ret = UserService.SaveUsersByRoleId(role.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);
@ -43,7 +43,7 @@ public partial class Roles
var groups = GroupService.GetAll().ToSelectedItemList(); var groups = GroupService.GetAll().ToSelectedItemList();
var values = GroupService.GetGroupsByRoleId(role.Id); var values = GroupService.GetGroupsByRoleId(role.Id);
await DialogService.ShowAssignmentDialog($"分配部门 - {role.RoleName}", groups, values, () => await DialogService.ShowAssignmentDialog($"分配部门 - 当前角色: {role.RoleName}", groups, values, () =>
{ {
var ret = GroupService.SaveGroupsByRoleId(role.Id, values); var ret = GroupService.SaveGroupsByRoleId(role.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);
@ -65,7 +65,7 @@ public partial class Roles
var apps = AppService.GetAll(); var apps = AppService.GetAll();
var values = AppService.GetAppsByRoleId(role.Id); var values = AppService.GetAppsByRoleId(role.Id);
await DialogService.ShowAssignmentDialog($"分配应用 - {role.RoleName}", apps, values, () => await DialogService.ShowAssignmentDialog($"分配应用 - 当前角色: {role.RoleName}", apps, values, () =>
{ {
var ret = AppService.SaveAppsByRoleId(role.Id, values); var ret = AppService.SaveAppsByRoleId(role.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);

View File

@ -30,7 +30,7 @@ public partial class Users
var groups = GroupService.GetAll().ToSelectedItemList(); var groups = GroupService.GetAll().ToSelectedItemList();
var values = GroupService.GetGroupsByUserId(user.Id); var values = GroupService.GetGroupsByUserId(user.Id);
await DialogService.ShowAssignmentDialog($"分配部门 - {user}", groups, values, () => await DialogService.ShowAssignmentDialog($"分配部门 - 当前用户: {user}", groups, values, () =>
{ {
var ret = GroupService.SaveGroupsByUserId(user.Id, values); var ret = GroupService.SaveGroupsByUserId(user.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);
@ -42,7 +42,7 @@ public partial class Users
var groups = RoleService.GetAll().ToSelectedItemList(); var groups = RoleService.GetAll().ToSelectedItemList();
var values = RoleService.GetRolesByUserId(user.Id); var values = RoleService.GetRolesByUserId(user.Id);
await DialogService.ShowAssignmentDialog($"分配角色 - {user}", groups, values, () => await DialogService.ShowAssignmentDialog($"分配角色 - 当前用户: {user}", groups, values, () =>
{ {
var ret = RoleService.SaveRolesByUserId(user.Id, values); var ret = RoleService.SaveRolesByUserId(user.Id, values);
return Task.FromResult(ret); return Task.FromResult(ret);