diff --git a/platform-auth-server/api/api_local_user_management.go b/platform-auth-server/api/api_local_user_management.go index ec16c9d2ca..270fb1e083 100644 --- a/platform-auth-server/api/api_local_user_management.go +++ b/platform-auth-server/api/api_local_user_management.go @@ -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(¶m); 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) diff --git a/platform-auth-server/api/routers.go b/platform-auth-server/api/routers.go index fce99de5e5..916aaa1924 100644 --- a/platform-auth-server/api/routers.go +++ b/platform-auth-server/api/routers.go @@ -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, diff --git a/platform-auth-server/api/support/response.go b/platform-auth-server/api/support/response.go index 982742debd..0d551b6ea0 100644 --- a/platform-auth-server/api/support/response.go +++ b/platform-auth-server/api/support/response.go @@ -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) diff --git a/platform-auth-server/deploy/db/upgrade/wecube_platform_auth_4.3.1.sql b/platform-auth-server/deploy/db/upgrade/wecube_platform_auth_4.3.1.sql new file mode 100644 index 0000000000..50480d7a94 --- /dev/null +++ b/platform-auth-server/deploy/db/upgrade/wecube_platform_auth_4.3.1.sql @@ -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; \ No newline at end of file diff --git a/platform-auth-server/model/model.go b/platform-auth-server/model/model.go index 657d24e53e..922cea7e6b 100644 --- a/platform-auth-server/model/model.go +++ b/platform-auth-server/model/model.go @@ -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"` +} diff --git a/platform-auth-server/model/query.go b/platform-auth-server/model/query.go index 4d3553ccca..66871277b0 100644 --- a/platform-auth-server/model/query.go +++ b/platform-auth-server/model/query.go @@ -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 diff --git a/platform-auth-server/service/db/repository.go b/platform-auth-server/service/db/repository.go index 728c0a69d7..b98789f8e7 100644 --- a/platform-auth-server/service/db/repository.go +++ b/platform-auth-server/service/db/repository.go @@ -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" @@ -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 { diff --git a/platform-auth-server/service/user_management_service.go b/platform-auth-server/service/user_management_service.go index 3d68d5fa93..2c8e7ba2ab 100644 --- a/platform-auth-server/service/user_management_service.go +++ b/platform-auth-server/service/user_management_service.go @@ -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() diff --git a/platform-core/api/api.go b/platform-core/api/api.go index 70ecc4310e..361ba56272 100644 --- a/platform-core/api/api.go +++ b/platform-core/api/api.go @@ -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"}, diff --git a/platform-core/api/v1/system/permission.go b/platform-core/api/v1/system/permission.go index ad9a8f6610..d5fde64e0f 100644 --- a/platform-core/api/v1/system/permission.go +++ b/platform-core/api/v1/system/permission.go @@ -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(¶m); 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") diff --git a/platform-core/models/query.go b/platform-core/models/query.go index ea6db1de3b..c93e8a8afb 100644 --- a/platform-core/models/query.go +++ b/platform-core/models/query.go @@ -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{} diff --git a/platform-core/models/response.go b/platform-core/models/response.go index 5640b53c3c..278588864f 100644 --- a/platform-core/models/response.go +++ b/platform-core/models/response.go @@ -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"` diff --git a/platform-core/services/remote/auth-server.go b/platform-core/services/remote/auth-server.go index 4dfea94314..54ebefd984 100644 --- a/platform-core/services/remote/auth-server.go +++ b/platform-core/services/remote/auth-server.go @@ -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 根据用户名查询角色 @@ -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)