OCT 1. 增加分页获取所有用户信息接口

This commit is contained in:
黄昕 2024-02-02 15:54:13 +08:00
parent 39cfe6d46d
commit a8fee399f3
6 changed files with 85 additions and 13 deletions

View File

@ -0,0 +1,14 @@
package com.cmhi.cf.authentication.pojo.vo;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"operationLog", "status", "message"})
public class UserListPagedResp <T> extends BaseRespStatus {
}

View File

@ -10,8 +10,11 @@ import com.cmhi.cf.misc.HelperUtils;
import com.cmhi.cf.restapi.annotation.DecryptionProtocol;
import com.cmhi.cf.restapi.annotation.EncryptionProtocol;
import com.cmhi.cf.restapi.annotation.OperationLogAnnotation;
import com.cmhi.cf.restapi.pojo.base.BasePageResultResp;
import com.cmhi.cf.restapi.pojo.base.BasePagedReq;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.dto.ProtocolReq;
import com.cmhi.cf.restapi.pojo.po.PageResults;
import com.cmhi.cf.restapi.pojo.vo.ProtocolResp;
import com.cmhi.cf.validation.group.ValidGroups;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -42,8 +45,7 @@ public class UserManagerApi {
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "用户管理模块", OperationType = "读取", OperationDesc = "获取用户信息")
public ProtocolResp<? extends BaseRespStatus> getUserInfoById(@RequestBody ProtocolReq<UserIdReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class,
ValidGroups.OperationLogReqValid.class);
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.OperationLogReqValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
UserInfo ui = userService.getUserInfoByUid(mr.getMsgContent().getUid());
@ -58,12 +60,33 @@ public class UserManagerApi {
}
}
@PostMapping("/userList")
@ResponseBody
@EncryptionProtocol
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "用户管理模块", OperationType = "读取", OperationDesc = "获取用户列表")
public ProtocolResp<? extends BaseRespStatus> getAllUserInfoPaged(@RequestBody ProtocolReq<BasePagedReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.BasePagedReqValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
BasePagedReq req = mr.getMsgContent();
PageResults<UserInfo> ui = userService.getAllUserInfo(req.getPageNumber(), req.getPageSize(), req.getTotalSize());
return ProtocolResp.result(BasePageResultResp.<UserInfo>builder()
.items(ui)
.status(ErrorCode.ERR_OK.getCode())
.message(new String[] {ErrorCode.ERR_OK.getDescription()})
.build());
} else {
return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
validate.toArray(new String[0]));
}
}
@GetMapping("/userInfo")
@ResponseBody
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "用户管理模块",
OperationType = "读取",
OperationDesc = "获取当前用户信息")
@OperationLogAnnotation(OperationModule = "用户管理模块", OperationType = "读取", OperationDesc = "获取当前用户信息")
public ProtocolResp<? extends BaseRespStatus> getCurrentUserInfo() {
return ProtocolResp.result(GetUserInfoResp.builder()
.userInfo(userService.getCurrentUserInfo())
@ -78,8 +101,7 @@ public class UserManagerApi {
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "用户管理模块", OperationType = "创建", OperationDesc = "创建新用户")
public ProtocolResp<? extends BaseRespStatus> createNewUser(@RequestBody ProtocolReq<RegisterUserReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class,
ValidGroups.UserReqValid.class);
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.UserReqValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
RegisterUserReq ui = mr.getMsgContent();
@ -106,8 +128,7 @@ public class UserManagerApi {
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "用户管理模块", OperationType = "删除", OperationDesc = "删除用户")
public ProtocolResp<? extends BaseRespStatus> removeUser(@RequestBody ProtocolReq<UserIdReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class,
ValidGroups.UserIdReqValid.class);
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.UserIdReqValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
UserIdReq ui = mr.getMsgContent();

View File

@ -6,6 +6,7 @@ import com.cmhi.cf.authentication.pojo.po.ResourceInfo;
import com.cmhi.cf.authentication.pojo.po.UserInfo;
import com.cmhi.cf.common.ErrorCode;
import com.cmhi.cf.database.authentication.entity.User;
import com.cmhi.cf.restapi.pojo.po.PageResults;
import com.mybatisflex.core.service.IService;
import java.util.List;
@ -31,6 +32,8 @@ public interface UserService extends IService<User> {
UserInfo getCurrentUserInfo();
PageResults<UserInfo> getAllUserInfo(Long pageNumber, Long pageSize, Long totalSize);
List<ResPermInfo> getCurrentUserResourcePerm();
List<ResPermInfo> getUserResourcePerm(Long userId);

View File

@ -21,6 +21,8 @@ import com.cmhi.cf.database.authentication.service.UserService;
import com.cmhi.cf.exception.CommonErrorCodeException;
import com.cmhi.cf.misc.HelperUtils;
import com.cmhi.cf.restapi.misc.ApiContextUtils;
import com.cmhi.cf.restapi.pojo.po.PageResults;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.spring.service.impl.ServiceImpl;
@ -102,7 +104,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
}
// 低优先级用户不能获取高优先级用户信息
if (verifyCurrentHasPermission(user)) {
if (verifyCurrentExceedPermission(user)) {
throw new CommonErrorCodeException(ErrorCode.ERR_PERMISSION);
}
@ -122,7 +124,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
}
// 低优先级用户不能获取高优先级用户信息
if (verifyCurrentHasPermission(user)) {
if (verifyCurrentExceedPermission(user)) {
throw new CommonErrorCodeException(ErrorCode.ERR_PERMISSION);
}
@ -158,6 +160,33 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
user.getUsername()).roleId(user.getRoleId()).userStatus(user.getUserCredential().getState()).build();
}
@Override
public PageResults<UserInfo> getAllUserInfo(Long pageNumber, Long pageSize, Long totalSize) {
QueryWrapper wrapper = QueryWrapper.create().from(USER).select();
List<UserInfo> ui = new ArrayList<>();
Page<User> user = userMapper.paginateWithRelations(pageNumber, pageSize, totalSize, wrapper);
user.getRecords().forEach(k -> {
ui.add(UserInfo.builder()
.uid(k.getUid())
.createdTime(k.getCreatedTime())
.gender(k.getGender())
.username(k.getUsername())
.roleId(k.getRoleId())
.userStatus(k.getUserCredential().getState())
.build());
});
return PageResults.<UserInfo>builder()
.items(ui)
.pageNumber(user.getPageNumber())
.pageSize(user.getPageSize())
.totalPage(user.getTotalPage())
.totalRow(user.getTotalRow())
.build();
}
@Override
public List<ResPermInfo> getCurrentUserResourcePerm() {
List<ResourceData> res;
@ -208,7 +237,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
}
// 低优先级用户不能获取高优先级用户信息
if (verifyCurrentHasPermission(user)) {
if (verifyCurrentExceedPermission(user)) {
throw new CommonErrorCodeException(ErrorCode.ERR_PERMISSION);
}
@ -396,7 +425,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
});
}
private boolean verifyCurrentHasPermission(User accessUser) {
private boolean verifyCurrentExceedPermission(User accessUser) {
String loginUsername = ApiContextUtils.get().getUsername();
if (!HelperUtils.stringNotEmptyOrNull(loginUsername)) {

View File

@ -8,6 +8,9 @@ public interface ValidGroups {
interface BaseProtocolValid extends ProtocolCommonValid {
}
interface BasePagedReqValid extends BaseProtocolValid {
}
interface LoginReqValid extends BaseProtocolValid {
}

View File

@ -37,6 +37,7 @@ INSERT IGNORE INTO rbac_resource_data VALUES (300, '58e36be5-f725-4d98-bd7d-8f73
INSERT IGNORE INTO rbac_resource_data VALUES (301, 'fa2b3740-8672-49d3-9792-25976b767ade', '/api/user/userInfo', '获取用户信息', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (302, '221c8d8e-c47a-4200-88a8-34d374a76d01', '/api/user/register', '注册新用户', 'PUT', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (303, '8adf6875-2be4-459e-9c1d-3bb87114cb08', '/api/user/remove', '删除用户', 'DELETE', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (304, '4c6c859b-2625-45eb-83b8-1426ff96dbe6', '/api/user/userList', '分页获取当前所有用户', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (400, '8ac928d8-5a6a-47c7-8697-0fb9ebd7249d', '/api/dict/allDictionary', '获取当前字典列表', 'GET', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (401, '9ecf1662-f891-4829-8d3e-81c42bf22e2a', '/api/dict/allDictionaryContent', '获取当前字典详细内容', 'GET', 0);
-- ----------------------------
@ -56,6 +57,7 @@ INSERT IGNORE INTO rbac_role_resource VALUES (1, 300, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 301, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 302, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 303, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 304, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 400, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 401, 1);