权限配置

This commit is contained in:
马梓彭 2026-04-12 16:54:39 +08:00
parent 3bd4b1dcd7
commit 47a6450e39
3 changed files with 89 additions and 5 deletions

View File

@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using YD_Event.Core.Enum;
namespace YD_Event.Application.Role.Dtos;
@ -6,7 +6,7 @@ namespace YD_Event.Application.Role.Dtos;
/// <summary>
/// 添加角色输入
/// </summary>
public class AddSysRoleInput
public class AddSysRoleInput : IValidatableObject
{
/// <summary>
/// 角色名称
@ -30,4 +30,23 @@ public class AddSysRoleInput
/// 状态
/// </summary>
public AvailabilityStatus Status { get; set; } = AvailabilityStatus.Enable;
/// <summary>
/// 菜单ID集合
/// </summary>
public List<long> MenuIds { get; set; }
/// <summary>
/// 菜单ID集合(前端使用)
/// </summary>
public List<long> Menus { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var menuIds = MenuIds ?? Menus;
if (menuIds == null || menuIds.Count == 0)
{
yield return new ValidationResult("请选择权限", new[] { nameof(MenuIds), nameof(Menus) });
}
}
}

View File

@ -1,4 +1,4 @@
using YD_Event.Application.Role.Dtos;
using YD_Event.Application.Role.Dtos;
using YD_Event.Core.Shared;
namespace YD_Event.Application.Role;
@ -70,6 +70,19 @@ public class SysRoleService : BaseService<SysRole>, ITransient
}
await _sysRoleRepository.InsertAsync(role);
// 保存角色权限
var menuIds = dto.MenuIds ?? dto.Menus;
if (menuIds != null && menuIds.Any())
{
var roleMenus = menuIds.Select(x => new SysRoleMenu()
{
MenuId = x,
RoleId = role.Id
}).ToList();
await _sysRoleMenuRepository.InsertRangeAsync(roleMenus);
}
}
/// <summary>
@ -95,6 +108,21 @@ public class SysRoleService : BaseService<SysRole>, ITransient
dto.Adapt(sysRole);
await _sysRoleRepository.UpdateAsync(sysRole);
// 更新角色权限
await _sysRoleMenuRepository.DeleteAsync(x => x.RoleId == dto.Id);
var menuIds = dto.MenuIds ?? dto.Menus;
if (menuIds != null && menuIds.Any())
{
var roleMenus = menuIds.Select(x => new SysRoleMenu()
{
MenuId = x,
RoleId = dto.Id
}).ToList();
await _sysRoleMenuRepository.InsertRangeAsync(roleMenus);
}
await _easyCachingProvider.RemoveByPrefixAsync(CacheConst.PermissionKey);
}
/// <summary>
@ -186,4 +214,15 @@ public class SysRoleService : BaseService<SysRole>, ITransient
Label = x.Name
}).ToListAsync();
}
/// <summary>
/// 角色下拉选项(兼容接口)
/// </summary>
/// <returns></returns>
[DisplayName("角色下拉选项(兼容接口)")]
[HttpGet("roleselect")]
public async Task<List<SelectOutput>> RoleSelectCompat()
{
return await RoleSelect();
}
}

View File

@ -99,7 +99,7 @@ public class SysUserService : BaseService<SysUser>, ITransient
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[UnitOfWork, HttpPost]
[UnitOfWork, HttpPost("user")]
[DisplayName("添加系统用户")]
public async Task AddUser(AddSysUserInput dto)
{
@ -107,6 +107,19 @@ public class SysUserService : BaseService<SysUser>, ITransient
if (await _repository.IsAnyAsync(x => x.Account == dto.Account))
throw new InvalidOperationException($"账号 {dto.Account} 已存在");
// 验证角色是否存在
if (dto.Roles != null && dto.Roles.Any())
{
var validRoleCount = await _roleRepository.AsQueryable()
.Where(x => dto.Roles.Contains(x.Id) && x.Status == AvailabilityStatus.Enable)
.CountAsync();
if (validRoleCount != dto.Roles.Count)
{
throw new InvalidOperationException("部分或全部角色不存在或已被禁用");
}
}
var user = dto.Adapt<SysUser>();
user.Id = _idGenerator.NextId();
string encode = _idGenerator.Encode(user.Id);
@ -137,7 +150,7 @@ public class SysUserService : BaseService<SysUser>, ITransient
/// <param name="dto"></param>
/// <returns></returns>
[DisplayName("更新系统用户信息")]
[UnitOfWork, HttpPut]
[UnitOfWork, HttpPut("user")]
public async Task UpdateUser(UpdateSysUserInput dto)
{
var user = await _repository.GetByIdAsync(dto.Id);
@ -147,6 +160,19 @@ public class SysUserService : BaseService<SysUser>, ITransient
if (await _repository.IsAnyAsync(x => x.Account == dto.Account && x.Id != dto.Id))
throw new InvalidOperationException($"账号 {dto.Account} 已被使用");
// 验证角色是否存在
if (dto.Roles != null && dto.Roles.Any())
{
var validRoleCount = await _roleRepository.AsQueryable()
.Where(x => dto.Roles.Contains(x.Id) && x.Status == AvailabilityStatus.Enable)
.CountAsync();
if (validRoleCount != dto.Roles.Count)
{
throw new InvalidOperationException("部分或全部角色不存在或已被禁用");
}
}
// 先更新除密码外的其他字段
user.Name = dto.Name;
user.Account = dto.Account;