merge from 2023-03-14
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Const;
|
||||
using JNPF.Common.Core.Handlers;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Enums;
|
||||
using JNPF.Common.Extension;
|
||||
using JNPF.Common.Manager;
|
||||
using JNPF.Common.Models.User;
|
||||
using JNPF.Common.Security;
|
||||
using JNPF.DatabaseAccessor;
|
||||
@@ -36,15 +39,29 @@ public class AuthorizeService : IAuthorizeService, IDynamicApiController, ITrans
|
||||
/// </summary>
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
/// <summary>
|
||||
/// 缓存管理器.
|
||||
/// </summary>
|
||||
private readonly ICacheManager _cacheManager;
|
||||
|
||||
/// <summary>
|
||||
/// IM中心处理程序.
|
||||
/// </summary>
|
||||
private IMHandler _imHandler;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化一个<see cref="AuthorizeService"/>类型的新实例.
|
||||
/// </summary>
|
||||
public AuthorizeService(
|
||||
ISqlSugarRepository<AuthorizeEntity> authorizeRepository,
|
||||
IUserManager userManager)
|
||||
ICacheManager cacheManager,
|
||||
IUserManager userManager,
|
||||
IMHandler imHandler)
|
||||
{
|
||||
_authorizeRepository = authorizeRepository;
|
||||
_cacheManager = cacheManager;
|
||||
_userManager = userManager;
|
||||
_imHandler = imHandler;
|
||||
}
|
||||
|
||||
#region Get
|
||||
@@ -391,42 +408,52 @@ public class AuthorizeService : IAuthorizeService, IDynamicApiController, ITrans
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("Model/{itemId}")]
|
||||
[UnitOfWork]
|
||||
public async Task UpdateModel(string itemId, [FromBody] AuthorizeModelInput input)
|
||||
{
|
||||
List<AuthorizeEntity>? authorizeList = new List<AuthorizeEntity>();
|
||||
|
||||
// 角色ID不为空
|
||||
if (input.objectId.Count > 0)
|
||||
try
|
||||
{
|
||||
input.objectId.ForEach(item =>
|
||||
_authorizeRepository.AsSugarClient().Ado.BeginTran();
|
||||
|
||||
// 角色ID不为空
|
||||
if (input.objectId.Count > 0)
|
||||
{
|
||||
AuthorizeEntity? entity = new AuthorizeEntity();
|
||||
entity.Id = SnowflakeIdHelper.NextId();
|
||||
entity.CreatorTime = DateTime.Now;
|
||||
entity.CreatorUserId = _userManager.UserId;
|
||||
entity.ItemId = itemId;
|
||||
entity.ItemType = input.itemType;
|
||||
entity.ObjectId = item;
|
||||
entity.ObjectType = input.objectType;
|
||||
entity.SortCode = input.objectId.IndexOf(item);
|
||||
authorizeList.Add(entity);
|
||||
});
|
||||
input.objectId.ForEach(item =>
|
||||
{
|
||||
AuthorizeEntity? entity = new AuthorizeEntity();
|
||||
entity.Id = SnowflakeIdHelper.NextId();
|
||||
entity.CreatorTime = DateTime.Now;
|
||||
entity.CreatorUserId = _userManager.UserId;
|
||||
entity.ItemId = itemId;
|
||||
entity.ItemType = input.itemType;
|
||||
entity.ObjectId = item;
|
||||
entity.ObjectType = input.objectType;
|
||||
entity.SortCode = input.objectId.IndexOf(item);
|
||||
authorizeList.Add(entity);
|
||||
});
|
||||
|
||||
// 删除除了门户外的相关权限
|
||||
await _authorizeRepository.DeleteAsync(a => a.ItemId == itemId);
|
||||
// 删除除了门户外的相关权限
|
||||
await _authorizeRepository.DeleteAsync(a => a.ItemId == itemId);
|
||||
|
||||
// 新增权限
|
||||
await _authorizeRepository.AsSugarClient().Insertable(authorizeList).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
|
||||
// 新增权限
|
||||
await _authorizeRepository.AsSugarClient().Insertable(authorizeList).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
|
||||
|
||||
// 编辑角色权限退出角色的登录用户
|
||||
await ForcedOffline(input.objectId);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 删除除了门户外的相关权限
|
||||
await _authorizeRepository.DeleteAsync(a => a.ItemId == itemId);
|
||||
}
|
||||
|
||||
_authorizeRepository.AsSugarClient().Ado.CommitTran();
|
||||
}
|
||||
else
|
||||
catch
|
||||
{
|
||||
// 删除除了门户外的相关权限
|
||||
await _authorizeRepository.DeleteAsync(a => a.ItemId == itemId);
|
||||
_authorizeRepository.AsSugarClient().Ado.RollbackTran();
|
||||
}
|
||||
|
||||
if(input.objectId.Any()) await ForcedOffline(input.objectId); // 编辑角色权限退出角色的登录用户
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -754,14 +781,17 @@ public class AuthorizeService : IAuthorizeService, IDynamicApiController, ITrans
|
||||
{
|
||||
// 查找该角色下的所有成员id
|
||||
var roleUserIds = await _authorizeRepository.AsSugarClient().Queryable<UserRelationEntity>().Where(x => x.ObjectType == "Role" && roleId.Contains(x.ObjectId)).Select(x => x.UserId).ToListAsync();
|
||||
Scoped.Create((_, scope) =>
|
||||
roleUserIds.ForEach(async id =>
|
||||
{
|
||||
roleUserIds.ForEach(id =>
|
||||
var tenantId = _userManager.TenantId;
|
||||
var list = await GetOnlineUserList(tenantId);
|
||||
var user = list.Find(it => it.tenantId == tenantId && it.userId == id);
|
||||
if (user != null)
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
var _onlineuser = App.GetService<OnlineUserService>(services);
|
||||
_onlineuser.ForcedOffline(id);
|
||||
});
|
||||
await _imHandler.SendMessageAsync(user.connectionId, new { method = "logout", msg = "此账号已在其他地方登陆" }.ToJsonString());
|
||||
await DelOnlineUser(tenantId, user.userId);
|
||||
await DelUserInfo(tenantId, user.userId);
|
||||
}
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
@@ -1018,5 +1048,44 @@ public class AuthorizeService : IAuthorizeService, IDynamicApiController, ITrans
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取在线用户列表.
|
||||
/// </summary>
|
||||
/// <param name="tenantId">租户ID.</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<UserOnlineModel>> GetOnlineUserList(string tenantId)
|
||||
{
|
||||
var cacheKey = string.Format("{0}{1}", CommonConst.CACHEKEYONLINEUSER, tenantId);
|
||||
return await _cacheManager.GetAsync<List<UserOnlineModel>>(cacheKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除在线用户ID.
|
||||
/// </summary>
|
||||
/// <param name="tenantId">租户ID.</param>
|
||||
/// <param name="userId">用户ID.</param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> DelOnlineUser(string tenantId, string userId)
|
||||
{
|
||||
var cacheKey = string.Format("{0}{1}", CommonConst.CACHEKEYONLINEUSER, tenantId);
|
||||
var list = await _cacheManager.GetAsync<List<UserOnlineModel>>(cacheKey);
|
||||
var online = list.Find(it => it.userId == userId);
|
||||
list.RemoveAll((x) => x.connectionId == online.connectionId);
|
||||
return await _cacheManager.SetAsync(cacheKey, list);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除用户登录信息缓存.
|
||||
/// </summary>
|
||||
/// <param name="tenantId">租户ID.</param>
|
||||
/// <param name="userId">用户ID.</param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> DelUserInfo(string tenantId, string userId)
|
||||
{
|
||||
var cacheKey = string.Format("{0}{1}_{2}", CommonConst.CACHEKEYUSER, tenantId, userId);
|
||||
return await _cacheManager.DelAsync(cacheKey);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user