Role修改以及日志模块

This commit is contained in:
筱锋xiao_lfeng 2024-01-19 14:27:10 +08:00
parent 762d9ebe66
commit 9ffdf3d33c
No known key found for this signature in database
GPG Key ID: F693AA12AABBFA87
10 changed files with 115 additions and 24 deletions

View File

@ -1,15 +1,20 @@
package com.jsl.oa.controllers; package com.jsl.oa.controllers;
import com.jsl.oa.model.voData.RoleEditVO;
import com.jsl.oa.services.RoleService; import com.jsl.oa.services.RoleService;
import com.jsl.oa.utils.BaseResponse; import com.jsl.oa.utils.BaseResponse;
import com.jsl.oa.utils.ErrorCode; import com.jsl.oa.utils.ErrorCode;
import com.jsl.oa.utils.Processing;
import com.jsl.oa.utils.ResultUtil; import com.jsl.oa.utils.ResultUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable; import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
/** /**
* <h1>角色控制器</h1> * <h1>角色控制器</h1>
@ -20,6 +25,7 @@ import javax.validation.constraints.NotNull;
* @see RoleService * @see RoleService
* @since v1.1.0 * @since v1.1.0
*/ */
@Slf4j
@RestController @RestController
@RequiredArgsConstructor @RequiredArgsConstructor
public class RoleController { public class RoleController {
@ -35,9 +41,30 @@ public class RoleController {
*/ */
@GetMapping("/role/get") @GetMapping("/role/get")
public BaseResponse roleGet(HttpServletRequest request, @RequestParam @Nullable String id) { public BaseResponse roleGet(HttpServletRequest request, @RequestParam @Nullable String id) {
log.info("请求接口[GET]: /role/get");
return roleService.roleGet(request, id); return roleService.roleGet(request, id);
} }
/**
* <h2>角色编辑</h2>
* <hr/>
* 角色编辑接口
*
* @param request 请求
* @param roleEditVO 角色编辑VO
* @param bindingResult 参数校验结果
* @return {@link BaseResponse}
*/
@PutMapping("/role/edit")
public BaseResponse roleEdit(HttpServletRequest request, @RequestBody @Validated RoleEditVO roleEditVO, @NotNull BindingResult bindingResult) {
log.info("请求接口[PUT]: /role/edit");
// 判断是否有参数错误
if (bindingResult.hasErrors()) {
return ResultUtil.error(ErrorCode.REQUEST_BODY_ERROR, Processing.getValidatedErrorList(bindingResult));
}
return roleService.roleEdit(request, roleEditVO);
}
/** /**
* 用户权限授予 * 用户权限授予
* *
@ -45,11 +72,12 @@ public class RoleController {
*/ */
@PostMapping("role/user/add") @PostMapping("role/user/add")
public BaseResponse roleAddUser(HttpServletRequest request, @RequestParam Long uid, @RequestParam Long rid) { public BaseResponse roleAddUser(HttpServletRequest request, @RequestParam Long uid, @RequestParam Long rid) {
log.info("请求接口[POST]: /role/user/add");
// 判断是否有参数错误 // 判断是否有参数错误
if (uid == null || rid == null) { if (uid == null || rid == null) {
return ResultUtil.error(ErrorCode.PARAMETER_ERROR); return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
} }
return roleService.roleAddUser(request,uid, rid); return roleService.roleAddUser(request, uid, rid);
} }
/** /**
@ -58,11 +86,12 @@ public class RoleController {
* @return * @return
*/ */
@DeleteMapping("role/user/remove") @DeleteMapping("role/user/remove")
public BaseResponse roleRemoveUser(HttpServletRequest request,@RequestParam Long uid) { public BaseResponse roleRemoveUser(HttpServletRequest request, @RequestParam Long uid) {
log.info("请求接口[POST]: /role/user/remove");
// 判断是否有参数错误 // 判断是否有参数错误
if (uid == null) { if (uid == null) {
return ResultUtil.error(ErrorCode.PARAMETER_ERROR); return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
} }
return roleService.roleRemoveUser(request,uid); return roleService.roleRemoveUser(request, uid);
} }
} }

View File

@ -21,13 +21,21 @@ public class RoleDAO {
roleMapper.roleRemoveUser(uid); roleMapper.roleRemoveUser(uid);
} }
public List<RoleDO> getRoleById(String id) { public List<RoleDO> getRolesById(String id) {
ArrayList<RoleDO> getRoleList = new ArrayList<>(); ArrayList<RoleDO> getRoleList = new ArrayList<>();
getRoleList.add(roleMapper.getRoleById(Long.valueOf(id))); getRoleList.add(roleMapper.getRoleById(Long.valueOf(id)));
return getRoleList; return getRoleList;
} }
public RoleDO getRoleById(Long id) {
return roleMapper.getRoleById(id);
}
public List<RoleDO> getRole() { public List<RoleDO> getRole() {
return roleMapper.getRole(); return roleMapper.getRole();
} }
public boolean roleEdit(RoleDO getRole) {
return roleMapper.roleEdit(getRole);
}
} }

