租户一键启用

v1.4.1
Parker 4 years ago
parent 6e18a64e61
commit 3260ad329b

@ -51,7 +51,7 @@ public class TenantModel extends ApiWrapper {
@ExcelInfo(dictType = "no_yes") @ExcelInfo(dictType = "no_yes")
@ValidationArgs({ValiArgsType.IS_NOT_NULL}) @ValidationArgs({ValiArgsType.IS_NOT_NULL})
@ValidationArgsLenMax(1) @ValidationArgsLenMax(1)
private String izUsable; private String enable;
/** 备注 */ /** 备注 */
@ApiModelProperty(value = "备注") @ApiModelProperty(value = "备注")

@ -2,16 +2,19 @@ package org.opsli.core.security.shiro.realm;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*; import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.PrincipalCollection;
import org.opsli.api.wrapper.system.tenant.TenantModel;
import org.opsli.api.wrapper.system.user.UserModel; import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.core.api.TokenThreadLocal; import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException; import org.opsli.common.exception.TokenException;
import org.opsli.core.msg.TokenMsg; import org.opsli.core.msg.TokenMsg;
import org.opsli.core.security.shiro.token.JwtToken; import org.opsli.core.security.shiro.token.JwtToken;
import org.opsli.core.utils.TenantUtil;
import org.opsli.core.utils.UserTokenUtil; import org.opsli.core.utils.UserTokenUtil;
import org.opsli.core.utils.UserUtil; import org.opsli.core.utils.UserUtil;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -91,6 +94,15 @@ public class JwtRealm extends AuthorizingRealm implements FlagRealm {
TokenMsg.EXCEPTION_LOGIN_ACCOUNT_LOCKED); TokenMsg.EXCEPTION_LOGIN_ACCOUNT_LOCKED);
} }
// 4. 验证租户是否启用
// 如果不是超级管理员 需要验证租户是否生效
if(!StringUtils.equals(UserUtil.SUPER_ADMIN, user.getUsername())){
TenantModel tenant = TenantUtil.getTenant(user.getTenantId());
if(tenant == null){
throw new TokenException(TokenMsg.EXCEPTION_LOGIN_TENANT_NOT_USABLE);
}
}
return new SimpleAuthenticationInfo(user, accessToken, getName()); return new SimpleAuthenticationInfo(user, accessToken, getName());
} }

@ -589,6 +589,15 @@ public class UserUtil {
return user.getTenantId(); return user.getTenantId();
} }
/**
* ID
* @return String
*/
public static String getRealTenantId(){
UserModel user = getUser();
return user.getTenantId();
}
/** /**
* *
* @param currUser model * @param currUser model

@ -72,6 +72,8 @@ public enum SystemMsg implements BaseMsg {
EXCEPTION_TENANT_UNIQUE(20400,"租户名称重复,该租户已存在"), EXCEPTION_TENANT_UNIQUE(20400,"租户名称重复,该租户已存在"),
EXCEPTION_TENANT_USED_DEL(20401,"该租户正在被其他用户绑定,无法删除"), EXCEPTION_TENANT_USED_DEL(20401,"该租户正在被其他用户绑定,无法删除"),
EXCEPTION_TENANT_HANDLE_SELF(20402,"不可操作自身"), EXCEPTION_TENANT_HANDLE_SELF(20402,"不可操作自身"),
EXCEPTION_TENANT_HANDLE_SUPER_ADMIN(20403,"不可操作超管租户"),
/** /**

@ -38,7 +38,7 @@ public class SysTenant extends BaseEntity {
private String tenantName; private String tenantName;
/** 是否启用 0是 1否*/ /** 是否启用 0是 1否*/
private String izUsable; private String enable;
/** 备注 */ /** 备注 */
@TableField(updateStrategy = FieldStrategy.IGNORED) @TableField(updateStrategy = FieldStrategy.IGNORED)

@ -64,17 +64,32 @@ public class TenantServiceImpl extends CrudServiceImpl<TenantMapper, SysTenant,
private IUserService iUserService; private IUserService iUserService;
@Override @Override
@Transactional(rollbackFor = Exception.class)
public boolean enableTenant(String tenantId, String enable) { public boolean enableTenant(String tenantId, String enable) {
if(!DictType.hasDict(DictType.NO_YES_YES.getType(), enable)){
// 非法参数
throw new ServiceException(SystemMsg.EXCEPTION_USER_ILLEGAL_PARAMETER);
}
TenantModel model = this.get(tenantId); TenantModel model = this.get(tenantId);
if(model == null){ if(model == null){
return false; return false;
} }
String currTenantId = UserUtil.getTenantId(); String currTenantId = UserUtil.getRealTenantId();
if(StringUtils.equals(currTenantId, tenantId)){ if(StringUtils.equals(currTenantId, tenantId)){
// 不可操作自身 // 不可操作自身
throw new ServiceException(SystemMsg.EXCEPTION_TENANT_HANDLE_SELF); throw new ServiceException(SystemMsg.EXCEPTION_TENANT_HANDLE_SELF);
}
// 超级管理员
UserModel superAdmin = UserUtil.getUserByUserName(UserUtil.SUPER_ADMIN);
if(superAdmin != null){
String superAdminTenantId = superAdmin.getTenantId();
if(StringUtils.equals(superAdminTenantId, tenantId)){
// 不可操作超管租户
throw new ServiceException(SystemMsg.EXCEPTION_TENANT_HANDLE_SUPER_ADMIN);
}
} }
UpdateWrapper<SysTenant> updateWrapper = new UpdateWrapper<>(); UpdateWrapper<SysTenant> updateWrapper = new UpdateWrapper<>();

@ -73,13 +73,8 @@ public class TenantRestController extends BaseRestController<SysTenant, TenantMo
// 演示模式 不允许操作 // 演示模式 不允许操作
super.demoError(); super.demoError();
if(!DictType.hasDict(DictType.NO_YES_YES.getType(), enable)){
// 非法参数
throw new ServiceException(SystemMsg.EXCEPTION_USER_ILLEGAL_PARAMETER);
}
// 变更租户状态账户 // 变更租户状态账户
boolean enableStatus = IService.enableTenant(tenantId, tenantId); boolean enableStatus = IService.enableTenant(tenantId, enable);
if(!enableStatus){ if(!enableStatus){
return ResultVo.error("变更用户状态账户失败"); return ResultVo.error("变更用户状态账户失败");
} }
@ -250,7 +245,7 @@ public class TenantRestController extends BaseRestController<SysTenant, TenantMo
QueryBuilder<SysTenant> queryBuilder = new GenQueryBuilder<>(); QueryBuilder<SysTenant> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysTenant> queryWrapper = queryBuilder.build(); QueryWrapper<SysTenant> queryWrapper = queryBuilder.build();
queryWrapper.eq("id", tenantId) queryWrapper.eq("id", tenantId)
.eq("iz_usable", "1"); .eq("enable", DictType.NO_YES_YES.getValue());
SysTenant entity = IService.getOne(queryWrapper); SysTenant entity = IService.getOne(queryWrapper);
return ResultVo.success( return ResultVo.success(

Loading…
Cancel
Save