Skip to content

Commit

Permalink
新增角色关联
Browse files Browse the repository at this point in the history
  • Loading branch information
lxy01128250 committed Dec 15, 2023
1 parent 1c9ed85 commit 5ba883b
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 72 deletions.
15 changes: 6 additions & 9 deletions src/main/java/com/beauty/aide/aop/AdminInterceptor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.beauty.aide.aop;

import com.beauty.aide.common.errors.ErrorCode;
import com.beauty.aide.constant.UserConstant;
import com.beauty.aide.exception.BusinessException;
import com.beauty.aide.model.vo.AccountVO;
import com.beauty.aide.common.model.vo.AccountVO;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
Expand All @@ -19,9 +17,9 @@
@Component
public class AdminInterceptor implements HandlerInterceptor {


private static final List<String> NO_LOGIN_PATHS = Arrays.asList(
"/api/account/**"
"/api/account/register",
"/api/account/login"
);

public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
Expand All @@ -38,10 +36,9 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}
}
// return false;
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");

// throw new BusinessException(CommonErrorCode.NOT_PERMISSION.getMessage());
throw new IllegalStateException("111");
}else {
// @TODO 已登录,查看是否拥有接口权限
}
return true;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/beauty/aide/common/enums/AccountStatusEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.beauty.aide.common.enums;

