67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using Learn.Archives.API.Controllers.Dto;
|
|
using Learn.Archives.API.Expand;
|
|
using Learn.Archives.Core.Common;
|
|
using Learn.Archives.Core.Model;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Diagnostics;
|
|
using System.Security.Claims;
|
|
using UserCenter.Model;
|
|
|
|
namespace Learn.Archives.API.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 管理员角色控制器
|
|
/// </summary>
|
|
public class MenuRelationController : BackController<MenuRelation>
|
|
{
|
|
readonly Repository<AdminRole> roleService;
|
|
readonly Repository<MenuRelation> mrService;
|
|
readonly LiveUserInfo userInfo;
|
|
public MenuRelationController(Repository<AdminRole> baseService, LiveUserInfo userInfo, Repository<MenuRelation> mrService) : base(mrService)
|
|
{
|
|
this.roleService = baseService;
|
|
this.userInfo = userInfo;
|
|
this.mrService = mrService;
|
|
}
|
|
public class SetMenuInput
|
|
{
|
|
public long RoleId { get; set; }
|
|
public long[] MenuId { get; set; }
|
|
}
|
|
/// <summary>
|
|
/// 获取角色菜单ids
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<long[]> RoleMenu(long roleId)
|
|
{
|
|
if (roleId == 0) Oh.Error("无效的角色");
|
|
if (!userInfo.IsSa) Oh.Error("无权访问");
|
|
var role = roleService.GetByIdAsync(roleId);
|
|
if (role == null) Oh.Error("无效的角色");
|
|
return await mrService.AsQueryable()
|
|
.Where(s=>s.RoleId==roleId)
|
|
.Select(s=>s.MenuId)
|
|
.ToArrayAsync();
|
|
}
|
|
/// <summary>
|
|
/// 设置角色菜单
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<bool> SetMenu(SetMenuInput input)
|
|
{
|
|
if (input.MenuId == null || input.MenuId.Length==0) Oh.Error("选择了无可用的菜单");
|
|
if (!userInfo.IsSa) Oh.Error("登录了无效的用户");
|
|
var role = await roleService.GetByIdAsync(input.RoleId);
|
|
if (role == null) Oh.Error("无效的角色");
|
|
var insertData= input.MenuId!
|
|
.Select(s=>new MenuRelation() {RoleId = role!.Id,MenuId = s })
|
|
.ToArray();
|
|
await mrService.AsDeleteable().Where(s=>s.RoleId == role!.Id).ExecuteCommandAsync();
|
|
return await mrService.InsertRangeAsync(insertData);
|
|
}
|
|
}
|
|
}
|