UserService: use the UserService instead of calling sqlstore directly (#55745)
* UserService: update callers to use the UserService instead of calling sqlstore directly There is one major change hiding in this PR. UserService.Delete originally called a number of services to delete user-related records. I moved everything except the actual call to the user table, and moved those into the API. This was done to avoid dependencies cycles; many of our services depend on the user service, so the user service itself should have as few dependencies as possible.
This commit is contained in:
@@ -51,7 +51,7 @@ type Store interface {
|
||||
func New(
|
||||
options Options, cfg *setting.Cfg, router routing.RouteRegister, license models.Licensing,
|
||||
ac accesscontrol.AccessControl, service accesscontrol.Service, sqlStore *sqlstore.SQLStore,
|
||||
teamService team.Service,
|
||||
teamService team.Service, userService user.Service,
|
||||
) (*Service, error) {
|
||||
var permissions []string
|
||||
actionSet := make(map[string]struct{})
|
||||
@@ -83,6 +83,7 @@ func New(
|
||||
sqlStore: sqlStore,
|
||||
service: service,
|
||||
teamService: teamService,
|
||||
userService: userService,
|
||||
}
|
||||
|
||||
s.api = newApi(ac, router, s)
|
||||
@@ -110,6 +111,7 @@ type Service struct {
|
||||
actions []string
|
||||
sqlStore *sqlstore.SQLStore
|
||||
teamService team.Service
|
||||
userService user.Service
|
||||
}
|
||||
|
||||
func (s *Service) GetPermissions(ctx context.Context, user *user.SignedInUser, resourceID string) ([]accesscontrol.ResourcePermission, error) {
|
||||
@@ -286,10 +288,8 @@ func (s *Service) validateUser(ctx context.Context, orgID, userID int64) error {
|
||||
return ErrInvalidAssignment
|
||||
}
|
||||
|
||||
if err := s.sqlStore.GetSignedInUser(ctx, &models.GetSignedInUserQuery{OrgId: orgID, UserId: userID}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
_, err := s.userService.GetSignedInUser(ctx, &user.GetSignedInUserQuery{OrgID: orgID, UserID: userID})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) validateTeam(ctx context.Context, orgID, teamID int64) error {
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/team"
|
||||
"github.com/grafana/grafana/pkg/services/team/teamimpl"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
@@ -223,12 +224,13 @@ func setupTestEnvironment(t *testing.T, permissions []accesscontrol.Permission,
|
||||
sql := sqlstore.InitTestDB(t)
|
||||
cfg := setting.NewCfg()
|
||||
teamSvc := teamimpl.ProvideService(sql, cfg)
|
||||
userSvc := userimpl.ProvideService(sql, nil, cfg, sql)
|
||||
license := licensingtest.NewFakeLicensing()
|
||||
license.On("FeatureEnabled", "accesscontrol.enforcement").Return(true).Maybe()
|
||||
mock := accesscontrolmock.New().WithPermissions(permissions)
|
||||
service, err := New(
|
||||
ops, cfg, routing.NewRouteRegister(), license,
|
||||
accesscontrolmock.New().WithPermissions(permissions), mock, sql, teamSvc,
|
||||
accesscontrolmock.New().WithPermissions(permissions), mock, sql, teamSvc, userSvc,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user