Skip to content

Commit

Permalink
Add options to the user.Count method
Browse files Browse the repository at this point in the history
  Fixes #16269, exclude the admin account by default
  Add excludeDefaultAdmin method -- exclude default admin by option
  Update authModeCanBeModified method -- the user count should be 0 without admin

Signed-off-by: stonezdj <stonezdj@gmail.com>
  • Loading branch information
stonezdj committed Jan 26, 2022
1 parent c9af6c0 commit 8adb47f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/controller/config/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,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 @@ -41,7 +41,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 @@ -116,7 +116,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) {
opts := models.NewOptions(options...)
if !opts.IncludeDefaultAdmin {
query = excludeDefaultAdmin(query)
}
return m.dao.Count(ctx, query)
}

Expand Down Expand Up @@ -191,11 +195,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.

0 comments on commit 8adb47f

Please sign in to comment.