merge from 2023-03-14

This commit is contained in:
2023-03-24 09:37:07 +08:00
parent f95ef4cf73
commit 3de92dab06
57 changed files with 1538 additions and 528 deletions

View File

@@ -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
}

View File

@@ -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);

View File

@@ -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") };
}

View File

@@ -46,7 +46,8 @@ namespace JNPF.Systems;
/// <summary>
/// 数据接口
/// 版 本V3.2
/// 版 权:拓通智联科技有限公司http://www.tuotong-tech.com
/// 版 权:引迈信息技术有限公司https://www.jnpfsoft.com
/// 作 者JNPF开发平台组
/// 日 期2021-06-01.
/// </summary>
[ApiDescriptionSettings(Tag = "System", Name = "DataInterface", Order = 204)]
@@ -345,7 +346,7 @@ public class DataInterfaceService : IDataInterfaceService, IDynamicApiController
{
string sheetData = Regex.Match(info.DataProcessing, @"\{(.*)\}", RegexOptions.Singleline).Groups[1].Value;
var scriptStr = "var result = function(data){data = JSON.parse(data);" + sheetData + "}";
return JsEngineUtil.CallFunction(scriptStr, output.ToJsonString());
return JsEngineUtil.CallFunction(scriptStr, output.ToJsonString(CommonConst.options));//此处时间非时间戳
}
}
@@ -732,7 +733,7 @@ public class DataInterfaceService : IDataInterfaceService, IDynamicApiController
list = await GetDynamicDataCache(dynamic.Id);
if (list == null)
if (list == null || list.Count == 0)
{
list = new List<StaticDataModel>();
// 远端数据 配置参数

View File

@@ -1,7 +1,12 @@
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.Filter;
using JNPF.Common.Manager;
using JNPF.Common.Models.User;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
@@ -27,6 +32,16 @@ public class SystemService : IDynamicApiController, ITransient
/// </summary>
private readonly ISqlSugarRepository<SystemEntity> _repository;
/// <summary>
/// 缓存管理器.
/// </summary>
private readonly ICacheManager _cacheManager;
/// <summary>
/// IM中心处理程序.
/// </summary>
private IMHandler _imHandler;
/// <summary>
/// 用户管理.
/// </summary>
@@ -37,9 +52,13 @@ public class SystemService : IDynamicApiController, ITransient
/// </summary>
public SystemService(
ISqlSugarRepository<SystemEntity> repository,
ICacheManager cacheManager,
IMHandler imHandler,
IUserManager userManager)
{
_repository = repository;
_cacheManager = cacheManager;
_imHandler = imHandler;
_userManager = userManager;
}
@@ -108,10 +127,99 @@ public class SystemService : IDynamicApiController, ITransient
{
if (await _repository.IsAnyAsync(x => x.Id != id && (x.EnCode == input.enCode || x.FullName == input.fullName) && x.DeleteMark == null))
throw Oops.Oh(ErrorCode.COM1004);
var entity = input.Adapt<SystemEntity>();
var isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync();
if (!isOk)
var mainSystem = await _repository.GetFirstAsync(it => it.IsMain.Equals(1) && it.EnabledMark.Equals(1) && it.DeleteMark == null);
// 判断主系统是否被禁用.
if (input.id.Equals(mainSystem.Id) && input.enabledMark.Equals(0))
throw Oops.Oh(ErrorCode.D1036);
// 判断主系统是否有修改系统编码.
if (input.id.Equals(mainSystem.Id) && !input.enCode.Equals(mainSystem.EnCode))
throw Oops.Oh(ErrorCode.D1037);
var isOk = await _repository.AsUpdateable().SetColumns(it => new SystemEntity()
{
FullName = input.fullName,
EnCode = input.enCode,
Icon = input.icon,
SortCode = input.sortCode,
Description = input.description,
EnabledMark = input.enabledMark,
LastModifyUserId = _userManager.UserId,
LastModifyTime = SqlFunc.GetDate(),
}).Where(it => it.Id.Equals(id)).ExecuteCommandAsync();
if (!(isOk > 0))
throw Oops.Oh(ErrorCode.COM1001);
// 当用户的子系统被禁用时,更换成其他未被禁用的系统.
if (!input.id.Equals(mainSystem.Id) && input.enabledMark.Equals(0))
{
var systemUser = await _repository.AsSugarClient().Queryable<UserEntity>()
.Where(it => it.DeleteMark == null && input.id.Equals(it.SystemId))
.Select(x => new UserEntity()
{
Id = x.Id,
SystemId = x.SystemId
}).ToListAsync();
// 获取用户所有角色id.
var userRoleIdList = await _repository.AsSugarClient().Queryable<UserRelationEntity>()
.Where(it => systemUser.Select(s => s.Id).ToList().Contains(it.UserId) && it.ObjectType.Equals("Role"))
.Select(x => x.ObjectId)
.ToListAsync();
var authorizeList = await _repository.AsSugarClient().Queryable<AuthorizeEntity>()
.Where(it => it.ItemType.Equals("module") && it.ObjectType.Equals("Role"))
.ToListAsync();
var moduleList = await _repository.AsSugarClient().Queryable<ModuleEntity>()
.Where(it => !it.SystemId.Equals(mainSystem.Id) && !it.SystemId.Equals(input.id) && it.DeleteMark == null)
.ToListAsync();
systemUser.ForEach(async item =>
{
// 获取用户所有角色的菜单.
var moduleIdList = authorizeList
.Where(x => userRoleIdList.Contains(x.ObjectId))
.Select(s => s.ItemId);
// 获取用户所有有权限的系统id.
var systemId = moduleList
.Where(x => moduleIdList.Contains(x.Id))
.Select(s => s.SystemId).FirstOrDefault();
if (systemId == null)
systemId = mainSystem.Id;
// 更新用户的系统id.
var res = await _repository.AsSugarClient().Updateable<UserEntity>()
.Where(x => item.Id.Equals(x.Id))
.SetColumns(x => new UserEntity()
{
SystemId = systemId
}).ExecuteCommandAsync();
if (!(res > 0))
throw Oops.Oh(ErrorCode.COM1001);
});
var tenantId = _userManager.TenantId;
var cacheKey = string.Format("{0}{1}", CommonConst.CACHEKEYONLINEUSER, tenantId);
var allUserOnlineList = await _cacheManager.GetAsync<List<UserOnlineModel>>(cacheKey);
var userOnlineList = allUserOnlineList.FindAll(it => systemUser.Select(s => s.Id).ToList().Contains(it.userId));
userOnlineList.ForEach(async item =>
{
await _imHandler.SendMessageAsync(item.connectionId, new { method = "logout", msg = "此系统已被禁用" }.ToJsonString());
// 删除在线用户ID.
allUserOnlineList.RemoveAll((x) => x.connectionId == item.connectionId);
// 删除用户登录信息缓存.
var mCacheKey = string.Format("{0}{1}_{2}", CommonConst.CACHEKEYUSER, tenantId, item.userId);
await _cacheManager.DelAsync(mCacheKey);
});
await _cacheManager.SetAsync(cacheKey, allUserOnlineList);
}
}
/// <summary>