admin: adds paging to global user list

Currently there is a limit of 1000 users in the global
user list. This change introduces paging so that an
admin can see all users and not just the first 1000.

Adds a new route to the api - /api/users/search that
returns a list of users and a total count. It takes
two parameters perpage and page that enable paging.

Fixes #7469
This commit is contained in:
Daniel Lee
2017-02-08 14:20:07 +01:00
committed by bergquist
parent e80f673264
commit 193d468ed3
15 changed files with 360 additions and 100 deletions

View File

@@ -63,8 +63,8 @@ func TestAccountDataAccess(t *testing.T) {
err := SearchUsers(&query)
So(err, ShouldBeNil)
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
So(query.Result[1].Email, ShouldEqual, "ac2@test.com")
So(query.Result.Users[0].Email, ShouldEqual, "ac1@test.com")
So(query.Result.Users[1].Email, ShouldEqual, "ac2@test.com")
})
Convey("Given an added org user", func() {

View File

@@ -344,12 +344,21 @@ func GetSignedInUser(query *m.GetSignedInUserQuery) error {
}
func SearchUsers(query *m.SearchUsersQuery) error {
query.Result = make([]*m.UserSearchHitDTO, 0)
query.Result = m.SearchUserQueryResult{
Users: make([]*m.UserSearchHitDTO, 0),
}
sess := x.Table("user")
sess.Where("email LIKE ?", query.Query+"%")
sess.Limit(query.Limit, query.Limit*query.Page)
offset := query.Limit * (query.Page - 1)
sess.Limit(query.Limit, offset)
sess.Cols("id", "email", "name", "login", "is_admin")
err := sess.Find(&query.Result)
if err := sess.Find(&query.Result.Users); err != nil {
return err
}
user := m.User{}
count, err := x.Count(&user)
query.Result.TotalCount = count
return err
}

View File

@@ -0,0 +1,45 @@
package sqlstore
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/grafana/grafana/pkg/models"
)
func TestUserDataAccess(t *testing.T) {
Convey("Testing DB", t, func() {
InitTestDB(t)
var err error
for i := 0; i < 5; i++ {
err = CreateUser(&models.CreateUserCommand{
Email: fmt.Sprint("user", i, "@test.com"),
Name: fmt.Sprint("user", i),
Login: fmt.Sprint("user", i),
})
So(err, ShouldBeNil)
}
Convey("Can return the first page of users and a total count", func() {
query := models.SearchUsersQuery{Query: "", Page: 1, Limit: 3}
err = SearchUsers(&query)
So(err, ShouldBeNil)
So(len(query.Result.Users), ShouldEqual, 3)
So(query.Result.TotalCount, ShouldEqual, 5)
})
Convey("Can return the second page of users and a total count", func() {
query := models.SearchUsersQuery{Query: "", Page: 2, Limit: 3}
err = SearchUsers(&query)
So(err, ShouldBeNil)
So(len(query.Result.Users), ShouldEqual, 2)
So(query.Result.TotalCount, ShouldEqual, 5)
})
})
}