mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore move Filter to user service (#53588)
This commit is contained in:
parent
a14621fff6
commit
da72a4ed2e
@ -34,6 +34,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/searchusers/filters"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
|
||||
"github.com/grafana/grafana/pkg/services/thumbs"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/validations"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@ -48,8 +49,6 @@ var wireExtsSet = wire.NewSet(
|
||||
wire.Bind(new(setting.Provider), new(*setting.OSSImpl)),
|
||||
osskmsproviders.ProvideService,
|
||||
wire.Bind(new(kmsproviders.Service), new(osskmsproviders.Service)),
|
||||
// ossencryption.ProvideService,
|
||||
// wire.Bind(new(encryption.Internal), new(*ossencryption.Service)),
|
||||
auth.ProvideUserAuthTokenService,
|
||||
wire.Bind(new(models.UserTokenService), new(*auth.UserAuthTokenService)),
|
||||
wire.Bind(new(models.UserTokenBackgroundService), new(*auth.UserAuthTokenService)),
|
||||
@ -69,7 +68,7 @@ var wireExtsSet = wire.NewSet(
|
||||
authinfoservice.ProvideOSSUserProtectionService,
|
||||
wire.Bind(new(login.UserProtectionService), new(*authinfoservice.OSSUserProtectionImpl)),
|
||||
filters.ProvideOSSSearchUserFilter,
|
||||
wire.Bind(new(models.SearchUserFilter), new(*filters.OSSSearchUserFilter)),
|
||||
wire.Bind(new(user.SearchUserFilter), new(*filters.OSSSearchUserFilter)),
|
||||
searchusers.ProvideUsersService,
|
||||
wire.Bind(new(searchusers.Service), new(*searchusers.OSSService)),
|
||||
signature.ProvideOSSAuthorizer,
|
||||
|
@ -1,30 +0,0 @@
|
||||
package models
|
||||
|
||||
type SearchUserFilter interface {
|
||||
GetFilter(filterName string, params []string) Filter
|
||||
GetFilterList() map[string]FilterHandler
|
||||
}
|
||||
|
||||
type WhereCondition struct {
|
||||
Condition string
|
||||
Params interface{}
|
||||
}
|
||||
|
||||
type InCondition struct {
|
||||
Condition string
|
||||
Params interface{}
|
||||
}
|
||||
|
||||
type JoinCondition struct {
|
||||
Operator string
|
||||
Table string
|
||||
Params string
|
||||
}
|
||||
|
||||
type FilterHandler func(params []string) (Filter, error)
|
||||
|
||||
type Filter interface {
|
||||
WhereCondition() *WhereCondition
|
||||
InCondition() *InCondition
|
||||
JoinCondition() *JoinCondition
|
||||
}
|
@ -85,7 +85,7 @@ type SearchUsersQuery struct {
|
||||
Page int
|
||||
Limit int
|
||||
AuthModule string
|
||||
Filters []Filter
|
||||
Filters []user.Filter
|
||||
|
||||
IsDisabled *bool
|
||||
|
||||
|
@ -5,6 +5,7 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/google/wire"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/provider"
|
||||
@ -33,6 +34,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/searchusers/filters"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
|
||||
"github.com/grafana/grafana/pkg/services/thumbs"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/validations"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@ -65,7 +67,7 @@ var wireExtsBasicSet = wire.NewSet(
|
||||
encryptionprovider.ProvideEncryptionProvider,
|
||||
wire.Bind(new(encryption.Provider), new(encryptionprovider.Provider)),
|
||||
filters.ProvideOSSSearchUserFilter,
|
||||
wire.Bind(new(models.SearchUserFilter), new(*filters.OSSSearchUserFilter)),
|
||||
wire.Bind(new(user.SearchUserFilter), new(*filters.OSSSearchUserFilter)),
|
||||
searchusers.ProvideUsersService,
|
||||
wire.Bind(new(searchusers.Service), new(*searchusers.OSSService)),
|
||||
signature.ProvideOSSAuthorizer,
|
||||
|
@ -53,20 +53,20 @@ type UserIDFilter struct {
|
||||
userIDs []int64
|
||||
}
|
||||
|
||||
func NewIDFilter(userIDs []int64) models.Filter {
|
||||
func NewIDFilter(userIDs []int64) user.Filter {
|
||||
return &UserIDFilter{userIDs: userIDs}
|
||||
}
|
||||
|
||||
func (a *UserIDFilter) WhereCondition() *models.WhereCondition {
|
||||
func (a *UserIDFilter) WhereCondition() *user.WhereCondition {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *UserIDFilter) JoinCondition() *models.JoinCondition {
|
||||
func (a *UserIDFilter) JoinCondition() *user.JoinCondition {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *UserIDFilter) InCondition() *models.InCondition {
|
||||
return &models.InCondition{
|
||||
func (a *UserIDFilter) InCondition() *user.InCondition {
|
||||
return &user.InCondition{
|
||||
Condition: "u.id",
|
||||
Params: a.userIDs,
|
||||
}
|
||||
@ -152,7 +152,7 @@ func (s *Service) Get(ctx context.Context, orgID int64, signedInUser *user.Signe
|
||||
Page: 0,
|
||||
Limit: len(userIds),
|
||||
SignedInUser: signedInUser,
|
||||
Filters: []models.Filter{NewIDFilter(userIds)},
|
||||
Filters: []user.Filter{NewIDFilter(userIds)},
|
||||
}
|
||||
if err := s.sqlStore.SearchUsers(ctx, query); err != nil {
|
||||
return nil, err
|
||||
|
@ -2,26 +2,26 @@ package filters
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
const activeLast30Days = "activeLast30Days"
|
||||
|
||||
type OSSSearchUserFilter struct {
|
||||
filters map[string]models.FilterHandler
|
||||
filters map[string]user.FilterHandler
|
||||
}
|
||||
|
||||
var fltlog = log.New("filters")
|
||||
|
||||
func ProvideOSSSearchUserFilter() *OSSSearchUserFilter {
|
||||
filters := make(map[string]models.FilterHandler)
|
||||
filters := make(map[string]user.FilterHandler)
|
||||
filters[activeLast30Days] = NewActiveLast30DaysFilter
|
||||
return &OSSSearchUserFilter{
|
||||
filters: filters,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *OSSSearchUserFilter) GetFilter(filterName string, params []string) models.Filter {
|
||||
func (o *OSSSearchUserFilter) GetFilter(filterName string, params []string) user.Filter {
|
||||
f, ok := o.filters[filterName]
|
||||
if !ok || len(params) == 0 {
|
||||
return nil
|
||||
@ -34,6 +34,6 @@ func (o *OSSSearchUserFilter) GetFilter(filterName string, params []string) mode
|
||||
return filter
|
||||
}
|
||||
|
||||
func (o *OSSSearchUserFilter) GetFilterList() map[string]models.FilterHandler {
|
||||
func (o *OSSSearchUserFilter) GetFilterList() map[string]user.FilterHandler {
|
||||
return o.filters
|
||||
}
|
||||
|
@ -4,14 +4,14 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type ActiveLast30DaysFilter struct {
|
||||
active bool
|
||||
}
|
||||
|
||||
func NewActiveLast30DaysFilter(params []string) (models.Filter, error) {
|
||||
func NewActiveLast30DaysFilter(params []string) (user.Filter, error) {
|
||||
active, err := strconv.ParseBool(params[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -19,21 +19,21 @@ func NewActiveLast30DaysFilter(params []string) (models.Filter, error) {
|
||||
return &ActiveLast30DaysFilter{active: active}, nil
|
||||
}
|
||||
|
||||
func (a *ActiveLast30DaysFilter) WhereCondition() *models.WhereCondition {
|
||||
func (a *ActiveLast30DaysFilter) WhereCondition() *user.WhereCondition {
|
||||
if !a.active {
|
||||
return nil
|
||||
}
|
||||
return &models.WhereCondition{
|
||||
return &user.WhereCondition{
|
||||
Condition: "last_seen_at > ?",
|
||||
Params: a.whereParams(),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ActiveLast30DaysFilter) JoinCondition() *models.JoinCondition {
|
||||
func (a *ActiveLast30DaysFilter) JoinCondition() *user.JoinCondition {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ActiveLast30DaysFilter) InCondition() *models.InCondition {
|
||||
func (a *ActiveLast30DaysFilter) InCondition() *user.InCondition {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
@ -17,10 +18,10 @@ type Service interface {
|
||||
|
||||
type OSSService struct {
|
||||
sqlStore sqlstore.Store
|
||||
searchUserFilter models.SearchUserFilter
|
||||
searchUserFilter user.SearchUserFilter
|
||||
}
|
||||
|
||||
func ProvideUsersService(sqlStore sqlstore.Store, searchUserFilter models.SearchUserFilter) *OSSService {
|
||||
func ProvideUsersService(sqlStore sqlstore.Store, searchUserFilter user.SearchUserFilter) *OSSService {
|
||||
return &OSSService{sqlStore: sqlStore, searchUserFilter: searchUserFilter}
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ func (s *OSSService) SearchUser(c *models.ReqContext) (*models.SearchUsersQuery,
|
||||
}
|
||||
|
||||
searchQuery := c.Query("query")
|
||||
filters := make([]models.Filter, 0)
|
||||
filters := make([]user.Filter, 0)
|
||||
for filterName := range s.searchUserFilter.GetFilterList() {
|
||||
filter := s.searchUserFilter.GetFilter(filterName, c.QueryStrings(filterName))
|
||||
if filter != nil {
|
||||
|
@ -101,6 +101,68 @@ type SetUsingOrgCommand struct {
|
||||
OrgID int64
|
||||
}
|
||||
|
||||
type SearchUsersQuery struct {
|
||||
SignedInUser *SignedInUser
|
||||
OrgID int64
|
||||
Query string
|
||||
Page int
|
||||
Limit int
|
||||
AuthModule string
|
||||
Filters []Filter
|
||||
|
||||
IsDisabled *bool
|
||||
}
|
||||
|
||||
type SearchUserQueryResult struct {
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
Users []*UserSearchHitDTO `json:"users"`
|
||||
Page int `json:"page"`
|
||||
PerPage int `json:"perPage"`
|
||||
}
|
||||
|
||||
type UserSearchHitDTO struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Login string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
AvatarUrl string `json:"avatarUrl"`
|
||||
IsAdmin bool `json:"isAdmin"`
|
||||
IsDisabled bool `json:"isDisabled"`
|
||||
LastSeenAt time.Time `json:"lastSeenAt"`
|
||||
LastSeenAtAge string `json:"lastSeenAtAge"`
|
||||
AuthLabels []string `json:"authLabels"`
|
||||
AuthModule AuthModuleConversion `json:"-"`
|
||||
}
|
||||
|
||||
// implement Conversion interface to define custom field mapping (xorm feature)
|
||||
type AuthModuleConversion []string
|
||||
|
||||
func (auth *AuthModuleConversion) FromDB(data []byte) error {
|
||||
auth_module := string(data)
|
||||
*auth = []string{auth_module}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Just a stub, we don't want to write to database
|
||||
func (auth *AuthModuleConversion) ToDB() ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
type DisableUserCommand struct {
|
||||
UserID int64
|
||||
IsDisabled bool
|
||||
}
|
||||
|
||||
type BatchDisableUsersCommand struct {
|
||||
UserIDs []int64
|
||||
IsDisabled bool
|
||||
}
|
||||
|
||||
type SetUserHelpFlagCommand struct {
|
||||
HelpFlags1 HelpFlags1
|
||||
UserID int64
|
||||
}
|
||||
|
||||
type GetSignedInUserQuery struct {
|
||||
UserID int64
|
||||
Login string
|
||||
@ -153,7 +215,7 @@ type ErrCaseInsensitiveLoginConflict struct {
|
||||
}
|
||||
|
||||
type UserDisplayDTO struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
ID int64 `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Login string `json:"login,omitempty"`
|
||||
AvatarUrl string `json:"avatarUrl"`
|
||||
@ -178,7 +240,7 @@ func (u *SignedInUser) NameOrFallback() string {
|
||||
|
||||
func (u *SignedInUser) ToUserDisplayDTO() *UserDisplayDTO {
|
||||
return &UserDisplayDTO{
|
||||
Id: u.UserID,
|
||||
ID: u.UserID,
|
||||
Login: u.Login,
|
||||
Name: u.Name,
|
||||
}
|
||||
@ -211,3 +273,32 @@ func (e *ErrCaseInsensitiveLoginConflict) Error() string {
|
||||
"Found a conflict in user login information. %d users already exist with either the same login or email: [%s].",
|
||||
n, strings.Join(userStrings, ", "))
|
||||
}
|
||||
|
||||
type Filter interface {
|
||||
WhereCondition() *WhereCondition
|
||||
InCondition() *InCondition
|
||||
JoinCondition() *JoinCondition
|
||||
}
|
||||
|
||||
type WhereCondition struct {
|
||||
Condition string
|
||||
Params interface{}
|
||||
}
|
||||
|
||||
type InCondition struct {
|
||||
Condition string
|
||||
Params interface{}
|
||||
}
|
||||
|
||||
type JoinCondition struct {
|
||||
Operator string
|
||||
Table string
|
||||
Params string
|
||||
}
|
||||
|
||||
type SearchUserFilter interface {
|
||||
GetFilter(filterName string, params []string) Filter
|
||||
GetFilterList() map[string]FilterHandler
|
||||
}
|
||||
|
||||
type FilterHandler func(params []string) (Filter, error)
|
||||
|
Loading…
Reference in New Issue
Block a user