Skip to content

Commit

Permalink
修复:补充校验
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Mar 29, 2023
1 parent d4aedaa commit 8b955a0
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,34 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import cn.hutool.core.lang.RegexPool;

/**
* 正则相关常量
*
* @author Charles7c
* @since 2023/1/10 20:06
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RegExpConsts {
public class RegexConsts implements RegexPool {

/**
* 密码正则(必须包含字母和数字的组合,可以使用特殊字符,长度在6-32之间)
* 用户名正则(长度为 4 到 16 位,可以包含字母、数字,下划线,以字母开头)
*/
public static final String USERNAME = "^[a-zA-Z][a-zA-Z0-9_]{3,15}$";

/**
* 密码正则(长度为 6 到 32 位,可以包含字母、数字、下划线,特殊字符,同时包含字母和数字)
*/
public static final String PASSWORD = "^(?=.*\\d)(?=.*[a-z]).{6,32}$";

/**
* 通用编码正则(长度为 2 到 16 位,可以包含字母、数字,下划线,以字母开头)
*/
public static final String GENERAL_CODE = "^[a-zA-Z][a-zA-Z0-9_]{1,15}$";

/**
* 通用名称正则(长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线)
*/
public static final String GENERAL_NAME = "^[\\u4e00-\\u9fa5a-zA-Z0-9_-]{1,20}$";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Data;

Expand All @@ -26,6 +27,7 @@
import org.hibernate.validator.constraints.Length;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;

