ludc
2023-10-10 94f5e5130f941deb0d69537851009ce56c204228
Source/UBCS/ubcs-service/ubcs-user/src/main/java/com/vci/ubcs/system/user/service/impl/UserServiceImpl.java
@@ -19,9 +19,11 @@
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vci.ubcs.system.cache.DictCache;
import com.vci.ubcs.system.cache.NacosConfigCache;
import com.vci.ubcs.system.cache.ParamCache;
import com.vci.ubcs.system.cache.SysCache;
import com.vci.ubcs.system.entity.Strategy;
@@ -44,18 +46,19 @@
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.BladeTenantProperties;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import java.util.*;
import java.util.stream.Collectors;
import static com.vci.ubcs.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD;
@@ -67,22 +70,23 @@
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {
   private static final String GUEST_NAME = "guest";
   private final IUserDeptService userDeptService;
   private final IUserOauthService userOauthService;
   private final ISysClient sysClient;
   private final BladeTenantProperties tenantProperties;
   //拿到配置的超管id
   @Value("${user-info.id}")
   private String adminUserId;
   //拿到配置的超管
   private final NacosConfigCache nacosConfigCache;
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean submit(User user) {
      if (StringUtil.isBlank(user.getTenantId())) {
         user.setTenantId(BladeConstant.ADMIN_TENANT_ID);
         // user.setTenantId(BladeConstant.ADMIN_TENANT_ID);
         // 默认设置为管理组下的用户
         user.setTenantId(nacosConfigCache.getAdminUserInfo().getTenantId());
      }
      String tenantId = user.getTenantId();
      //Tenant tenant = SysCache.getTenant(tenantId);
@@ -160,6 +164,21 @@
   }
   @Override
   public List<User> selectAllUser(User user, Long deptId){
      List<Long> deptIdList = SysCache.getDeptChildIds(deptId);
      List<User> users = baseMapper.selectUserPage(user, deptIdList, (AuthUtil.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : AuthUtil.getTenantId()));
      return users;
   }
   @Override
   public List<User> selectAllUser(){
      LambdaQueryWrapper<User> wrapper=   Wrappers.lambdaQuery();
      wrapper.eq(User::getIsDeleted,0);
      List<User> users = baseMapper.selectList(wrapper);
      return users;
   }
   @Override
   public IPage<UserVO> selectUserSearch(UserVO user, Query query) {
      LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>query().lambda();
      String tenantId = AuthUtil.getTenantId();
@@ -204,13 +223,20 @@
   @Override
   public UserInfo userInfo(String tenantId, String account) {
      User user = baseMapper.getUser(tenantId, account);
      User user = baseMapper.getUser(tenantId, account,null);
      return buildUserInfo(user);
   }
   @Override
   public UserInfo userInfo(String tenantId, String account,String name) {
      User user = baseMapper.getUser(tenantId, account,name);
      UserInfo userInfo = buildUserInfo(user);
      return null;
   }
   @Override
   public UserInfo userInfo(String tenantId, String account, UserEnum userEnum) {
      User user = baseMapper.getUser(tenantId, account);
      User user = baseMapper.getUser(tenantId, account,null);
      return buildUserInfo(user, userEnum);
   }
@@ -222,6 +248,7 @@
      if (ObjectUtil.isEmpty(user)) {
         return null;
      }
      user.setDeptName(Func.join(SysCache.getDeptNames(user.getDeptId())));
      UserInfo userInfo = new UserInfo();
      userInfo.setUser(user);
      if (Func.isNotEmpty(user)) {
@@ -307,6 +334,10 @@
      }
      //获取用户采用的密码策略
      Strategy strategy = sysClient.getByUserId(userId).getData();
      // 几乎不会出现这种情况
      if(ObjectUtil.isEmpty(strategy)) {
         throw new ServiceException("当前用户未应用密码策略!");
      }
      //密码长度校验
      if(newPassword1.length() < strategy.getMinPwdLen() || newPassword1.length() > strategy.getMaxPwdLen()){
         throw new ServiceException("密码中必须含有【"+strategy.getCombinationNames()+"】中的【"+strategy.getRequiredType()+"】种密码组合方式,且密码长度必须在【"+strategy.getMinPwdLen()+"-"+strategy.getMaxPwdLen()+"】范围内");
@@ -318,7 +349,7 @@
         if(reqType>=strategy.getRequiredType()){
            break;
         }
         if(!Func.isEmpty(RegexUtil.findResult(regexs.get(i),newPassword1))){
         if(RegexUtil.find(regexs.get(i),newPassword1)){
            reqType++;
         }
      }
@@ -326,12 +357,13 @@
      if(reqType<strategy.getRequiredType()){
         throw new ServiceException(resException);
      }
      // 是否属于组合方式中的类型
      // 是否属于组合方式中的类型,以前是密码必须是包含在组合方式中的类型
      String regex = sysClient.getRegex(Arrays.asList(strategy.getCombinationIds().split(","))).getData();
      regex = "^"+regex+"{"+strategy.getRequiredType()+",}$";
      boolean result = RegexUtil.find(regex, newPassword1);
      if(!result){
         throw new ServiceException(resException);
         throw new ServiceException("密码中只能存在【"+strategy.getCombinationNames()+"】中包含的字符!");
         //throw new ServiceException(resException);
      }
      //修改密码同时,改变用户信息中的密码修改状态字段,密码修改时间
      return this.update(Wrappers.<User>update().lambda()
@@ -352,6 +384,7 @@
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void importUser(List<UserExcel> data, Boolean isCovered) {
      ArrayList<User> addUsers = new ArrayList<>();
      data.forEach(userExcel -> {
         User user = Objects.requireNonNull(BeanUtil.copy(userExcel, User.class));
         // 设置用户平台
@@ -379,8 +412,10 @@
         // 获取默认密码配置
         String initPassword = ParamCache.getValue(DEFAULT_PARAM_PASSWORD);
         user.setPassword(initPassword);
         this.submit(user);
         addUsers.add(user);
         //this.submit(user);
      });
      this.submitList(addUsers);
   }
   @Override
@@ -417,6 +452,7 @@
      boolean oauthTemp = userOauthService.updateById(userOauth);
      return (userTemp && oauthTemp);
   }
   @Override
   public boolean updatePlatform(Long userId, Integer userType, String userExt) {
      if (userType.equals(UserEnum.WEB.getCategory())) {
@@ -478,11 +514,13 @@
   @Override
   public Long checkRenAndExpr(Long userId) {
      //超级管理员直接返回不需要提醒密码修改
      if(adminUserId.equals(userId)){
      if(nacosConfigCache.getAdminUserInfo().getUserId().equals(userId)){
         return 0L;
      }
      QueryWrapper<User> wrapper = Wrappers.<User>query().eq("ID", userId);
      User dbUser = this.getOne(wrapper);
      //获取到密码修改时间
      Date pwdUpdateTime = this.getOne(Wrappers.<User>query().eq("ID", userId)).getPwdUpdateTime();
      Date pwdUpdateTime = Func.isNotEmpty(dbUser) ? dbUser.getPwdUpdateTime():new Date();
      Long pwdupdateday = 0L;
      if(!Func.isEmpty(pwdUpdateTime)){
         pwdupdateday = dateToDay(pwdUpdateTime);
@@ -503,8 +541,37 @@
      return 0L;
   }
   @Override
   public boolean updateByUseStrategyId(List<Long> userIds) {
      return this.update(Wrappers.<User>lambdaUpdate().in(User::getId, userIds).set(User::getStrategyUpdateStatus,CommonConstant.IS_DEFAULT));
   }
   /**
    * 时间格式转天
    * 获取到指定身份权限的用户列表
    * @param user 用户查询的用户信息,如租户信息,通常为自动注入,前端可选择不传
    * @param roleName 要查询的角色身份
    * @return
    */
   @Override
   public List<Map<String,String>> getByRoleUserList(BladeUser user, String roleName) {
      // 考虑到一个用户可以拥有多种角色权限,而用户关联角色权限是用role_id字段用逗号分隔角色id的,直接采用子查询来in查询不能实现,所以先查询角色id
      R<String> roleIds = sysClient.getRoleIds(user.getTenantId(), roleName);
      if(!roleIds.isSuccess()){
         throw new ServiceException("系统服务feign接口调用错误!");
      }
      if(Func.isBlank(roleIds.getData())){
         return new ArrayList<>();
      }
      List<Map<String,String>> list = new ArrayList<>();
      Arrays.stream(roleIds.getData().split(",")).forEach(item->{
         list.addAll(this.baseMapper.getUserMap(item,user.getUserId().toString()));
      });
      // 去除重复
      return list.stream().distinct().collect(Collectors.toList());
   }
   /**
    * 日期时间格式转天
    * @param date
    * @return
    */