Skip to content

Commit

Permalink
#2469 用户添加分页展示&添加用户搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjiawei committed Nov 22, 2024
1 parent 188bca3 commit 7a8943d
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 0 deletions.
16 changes: 16 additions & 0 deletions platform-auth-server/api/api_local_user_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ func RetrieveAllUsers(c *gin.Context) {
}
}

func QueryUser(c *gin.Context) {
var param model.QueryUserParam
var err error
var page model.PageInfo
var data []*model.SimpleLocalUserDto
if err = c.ShouldBindJSON(&param); err != nil {
support.ReturnError(c, err)
return
}
if page, data, err = service.UserManagementServiceInstance.QueryUserPage(param); err != nil {
support.ReturnError(c, err)
return
}
support.ReturnPageData(c, page, data)
}

func RetrieveUserByUserId(c *gin.Context) {
userId := c.Param("user-id")
result, err := service.UserManagementServiceInstance.RetireveLocalUserByUserid(userId)
Expand Down
2 changes: 2 additions & 0 deletions platform-auth-server/api/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func init() {
ApiCode: "ModifyLocalUserInfomation"},
&handlerFuncObj{Url: "/v1/users", Method: http.MethodGet, HandlerFunc: RetrieveAllUsers,
ApiCode: "RetrieveAllUsers"},
&handlerFuncObj{Url: "/v1/users/query", Method: http.MethodPost, HandlerFunc: QueryUser,
ApiCode: "QueryUser"},
&handlerFuncObj{Url: "/v1/users/:user-id", Method: http.MethodGet, HandlerFunc: RetrieveUserByUserId,
ApiCode: "RetrieveUserByUserId"},
&handlerFuncObj{Url: "/v1/user-message/:username", Method: http.MethodGet, HandlerFunc: RetrieveUserByUsername,
Expand Down
10 changes: 10 additions & 0 deletions platform-auth-server/api/support/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
"github.com/gin-gonic/gin"
)

func ReturnPageData(c *gin.Context, pageInfo model.PageInfo, contents interface{}) {
if contents == nil {
contents = []string{}
}
obj := model.ResponseWrap{Status: model.ResponseStatusOk, Message: model.ResponseMessageOk, Data: model.ResponsePageData{PageInfo: pageInfo, Contents: contents}}
bodyBytes, _ := json.Marshal(obj)
c.Set("responseBody", string(bodyBytes))
c.JSON(http.StatusOK, obj)
}

func ReturnData(c *gin.Context, data interface{}) {
obj := model.ResponseWrap{Status: model.ResponseStatusOk, Message: model.ResponseMessageOk, Data: data}
bodyBytes, _ := json.Marshal(obj)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SET FOREIGN_KEY_CHECKS = 0;
alter table auth_sys_user_role add index user_role_index(user_id);
SET FOREIGN_KEY_CHECKS = 1;
5 changes: 5 additions & 0 deletions platform-auth-server/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ type PlatSystemVariablesListPageData struct {
PageInfo *PageInfo `json:"pageInfo"` // 分页信息
Contents []*PlatSystemVariables `json:"contents"` // 列表内容
}

type QueryUserParam struct {
Pageable *PageInfo `json:"pageable"` // 分页信息,默认为空
UserName string `json:"userName"`
}
5 changes: 5 additions & 0 deletions platform-auth-server/model/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ type PageInfo struct {
TotalRows int `json:"totalRows"` // 总量,查询时不用传
}

type ResponsePageData struct {
PageInfo PageInfo `json:"pageInfo"`
Contents interface{} `json:"contents"`
}

type QueryRequestParam struct {
Filters []*QueryRequestFilterObj `json:"filters"` // 过滤条件,默认为空
Paging bool `json:"paging"` // 是否分页,默认为false
Expand Down
18 changes: 18 additions & 0 deletions platform-auth-server/service/db/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"strings"

"github.com/WeBankPartners/wecube-platform/platform-auth-server/common/constant"
"github.com/WeBankPartners/wecube-platform/platform-auth-server/model"
Expand Down Expand Up @@ -218,6 +219,23 @@ func (UserRepository) FindAllActiveUsers() ([]*model.SysUserEntity, error) {
return users, nil
}

func (UserRepository) QueryUsers(param model.QueryUserParam) (int, []*model.SysUserEntity, error) {
var users []*model.SysUserEntity
var err error
var count int
if strings.TrimSpace(param.UserName) != "" {
err = Engine.Where("is_deleted = ?", false).And("is_active = ?", true).And("is_blocked = ?", false).And("username like ?", "%"+param.UserName+"%").Limit(param.Pageable.PageSize, param.Pageable.StartIndex).Find(&users)
Engine.SQL("select count(1) from auth_sys_user where is_deleted = false and is_active = true and is_blocked = false and username like ?", "%"+param.UserName+"%").Find(&count)
} else {
err = Engine.Where("is_deleted = ?", false).And("is_active = ?", true).And("is_blocked = ?", false).Limit(param.Pageable.PageSize, param.Pageable.StartIndex).Find(&users)
Engine.SQL("select count(1) from auth_sys_user where is_deleted = false and is_active = true and is_blocked = false ").Find(&count)
}
if err != nil {
return count, nil, err
}
return count, users, nil
}

var UserRoleRsRepositoryInstance UserRoleRsRepository

type UserRoleRsRepository struct {
Expand Down
51 changes: 51 additions & 0 deletions platform-auth-server/service/user_management_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,57 @@ func (UserManagementService) RetrieveAllActiveUsers() ([]*model.SimpleLocalUserD
return result, nil
}

func (UserManagementService) QueryUserPage(param model.QueryUserParam) (page model.PageInfo, data []*model.SimpleLocalUserDto, err error) {
page.PageSize = param.Pageable.PageSize
page.StartIndex = param.Pageable.StartIndex
count, userEntities, err := db.UserRepositoryInstance.QueryUsers(param)
if err != nil {
log.Logger.Error("failed to find all active users", log.Error(err))
return page, nil, err
}
page.TotalRows = count
result := make([]*model.SimpleLocalUserDto, 0)
if len(userEntities) == 0 {
return page, nil, nil
}
for _, user := range userEntities {
userDto := convertToSimpleLocalUserDto(user, "")

userRoles, err := db.UserRoleRsRepositoryInstance.FindAllByUserId(user.Id)
if err != nil {
log.Logger.Error("failed to find all UserRoleRs", log.String("userId", user.Id),
log.Error(err))
return page, nil, err
}
if len(userRoles) > 0 {
for _, userRole := range userRoles {

role := &model.SysRoleEntity{}
found, err := db.Engine.ID(userRole.RoleId).Get(role)
if err != nil {
log.Logger.Error("failed to get role", log.String("roleId", userRole.RoleId),
log.Error(err))
return page, nil, err
}

if found {
roleDto := &model.SimpleLocalRoleDto{
ID: role.Id,
DisplayName: role.DisplayName,
Name: role.Name,
Email: role.EmailAddress,
Status: role.GetRoleDeletedStatus(),
}

userDto.AddRoles([]*model.SimpleLocalRoleDto{roleDto})
}
}
}
result = append(result, userDto)
}
return page, result, nil
}

// @Transactional
func (UserManagementService) UnregisterLocalUser(userId string, curUser string) error {
session := db.Engine.NewSession()
Expand Down
1 change: 1 addition & 0 deletions platform-core/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func init() {
&handlerFuncObj{Url: "/user/:username/get", Method: "GET", HandlerFunc: system.GetUserByUsername, ApiCode: "get-user"},
&handlerFuncObj{Url: "/user/:username/update", Method: "POST", HandlerFunc: system.UpdateUser, ApiCode: "update-user"},
&handlerFuncObj{Url: "/users/retrieve", Method: "GET", HandlerFunc: system.GetAllUser, ApiCode: "get-all-user"},
&handlerFuncObj{Url: "/users/query", Method: "POST", HandlerFunc: system.QueryUser, ApiCode: "query-user"},
&handlerFuncObj{Url: "/roles/create", Method: "POST", HandlerFunc: system.CreateRole, ApiCode: "create-roles"},
&handlerFuncObj{Url: "/roles/retrieve", Method: "GET", HandlerFunc: system.QueryRoles, ApiCode: "query-roles"},
&handlerFuncObj{Url: "/roles/:role-id/menus", Method: "GET", HandlerFunc: system.GetMenusByRoleId, ApiCode: "get-role-menu"},
Expand Down
14 changes: 14 additions & 0 deletions platform-core/api/v1/system/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ func GetAllUser(c *gin.Context) {
middleware.ReturnData(c, list)
}

// QueryUser 查询用户
func QueryUser(c *gin.Context) {
var param models.QueryUserParam
var err error
var pageInfo models.PageInfo
var data []*models.SimpleLocalUserDto
if err = c.ShouldBindJSON(&param); err != nil {
middleware.ReturnError(c, exterror.Catch(exterror.New().RequestParamValidateError, err))
return
}
pageInfo, data, err = remote.QueryUser(param, c.GetHeader("Authorization"), c.GetHeader("Accept-Language"))
middleware.ReturnPageData(c, pageInfo, data)
}

// QueryRoles 查询角色
func QueryRoles(c *gin.Context) {
requiredAll := c.Query("all")
Expand Down
5 changes: 5 additions & 0 deletions platform-core/models/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type QueryRequestParam struct {
ResultColumns []string `json:"resultColumns"` // 返回列,默认为空全返回
}

type QueryUserParam struct {
Pageable *PageInfo `json:"pageable"` // 分页信息,默认为空
UserName string `json:"userName"`
}

type TransFiltersParam struct {
IsStruct bool
StructObj interface{}
Expand Down
10 changes: 10 additions & 0 deletions platform-core/models/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ type QueryUserResponse struct {
Data []*SimpleLocalUserDto `json:"data"`
}

type QueryUserPageResponse struct {
HttpResponseMeta
Data *UserPageData `json:"data"`
}

type UserPageData struct {
PageInfo PageInfo `json:"pageInfo"`
Contents []*SimpleLocalUserDto `json:"contents"`
}

type QueryRolesResponse struct {
HttpResponseMeta
Data []*SimpleLocalRoleDto `json:"data"`
Expand Down
22 changes: 22 additions & 0 deletions platform-core/services/remote/auth-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
pathRegisterLocalUser = "/auth/v1/users"
// pathRetrieveAllUserAccounts 查询所有用户
pathRetrieveAllUserAccounts = "/auth/v1/users"
// pathQueryUser 查询用户
pathQueryUser = "/auth/v1/users/query"
// pathRetrieveAllRoles 查询所有角色
pathRetrieveAllRoles = "/auth/v1/roles?all=%s&roleAdmin=%s"
// pathRetrieveGrantedRolesByUsername 根据用户名查询角色
Expand Down Expand Up @@ -117,6 +119,26 @@ func RetrieveAllUsers(userToken, language string) (response models.QueryUserResp
return
}

func QueryUser(param models.QueryUserParam, userToken, language string) (pageInfo models.PageInfo, data []*models.SimpleLocalUserDto, err error) {
var response models.QueryUserPageResponse
postBytes, _ := json.Marshal(param)
byteArr, err := network.HttpPost(models.Config.Auth.Url+pathQueryUser, userToken, language, postBytes)
if err != nil {
return
}
err = json.Unmarshal(byteArr, &response)
if err != nil {
err = fmt.Errorf("try to json unmarshal response body fail,%s ", err.Error())
return
}
if response.Data == nil {
return
}
pageInfo = response.Data.PageInfo
data = response.Data.Contents
return
}

// RetrieveAllLocalRoles 查询所有角色
func RetrieveAllLocalRoles(requiredAll, userToken, language string, roleAdmin bool) (response models.QueryRolesResponse, err error) {
byteArr, err := network.HttpGet(fmt.Sprintf(models.Config.Auth.Url+pathRetrieveAllRoles, requiredAll, strconv.FormatBool(roleAdmin)), userToken, language)
Expand Down

0 comments on commit 7a8943d

Please sign in to comment.