| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springblade.common.constant.CommonConstant; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.mp.base.BaseServiceImpl; |
| | |
| | | import org.springblade.system.cache.DictCache; |
| | | import org.springblade.system.cache.ParamCache; |
| | | import org.springblade.system.cache.SysCache; |
| | | import org.springblade.system.entity.Strategy; |
| | | import org.springblade.system.entity.Tenant; |
| | | import org.springblade.system.enums.DictEnum; |
| | | import org.springblade.system.feign.ISysClient; |
| | |
| | | import org.springblade.system.user.service.IUserService; |
| | | import org.springblade.system.user.vo.UserVO; |
| | | import org.springblade.system.user.wrapper.UserWrapper; |
| | | 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.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | |
| | | import static org.springblade.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD; |
| | | |
| | |
| | | * @author Chill |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | @RequiredArgsConstructor |
| | | public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService { |
| | | private static final String GUEST_NAME = "guest"; |
| | | |
| | |
| | | private final IUserOauthService userOauthService; |
| | | private final ISysClient sysClient; |
| | | private final BladeTenantProperties tenantProperties; |
| | | //拿到配置的超管id |
| | | @Value("${user-info.id}") |
| | | private String adminUserId; |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | if (!user.getPassword().equals(DigestUtil.hex(oldPassword))) { |
| | | throw new ServiceException("原密码不正确!"); |
| | | } |
| | | return this.update(Wrappers.<User>update().lambda().set(User::getPassword, DigestUtil.hex(newPassword)).eq(User::getId, userId)); |
| | | //获取用户采用的密码策略 |
| | | Strategy strategy = sysClient.getByUserId(userId).getData(); |
| | | //密码长度校验 |
| | | if(newPassword1.length() < strategy.getMinPwdLen() || newPassword1.length() > strategy.getMaxPwdLen()){ |
| | | throw new ServiceException("密码中必须含有【"+strategy.getCombinationNames()+"】中的【"+strategy.getRequiredType()+"】种密码组合方式,且密码长度必须在【"+strategy.getMinPwdLen()+"-"+strategy.getMaxPwdLen()+"】范围内"); |
| | | } |
| | | List<String> regexs = sysClient.getRegexByList(Arrays.asList(strategy.getCombinationIds().split(","))).getData(); |
| | | //判断是否满足组合方式中的必填种类数 |
| | | int reqType = 0; |
| | | for (int i = 0; i < regexs.size(); i++) { |
| | | if(reqType>=strategy.getRequiredType()){ |
| | | break; |
| | | } |
| | | if(!Func.isEmpty(RegexUtil.findResult(regexs.get(i),newPassword1))){ |
| | | reqType++; |
| | | } |
| | | } |
| | | String resException = "密码中必须含有【"+strategy.getCombinationNames()+"】中的【"+strategy.getRequiredType()+"】种类型,请重新输入密码!"; |
| | | 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); |
| | | } |
| | | //修改密码同时,改变用户信息中的密码修改状态字段,密码修改时间 |
| | | return this.update(Wrappers.<User>update().lambda() |
| | | .set(User::getPassword, DigestUtil.hex(DigestUtils.md5DigestAsHex((newPassword1).getBytes()))) |
| | | .set(User::getStrategyUpdateStatus,CommonConstant.TOP_PARENT_ID) |
| | | .set(User::getPwdUpdateTime,new Date()) |
| | | .eq(User::getId, userId)); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return userVO; |
| | | } |
| | | |
| | | @Override |
| | | public Long checkRenAndExpr(Long userId) { |
| | | //超级管理员直接返回不需要提醒密码修改 |
| | | if(adminUserId.equals(userId)){ |
| | | return 0L; |
| | | } |
| | | //获取到密码修改时间 |
| | | Date pwdUpdateTime = this.getOne(Wrappers.<User>query().eq("ID", userId)).getPwdUpdateTime(); |
| | | Long pwdupdateday = 0L; |
| | | if(!Func.isEmpty(pwdUpdateTime)){ |
| | | pwdupdateday = dateToDay(pwdUpdateTime); |
| | | } |
| | | Strategy strategy = sysClient.getByUserId(userId).getData(); |
| | | //是否提醒通过最后一次修改密码的时间加上过期时间减去当前时间,如果小于过期提醒时间就进行提醒,如果<=0就提醒必须修改密码 |
| | | long reminder = pwdupdateday+strategy.getExpirationTime()-dateToDay(pwdUpdateTime); |
| | | //提醒用户必须修改密码 |
| | | if(reminder<=0){ |
| | | return -1L; |
| | | } |
| | | //提醒用户还有多久过期 |
| | | if(reminder<=strategy.getReminderTime()){ |
| | | long res = (strategy.getReminderTime()+pwdupdateday)-dateToDay(new Date()); |
| | | return res; |
| | | } |
| | | //代表正常状态还未到提醒与过期时间,既不提醒也不要求修改 |
| | | return 0L; |
| | | } |
| | | |
| | | /** |
| | | * 时间格式转天 |
| | | * @param date |
| | | * @return |
| | | */ |
| | | private Long dateToDay(Date date){ |
| | | long time = date.getTime(); |
| | | return time/(1000 * 60 * 60 * 24); |
| | | } |
| | | |
| | | |
| | | } |