View File

@ -17,19 +17,19 @@ public class ProcessException {
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public ResponseEntity<BaseResponse> businessMethodNotAllowedException() { public ResponseEntity<BaseResponse> businessMethodNotAllowedException() {
log.debug("请求方法错误"); log.warn("请求方法错误");
return ResultUtil.error("MethodNotAllowed", 405, "请求方法错误"); return ResultUtil.error("MethodNotAllowed", 405, "请求方法错误");
} }
@ExceptionHandler(value = DuplicateKeyException.class) @ExceptionHandler(value = DuplicateKeyException.class)
public ResponseEntity<BaseResponse> businessDuplicateKeyException(@NotNull DuplicateKeyException e) { public ResponseEntity<BaseResponse> businessDuplicateKeyException(@NotNull DuplicateKeyException e) {
log.debug(e.getMessage(), e); log.warn(e.getMessage(), e);
return ResultUtil.error("DuplicateEntry", 400, "数据重复/外键约束"); return ResultUtil.error("DuplicateEntry", 400, "数据重复/外键约束");
} }
@ExceptionHandler(value = HttpMessageNotReadableException.class) @ExceptionHandler(value = HttpMessageNotReadableException.class)
public ResponseEntity<BaseResponse> businessHttpMessageNotReadableException(HttpMessageNotReadableException e) { public ResponseEntity<BaseResponse> businessHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.debug(e.getMessage(), e); log.warn(e.getMessage(), e);
return ResultUtil.error("HttpMessageNotReadable", 400, "请求参数错误"); return ResultUtil.error("HttpMessageNotReadable", 400, "请求参数错误");
} }

View File

@ -2,10 +2,7 @@ package com.jsl.oa.mapper;
import com.jsl.oa.model.doData.RoleDO; import com.jsl.oa.model.doData.RoleDO;
import com.jsl.oa.model.doData.RoleUserDO; import com.jsl.oa.model.doData.RoleUserDO;
import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List; import java.util.List;
@ -13,7 +10,7 @@ import java.util.List;
public interface RoleMapper { public interface RoleMapper {
@Insert("insert into organize_oa.oa_role_user (uid, rid) VALUE (#{uid},#{rid})") @Insert("insert into organize_oa.oa_role_user (uid, rid) VALUE (#{uid},#{rid})")
void roleAddUser(Long uid,Long rid); void roleAddUser(Long uid, Long rid);
@Delete("delete from organize_oa.oa_role_user where uid=#{uid}") @Delete("delete from organize_oa.oa_role_user where uid=#{uid}")
void roleRemoveUser(Long uid); void roleRemoveUser(Long uid);
@ -29,4 +26,7 @@ public interface RoleMapper {
@Select("SELECT * FROM organize_oa.oa_role ORDER BY id DESC") @Select("SELECT * FROM organize_oa.oa_role ORDER BY id DESC")
List<RoleDO> getRole(); List<RoleDO> getRole();
@Update("UPDATE organize_oa.oa_role SET role_name=#{roleName},display_name=#{displayName} WHERE id=#{id}")
boolean roleEdit(RoleDO getRole);
} }

View File

@ -19,6 +19,7 @@ import java.sql.Timestamp;
public class RoleDO { public class RoleDO {
private Long id; private Long id;
private String roleName; private String roleName;
private String displayName;
private Timestamp createdAt; private Timestamp createdAt;
private Timestamp updatedAt; private Timestamp updatedAt;
} }

View File

@ -0,0 +1,25 @@
package com.jsl.oa.model.voData;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
/**
* <h1>角色编辑VO</h1>
* <hr/>
* 角色编辑VO用于接收角色编辑请求
*
* @version v1.1.0
* @author 筱锋xiao_lfeng
* @since v1.1.0
*/
@Data
public class RoleEditVO {
private Long id;
@NotBlank
@Pattern(regexp = "^[a-zA-Z0-9_]{3,20}$", message = "角色名只能为3-16位的字母、数字、下划线组成")
private String name;
@NotBlank
private String displayName;
}

View File

@ -1,5 +1,6 @@
package com.jsl.oa.services; package com.jsl.oa.services;
import com.jsl.oa.model.voData.RoleEditVO;
import com.jsl.oa.utils.BaseResponse; import com.jsl.oa.utils.BaseResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -18,4 +19,6 @@ public interface RoleService {
BaseResponse roleRemoveUser(HttpServletRequest request,Long uid); BaseResponse roleRemoveUser(HttpServletRequest request,Long uid);
BaseResponse roleGet(HttpServletRequest request, String id); BaseResponse roleGet(HttpServletRequest request, String id);
BaseResponse roleEdit(HttpServletRequest request, RoleEditVO roleEditVO);
} }

