2024-04-08 18:57:59 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using Volo.Abp.Authorization;
|
|
|
|
|
|
using Volo.Abp.Caching;
|
|
|
|
|
|
using Volo.Abp.Domain.Services;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp.Guids;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Entities;
|
2024-04-08 18:57:59 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Repositories;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Caches;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Options;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
namespace Yi.Framework.Rbac.Domain.Managers
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public class UserManager : DomainService
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-01-07 13:34:50 +08:00
|
|
|
|
public readonly ISqlSugarRepository<UserEntity> _repository;
|
|
|
|
|
|
public readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
|
|
|
|
|
|
public readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
|
2024-04-08 18:57:59 +08:00
|
|
|
|
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private readonly IGuidGenerator _guidGenerator;
|
2024-04-08 18:57:59 +08:00
|
|
|
|
private IUserRepository _userRepository;
|
|
|
|
|
|
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository) =>
|
|
|
|
|
|
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository) =
|
|
|
|
|
|
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给用户设置角色
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userIds"></param>
|
|
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task GiveUserSetRoleAsync(List<Guid> userIds, List<Guid> roleIds)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
|
|
|
|
|
|
await _repositoryUserRole.DeleteAsync(u => userIds.Contains(u.UserId));
|
|
|
|
|
|
|
|
|
|
|
|
if (roleIds is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//遍历用户
|
|
|
|
|
|
foreach (var userId in userIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
//添加新的关系
|
|
|
|
|
|
List<UserRoleEntity> userRoleEntities = new();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var roleId in roleIds)
|
|
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
userRoleEntities.Add(new UserRoleEntity() { UserId = userId, RoleId = roleId });
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
//一次性批量添加
|
|
|
|
|
|
await _repositoryUserRole.InsertRangeAsync(userRoleEntities);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给用户设置岗位
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userIds"></param>
|
|
|
|
|
|
/// <param name="postIds"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task GiveUserSetPostAsync(List<Guid> userIds, List<Guid> postIds)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
|
|
|
|
|
|
await _repositoryUserPost.DeleteAsync(u => userIds.Contains(u.UserId));
|
|
|
|
|
|
if (postIds is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//遍历用户
|
|
|
|
|
|
foreach (var userId in userIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
//添加新的关系
|
|
|
|
|
|
List<UserPostEntity> userPostEntities = new();
|
|
|
|
|
|
foreach (var post in postIds)
|
|
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
userPostEntities.Add(new UserPostEntity() { UserId = userId, PostId = post });
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//一次性批量添加
|
|
|
|
|
|
await _repositoryUserPost.InsertRangeAsync(userPostEntities);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-08 18:57:59 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询用户信息,已缓存
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<UserRoleMenuDto> Get(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
//if (userId is null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// throw new UserFriendlyException("用户未登录");
|
|
|
|
|
|
//}
|
|
|
|
|
|
//此处优先从缓存中获取
|
|
|
|
|
|
UserRoleMenuDto output = null;
|
|
|
|
|
|
|
|
|
|
|
|
var cacheData = await _userCache.GetAsync(new UserInfoCacheKey(userId));
|
|
|
|
|
|
if (cacheData is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
output = cacheData.Info;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = await _userRepository.GetUserAllInfoAsync(userId);
|
|
|
|
|
|
//系统用户数据被重置,老前端访问重新授权
|
|
|
|
|
|
if (data is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new AbpAuthorizationException();
|
|
|
|
|
|
}
|
|
|
|
|
|
data.Menus.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
output = data;
|
|
|
|
|
|
|
|
|
|
|
|
var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime;
|
|
|
|
|
|
//将用户信息放入缓存,下次获取直接从缓存中获取即可,过期时间为token过期时间
|
|
|
|
|
|
await _userCache.SetAsync(new UserInfoCacheKey(userId), new UserInfoCacheItem(data), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(tokenExpiresMinuteTime) });
|
|
|
|
|
|
}
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|