Skip to content

Commit

Permalink
perf: 系统参数新增根据类别查询方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed May 30, 2024
1 parent 7a6cafc commit 694cbb2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ public void validate(String password, int value, UserDO user) {
private final String msg;

/**
* 策略前缀
* 策略类别
*/
public static final String PREFIX = "PASSWORD_";
public static final String CATEGORY = "PASSWORD";

/**
* 校验取值范围
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@

package top.continew.admin.system.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.continew.admin.system.model.entity.OptionDO;
import top.continew.starter.data.mybatis.plus.base.BaseMapper;

import java.util.List;

/**
* 参数 Mapper
*
* @author Bull-BCLS
* @since 2023/8/26 19:38
*/
public interface OptionMapper extends BaseMapper<OptionDO> {

/**
* 根据类别查询
*
* @param category 类别
* @return 列表
*/
@Select("SELECT code, value, default_value FROM sys_option WHERE category = #{category}")
List<OptionDO> selectByCategory(@Param("category") String category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import top.continew.admin.system.model.resp.OptionResp;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
Expand All @@ -40,6 +41,14 @@ public interface OptionService {
*/
List<OptionResp> list(OptionQuery query);

/**
* 根据类别查询
*
* @param category 类别
* @return 参数信息
*/
Map<String, String> getByCategory(String category);

/**
* 修改参数
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -56,14 +58,25 @@ public class OptionServiceImpl implements OptionService {
private final OptionMapper baseMapper;

@Override
@Cached(key = "#query.category", name = CacheConstants.OPTION_KEY_PREFIX)
public List<OptionResp> list(OptionQuery query) {
return BeanUtil.copyToList(baseMapper.selectList(QueryWrapperHelper.build(query)), OptionResp.class);
}

@Override
@Cached(key = "#category", name = CacheConstants.OPTION_KEY_PREFIX + "MAP:")
public Map<String, String> getByCategory(String category) {
return baseMapper.selectByCategory(category)
.stream()
.collect(Collectors.toMap(OptionDO::getCode, o -> StrUtil.emptyIfNull(ObjectUtil.defaultIfNull(o
.getValue(), o.getDefaultValue())), (oldVal, newVal) -> oldVal));
}

@Override
public void update(List<OptionReq> options) {
Map<String, String> passwordPolicyOptionMap = options.stream()
.filter(option -> StrUtil.startWith(option.getCode(), PasswordPolicyEnum.PREFIX))
.filter(option -> StrUtil.startWith(option
.getCode(), PasswordPolicyEnum.CATEGORY + StringConstants.UNDERLINE))
.collect(Collectors.toMap(OptionReq::getCode, OptionReq::getValue, (oldVal, newVal) -> oldVal));
// 校验密码策略参数取值范围
for (Map.Entry<String, String> passwordPolicyOptionEntry : passwordPolicyOptionMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.CacheInvalidate;
Expand Down Expand Up @@ -333,16 +334,17 @@ protected void afterAdd(UserReq req, UserDO user) {
* @param user 用户信息
*/
private int checkPassword(String password, UserDO user) {
Map<String, String> passwordPolicy = optionService.getByCategory(CATEGORY);
// 密码最小长度
PASSWORD_MIN_LENGTH.validate(password, optionService.getValueByCode2Int(PASSWORD_MIN_LENGTH.name()), user);
PASSWORD_MIN_LENGTH.validate(password, MapUtil.getInt(passwordPolicy, PASSWORD_MIN_LENGTH.name()), user);
// 密码是否必须包含特殊字符
PASSWORD_CONTAIN_SPECIAL_CHARACTERS.validate(password, optionService
.getValueByCode2Int(PASSWORD_CONTAIN_SPECIAL_CHARACTERS.name()), user);
PASSWORD_CONTAIN_SPECIAL_CHARACTERS.validate(password, MapUtil
.getInt(passwordPolicy, PASSWORD_CONTAIN_SPECIAL_CHARACTERS.name()), user);
// 密码是否允许包含正反序账号名
PASSWORD_ALLOW_CONTAIN_USERNAME.validate(password, optionService
.getValueByCode2Int(PASSWORD_ALLOW_CONTAIN_USERNAME.name()), user);
PASSWORD_ALLOW_CONTAIN_USERNAME.validate(password, MapUtil
.getInt(passwordPolicy, PASSWORD_ALLOW_CONTAIN_USERNAME.name()), user);
// 密码重复使用规则
int passwordReusePolicy = optionService.getValueByCode2Int(PASSWORD_REUSE_POLICY.name());
int passwordReusePolicy = MapUtil.getInt(passwordPolicy, PASSWORD_REUSE_POLICY.name());
PASSWORD_REUSE_POLICY.validate(password, passwordReusePolicy, user);
return passwordReusePolicy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.Cached;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand All @@ -31,7 +30,6 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.model.resp.LabelValueResp;
import top.continew.admin.system.model.query.DeptQuery;
import top.continew.admin.system.model.query.MenuQuery;
Expand Down Expand Up @@ -112,7 +110,6 @@ public R<List<LabelValueResp<Serializable>>> listDict(@PathVariable String code)
@SaIgnore
@Operation(summary = "查询参数字典", description = "查询参数字典")
@GetMapping("/dict/option")
@Cached(key = "#query.category", name = CacheConstants.OPTION_KEY_PREFIX)
public R<List<LabelValueResp<String>>> listOptionDict(@Validated OptionQuery query) {
return R.ok(optionService.list(query)
.stream()
Expand Down

0 comments on commit 694cbb2

Please sign in to comment.