feat: 增加角色对应用授权功能
This commit is contained in:
parent
570775ad1b
commit
77f1f2e0d9
|
@ -1,9 +1,15 @@
|
|||
using BootstrapAdmin.Web.Core;
|
||||
using BootstrapBlazor.Components;
|
||||
|
||||
namespace BootstrapAdmin.DataAccess.EFCore.Services
|
||||
{
|
||||
class DictsService : IDict
|
||||
{
|
||||
public List<SelectedItem> GetApps()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string GetWebFooter()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
|
|
@ -40,6 +40,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.AddSingleton<ILogin, LoginService>();
|
||||
services.AddSingleton<IRole, RoleService>();
|
||||
services.AddSingleton<IGroup, GroupService>();
|
||||
services.AddSingleton<IApp, AppService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
using BootstrapAdmin.DataAccess.Models;
|
||||
using BootstrapAdmin.Web.Core;
|
||||
using BootstrapBlazor.Components;
|
||||
using PetaPoco;
|
||||
|
||||
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
|
||||
|
||||
class AppService : BaseDatabase, IApp
|
||||
{
|
||||
private List<SelectedItem> Apps { get; set; }
|
||||
|
||||
public AppService(IDatabase db, IDict dict)
|
||||
{
|
||||
Database = db;
|
||||
Apps = dict.GetApps();
|
||||
}
|
||||
|
||||
public List<SelectedItem> GetAll() => Apps;
|
||||
|
||||
public List<string> GetAppsByRoleId(string? roleId) => Database.Fetch<string>("select AppID from RoleApp where RoleID = @0", roleId);
|
||||
|
||||
public bool SaveAppsByRoleId(string? roleId, IEnumerable<string> appIds)
|
||||
{
|
||||
var ret = false;
|
||||
try
|
||||
{
|
||||
Database.BeginTransaction();
|
||||
Database.Execute("delete from RoleApp where RoleID = @0", roleId);
|
||||
Database.InsertBatch("RoleApp", appIds.Select(g => new { AppID = g, RoleID = roleId }));
|
||||
Database.CompleteTransaction();
|
||||
ret = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Database.AbortTransaction();
|
||||
throw;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
|
@ -1,59 +1,65 @@
|
|||
using BootstrapAdmin.DataAccess.Models;
|
||||
using BootstrapAdmin.Web.Core;
|
||||
using BootstrapBlazor.Components;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using PetaPoco;
|
||||
|
||||
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
|
||||
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
|
||||
|
||||
class DictService : BaseDatabase, IDict
|
||||
{
|
||||
class DictService : BaseDatabase, IDict
|
||||
private string AppId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public DictService(IDatabase db, IConfiguration configuration)
|
||||
{
|
||||
private string AppId { get; set; }
|
||||
Database = db;
|
||||
AppId = configuration.GetValue("AppId", "BA");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public DictService(IDatabase db, IConfiguration configuration)
|
||||
private List<Dict> GetAll() => Database.Fetch<Dict>();
|
||||
|
||||
public List<SelectedItem> GetApps()
|
||||
{
|
||||
var dicts = GetAll();
|
||||
return dicts.Where(d => d.Category == "应用程序").Select(d => new SelectedItem(d.Code, d.Name)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 站点 Title 配置信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetWebTitle()
|
||||
{
|
||||
var dicts = GetAll();
|
||||
var title = "网站标题";
|
||||
var name = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == AppId)?.Name;
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
Database = db;
|
||||
AppId = configuration.GetValue("AppId", "BA");
|
||||
var dict = dicts.FirstOrDefault(d => d.Category == name && d.Name == "网站标题") ?? dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "网站标题");
|
||||
title = dict?.Code ?? "网站标题";
|
||||
}
|
||||
return title;
|
||||
}
|
||||
|
||||
private List<Dict> GetAll() => Database.Fetch<Dict>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取 站点 Title 配置信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetWebTitle()
|
||||
/// <summary>
|
||||
/// 获取站点 Footer 配置信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetWebFooter()
|
||||
{
|
||||
var dicts = GetAll();
|
||||
var title = "网站页脚";
|
||||
var name = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == AppId)?.Name;
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
var dicts = GetAll();
|
||||
var title = "网站标题";
|
||||
var name = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == AppId)?.Name;
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
var dict = dicts.FirstOrDefault(d => d.Category == name && d.Name == "网站标题") ?? dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "网站标题");
|
||||
title = dict?.Code ?? "网站标题";
|
||||
}
|
||||
return title;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取站点 Footer 配置信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetWebFooter()
|
||||
{
|
||||
var dicts = GetAll();
|
||||
var title = "网站页脚";
|
||||
var name = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == AppId)?.Name;
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
var dict = dicts.FirstOrDefault(d => d.Category == name && d.Name == "网站页脚") ?? dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "网站页脚");
|
||||
title = dict?.Code ?? "网站标题";
|
||||
}
|
||||
return title;
|
||||
var dict = dicts.FirstOrDefault(d => d.Category == name && d.Name == "网站页脚") ?? dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "网站页脚");
|
||||
title = dict?.Code ?? "网站标题";
|
||||
}
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ class GroupService : BaseDatabase, IGroup
|
|||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetGroupsByRoleId(string? roleId) => Database.Fetch<string>("select GroupID from RoleGroup where RoleGroup = @0", roleId);
|
||||
public List<string> GetGroupsByRoleId(string? roleId) => Database.Fetch<string>("select GroupID from RoleGroup where RoleID = @0", roleId);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -69,7 +69,7 @@ class GroupService : BaseDatabase, IGroup
|
|||
try
|
||||
{
|
||||
Database.BeginTransaction();
|
||||
Database.Execute("delete from RoleGroup where RoleGroup = @0", roleId);
|
||||
Database.Execute("delete from RoleGroup where RoleID = @0", roleId);
|
||||
Database.InsertBatch("RoleGroup", groupIds.Select(g => new { GroupID = g, RoleID = roleId }));
|
||||
Database.CompleteTransaction();
|
||||
ret = true;
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
using BootstrapBlazor.Components;
|
||||
|
||||
namespace BootstrapAdmin.Web.Core;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IApp
|
||||
{
|
||||
/// <summary>
|
||||
/// 获得所有用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
List<SelectedItem> GetAll();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
List<string> GetAppsByRoleId(string? roleId);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <param name="appIds"></param>
|
||||
/// <returns></returns>
|
||||
bool SaveAppsByRoleId(string? roleId, IEnumerable<string> appIds);
|
||||
}
|
|
@ -1,10 +1,14 @@
|
|||
namespace BootstrapAdmin.Web.Core
|
||||
using BootstrapBlazor.Components;
|
||||
|
||||
namespace BootstrapAdmin.Web.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Dict 字典表接口
|
||||
/// </summary>
|
||||
public interface IDict
|
||||
{
|
||||
List<SelectedItem> GetApps();
|
||||
|
||||
/// <summary>
|
||||
/// 获取 站点 Title 配置信息
|
||||
/// </summary>
|
||||
|
|
|
@ -7,7 +7,6 @@ namespace BootstrapAdmin.Web.Core;
|
|||
/// </summary>
|
||||
public interface IGroup
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获得所有用户
|
||||
/// </summary>
|
||||
|
|
|
@ -22,12 +22,16 @@ public partial class Roles
|
|||
[NotNull]
|
||||
private IUser? UserService { get; set; }
|
||||
|
||||
[Inject]
|
||||
[NotNull]
|
||||
private IApp? AppService { get; set; }
|
||||
|
||||
private async Task OnAssignmentUsers(Role role)
|
||||
{
|
||||
var users = UserService.GetAll().ToSelectedItemList();
|
||||
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);
|
||||
return Task.FromResult(ret);
|
||||
|
@ -58,12 +62,13 @@ public partial class Roles
|
|||
|
||||
private async Task OnAssignmentApps(Role role)
|
||||
{
|
||||
var option = new DialogOption()
|
||||
var apps = AppService.GetAll();
|
||||
var values = AppService.GetAppsByRoleId(role.Id);
|
||||
|
||||
await DialogService.ShowAssignmentDialog($"分配应用 - {role.RoleName}", apps, values, () =>
|
||||
{
|
||||
Title = $"分配应用 - {role}",
|
||||
};
|
||||
|
||||
await DialogService.Show(option);
|
||||
var ret = AppService.SaveAppsByRoleId(role.Id, values);
|
||||
return Task.FromResult(ret);
|
||||
}, ToastService);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue