后端:增加 InEnum 自定义 Validator ,用于校验传递的字段,在枚举范围内。

This commit is contained in:
YunaiV 2019-04-03 19:17:04 +08:00
parent e1db53e357
commit 02e2b15ad0
7 changed files with 103 additions and 27 deletions

View File

@ -1,5 +1,7 @@
package cn.iocoder.mall.admin.api;
import cn.iocoder.common.framework.constant.CommonStatusEnum;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.bo.AdminBO;
import cn.iocoder.mall.admin.api.bo.AdminPageBO;
@ -20,10 +22,11 @@ public interface AdminService {
CommonResult<Boolean> updateAdmin(Integer adminId, AdminUpdateDTO adminUpdateDTO);
CommonResult<Boolean> updateAdminStatus(Integer adminId, Integer updateAdminId, Integer status);
CommonResult<Boolean> updateAdminStatus(Integer adminId, Integer updateAdminId,
@InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}") Integer status);
CommonResult<Boolean> deleteAdmin(Integer adminId, Integer updateAdminId);
CommonResult<Boolean> assignRole(Integer adminId, Integer updateAdminId, Set<Integer> roleIds);
}
}

View File

@ -7,15 +7,6 @@ import cn.iocoder.common.framework.dataobject.DeletableDO;
*/
public class AdminDO extends DeletableDO {
/**
* 账号状态 - 开启
*/
public static final Integer STATUS_ENABLE = 1;
/**
* 账号状态 - 禁用
*/
public static final Integer STATUS_DISABLE = 2;
/**
* 管理员编号
*/

View File

@ -1,7 +1,7 @@
package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.constant.CommonStatusEnum;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.AdminService;
@ -53,7 +53,7 @@ public class AdminServiceImpl implements AdminService {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_PASSWORD_ERROR.getCode());
}
// 账号被禁用
if (AdminDO.STATUS_DISABLE.equals(admin.getStatus())) {
if (CommonStatusEnum.DISABLE.getValue().equals(admin.getStatus())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_IS_DISABLE.getCode());
}
// 校验成功返回管理员并且去掉一些非关键字段考虑安全性
@ -87,7 +87,7 @@ public class AdminServiceImpl implements AdminService {
// 保存到数据库
AdminDO admin = AdminConvert.INSTANCE.convert(adminAddDTO)
.setPassword(encodePassword(adminAddDTO.getPassword())) // 加密密码
.setStatus(AdminDO.STATUS_ENABLE);
.setStatus(CommonStatusEnum.ENABLE.getValue());
admin.setCreateTime(new Date());
admin.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
adminMapper.insert(admin);
@ -118,10 +118,6 @@ public class AdminServiceImpl implements AdminService {
@Override
@Transactional
public CommonResult<Boolean> updateAdminStatus(Integer adminId, Integer updateAdminId, Integer status) {
// 校验参数
if (!isValidStatus(status)) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启1或关闭2"); // TODO 有点搓
}
// 校验账号存在
AdminDO admin = adminMapper.selectById(updateAdminId);
if (admin == null) {
@ -138,7 +134,7 @@ public class AdminServiceImpl implements AdminService {
AdminDO updateAdmin = new AdminDO().setId(updateAdminId).setStatus(status);
adminMapper.update(updateAdmin);
// 如果是关闭管理员则标记 token 失效否则管理员还可以继续蹦跶
if (AdminDO.STATUS_DISABLE.equals(status)) {
if (CommonStatusEnum.DISABLE.getValue().equals(status)) {
oAuth2Service.removeToken(updateAdminId);
}
// TODO 插入操作日志
@ -155,7 +151,7 @@ public class AdminServiceImpl implements AdminService {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
// 只有禁用的账号才可以删除
if (AdminDO.STATUS_ENABLE.equals(admin.getStatus())) {
if (CommonStatusEnum.ENABLE.getValue().equals(admin.getStatus())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_DELETE_ONLY_DISABLE.getCode());
}
// 标记删除 AdminDO
@ -204,8 +200,4 @@ public class AdminServiceImpl implements AdminService {
return DigestUtils.md5DigestAsHex(password.getBytes());
}
private boolean isValidStatus(Integer status) {
return AdminDO.STATUS_ENABLE.equals(status)
|| AdminDO.STATUS_DISABLE.equals(status);
}
}
}

View File

@ -1,13 +1,19 @@
package cn.iocoder.common.framework.constant;
import cn.iocoder.common.framework.core.IntArrayValuable;
import java.util.Arrays;
/**
* 通用状态枚举
*/
public enum CommonStatusEnum {
public enum CommonStatusEnum implements IntArrayValuable {
ENABLE(1, "开启"),
DISABLE(2, "关闭");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CommonStatusEnum::getValue).toArray();
/**
* 状态值
*/
@ -48,4 +54,9 @@ public enum CommonStatusEnum {
|| DISABLE.value.equals(status);
}
}
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -0,0 +1,13 @@
package cn.iocoder.common.framework.core;
/**
* 可生成 Int 数组的接口
*/
public interface IntArrayValuable {
/**
* @return int 数组
*/
int[] array();
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.common.framework.validator;
import cn.iocoder.common.framework.core.IntArrayValuable;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(
validatedBy = InEnumValidator.class
)
public @interface InEnum {
/**
* @return 实现 EnumValuable 接口的
*/
Class<? extends IntArrayValuable> value();
String message() default "必须在指定范围 {value}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -0,0 +1,39 @@
package cn.iocoder.common.framework.validator;
import cn.iocoder.common.framework.core.IntArrayValuable;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class InEnumValidator implements ConstraintValidator<InEnum, Integer> {
private List<Integer> values;
@Override
public void initialize(InEnum annotation) {
IntArrayValuable[] values = annotation.value().getEnumConstants();
if (values.length == 0) {
this.values = Collections.emptyList();
} else {
this.values = Arrays.stream(values[0].array()).boxed().collect(Collectors.toList());
}
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
// 校验通过
if (values.contains(value)) {
return true;
}
// 校验不通过自定义提示语句因为注解上的 value 是枚举类无法获得枚举类的实际值
context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()
.replaceAll("\\{value}", values.toString())).addConstraintViolation(); // 重新添加错误提示语句
return false;
}
}