From 217fe0dce4fadefa8f78b21bc576085d17790513 Mon Sep 17 00:00:00 2001 From: zhangdaiscott Date: Wed, 15 Mar 2023 11:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=9F=E6=88=B7=E9=82=80=E8=AF=B7=E4=BA=BA?= =?UTF-8?q?=E6=94=B9=E6=88=90=E9=87=87=E7=94=A8=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=EF=BC=8C=E7=A7=9F=E6=88=B7=E6=95=8F=E6=84=9F=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8A=A0=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SysTenantController.java | 48 ++++++++++++++++-- .../system/service/ISysTenantService.java | 6 +-- .../service/impl/SysTenantServiceImpl.java | 49 ++++++++++--------- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysTenantController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysTenantController.java index 2ea89067..9ad486cf 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysTenantController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysTenantController.java @@ -12,6 +12,7 @@ import org.apache.shiro.authz.annotation.RequiresPermissions; import org.jeecg.common.api.vo.Result; import org.jeecg.common.aspect.annotation.PermissionData; import org.jeecg.common.config.TenantContext; +import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.constant.SymbolConstant; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.system.vo.LoginUser; @@ -19,6 +20,7 @@ import org.jeecg.common.util.PasswordUtil; import org.jeecg.common.util.TokenUtils; import org.jeecg.common.util.oConvertUtils; import org.jeecg.config.mybatis.MybatisPlusSaasConfig; +import org.jeecg.modules.base.service.BaseCommonService; import org.jeecg.modules.system.entity.*; import org.jeecg.modules.system.service.ISysTenantPackService; import org.jeecg.modules.system.service.ISysTenantService; @@ -55,6 +57,9 @@ public class SysTenantController { @Autowired private ISysTenantPackService sysTenantPackService; + + @Autowired + private BaseCommonService baseCommonService; /** * 获取列表数据 @@ -167,6 +172,22 @@ public class SysTenantController { //@RequiresPermissions("system:tenant:delete") @RequestMapping(value = "/delete", method ={RequestMethod.DELETE, RequestMethod.POST}) public Result delete(@RequestParam(name="id",required=true) String id) { + //------------------------------------------------------------------ + //如果是saas隔离的情况下,判断当前租户id是否是当前租户下的 + if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) { + //获取当前用户 + LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + SysTenant sysTenant = sysTenantService.getById(id); + + String username = "admin"; + String createdBy = sysUser.getUsername(); + if (!sysTenant.getCreateBy().equals(createdBy) && !username.equals(createdBy)) { + baseCommonService.addLog("未经授权,不能删除非自己创建的租户,租户ID:" + id + ",操作人:" + sysUser.getUsername(), CommonConstant.LOG_TYPE_2, CommonConstant.OPERATE_TYPE_3); + return Result.error("删除租户失败,当前操作人不是租户的创建人!"); + } + } + //------------------------------------------------------------------ + sysTenantService.removeTenantById(id); return Result.ok("删除成功"); } @@ -187,6 +208,22 @@ public class SysTenantController { // 过滤掉已被引用的租户 List idList = new ArrayList<>(); for (String id : ls) { + //------------------------------------------------------------------ + //如果是saas隔离的情况下,判断当前租户id是否是当前租户下的 + if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) { + //获取当前用户 + LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + SysTenant sysTenant = sysTenantService.getById(id); + + String username = "admin"; + String createdBy = sysUser.getUsername(); + if (!sysTenant.getCreateBy().equals(createdBy) && !username.equals(createdBy)) { + baseCommonService.addLog("未经授权,不能删除非自己创建的租户,租户ID:" + id + ",操作人:" + sysUser.getUsername(), CommonConstant.LOG_TYPE_2, CommonConstant.OPERATE_TYPE_3); + return Result.error("删除租户失败,当前操作人不是租户的创建人!"); + } + } + //------------------------------------------------------------------ + Long userCount = sysTenantService.countUserLinkTenant(id); if (userCount == 0) { idList.add(Integer.parseInt(id)); @@ -357,13 +394,13 @@ public class SysTenantController { /** * 邀请用户【低代码应用专用接口】 * @param ids - * @param userIds + * @param phone * @return */ @PutMapping("/invitationUserJoin") //@RequiresPermissions("system:tenant:invitation:user") - public Result invitationUserJoin(@RequestParam("ids") String ids, @RequestParam("userIds") String userIds){ - sysTenantService.invitationUserJoin(ids,userIds); + public Result invitationUserJoin(@RequestParam("ids") String ids, @RequestParam("phone") String phone){ + sysTenantService.invitationUserJoin(ids,phone); return Result.ok("邀请用户成功"); } @@ -402,7 +439,8 @@ public class SysTenantController { @RequestParam("tenantId") String tenantId){ Result result = new Result<>(); //是否开启系统管理模块的多租户数据隔离【SAAS多租户模式】 - if(MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL){ + LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + if(MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL && !"admin".equals(sysUser.getUsername())){ Integer loginSessionTenant = oConvertUtils.getInt(TenantContext.getTenant()); if(loginSessionTenant!=null && !loginSessionTenant.equals(Integer.valueOf(tenantId))){ result.error500("无权限访问他人租户!"); @@ -565,7 +603,7 @@ public class SysTenantController { return Result.error("未找到当前租户信息"); } if (!sysUser.getUsername().equals(tenant.getCreateBy())) { - return Result.error("没有权限"); + return Result.error("无权限,只能注销自己创建的租户!"); } SysUser userById = sysUserService.getById(sysUser.getId()); String loginPassword = request.getParameter("loginPassword"); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/ISysTenantService.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/ISysTenantService.java index 1d840b57..0a858adf 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/ISysTenantService.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/ISysTenantService.java @@ -45,11 +45,11 @@ public interface ISysTenantService extends IService { boolean removeTenantById(String id); /** - * 邀请用户加入租户 + * 邀请用户加入租户,通过手机号 * @param ids - * @param userIds + * @param phone */ - void invitationUserJoin(String ids, String userIds); + void invitationUserJoin(String ids, String phone); /** * 请离用户(租户) diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysTenantServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysTenantServiceImpl.java index d855b718..330118a7 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysTenantServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysTenantServiceImpl.java @@ -26,10 +26,8 @@ import org.jeecg.modules.system.service.ISysTenantService; import org.jeecg.modules.system.service.ISysUserService; import org.jeecg.modules.system.vo.tenant.*; import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; -import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -91,27 +89,31 @@ public class SysTenantServiceImpl extends ServiceImpl query = new LambdaQueryWrapper<>(); - query.eq(SysUserTenant::getTenantId,id); - query.eq(SysUserTenant::getUserId,userId); - long count = userTenantMapper.selectCount(query); - if(count == 0){ - SysUserTenant relation = new SysUserTenant(); - relation.setUserId(userId); - relation.setTenantId(Integer.valueOf(id)); - relation.setStatus(CommonConstant.USER_TENANT_NORMAL); - userTenantMapper.insert(relation); - } - //update-end---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表------------ + //update-begin---author:wangshuai ---date:20230313 for:【QQYUN-4605】后台的邀请谁加入租户,没办法选不是租户下的用户,通过手机号邀请------------ + SysUser userByPhone = userService.getUserByPhone(phone); + //说明用户不存在 + if(null == userByPhone){ + throw new JeecgBootException("当前用户不存在,请核对手机号"); + } + String userId = userByPhone.getId(); + //循环租户id + for (String id:idArray) { + //update-begin---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表------------ + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(SysUserTenant::getTenantId,id); + query.eq(SysUserTenant::getUserId,userId); + long count = userTenantMapper.selectCount(query); + if(count == 0){ + SysUserTenant relation = new SysUserTenant(); + relation.setUserId(userId); + relation.setTenantId(Integer.valueOf(id)); + relation.setStatus(CommonConstant.USER_TENANT_NORMAL); + userTenantMapper.insert(relation); } + //update-end---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表------------ + //update-end---author:wangshuai ---date:20230313 for:【QQYUN-4605】后台的邀请谁加入租户,没办法选不是租户下的用户,通过手机号邀请------------ } } @@ -477,6 +479,7 @@ public class SysTenantServiceImpl extends ServiceImpl userIdList = userList.stream().map(i->i.getId()).collect(Collectors.toList()); // 部门 List depList = baseMapper.queryUserDepartList(userIdList); - // 职位 - //List userPositions = baseMapper.queryUserPositionList(userIdList); +// // 职位 +// List userPositions = baseMapper.queryUserPositionList(userIdList); // 遍历用户 往用户中添加 部门信息和职位信息 for (TenantPackUser user : userList) { //添加部门