You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.0 KiB
128 lines
4.0 KiB
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using CellularManagement.Domain.Entities;
|
|
using CellularManagement.Domain.Repositories;
|
|
using CellularManagement.Infrastructure.Repositories.Base;
|
|
using CellularManagement.Domain.Repositories.Base;
|
|
using CellularManagement.Domain.Repositories.Identity;
|
|
namespace CellularManagement.Infrastructure.Repositories.Identity;
|
|
|
|
/// <summary>
|
|
/// 用户角色仓储实现类
|
|
/// </summary>
|
|
public class UserRoleRepository : BaseRepository<UserRole>, IUserRoleRepository
|
|
{
|
|
private readonly ILogger<UserRoleRepository> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化仓储
|
|
/// </summary>
|
|
public UserRoleRepository(
|
|
ICommandRepository<UserRole> commandRepository,
|
|
IQueryRepository<UserRole> queryRepository,
|
|
ILogger<UserRoleRepository> logger)
|
|
: base(commandRepository, queryRepository, logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
#region IUserRoleCommandRepository 实现
|
|
|
|
/// <summary>
|
|
/// 添加用户角色关系
|
|
/// </summary>
|
|
public async Task<UserRole> AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
|
{
|
|
return await CommandRepository.AddAsync(userRole, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除用户角色关系
|
|
/// </summary>
|
|
public async Task DeleteUserRoleAsync(string userId, string roleId, CancellationToken cancellationToken = default)
|
|
{
|
|
var userRole = await QueryRepository.FirstOrDefaultAsync(
|
|
ur => ur.UserId == userId && ur.RoleId == roleId,
|
|
cancellationToken);
|
|
|
|
if (userRole != null)
|
|
{
|
|
CommandRepository.Delete(userRole);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量添加用户角色关系
|
|
/// </summary>
|
|
public async Task AddUserRolesAsync(IEnumerable<UserRole> userRoles, CancellationToken cancellationToken = default)
|
|
{
|
|
await CommandRepository.AddRangeAsync(userRoles, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除用户角色关系
|
|
/// </summary>
|
|
public async Task DeleteUserRolesAsync(string userId, IEnumerable<string> roleIds, CancellationToken cancellationToken = default)
|
|
{
|
|
var userRoles = await QueryRepository.FindAsync(
|
|
ur => ur.UserId == userId && roleIds.Contains(ur.RoleId),
|
|
cancellationToken);
|
|
|
|
if (userRoles.Any())
|
|
{
|
|
CommandRepository.DeleteRange(userRoles);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IUserRoleQueryRepository 实现
|
|
|
|
/// <summary>
|
|
/// 获取用户的所有角色
|
|
/// </summary>
|
|
public async Task<IList<string>> GetUserRolesAsync(string userId, CancellationToken cancellationToken = default)
|
|
{
|
|
var userRoles = await QueryRepository.FindAsync(
|
|
ur => ur.UserId == userId,
|
|
cancellationToken);
|
|
|
|
return userRoles.Select(ur => ur.RoleId).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取角色的所有用户
|
|
/// </summary>
|
|
public async Task<IList<string>> GetRoleUsersAsync(string roleId, CancellationToken cancellationToken = default)
|
|
{
|
|
var userRoles = await QueryRepository.FindAsync(
|
|
ur => ur.RoleId == roleId,
|
|
cancellationToken);
|
|
|
|
return userRoles.Select(ur => ur.UserId).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查用户是否拥有指定角色
|
|
/// </summary>
|
|
public async Task<bool> HasRoleAsync(string userId, string roleId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.AnyAsync(
|
|
ur => ur.UserId == userId && ur.RoleId == roleId,
|
|
cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户角色关系
|
|
/// </summary>
|
|
public async Task<UserRole?> GetUserRoleAsync(string userId, string roleId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.FirstOrDefaultAsync(
|
|
ur => ur.UserId == userId && ur.RoleId == roleId,
|
|
cancellationToken);
|
|
}
|
|
|
|
#endregion
|
|
}
|