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#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion.DependencyInjection;
using Myshipping.Core.Entity;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Myshipping.Core.Service;
/// <summary>
/// 用户角色服务
/// </summary>
public class SysUserRoleService : ISysUserRoleService, ITransient
{
private readonly SqlSugarRepository<SysUserRole> _sysUserRoleRep; // 用户权限表仓储
private readonly ISysRoleService _sysRoleService;
private readonly ISysCacheService _sysCacheService;
public SysUserRoleService(SqlSugarRepository<SysUserRole> sysUserRoleRep, ISysRoleService sysRoleService, ISysCacheService sysCacheService)
{
_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();
//清除缓存
await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU);
await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION);
}
catch (System.Exception)
{
_sysUserRoleRep.CurrentRollbackTran();
throw;
}
}
/// <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);
}
/// <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();
}
}