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
|
||||
}
|
||||
@@ -474,6 +474,13 @@ public class UsersCurrentService : IUsersCurrentService, IDynamicApiController,
|
||||
}
|
||||
else
|
||||
{
|
||||
// 当前系统已被管理员禁用.
|
||||
var switchSystem = await _repository.AsSugarClient().Queryable<SystemEntity>()
|
||||
.Where(it => input.majorId.Equals(it.Id) && it.DeleteMark == null)
|
||||
.FirstAsync();
|
||||
if (switchSystem != null && !switchSystem.EnabledMark.Equals(1))
|
||||
throw Oops.Oh(ErrorCode.D4014);
|
||||
|
||||
// 系统下没有菜单不允许切换.
|
||||
var mList = await _repository.AsSugarClient().Queryable<ModuleEntity>().Where(x => x.SystemId.Equals(input.majorId) && x.DeleteMark == null && x.Category.Equals("Web")).Select(x => x.Id).ToListAsync();
|
||||
if (!mList.Any()) throw Oops.Oh(ErrorCode.D4009);
|
||||
|
||||
@@ -6,6 +6,7 @@ using JNPF.Common.Enums;
|
||||
using JNPF.Common.Extension;
|
||||
using JNPF.Common.Filter;
|
||||
using JNPF.Common.Helper;
|
||||
using JNPF.Common.Manager;
|
||||
using JNPF.Common.Models.NPOI;
|
||||
using JNPF.Common.Models.User;
|
||||
using JNPF.Common.Security;
|
||||
@@ -69,6 +70,11 @@ public class UsersService : IUsersService, IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
private readonly IFileManager _fileManager;
|
||||
|
||||
/// <summary>
|
||||
/// 缓存管理.
|
||||
/// </summary>
|
||||
private readonly ICacheManager _cacheManager;
|
||||
|
||||
/// <summary>
|
||||
/// 用户管理.
|
||||
/// </summary>
|
||||
@@ -83,6 +89,7 @@ public class UsersService : IUsersService, IDynamicApiController, ITransient
|
||||
IUserRelationService userRelationService,
|
||||
ISysConfigService sysConfigService,
|
||||
ISynThirdInfoService synThirdInfoService,
|
||||
ICacheManager cacheManager,
|
||||
IFileManager fileService,
|
||||
IUserManager userManager)
|
||||
{
|
||||
@@ -91,6 +98,7 @@ public class UsersService : IUsersService, IDynamicApiController, ITransient
|
||||
_userRelationService = userRelationService;
|
||||
_sysConfigService = sysConfigService;
|
||||
_userManager = userManager;
|
||||
_cacheManager = cacheManager;
|
||||
_synThirdInfoService = synThirdInfoService;
|
||||
_fileManager = fileService;
|
||||
}
|
||||
@@ -1522,6 +1530,7 @@ public class UsersService : IUsersService, IDynamicApiController, ITransient
|
||||
fs.Close();
|
||||
}
|
||||
|
||||
_cacheManager.Set(excelconfig.FileName, string.Empty);
|
||||
return new { name = excelconfig.FileName, url = "/api/file/Download?encryption=" + DESCEncryption.Encrypt(_userManager.UserId + "|" + excelconfig.FileName + "|" + addPath, "JNPF") };
|
||||
}
|
||||
|
||||
@@ -1551,6 +1560,7 @@ public class UsersService : IUsersService, IDynamicApiController, ITransient
|
||||
string? addPath = Path.Combine(FileVariable.TemporaryFilePath, excelconfig.FileName);
|
||||
ExcelExportHelper<UserListImportDataInput>.Export(dataList, excelconfig, addPath);
|
||||
|
||||
_cacheManager.Set(excelconfig.FileName, string.Empty);
|
||||
return new { name = excelconfig.FileName, url = "/api/file/Download?encryption=" + DESCEncryption.Encrypt(_userManager.UserId + "|" + excelconfig.FileName + "|" + addPath, "JNPF") };
|
||||
}
|
||||
|
||||
@@ -1631,6 +1641,7 @@ public class UsersService : IUsersService, IDynamicApiController, ITransient
|
||||
string? addPath = Path.Combine(FileVariable.TemporaryFilePath, excelconfig.FileName);
|
||||
ExcelExportHelper<UserListImportDataInput>.Export(errorlist, excelconfig, addPath);
|
||||
|
||||
_cacheManager.Set(excelconfig.FileName, string.Empty);
|
||||
return new { name = excelconfig.FileName, url = "/api/file/Download?encryption=" + DESCEncryption.Encrypt(_userManager.UserId + "|" + excelconfig.FileName + "|" + addPath, "JNPF") };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user