/**
Expand All @@ -52,6 +54,7 @@ public class DeptRequest extends BaseRequest {
*/
@Schema(description = "部门名称")
@NotBlank(message = "部门名称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "部门名称长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线")
private String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Data;

import io.swagger.v3.oas.annotations.media.Schema;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.enums.MenuTypeEnum;

Expand Down Expand Up @@ -57,6 +59,7 @@ public class MenuRequest extends BaseRequest {
*/
@Schema(description = "菜单标题")
@NotBlank(message = "菜单标题不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "菜单标题长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线")
private String title;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Data;

Expand All @@ -29,6 +30,7 @@
import org.hibernate.validator.constraints.Length;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.DataScopeEnum;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;

Expand All @@ -49,12 +51,15 @@ public class RoleRequest extends BaseRequest {
*/
@Schema(description = "角色名称")
@NotBlank(message = "角色名称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "角色名称长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线")
private String name;

/**
* 角色编码
*/
@Schema(description = "角色编码")
@NotBlank(message = "角色编码不能为空")
@Pattern(regexp = RegexConsts.GENERAL_CODE, message = "角色编码长度为 2 到 16 位,可以包含字母、数字,下划线,以字母开头")
private String code;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Data;

import io.swagger.v3.oas.annotations.media.Schema;

import org.hibernate.validator.constraints.Length;

import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.GenderEnum;

/**
Expand All @@ -46,7 +46,7 @@ public class UpdateBasicInfoRequest implements Serializable {
*/
@Schema(description = "昵称")
@NotBlank(message = "昵称不能为空")
@Length(max = 32, message = "昵称长度不能超过 {max} 个字符")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "昵称长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线")
private String nickname;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import org.hibernate.validator.constraints.Length;

import cn.hutool.core.lang.RegexPool;
import top.charles7c.cnadmin.common.constant.RegexConsts;

/**
* 修改邮箱信息
Expand All @@ -46,7 +46,7 @@ public class UpdateEmailRequest implements Serializable {
*/
@Schema(description = "新邮箱")
@NotBlank(message = "新邮箱不能为空")
@Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误")
@Pattern(regexp = RegexConsts.EMAIL, message = "邮箱格式错误")
private String newEmail;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

import java.util.List;

import javax.validation.constraints.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.Data;

import io.swagger.v3.oas.annotations.media.Schema;

import org.hibernate.validator.constraints.Length;

import cn.hutool.core.lang.RegexPool;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.enums.GenderEnum;

Expand All @@ -49,27 +50,29 @@ public class UserRequest extends BaseRequest {
*/
@Schema(description = "用户名")
@NotBlank(message = "用户名不能为空")
@Pattern(regexp = RegexConsts.USERNAME, message = "用户名长度为 4 到 16 位,可以包含字母、数字,下划线,以字母开头")
private String username;

/**
* 昵称
*/
@Schema(description = "昵称")
@Length(max = 32, message = "昵称长度不能超过 {max} 个字符")
@NotBlank(message = "昵称不能为空")
@Pattern(regexp = RegexConsts.GENERAL_NAME, message = "昵称长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线")
private String nickname;

/**
* 邮箱
*/
@Schema(description = "邮箱")
@Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误")
@Pattern(regexp = RegexConsts.EMAIL, message = "邮箱格式错误")
private String email;

/**
* 手机号码
*/
@Schema(description = "手机号码")
@Pattern(regexp = RegexPool.MOBILE, message = "手机号码格式错误")
@Pattern(regexp = RegexConsts.MOBILE, message = "手机号码格式错误")
private String phone;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import top.charles7c.cnadmin.common.base.BaseService;
import top.charles7c.cnadmin.system.model.entity.UserDO;
import top.charles7c.cnadmin.system.model.query.UserQuery;
import top.charles7c.cnadmin.system.model.request.UpdateBasicInfoRequest;
import top.charles7c.cnadmin.system.model.request.UpdateUserRoleRequest;
import top.charles7c.cnadmin.system.model.request.UserRequest;
import top.charles7c.cnadmin.system.model.vo.UserDetailVO;
Expand All @@ -47,6 +48,16 @@ public interface UserService extends BaseService<UserVO, UserDetailVO, UserQuery
*/
String uploadAvatar(MultipartFile avatar, Long id);

/**
* 修改基础信息
*
* @param request
* 修改信息
* @param id
* ID
*/
void updateBasicInfo(UpdateBasicInfoRequest request, Long id);

/**
* 修改密码
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@
import top.charles7c.cnadmin.system.mapper.UserMapper;
import top.charles7c.cnadmin.system.model.entity.UserDO;
import top.charles7c.cnadmin.system.model.query.UserQuery;
import top.charles7c.cnadmin.system.model.request.UpdateBasicInfoRequest;
import top.charles7c.cnadmin.system.model.request.UpdateUserRoleRequest;
import top.charles7c.cnadmin.system.model.request.UserRequest;
import top.charles7c.cnadmin.system.model.vo.UserDetailVO;
import top.charles7c.cnadmin.system.model.vo.UserVO;
import top.charles7c.cnadmin.system.service.*;
import top.charles7c.cnadmin.system.service.DeptService;
import top.charles7c.cnadmin.system.service.RoleService;
import top.charles7c.cnadmin.system.service.UserRoleService;
import top.charles7c.cnadmin.system.service.UserService;

/**
* 用户业务实现类
Expand Down Expand Up @@ -181,6 +185,13 @@ public String uploadAvatar(MultipartFile avatarFile, Long id) {
return newAvatar;
}

@Override
public void updateBasicInfo(UpdateBasicInfoRequest request, Long id) {
super.getById(id);
baseMapper.lambdaUpdate().set(UserDO::getNickname, request.getNickname())
.set(UserDO::getGender, request.getGender()).eq(UserDO::getId, id).update();
}

@Override
@Transactional(rollbackFor = Exception.class)
public void updatePassword(String oldPassword, String newPassword, Long id) {
Expand Down
15 changes: 14 additions & 1 deletion continew-admin-ui/src/views/system/role/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,20 @@
form: {} as RoleRecord,
// 表单验证规则
rules: {
name: [{ required: true, message: '请输入角色名称' }],
name: [
{ required: true, message: '请输入角色名称' },
{
match: /^[\u4e00-\u9fa5a-zA-Z0-9_-]{1,20}$/,
message: '长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线'
},
],
code: [
{ required: true, message: '请输入角色编码' },
{
match: /^[a-zA-Z][a-zA-Z0-9_]{1,15}$/,
message: '长度为 2 到 16 位,可以包含字母、数字,下划线,以字母开头'
},
],
dataScope: [{ required: true, message: '请选择数据权限' }],
sort: [{ required: true, message: '请输入角色排序' }],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<a-input
v-model="form.username"
:placeholder="$t('userCenter.basicInfo.form.placeholder.username')"
max-length="50"
max-length="16"
/>
</a-form-item>
<a-form-item :label="$t('userCenter.basicInfo.form.label.nickname')" field="nickname">
<a-input
v-model="form.nickname"
:placeholder="$t('userCenter.basicInfo.form.placeholder.nickname')"
max-length="32"
max-length="20"
/>
</a-form-item>
<a-form-item :label="$t('userCenter.basicInfo.form.label.gender')" field="gender">
Expand Down Expand Up @@ -70,12 +70,20 @@
required: true,
message: t('userCenter.basicInfo.form.error.required.username'),
},
{
match: /^[a-zA-Z][a-zA-Z0-9_]{3,15}$/,
message: t('userCenter.basicInfo.form.error.match.username'),
},
],
nickname: [
{
required: true,
message: t('userCenter.basicInfo.form.error.required.nickname'),
},
{
match: /^[\u4e00-\u9fa5a-zA-Z0-9_-]{1,20}$/,
message: t('userCenter.basicInfo.form.error.match.nickname'),
},
],
};
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default {
'userCenter.basicInfo.form.placeholder.nickname': 'Please enter nickname',

'userCenter.basicInfo.form.error.required.username': 'Please enter username',
'userCenter.basicInfo.form.error.match.username': 'Username are 4 to 16 characters long and can contain letters, numbers, underscores, and start with a letter',
'userCenter.basicInfo.form.error.required.nickname': 'Please enter nickname',
'userCenter.basicInfo.form.error.match.nickname': 'Nickname are 1 to 20 digits long and can contain Chinese, letters, numbers, underscores, dashes',

'userCenter.basicInfo.form.save': 'Save',
'userCenter.basicInfo.form.save.success': 'Save success',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default {
'userCenter.basicInfo.form.placeholder.nickname': '请输入昵称',

'userCenter.basicInfo.form.error.required.username': '请输入用户名',
'userCenter.basicInfo.form.error.match.username': '长度为 4 到 16 位,可以包含字母、数字,下划线,以字母开头',
'userCenter.basicInfo.form.error.required.nickname': '请输入昵称',
'userCenter.basicInfo.form.error.match.nickname': '长度为 1 到 20 位,可以包含中文、字母、数字、下划线,短横线',

'userCenter.basicInfo.form.save': '保存',
'userCenter.basicInfo.form.save.success': '保存成功',
Expand All @@ -44,12 +46,12 @@ export default {
'userCenter.securitySettings.updatePwd.form.label.rePassword': '确认新密码',

'userCenter.securitySettings.updatePwd.form.placeholder.oldPassword': '请输入当前密码',
'userCenter.securitySettings.updatePwd.form.placeholder.newPassword': '密码长度 6 到 32 位,同时包含数字和字母',
'userCenter.securitySettings.updatePwd.form.placeholder.newPassword': '长度为 6 到 32 位,同时包含字母和数字',
'userCenter.securitySettings.updatePwd.form.placeholder.rePassword': '请再次输入新密码',

'userCenter.securitySettings.updatePwd.form.error.required.oldPassword': '请输入当前密码',
'userCenter.securitySettings.updatePwd.form.error.required.newPassword': '请输入新密码',
'userCenter.securitySettings.updatePwd.form.error.match.newPassword': '密码长度 6 到 32 位,同时包含数字和字母',
'userCenter.securitySettings.updatePwd.form.error.match.newPassword': '长度为 6 到 32 位,同时包含字母和数字',
'userCenter.securitySettings.updatePwd.form.error.validator.newPassword': '新密码不能与当前密码相同',
'userCenter.securitySettings.updatePwd.form.error.required.rePassword': '请再次输入新密码',
'userCenter.securitySettings.updatePwd.form.error.validator.rePassword': '两次输入的密码不一致',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;

import top.charles7c.cnadmin.common.config.properties.CaptchaProperties;
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
import top.charles7c.cnadmin.common.constant.CacheConsts;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.model.vo.CaptchaVO;
import top.charles7c.cnadmin.common.model.vo.R;
import top.charles7c.cnadmin.common.util.*;
import top.charles7c.cnadmin.common.util.MailUtils;
import top.charles7c.cnadmin.common.util.RedisUtils;
import top.charles7c.cnadmin.common.util.TemplateUtils;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;

/**
Expand Down Expand Up @@ -86,7 +88,7 @@ public R<CaptchaVO> getImageCaptcha() {
@Operation(summary = "获取邮箱验证码", description = "发送验证码到指定邮箱")
@GetMapping("/mail")
public R getMailCaptcha(
@NotBlank(message = "邮箱不能为空") @Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误") String email)
@NotBlank(message = "邮箱不能为空") @Pattern(regexp = RegexConsts.EMAIL, message = "邮箱格式错误") String email)
throws MessagingException {
String limitKeyPrefix = CacheConsts.LIMIT_KEY_PREFIX;
String captchaKeyPrefix = CacheConsts.CAPTCHA_KEY_PREFIX;
Expand Down
Loading

0 comments on commit 8b955a0

Please sign in to comment.