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 options to the user.Count method #16285

Merged
merged 1 commit into from
Jul 6, 2022
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
2 changes: 1 addition & 1 deletion src/controller/config/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,5 @@ func (c *controller) authModeCanBeModified(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
return cnt == 1, nil // admin user only
return cnt == 0, nil
}
21 changes: 18 additions & 3 deletions src/pkg/user/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Manager interface {
// List users according to the query
List(ctx context.Context, query *q.Query, options ...models.Option) (commonmodels.Users, error)
// Count counts the number of users according to the query
Count(ctx context.Context, query *q.Query) (int64, error)
Count(ctx context.Context, query *q.Query, options ...models.Option) (int64, error)
// Create creates the user, the password of input should be plaintext
Create(ctx context.Context, user *commonmodels.User) (int, error)
// Delete deletes the user by updating user's delete flag and update the name and Email
Expand Down Expand Up @@ -131,7 +131,11 @@ func (m *manager) MatchLocalPassword(ctx context.Context, usernameOrEmail, passw
return nil, nil
}

func (m *manager) Count(ctx context.Context, query *q.Query) (int64, error) {
func (m *manager) Count(ctx context.Context, query *q.Query, options ...models.Option) (int64, error) {
stonezdj marked this conversation as resolved.
Show resolved Hide resolved
opts := models.NewOptions(options...)
if !opts.IncludeDefaultAdmin {
query = excludeDefaultAdmin(query)
}
return m.dao.Count(ctx, query)
}

Expand Down Expand Up @@ -206,11 +210,22 @@ func (m *manager) List(ctx context.Context, query *q.Query, options ...models.Op
}
opts := models.NewOptions(options...)
if !opts.IncludeDefaultAdmin {
query.Keywords["user_id__gt"] = 1
query = excludeDefaultAdmin(query)
}
return m.dao.List(ctx, query)
}

func excludeDefaultAdmin(query *q.Query) (qu *q.Query) {
if query == nil {
query = q.New(q.KeyWords{})
}
if query.Keywords == nil {
query.Keywords = q.KeyWords{}
}
query.Keywords["user_id__gt"] = 1
return query
}

func injectPasswd(u *commonmodels.User, password string) {
salt := utils.GenerateRandomString()
u.Password = utils.Encrypt(password, salt, utils.SHA256)
Expand Down
76 changes: 42 additions & 34 deletions src/testing/pkg/user/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.