using Furion.DependencyInjection; using Myshipping.Core.Entity; using System.Collections.Generic; using System.Threading.Tasks; namespace Myshipping.Core.Service; /// /// 用户角色服务 /// public class SysUserRoleService : ISysUserRoleService, ITransient { private readonly SqlSugarRepository _sysUserRoleRep; // 用户权限表仓储 private readonly ISysRoleService _sysRoleService; private readonly ISysCacheService _sysCacheService; public SysUserRoleService(SqlSugarRepository sysUserRoleRep, ISysRoleService sysRoleService, ISysCacheService sysCacheService) { _sysUserRoleRep = sysUserRoleRep; _sysRoleService = sysRoleService; _sysCacheService = sysCacheService; } /// /// 获取用户的角色Id集合 /// /// /// public async Task> GetUserRoleIdList(long userId) { return await _sysUserRoleRep.Where(u => u.SysUserId == userId).Select(u => u.SysRoleId).ToListAsync(); } /// /// 授权用户角色 /// /// /// public async Task GrantRole(UpdateUserInput input) { try { _sysUserRoleRep.CurrentBeginTran(); await _sysUserRoleRep.DeleteAsync(u => u.SysUserId == input.Id); var grantRoleIdList = new List(); input.GrantRoleIdList.ForEach(u => { grantRoleIdList.Add( new SysUserRole { SysUserId = input.Id, SysRoleId = u }); }); await _sysUserRoleRep.InsertAsync(grantRoleIdList); _sysUserRoleRep.CurrentCommitTran(); //清除缓存 await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU); await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION); } catch (System.Exception) { _sysUserRoleRep.CurrentRollbackTran(); throw; } } /// /// 获取用户所有角色的数据范围(组织机构Id集合) /// /// /// /// public async Task> GetUserRoleDataScopeIdList(long userId, long orgId) { var roleIdList = await GetUserRoleIdList(userId); // 获取这些角色对应的数据范围 if (roleIdList.Count > 0) return await _sysRoleService.GetUserDataScopeIdList(roleIdList, orgId); return roleIdList; } /// /// 根据角色Id删除对应的用户-角色表关联信息 /// /// /// public async Task DeleteUserRoleListByRoleId(long roleId) { await _sysUserRoleRep.DeleteAsync(u => u.SysRoleId == roleId); } /// /// 根据用户Id删除对应的用户-角色表关联信息 /// /// /// public async Task DeleteUserRoleListByUserId(long userId) { await _sysUserRoleRep.DeleteAsync(u => u.SysUserId == userId); } /// /// 判断某用户是否属于指定角色 /// public async Task IsUserInRole(long userId, string roleCode) { return await _sysUserRoleRep.AsQueryable() .LeftJoin((ur, r) => ur.SysRoleId == r.Id) .Where((ur, r) => ur.SysUserId == userId && r.Code == roleCode) .AnyAsync(); } }