diff --git a/pkg/microservice/user/core/repository/orm/user.go b/pkg/microservice/user/core/repository/orm/user.go index d19e979df8..5919381d06 100644 --- a/pkg/microservice/user/core/repository/orm/user.go +++ b/pkg/microservice/user/core/repository/orm/user.go @@ -94,6 +94,28 @@ func ListUsers(page int, perPage int, name string, db *gorm.DB) ([]models.User, return users, nil } +// ListUsersByNameAndRole gets a list of users based on paging constraints, the name of the user, and the roles of the user +func ListUsersByNameAndRole(page int, perPage int, name string, roles []string, db *gorm.DB) ([]models.User, error) { + var ( + users []models.User + err error + ) + + err = db.Where("user.name LIKE ? AND role.name IN ?", "%"+name+"%", roles). + Joins("INNER JOIN role_binding on role_binding.uid = user.uid"). + Joins("INNER JOIN role on role_binding.role_id = role.id").Order("account ASC").Offset((page - 1) * perPage). + Group("user.uid"). + Limit(perPage). + Find(&users). + Error + + if err != nil && err != gorm.ErrRecordNotFound { + return nil, err + } + + return users, nil +} + func ListUsersByGroup(groupID string, db *gorm.DB) ([]*models.User, error) { resp := make([]*models.User, 0) @@ -178,6 +200,29 @@ func GetUsersCount(name string) (int64, error) { return count, nil } +// GetUsersCount gets user count +func GetUsersCountByRoles(name string, roles []string) (int64, error) { + var ( + users []models.User + err error + count int64 + ) + + err = repository.DB.Where("user.name LIKE ? AND role.name IN ?", "%"+name+"%", roles). + Joins("INNER JOIN role_binding on role_binding.uid = user.uid"). + Joins("INNER JOIN role on role_binding.role_id = role.id"). + Group("user.uid"). + Find(&users). + Count(&count). + Error + + if err != nil { + return 0, err + } + + return count, nil +} + // UpdateUser update user info func UpdateUser(uid string, user *models.User, db *gorm.DB) error { if err := db.Model(&models.User{}).Where("uid = ?", uid).Updates(user).Error; err != nil { diff --git a/pkg/microservice/user/core/service/permission/user.go b/pkg/microservice/user/core/service/permission/user.go index 165b2227f7..e0ea1dd910 100644 --- a/pkg/microservice/user/core/service/permission/user.go +++ b/pkg/microservice/user/core/service/permission/user.go @@ -74,7 +74,8 @@ type QueryArgs struct { IdentityType string `json:"identity_type,omitempty"` UIDs []string `json:"uids,omitempty"` PerPage int `json:"per_page,omitempty" form:"perPage"` - Page int `json:"page,omitempty" form:"page"` + Page int `json:"page,omitempty" form:"page"` + Roles []string `json:"roles,omitempty" form:"roles"` } type Password struct { @@ -291,22 +292,43 @@ func SearchUserByAccount(args *QueryArgs, logger *zap.SugaredLogger) (*types.Use } func SearchUsers(args *QueryArgs, logger *zap.SugaredLogger) (*types.UsersResp, error) { - count, err := orm.GetUsersCount(args.Name) - if err != nil { - logger.Errorf("SeachUsers GetUsersCount By name:%s error, error msg:%s", args.Name, err.Error()) - return nil, err + var count int64 + var err error + if len(args.Roles) == 0 { + count, err = orm.GetUsersCount(args.Name) + if err != nil { + logger.Errorf("SeachUsers GetUsersCount By name:%s error, error msg:%s", args.Name, err.Error()) + return nil, err + } + } else { + count, err = orm.GetUsersCountByRoles(args.Name, args.Roles) + if err != nil { + logger.Errorf("SeachUsers GetUsersCount By name:%s error, error msg:%s", args.Name, err.Error()) + return nil, err + } } + if count == 0 { return &types.UsersResp{ TotalCount: 0, }, nil } - users, err := orm.ListUsers(args.Page, args.PerPage, args.Name, repository.DB) - if err != nil { - logger.Errorf("SeachUsers SeachUsers By name:%s error, error msg:%s", args.Name, err.Error()) - return nil, err + var users []models.User + if len(args.Roles) == 0 { + users, err = orm.ListUsers(args.Page, args.PerPage, args.Name, repository.DB) + if err != nil { + logger.Errorf("SeachUsers SeachUsers By name:%s error, error msg:%s", args.Name, err.Error()) + return nil, err + } + } else { + users, err = orm.ListUsersByNameAndRole(args.Page, args.PerPage, args.Name, args.Roles, repository.DB) + if err != nil { + logger.Errorf("SeachUsers SeachUsers By name:%s error, error msg:%s", args.Name, err.Error()) + return nil, err + } } + var uids []string for _, user := range users { uids = append(uids, user.UID)