Files
Yi.Framework/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/RoleManager.cs
ccnetcore e6e4829164 feat: 新增VIP过期自动卸载功能
- 新增`AiRechargeManager`类,实现VIP过期用户的自动卸载逻辑。
- 新增`AiHubConst`常量类,统一管理角色名称。
- 在`IRoleService`中添加`RemoveUserRoleByRoleCodeAsync`方法,用于移除指定用户的角色。
- 在`RoleManager`中实现`RemoveUserRoleByRoleCodeAsync`方法。
- 优化`CurrentExtensions`中VIP角色判断逻辑,使用常量替代硬编码。
- 调整`YiAbpWebModule`中部分代码格式,提升可读性。
2025-08-09 13:14:15 +08:00

70 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Volo.Abp.Domain.Services;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Domain.Managers
{
public class RoleManager : DomainService
{
private ISqlSugarRepository<RoleAggregateRoot> _repository;
private ISqlSugarRepository<RoleMenuEntity> _roleMenuRepository;
private ISqlSugarRepository<UserRoleEntity> _userRoleRepository;
public RoleManager(ISqlSugarRepository<RoleAggregateRoot> repository, ISqlSugarRepository<RoleMenuEntity> roleMenuRepository, ISqlSugarRepository<UserRoleEntity> userRoleRepository)
{
_repository = repository;
_roleMenuRepository = roleMenuRepository;
_userRoleRepository = userRoleRepository;
}
/// <summary>
/// 给角色设置菜单
/// </summary>
/// <param name="roleIds"></param>
/// <param name="menuIds"></param>
/// <returns></returns>
public async Task GiveRoleSetMenuAsync(List<Guid> roleIds, List<Guid> menuIds)
{
//这个是需要事务的在service中进行工作单元
await _roleMenuRepository.DeleteAsync(u => roleIds.Contains(u.RoleId));
//遍历用户
foreach (var roleId in roleIds)
{
//添加新的关系
List<RoleMenuEntity> roleMenuEntity = new();
foreach (var menu in menuIds)
{
roleMenuEntity.Add(new RoleMenuEntity() { RoleId = roleId, MenuId = menu });
}
//一次性批量添加
await _roleMenuRepository.InsertRangeAsync(roleMenuEntity);
}
}
/// <summary>
/// 根据角色名称移除指定用户的角色
/// </summary>
/// <param name="userIds">用户ID列表</param>
/// <param name="roleName">角色名称</param>
/// <returns>移除的角色关系数量</returns>
public async Task<int> RemoveUserRoleByRoleCodeAsync(List<Guid> userIds, string roleName)
{
// 获取角色ID
var role = await _repository._DbQueryable
.Where(x => x.RoleCode == roleName)
.FirstAsync();
if (role == null)
{
return 0;
}
// 移除用户角色关系
var removedCount = await _userRoleRepository._Db.Deleteable<UserRoleEntity>()
.Where(x => userIds.Contains(x.UserId) && x.RoleId == role.Id)
.ExecuteCommandAsync();
return removedCount;
}
}
}