Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add role query to list users #3938

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/microservice/user/core/repository/orm/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 31 additions & 9 deletions pkg/microservice/user/core/service/permission/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Loading