grafana/pkg/services/searchusers/searchusers.go
idafurjes 5167c55760
Chore: Move methods from sqlstore to user store (#56305)
* Copy sqlstore methods to suer store

* Adjust ProvideService signatures in test

* Add xorm tags and tests for search

* Remove methods from sqlstore

* fix lint in tests
2022-10-05 09:34:36 +02:00

118 lines
2.9 KiB
Go

package searchusers
import (
"net/http"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/user"
)
type Service interface {
SearchUsers(c *models.ReqContext) response.Response
SearchUsersWithPaging(c *models.ReqContext) response.Response
}
type OSSService struct {
searchUserFilter user.SearchUserFilter
userService user.Service
}
func ProvideUsersService(searchUserFilter user.SearchUserFilter, userService user.Service,
) *OSSService {
return &OSSService{
searchUserFilter: searchUserFilter,
userService: userService,
}
}
// swagger:route GET /users users searchUsers
//
// Get users.
//
// Returns all users that the authenticated user has permission to view, admin permission required.
//
// Responses:
// 200: searchUsersResponse
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
func (s *OSSService) SearchUsers(c *models.ReqContext) response.Response {
result, err := s.SearchUser(c)
if err != nil {
return response.Error(500, "Failed to fetch users", err)
}
return response.JSON(http.StatusOK, result.Users)
}
// swagger:route GET /users/search users searchUsersWithPaging
//
// Get users with paging.
//
// Responses:
// 200: searchUsersResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
func (s *OSSService) SearchUsersWithPaging(c *models.ReqContext) response.Response {
result, err := s.SearchUser(c)
if err != nil {
return response.Error(500, "Failed to fetch users", err)
}
return response.JSON(http.StatusOK, result)
}
func (s *OSSService) SearchUser(c *models.ReqContext) (*user.SearchUserQueryResult, error) {
perPage := c.QueryInt("perpage")
if perPage <= 0 {
perPage = 1000
}
page := c.QueryInt("page")
if page < 1 {
page = 1
}
searchQuery := c.Query("query")
filters := make([]user.Filter, 0)
for filterName := range s.searchUserFilter.GetFilterList() {
filter := s.searchUserFilter.GetFilter(filterName, c.QueryStrings(filterName))
if filter != nil {
filters = append(filters, filter)
}
}
query := &user.SearchUsersQuery{
// added SignedInUser to the query, as to only list the users that the user has permission to read
SignedInUser: c.SignedInUser,
Query: searchQuery,
Filters: filters,
Page: page,
Limit: perPage,
}
res, err := s.userService.Search(c.Req.Context(), query)
if err != nil {
return nil, err
}
for _, user := range res.Users {
user.AvatarURL = dtos.GetGravatarUrl(user.Email)
user.AuthLabels = make([]string, 0)
if user.AuthModule != nil && len(user.AuthModule) > 0 {
for _, authModule := range user.AuthModule {
user.AuthLabels = append(user.AuthLabels, login.GetAuthProviderLabel(authModule))
}
}
}
res.Page = page
res.PerPage = perPage
return res, nil
}