From 2a3164d91956bdba4ca78459c89e28706f06d9a9 Mon Sep 17 00:00:00 2001 From: wet <1034391973@qq.com> Date: Mon, 22 May 2023 16:48:12 +0800 Subject: [PATCH] 1 --- .../MQ/RecCompanyUserChangeService.cs | 142 +++++++++++++----- Myshipping.Core/Myshipping.Core.xml | 8 +- .../Service/Tenant/SysTenantService.cs | 23 ++- Myshipping.Core/Service/User/Dto/UserInput.cs | 5 + .../Service/User/ISysUserService.cs | 2 +- .../Service/User/SysUserService.cs | 32 +++- 6 files changed, 171 insertions(+), 41 deletions(-) diff --git a/Myshipping.Core/MQ/RecCompanyUserChangeService.cs b/Myshipping.Core/MQ/RecCompanyUserChangeService.cs index 7ce3055..304e517 100644 --- a/Myshipping.Core/MQ/RecCompanyUserChangeService.cs +++ b/Myshipping.Core/MQ/RecCompanyUserChangeService.cs @@ -1,4 +1,7 @@ -using Microsoft.Extensions.DependencyInjection; +using Furion; +using Furion.DataEncryption; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Myshipping.Core.Entity; @@ -9,6 +12,7 @@ using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Linq; +using System.Security.Claims; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -23,28 +27,36 @@ namespace Myshipping.Core.MQ { private readonly IServiceScopeFactory _scopeFactory; private readonly IServiceScope _serviceScope; - + private readonly ISysUserService _serviceUser; private readonly ILogger _logger; private readonly ISysTenantService _tenantService; private readonly ISysUserService _userService; private readonly SqlSugarRepository _sysUserRep; private readonly SqlSugarRepository _sysTenantRep; - + private readonly SqlSugarRepository _sysOrgRep; + private readonly SqlSugarRepository _sysPosRep; + private readonly SqlSugarRepository _sysRoleRep; + private readonly SqlSugarRepository _sysMenuRep; + private readonly ISysRoleMenuService _sysRoleMenuService; private IConnection mqConn; private IModel model; - public RecCompanyUserChangeService(IServiceScopeFactory scopeFactory) + public RecCompanyUserChangeService(IServiceScopeFactory scopeFactory, ISysUserService serviceUser, ISysRoleMenuService sysRoleMenuService) { _scopeFactory = scopeFactory; //通过这个注入DBContext _serviceScope = _scopeFactory.CreateScope(); - + _serviceUser = serviceUser; + _sysRoleMenuService = sysRoleMenuService; _logger = _serviceScope.ServiceProvider.GetService>(); _tenantService = _serviceScope.ServiceProvider.GetService(); _userService = _serviceScope.ServiceProvider.GetService(); _sysUserRep = _serviceScope.ServiceProvider.GetService>(); _sysTenantRep = _serviceScope.ServiceProvider.GetService>(); - + _sysOrgRep = _serviceScope.ServiceProvider.GetService>(); + _sysPosRep = _serviceScope.ServiceProvider.GetService>(); + _sysRoleRep = _serviceScope.ServiceProvider.GetService>(); + _sysMenuRep = _serviceScope.ServiceProvider.GetService>(); } public override void Dispose() @@ -88,8 +100,29 @@ namespace Myshipping.Core.MQ } - private void Consumer_Received(object ch, BasicDeliverEventArgs ea) + private async void Consumer_Received(object ch, BasicDeliverEventArgs ea) { + + //授权---------- (用超级管理员的权限) + var httpContext = App.HttpContext; + var sysTenant = _sysTenantRep.AsQueryable().Filter(null, true).First(x => x.TenantType == TenantTypeEnum.SYSTEM); + var systemtenantid = sysTenant.Id; + var userauth = _sysUserRep.AsQueryable().Filter(null, true).First(x => x.Account == "superAdmin" && x.TenantId == systemtenantid); + + ClaimsIdentity identity = new ClaimsIdentity("AuthenticationTypes.Federation"); + identity.AddClaim(new Claim(type: ClaimConst.CLAINM_USERID, value: userauth.Id.ToString())); + identity.AddClaim(new Claim(type: ClaimConst.CLAINM_ACCOUNT, value: userauth.Account)); + identity.AddClaim(new Claim(type: ClaimConst.CLAINM_NAME, value: userauth.Name)); + identity.AddClaim(new Claim(type: ClaimConst.CLAINM_SUPERADMIN, value: ((int)userauth.AdminType).ToString())); + identity.AddClaim(new Claim(type: ClaimConst.CLAINM_TENANT_TYPE, value: sysTenant.TenantType.ToString())); + identity.AddClaim(new Claim(type: ClaimConst.TENANT_ID, value: sysTenant.Id.ToString())); + identity.AddClaim(new Claim(type: ClaimConst.TENANT_NAME, value: sysTenant.Name)); + identity.AddClaim(new Claim(type: ClaimConst.DjyUserId, value: userauth.DjyUserId)); + ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity); + httpContext.User = claimsPrincipal; + + //----- + var body = ea.Body; var strBody = Encoding.UTF8.GetString(body.ToArray()); @@ -107,7 +140,7 @@ namespace Myshipping.Core.MQ { findTenant = _sysTenantRep.FirstOrDefault(x => x.Name == compname); //再根据公司全称查找 } - + if (findTenant == null) //找不到,新建 { var tenant = new SysTenant(); @@ -116,25 +149,33 @@ namespace Myshipping.Core.MQ tenant.AdminName = adminShowName; tenant.Email = $"{tenant.Id}"; tenant.TenantType = TenantTypeEnum.COMMON; - var newTenant = _sysTenantRep.InsertReturnEntity(tenant); - _tenantService.InitNewTenant(tenant); + tenant.CompId = compid; + findTenant = _sysTenantRep.InsertReturnEntity(tenant); + await _tenantService.InitNewTenant(findTenant); } - } - else if (type == "UserJoin") //用户加入公司 - { + } + else if (type == "UserJoin") //用户加入公司 + { + var compId = jobj.GetStringValue("compId"); + } else if (type == "UserLeave") //用户离职 { + var userId = jobj.GetJObjectValue("userId"); + var compId = jobj.GetStringValue("compId"); + //删除用户 + _sysUserRep.Delete(x => x.DjyUserId == userId.ToString()); + } else if (type == "CompanyUserSync") //公司及员工数据同步 { - /////同步租户 + //----------同步租户 var comp = jobj.GetJObjectValue("company"); var compid = comp.GetStringValue("CompId"); var compname = comp.GetStringValue("CompName"); @@ -155,35 +196,62 @@ namespace Myshipping.Core.MQ tenant.AdminName = adminShowName; tenant.Email = $"{tenant.Id}"; tenant.TenantType = TenantTypeEnum.COMMON; + tenant.CompId = compid; findTenant = _sysTenantRep.InsertReturnEntity(tenant); - _tenantService.InitNewTenant(tenant); + await _tenantService.InitNewTenant(findTenant); + //新增的系统租户新增权限 + GrantRoleMenuInput tenantmenuInput = new GrantRoleMenuInput(); + tenantmenuInput.Id = findTenant.Id; + List tenantGrantMenuIdList = new List(); + tenantGrantMenuIdList = _sysMenuRep.AsQueryable().Where(x => x.Pids.Contains("142307070910563")).Select(x => x.Id).ToList();//暂时写死默认新增权限管理 + tenantmenuInput.GrantMenuIdList = tenantGrantMenuIdList; + await _sysRoleMenuService.GrantMenu(tenantmenuInput); } - ////同步用户 - var user = jobj.GetJArrayValue("users"); - ///租户id + //---------- + //同步用户 + var user = jobj.GetJArrayValue("users"); + //租户id var tenantid = findTenant.Id; - foreach (var item in user) + var keyDES = App.GetOptions().DES; + foreach (JObject item in user) { - - - - - - - + var GID = item.GetStringValue("GID"); + var Accont = item.GetStringValue("CODENAME"); + var Name = item.GetStringValue("SHOWNAME"); + var Pass = DESCEncryption.Encrypt(item.GetStringValue("PASSWORD"), keyDES); + var Email = item.GetStringValue("EMAIL1"); + var Phone = item.GetStringValue("MOBILE"); + var PassWord = item.GetStringValue("PASSWORD"); + var Org = await _sysOrgRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.IsDeleted == false && x.Pid == 0).FirstAsync(); + + //------ 拼接对象调用新增用户接口 + + AddUserInput adduser = new AddUserInput(); + adduser.Account = Accont; + adduser.Password = PassWord; + adduser.Confirm = PassWord; + adduser.Sex = 1; + adduser.Phone = Phone; + adduser.TenantId = tenantid; + EmpOutput2 empOutput2 = new EmpOutput2(); + empOutput2.OrgId = Org.Id.ToString(); + empOutput2.OrgName = Org.Name; + List PosIdList = new List(); + var pos = _sysPosRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.IsDeleted == false).FirstAsync(); + PosIdList.Add(pos.Id); + empOutput2.PosIdList = PosIdList; + adduser.SysEmpParam = empOutput2; + await _serviceUser.AddUser(adduser, true); } - - - - - - - - - - - + //-----授权角色id普通用户 + var role = _sysRoleRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.Name == "普通用户").FirstAsync(); + GrantRoleMenuInput menuInput = new GrantRoleMenuInput(); + menuInput.Id = Convert.ToInt64(role.Id); + List GrantMenuIdList = new List(); + GrantMenuIdList.Add(351064299098181);//暂时写死 ,后续从字典获取 新增订舱台账 + menuInput.GrantMenuIdList = GrantMenuIdList; + await _sysRoleMenuService.GrantMenu(menuInput); } _logger.LogInformation($"收到消息:{strBody}"); diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index 52909c5..9fcb49c 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -15343,6 +15343,11 @@ 搜索状态(字典 0正常 1停用 2删除) + + + 租户id + + 账号 @@ -15601,11 +15606,12 @@ - + 增加用户 + diff --git a/Myshipping.Core/Service/Tenant/SysTenantService.cs b/Myshipping.Core/Service/Tenant/SysTenantService.cs index bfba0af..aaf1920 100644 --- a/Myshipping.Core/Service/Tenant/SysTenantService.cs +++ b/Myshipping.Core/Service/Tenant/SysTenantService.cs @@ -144,7 +144,15 @@ public class SysTenantService : ISysTenantService, IDynamicApiController, ITrans }; newRole = await _sysRoleRep.InsertReturnEntityAsync(newRole); - + //初始化角色普通用户 + await _sysRoleRep.InsertReturnEntityAsync(new SysRole + { + TenantId = tenantId, + DataScopeType = DataScopeType.ALL, + Code = "10", + Name = "普通用户", + RoleType = RoleType.AdminRole + }); // 初始化租户管理员 var newUser = new SysUser { @@ -194,6 +202,19 @@ public class SysTenantService : ISysTenantService, IDynamicApiController, ITrans await _sysRoleDataScopeRep.InsertAsync(sysRoleDataScope); await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE); await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_DATASCOPE); + + //初始化租户职位 + var syspos = new SysPos + { + Name = "普通职位", + Code = "100", + Sort = 100, + TenantId = tenantId + }; + await _sysPosRep.InsertAsync(syspos); + + + } /// diff --git a/Myshipping.Core/Service/User/Dto/UserInput.cs b/Myshipping.Core/Service/User/Dto/UserInput.cs index dc8fcd7..f986e19 100644 --- a/Myshipping.Core/Service/User/Dto/UserInput.cs +++ b/Myshipping.Core/Service/User/Dto/UserInput.cs @@ -72,6 +72,11 @@ public class UserInput : InputBase /// 搜索状态(字典 0正常 1停用 2删除) /// public CommonStatus SearchStatus { get; set; } = CommonStatus.ENABLE; + /// + /// 租户id + /// + public virtual long TenantId { get; set; } + } public class AddUserInput : UserInput diff --git a/Myshipping.Core/Service/User/ISysUserService.cs b/Myshipping.Core/Service/User/ISysUserService.cs index 4584aea..31db3b6 100644 --- a/Myshipping.Core/Service/User/ISysUserService.cs +++ b/Myshipping.Core/Service/User/ISysUserService.cs @@ -8,7 +8,7 @@ namespace Myshipping.Core.Service; public interface ISysUserService { - Task AddUser(AddUserInput input); + Task AddUser(AddUserInput input,bool isrole=false); Task ChangeUserStatus(UpdateUserInput input); Task DeleteUser(DeleteUserInput input); //Task ExportUser([FromQuery] UserInput input); diff --git a/Myshipping.Core/Service/User/SysUserService.cs b/Myshipping.Core/Service/User/SysUserService.cs index 3206cd2..90614a5 100644 --- a/Myshipping.Core/Service/User/SysUserService.cs +++ b/Myshipping.Core/Service/User/SysUserService.cs @@ -38,8 +38,12 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient private readonly ISysUserDataScopeService _sysUserDataScopeService; private readonly ISysUserRoleService _sysUserRoleService; private readonly ISysEmpPosService _sysEmpPosService; + private readonly SqlSugarRepository _sysUserRoleRep; // 用户权限表仓储 + private readonly SqlSugarRepository _sysRoleRep; public SysUserService(SqlSugarRepository sysUserRep, + SqlSugarRepository sysUserRoleRep, + SqlSugarRepository sysRoleRep, ISysCacheService sysCacheService, ISysEmpService sysEmpService, ISysUserDataScopeService sysUserDataScopeService, @@ -48,12 +52,14 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient ISysEmpPosService sysEmpPosService) { _sysUserRep = sysUserRep; + _sysUserRoleRep = sysUserRoleRep; _sysCacheService = sysCacheService; _sysEmpService = sysEmpService; _sysUserDataScopeService = sysUserDataScopeService; _sysUserRoleService = sysUserRoleService; _sysConfigService = sysConfigService; _sysEmpPosService = sysEmpPosService; + _sysRoleRep = sysRoleRep; } /// @@ -97,9 +103,10 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient /// 增加用户 /// /// + /// /// [HttpPost("/sysUser/add")] - public async Task AddUser(AddUserInput input) + public async Task AddUser(AddUserInput input,bool isrole =false) { // 数据范围检查 CheckDataScope(input.SysEmpParam == null || string.IsNullOrEmpty(input.SysEmpParam.OrgId) ? 0 : long.Parse(input.SysEmpParam.OrgId)); @@ -125,6 +132,29 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient await _sysEmpService.AddOrUpdate(input.SysEmpParam); _sysUserRep.CurrentCommitTran(); await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE); + //增加用户角色----同步数据新增角色为普通用户 + if (isrole) { + + _sysUserRoleRep.CurrentBeginTran(); + await _sysUserRoleRep.DeleteAsync(u => u.SysUserId == newUser.Id); + var role = _sysRoleRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == input.TenantId && x.Name == "普通用户").FirstAsync(); + var grantRoleIdList = new List(); + + grantRoleIdList.Add( + new SysUserRole + { + SysUserId = newUser.Id, + SysRoleId = role.Id + }); + + await _sysUserRoleRep.InsertAsync(grantRoleIdList); + _sysUserRoleRep.CurrentCommitTran(); + //清除缓存 + await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU); + await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION); + } + + } catch (Exception) {