Extract search users functions into a service (#39002)
* Extract search users to a new service * Fix wire provider * Fix common_test and remove RouteRegister * Remove old endpoints * Fix test * Add indexes to dashboards and orgs tables * Fix lint
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package searchusers
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
SearchUsers(c *models.ReqContext) response.Response
|
||||
SearchUsersWithPaging(c *models.ReqContext) response.Response
|
||||
}
|
||||
|
||||
type OSSService struct {
|
||||
bus bus.Bus
|
||||
}
|
||||
|
||||
func ProvideUsersService(bus bus.Bus) *OSSService {
|
||||
return &OSSService{bus: bus}
|
||||
}
|
||||
|
||||
func (s *OSSService) SearchUsers(c *models.ReqContext) response.Response {
|
||||
query, err := s.SearchUser(c)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to fetch users", err)
|
||||
}
|
||||
|
||||
return response.JSON(200, query.Result.Users)
|
||||
}
|
||||
|
||||
func (s *OSSService) SearchUsersWithPaging(c *models.ReqContext) response.Response {
|
||||
query, err := s.SearchUser(c)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to fetch users", err)
|
||||
}
|
||||
|
||||
return response.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
func (s *OSSService) SearchUser(c *models.ReqContext) (*models.SearchUsersQuery, error) {
|
||||
perPage := c.QueryInt("perpage")
|
||||
if perPage <= 0 {
|
||||
perPage = 1000
|
||||
}
|
||||
page := c.QueryInt("page")
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
searchQuery := c.Query("query")
|
||||
filter := c.Query("filter")
|
||||
|
||||
query := &models.SearchUsersQuery{Query: searchQuery, Filter: models.SearchUsersFilter(filter), Page: page, Limit: perPage}
|
||||
if err := s.bus.Dispatch(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, user := range query.Result.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, GetAuthProviderLabel(authModule))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query.Result.Page = page
|
||||
query.Result.PerPage = perPage
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func GetAuthProviderLabel(authModule string) string {
|
||||
switch authModule {
|
||||
case "oauth_github":
|
||||
return "GitHub"
|
||||
case "oauth_google":
|
||||
return "Google"
|
||||
case "oauth_azuread":
|
||||
return "AzureAD"
|
||||
case "oauth_gitlab":
|
||||
return "GitLab"
|
||||
case "oauth_grafana_com", "oauth_grafananet":
|
||||
return "grafana.com"
|
||||
case "auth.saml":
|
||||
return "SAML"
|
||||
case "ldap", "":
|
||||
return "LDAP"
|
||||
default:
|
||||
return "OAuth"
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,10 @@ func addDashboardAclMigrations(mg *Migrator) {
|
||||
{Cols: []string{"dashboard_id"}},
|
||||
{Cols: []string{"dashboard_id", "user_id"}, Type: UniqueIndex},
|
||||
{Cols: []string{"dashboard_id", "team_id"}, Type: UniqueIndex},
|
||||
{Cols: []string{"user_id"}},
|
||||
{Cols: []string{"team_id"}},
|
||||
{Cols: []string{"org_id", "role"}},
|
||||
{Cols: []string{"permission"}},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -29,6 +33,10 @@ func addDashboardAclMigrations(mg *Migrator) {
|
||||
mg.AddMigration("add index dashboard_acl_dashboard_id", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[0]))
|
||||
mg.AddMigration("add unique index dashboard_acl_dashboard_id_user_id", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[1]))
|
||||
mg.AddMigration("add unique index dashboard_acl_dashboard_id_team_id", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[2]))
|
||||
mg.AddMigration("add index dashboard_acl_user_id", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[3]))
|
||||
mg.AddMigration("add index dashboard_acl_team_id", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[4]))
|
||||
mg.AddMigration("add index dashboard_acl_org_id_role", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[5]))
|
||||
mg.AddMigration("add index dashboard_permission", NewAddIndexMigration(dashboardAclV1, dashboardAclV1.Indices[6]))
|
||||
|
||||
const rawSQL = `
|
||||
INSERT INTO dashboard_acl
|
||||
|
||||
@@ -225,4 +225,9 @@ func addDashboardMigration(mg *Migrator) {
|
||||
|
||||
mg.AddMigration("delete stars for deleted dashboards", NewRawSQLMigration(
|
||||
"DELETE FROM star WHERE dashboard_id NOT IN (SELECT id FROM dashboard)"))
|
||||
|
||||
mg.AddMigration("Add index for dashboard_is_folder", NewAddIndexMigration(dashboardV2, &Index{
|
||||
Cols: []string{"is_folder"},
|
||||
Type: IndexType,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ func addOrgMigrations(mg *Migrator) {
|
||||
Indices: []*Index{
|
||||
{Cols: []string{"org_id"}},
|
||||
{Cols: []string{"org_id", "user_id"}, Type: UniqueIndex},
|
||||
{Cols: []string{"user_id"}},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user