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.
129 lines
4.4 KiB
129 lines
4.4 KiB
using Microsoft.AspNetCore.Http;
|
|
using Wood.Cache;
|
|
using Wood.Data.Repository;
|
|
using Wood.Entity;
|
|
using Wood.Entity.SystemManage;
|
|
using Wood.Util;
|
|
using Wood.Util.JwtAuthorization;
|
|
|
|
namespace Wood.Service.SystemManage.Manager
|
|
{
|
|
public class UserManager : ApiManager<UserEntity>, ITransient
|
|
{
|
|
private readonly ICache _cache;
|
|
private readonly SqlSugarRepository<OrgEntity> _orgRepository;
|
|
private readonly SqlSugarRepository<UserEntity> _userRepository;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
public UserManager(SqlSugarRepository<UserEntity> repository, ICache cache, SqlSugarRepository<OrgEntity> orgRepository, SqlSugarRepository<UserEntity> userRepository, IHttpContextAccessor httpContextAccessor) : base(repository)
|
|
{
|
|
_cache = cache;
|
|
_orgRepository = orgRepository;
|
|
_userRepository = userRepository;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
public async Task InitCache(long userId, DateTime tokenExpiresTime)
|
|
{
|
|
UserEntity? userInfo = await _userRepository.AsQueryable()
|
|
.Includes(it => it.Roles)
|
|
.Includes(it => it.Org)
|
|
.Includes(it => it.Position)
|
|
.Where(it => it.Id == userId)
|
|
.FirstAsync();
|
|
|
|
await InitCache(userInfo, tokenExpiresTime);
|
|
}
|
|
|
|
public async Task InitCache(UserEntity userInfo, DateTime tokenExpiresTime)
|
|
{
|
|
string cacheKey = userInfo.Id + "-" + userInfo.TenantId;
|
|
|
|
string roleName = userInfo!.AccountType.GetDescription();
|
|
if (userInfo!.Roles != null && userInfo!.Roles.Any())
|
|
roleName = string.Join(',', userInfo.Roles.Select(it => it.RoleName));
|
|
|
|
UserCache userCache = new UserCache()
|
|
{
|
|
RealName = userInfo.RealName,
|
|
TenantId = userInfo.TenantId,
|
|
AccountType = userInfo.AccountType,
|
|
Email = userInfo.Email,
|
|
Id = userInfo.Id,
|
|
Mobile = userInfo.Mobile,
|
|
NickName = userInfo.NickName,
|
|
OrgId = userInfo.OrgId,
|
|
Salt = userInfo.Salt,
|
|
UserName = userInfo.UserName,
|
|
PositionId = userInfo.PositionId
|
|
};
|
|
|
|
userCache.Roles = userInfo.Roles!.Select(it => it.Id).ToList();
|
|
|
|
var dataScopeType = 0;
|
|
foreach (var item in userInfo.Roles!)
|
|
dataScopeType |= (int)item.DataScopeType;
|
|
|
|
//拥有全部数据访问权限则直接返回即可
|
|
if ((dataScopeType & (int)DataScopeTypeEnum.All) == 1)
|
|
{
|
|
userCache.DataScopeType = DataScopeTypeEnum.All;
|
|
_cache.SetCache(cacheKey, userCache, tokenExpiresTime);
|
|
}
|
|
else
|
|
{
|
|
List<long> customScope = new List<long>();
|
|
//有自定义的部门权限
|
|
if ((dataScopeType & (int)DataScopeTypeEnum.Custom) == 1)
|
|
{
|
|
var ids = userInfo.Roles.Where(it => it.DataScopeType == DataScopeTypeEnum.Custom).Select(it => it.Id).ToList();
|
|
var childs = await _orgRepository.AsQueryable().ToChildListAsync(it => it.ParentId, ids);
|
|
customScope = childs.Select(it => it.Id).ToList();
|
|
}
|
|
|
|
List<long> orgScope = new List<long>();
|
|
if ((dataScopeType & (int)DataScopeTypeEnum.MyOrgAndLower) == 1)
|
|
{
|
|
var childs = await _orgRepository.AsQueryable().ToChildListAsync(it => it.ParentId, userInfo.OrgId);
|
|
orgScope = childs.Select(it => it.Id).ToList();
|
|
}
|
|
else if ((dataScopeType & (int)DataScopeTypeEnum.MyOrg) == 1)
|
|
orgScope.Add(userInfo.OrgId);
|
|
else if ((dataScopeType & (int)DataScopeTypeEnum.MySelf) == 1)
|
|
orgScope.Add(userInfo.OrgId);
|
|
|
|
userCache.DataScopeOrgs = orgScope.Union(customScope).Distinct().ToList();
|
|
_cache.SetCache(cacheKey, userCache, tokenExpiresTime);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取自己所管理的员工id列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<long>> GetOwnUserIdList()
|
|
{
|
|
var user = UserInfo();
|
|
if (user == null)
|
|
return new List<long>();
|
|
if (user!.IsSuperAdmin)
|
|
return await _userRepository.AsQueryable().Where(it => it.Id != user.UserId).Select(it => it.Id).ToListAsync();
|
|
else if (user!.IsTenantAdmin)
|
|
return await _userRepository.AsQueryable().Where(it => it.Id != user.UserId && it.TenantId == user.TenantId).Select(it => it.Id).ToListAsync();
|
|
else if (user.IsAdmin)
|
|
return await _userRepository.AsQueryable().Where(it => it.Id != user.UserId && it.OrgId == user.OrgId).Select(it => it.Id).ToListAsync();
|
|
else
|
|
return new List<long>();
|
|
}
|
|
|
|
|
|
public JwtUserInfo? UserInfo() => _httpContextAccessor.UserInfo();
|
|
|
|
public UserCache? CacheInfo()
|
|
{
|
|
var user = UserInfo();
|
|
if (user == null) return null;
|
|
else
|
|
return _cache.GetCache<UserCache>(user.CacheKey);
|
|
}
|
|
}
|
|
}
|
|
|