You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Service/User/SysUserRoleService.cs

120 lines
3.8 KiB
C#

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