View File

@ -1,8 +1,8 @@
package com.jsl.oa.services.impl; package com.jsl.oa.services.impl;
import com.jsl.oa.dao.RoleDAO; import com.jsl.oa.dao.RoleDAO;
import com.jsl.oa.dao.UserDAO;
import com.jsl.oa.model.doData.RoleDO; import com.jsl.oa.model.doData.RoleDO;
import com.jsl.oa.model.voData.RoleEditVO;
import com.jsl.oa.services.RoleService; import com.jsl.oa.services.RoleService;
import com.jsl.oa.utils.BaseResponse; import com.jsl.oa.utils.BaseResponse;
import com.jsl.oa.utils.ErrorCode; import com.jsl.oa.utils.ErrorCode;
@ -47,7 +47,7 @@ public class RoleServiceImpl implements RoleService {
ArrayList<RoleDO> getRoleList; ArrayList<RoleDO> getRoleList;
if (id != null && !id.isEmpty()) { if (id != null && !id.isEmpty()) {
if (Pattern.matches("^[0-9]+$", id)) { if (Pattern.matches("^[0-9]+$", id)) {
getRoleList = (ArrayList<RoleDO>) roleDAO.getRoleById(id); getRoleList = (ArrayList<RoleDO>) roleDAO.getRolesById(id);
} else { } else {
ArrayList<String> error = new ArrayList<>(); ArrayList<String> error = new ArrayList<>();
error.add("id 只能为数字"); error.add("id 只能为数字");
@ -60,4 +60,28 @@ public class RoleServiceImpl implements RoleService {
// 返回数据 // 返回数据
return ResultUtil.success(getRoleList); return ResultUtil.success(getRoleList);
} }
@Override
public BaseResponse roleEdit(HttpServletRequest request, RoleEditVO roleEditVO) {
// 检查用户权限
if (!Processing.checkUserIsAdmin(request, roleDAO.roleMapper)) {
return ResultUtil.error(ErrorCode.NOT_ADMIN);
}
// 获取 Role 相关信息
RoleDO getRole = roleDAO.getRoleById(roleEditVO.getId());
// 判断是否存在该 Role
if (getRole != null) {
// 替换 Role 信息
getRole.setRoleName(roleEditVO.getName())
.setDisplayName(roleEditVO.getDisplayName());
// 更新 Role 信息
if (roleDAO.roleEdit(getRole)) {
return ResultUtil.success();
} else {
return ResultUtil.error(ErrorCode.DATABASE_UPDATE_ERROR);
}
} else {
return ResultUtil.error(ErrorCode.ROLE_NOT_FOUNDED);
}
}
} }

View File

@ -21,6 +21,7 @@ public enum ErrorCode {
NOT_ADMIN("NotAdmin", 40300, "不是管理员"), NOT_ADMIN("NotAdmin", 40300, "不是管理员"),
EMAIL_LOGIN_NOT_SUPPORT("EmailLoginNotSupport", 40300, "请使用邮箱登陆"), EMAIL_LOGIN_NOT_SUPPORT("EmailLoginNotSupport", 40300, "请使用邮箱登陆"),
PASSWORD_NOT_SAME("PasswordNotSame", 40301, "两次密码不一致"), PASSWORD_NOT_SAME("PasswordNotSame", 40301, "两次密码不一致"),
ROLE_NOT_FOUNDED("RoleNotFounded", 40400, "角色不存在"),
DATABASE_INSERT_ERROR("DatabaseInsertError", 50010, "数据库插入错误"), DATABASE_INSERT_ERROR("DatabaseInsertError", 50010, "数据库插入错误"),
DATABASE_UPDATE_ERROR("DatabaseUpdateError", 50011, "数据库更新错误"), DATABASE_UPDATE_ERROR("DatabaseUpdateError", 50011, "数据库更新错误"),
DATABASE_DELETE_ERROR("DatabaseDeleteError", 50012, "数据库删除错误"), DATABASE_DELETE_ERROR("DatabaseDeleteError", 50012, "数据库删除错误"),

View File

@ -10,48 +10,48 @@ public class ResultUtil {
@Contract(" -> new") @Contract(" -> new")
public static @NotNull BaseResponse success() { public static @NotNull BaseResponse success() {
log.debug("请求接口成功"); log.info("请求接口成功[200] 不含数据");
return new BaseResponse("Success", 200, "操作成功", null); return new BaseResponse("Success", 200, "操作成功", null);
} }
@Contract("_ -> new") @Contract("_ -> new")
public static @NotNull BaseResponse success(String message) { public static @NotNull BaseResponse success(String message) {
log.debug("请求接口成功"); log.info(message + "[200]");
return new BaseResponse("Success", 200, message, null); return new BaseResponse("Success", 200, message, null);
} }
@Contract(value = "_ -> new", pure = true) @Contract(value = "_ -> new", pure = true)
public static @NotNull BaseResponse success(Object data) { public static @NotNull BaseResponse success(Object data) {
log.debug("请求接口成功"); log.info("请求接口成功[200] 带数据");
return new BaseResponse("Success", 200, "操作成功", data); return new BaseResponse("Success", 200, "操作成功", data);
} }
@Contract(value = "_, _ -> new", pure = true) @Contract(value = "_, _ -> new", pure = true)
public static @NotNull BaseResponse success(String message, Object data) { public static @NotNull BaseResponse success(String message, Object data) {
log.debug("请求接口成功"); log.info(message + "[200] 带数据");
return new BaseResponse("Success", 200, message, data); return new BaseResponse("Success", 200, message, data);
} }
@Contract("_ -> new") @Contract("_ -> new")
public static @NotNull BaseResponse error(@NotNull ErrorCode errorCode) { public static @NotNull BaseResponse error(@NotNull ErrorCode errorCode) {
log.debug("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage()); log.info("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage());
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage()); return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage());
} }
@Contract("_, _ -> new") @Contract("_, _ -> new")
public static @NotNull BaseResponse error(@NotNull ErrorCode errorCode, Object data) { public static @NotNull BaseResponse error(@NotNull ErrorCode errorCode, Object data) {
log.debug("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage()); log.info("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage());
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), data); return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), data);
} }
@Contract(value = "_, _, _, _ -> new", pure = true) @Contract(value = "_, _, _, _ -> new", pure = true)
public static @NotNull BaseResponse error(String output, Integer code, String message, Object data) { public static @NotNull BaseResponse error(String output, Integer code, String message, Object data) {
log.debug("请求接口错误[" + code + "] " + message); log.info("请求接口错误[" + code + "] " + message);
return new BaseResponse(output, code, message, data); return new BaseResponse(output, code, message, data);
} }
public static @NotNull ResponseEntity<BaseResponse> error(String output, Integer code, String message) { public static @NotNull ResponseEntity<BaseResponse> error(String output, Integer code, String message) {
log.debug("请求接口错误[" + code + "] " + message); log.info("请求接口错误[" + code + "] " + message);
return ResponseEntity.status(code) return ResponseEntity.status(code)
.body(new BaseResponse(output, code, message)); .body(new BaseResponse(output, code, message));
} }