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, ITransient { private readonly ICache _cache; private readonly SqlSugarRepository _orgRepository; private readonly SqlSugarRepository _userRepository; private readonly IHttpContextAccessor _httpContextAccessor; public UserManager(SqlSugarRepository repository, ICache cache, SqlSugarRepository orgRepository, SqlSugarRepository 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 customScope = new List(); //有自定义的部门权限 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 orgScope = new List(); 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); } } /// /// 获取自己所管理的员工id列表 /// /// public async Task> GetOwnUserIdList() { var user = UserInfo(); if (user == null) return new List(); 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(); } public JwtUserInfo? UserInfo() => _httpContextAccessor.UserInfo(); public UserCache? CacheInfo() { var user = UserInfo(); if (user == null) return null; else return _cache.GetCache(user.CacheKey); } } }