/**
* @author xiaoliu
*/
public enum AccountStatusEnum {
ACTIVE("已激活"),
DISABLED("已禁用"),
;

private String desc;

AccountStatusEnum(String desc) {
this.desc = desc;
}

public String getDesc() {
return desc;
}

public static String getByName(String name) {
for (AccountStatusEnum e : AccountStatusEnum.values()) {
if (e.name().equals(name)) {
return e.getDesc();
}
}
return null;
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/beauty/aide/common/enums/UserRoleEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.beauty.aide.common.enums;

public enum UserRoleEnum {
SUPER_ADMIN(1L, "超级管理员"),
ADMIN(2L, "管理员"),
COMMON_USER(3L, "普通用户"),
;

private String desc;
private Long code;

UserRoleEnum(long code, String desc) {
this.code = code;
this.desc = desc;
}

public static String getByCode(Long code) {
for (UserRoleEnum e : UserRoleEnum.values()) {
if (e.getCode().equals(code)) {
return e.getDesc();
}
}
return null;
}

public String getDesc() {
return desc;
}

public Long getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum UserErrorCode implements IErrorCode {
PASSWORD_ERROR_TOO_MANY("密码错误次数过多,请2小时后再试"),

PHONE_NUM_USED("手机号已被占用"),
ACCOUNT_SUSPENDED("账号已被封禁"),
;
/**
* 错误信息
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.beauty.aide.model.entity;
package com.beauty.aide.common.model.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -12,6 +12,7 @@
@AllArgsConstructor
public class AccountDO extends BaseDO {

private Long roleId;
private String account;
private String password;
private String headImg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.beauty.aide.model.entity;
package com.beauty.aide.common.model.entity;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.beauty.aide.model.so;
package com.beauty.aide.common.model.so;

import lombok.Data;

Expand All @@ -10,4 +10,5 @@ public class AccountSO extends BaseSO{
private static final long serialVersionUID = -4243742749263159391L;
private String nickName;
private String account;
private Long roleId;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.beauty.aide.model.so;
package com.beauty.aide.common.model.so;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.beauty.aide.model.so;
package com.beauty.aide.common.model.so;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.beauty.aide.model.vo;
package com.beauty.aide.common.model.vo;

import lombok.Data;

import java.util.List;

/**
* @author xiaoliu
*/
Expand All @@ -12,5 +14,8 @@ public class AccountVO {
private String account;
private String headImg;
private String nickName;
private Long roleId;
private String roleName;
private String statusDesc;

}
29 changes: 12 additions & 17 deletions src/main/java/com/beauty/aide/constant/UserConstant.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.beauty.aide.constant;

import com.beauty.aide.common.enums.UserRoleEnum;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 用户常量
*
Expand All @@ -11,22 +18,10 @@ public interface UserConstant {
*/
String USER_LOGIN_STATE = "user_login";

// region 权限

/**
* 默认角色
*/
String DEFAULT_ROLE = "user";

/**
* 管理员角色
*/
String ADMIN_ROLE = "admin";

/**
* 被封号
*/
String BAN_ROLE = "ban";
// Map<String, List<String>> ROLE_PERMISSION = new HashMap<String, List<String>>(){{
// put(UserRoleEnum.SUPER_ADMIN.name(), Arrays.asList("/**"));
// put(UserRoleEnum.ADMIN.name(), Arrays.asList("/**"));
// put(UserRoleEnum.COMMON_USER.name(), Arrays.asList(""));
// }};

// endregion
}
53 changes: 42 additions & 11 deletions src/main/java/com/beauty/aide/controller/AccountController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.beauty.aide.controller;

import com.beauty.aide.common.enums.AccountStatusEnum;
import com.beauty.aide.common.enums.UserRoleEnum;
import com.beauty.aide.common.errors.CommonErrorCode;
import com.beauty.aide.common.errors.UserErrorCode;
import com.beauty.aide.common.result.ResultDO;
import com.beauty.aide.constant.UserConstant;
import com.beauty.aide.manager.AccountManager;
import com.beauty.aide.mapper.AccountDAO;
import com.beauty.aide.model.entity.AccountDO;
import com.beauty.aide.model.vo.AccountVO;
import com.beauty.aide.common.model.entity.AccountDO;
import com.beauty.aide.common.model.vo.AccountVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
Expand All @@ -24,6 +26,8 @@
@Slf4j
public class AccountController {

private static final String DEFAULT_HEADIMG = "https://img.alicdn.com/imgextra/i4/O1CN01zJMPkf1vvDtzGGQKW_!!6000000006234-2-tps-144-144.png";

@Resource
private HttpServletRequest request;

Expand All @@ -41,18 +45,19 @@ public class AccountController {
public ResultDO<Long> register() {
String account = request.getParameter("account");
String password = request.getParameter("password");
String headImg = request.getParameter("headImg");
String nickName = request.getParameter("nickName");

if (StringUtils.isAnyBlank(account,password,headImg,nickName)) {
if (StringUtils.isAnyBlank(account,password,nickName)) {
return ResultDO.errorOf(CommonErrorCode.PARAM_EMPTY);
}

AccountDO accountDO = new AccountDO();
accountDO.setAccount(account);
accountDO.setHeadImg(headImg);
accountDO.setHeadImg(DEFAULT_HEADIMG);
accountDO.setNickName(nickName);
accountDO.setPassword(password);
accountDO.setRoleId(UserRoleEnum.COMMON_USER.getCode());
accountDO.setStatus(AccountStatusEnum.ACTIVE.name());

// 检查重复账号
if (!accountManager.checkRepeatAccount(account)) {
Expand All @@ -63,6 +68,8 @@ public ResultDO<Long> register() {
if (!accountManager.checkAccountParam(accountDO)) {
return ResultDO.errorOf(CommonErrorCode.PARAM_INVALID.getCode(), "请检查注册参数");
}

// 加密密码
String encryptPassword = accountManager.encryptPassword(accountDO.getPassword());
accountDO.setPassword(encryptPassword);
accountDAO.insert(accountDO);
Expand All @@ -85,6 +92,9 @@ public ResultDO<Void> login() {
if (accountDO == null) {
return ResultDO.errorOf(UserErrorCode.USER_NOT_EXIST);
}
if (AccountStatusEnum.DISABLED.name().equals(accountDO.getStatus())) {
return ResultDO.errorOf(UserErrorCode.ACCOUNT_SUSPENDED);
}
String encryptPassword = accountManager.encryptPassword(password);
if (!Objects.equals(accountDO.getPassword(), encryptPassword)) {
return ResultDO.errorOf(UserErrorCode.PASSWORD_ERROR);
Expand All @@ -100,7 +110,7 @@ public ResultDO<Void> login() {
*/
@GetMapping("/getInfo")
public ResultDO<AccountVO> getInfo() {
AccountVO accountVO = (AccountVO) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
AccountVO accountVO = accountManager.getLoginUser(request);
if (accountVO == null) {
return ResultDO.errorOf(UserErrorCode.USER_NOT_EXIST);
}
Expand All @@ -123,7 +133,7 @@ public ResultDO<AccountVO> getInfo() {
*/
@PostMapping("/update")
public ResultDO<Void> update() {
AccountVO accountVO = (AccountVO) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
AccountVO accountVO = accountManager.getLoginUser(request);
String nickName = request.getParameter("nickName");
String headImg = request.getParameter("headImg");
if (accountVO == null) {
Expand All @@ -133,9 +143,16 @@ public ResultDO<Void> update() {
return ResultDO.errorOf(CommonErrorCode.PARAM_EMPTY);
}
AccountDO accountDO = accountManager.vo2Do(accountVO);
accountDO.setNickName(nickName);
accountDO.setHeadImg(headImg);
if (StringUtils.isNotBlank(nickName)) {
accountDO.setNickName(nickName);
}
if (StringUtils.isNotBlank(headImg)) {
accountDO.setHeadImg(headImg);
}
accountDAO.update(accountDO);
// 更新session信息
AccountVO vo = accountManager.do2Vo(accountDO);
request.getSession().setAttribute(UserConstant.USER_LOGIN_STATE, vo);
return ResultDO.succOf();
}

Expand All @@ -145,7 +162,7 @@ public ResultDO<Void> update() {
*/
@PostMapping("/updatePassword")
public ResultDO<Void> updatePassword() {
AccountVO accountVO = (AccountVO) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
AccountVO accountVO = accountManager.getLoginUser(request);
String password = request.getParameter("password");
String newPassword = request.getParameter("newPassword");
if (accountVO == null) {
Expand Down Expand Up @@ -176,6 +193,20 @@ public ResultDO<Void> logout() {
return ResultDO.succOf();
}

// @TODO 注销账户
/**
* 注销
* @return
*/
@PostMapping("/disable")
public ResultDO<Void> disable() {
AccountVO accountVO = accountManager.getLoginUser(request);
if (accountVO == null) {
return ResultDO.errorOf(UserErrorCode.USER_NOT_EXIST);
}
// 移除登录状态
request.getSession().removeAttribute(UserConstant.USER_LOGIN_STATE);
accountDAO.updateStatusById(accountVO.getId(),AccountStatusEnum.DISABLED.name());
return ResultDO.succOf();
}

}
Loading

0 comments on commit 5ba883b

Please sign in to comment.