mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add user service method UpdateLastSeenAt (#53309)
* Chore: Add user service method UpdateLastSeenAt * Fix lint
This commit is contained in:
@@ -204,7 +204,7 @@ func getContextHandler(t *testing.T, cfg *setting.Cfg) *contexthandler.ContextHa
|
|||||||
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginservice.LoginServiceMock{}, sqlStore)
|
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginservice.LoginServiceMock{}, sqlStore)
|
||||||
loginService := &logintest.LoginServiceFake{}
|
loginService := &logintest.LoginServiceFake{}
|
||||||
authenticator := &logintest.AuthenticatorFake{}
|
authenticator := &logintest.AuthenticatorFake{}
|
||||||
ctxHdlr := contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, sqlStore, tracer, authProxy, loginService, nil, authenticator)
|
ctxHdlr := contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, sqlStore, tracer, authProxy, loginService, nil, authenticator, usertest.NewUserServiceFake())
|
||||||
|
|
||||||
return ctxHdlr
|
return ctxHdlr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/rendering"
|
"github.com/grafana/grafana/pkg/services/rendering"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
||||||
"github.com/grafana/grafana/pkg/services/user"
|
"github.com/grafana/grafana/pkg/services/user"
|
||||||
|
"github.com/grafana/grafana/pkg/services/user/usertest"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
"github.com/grafana/grafana/pkg/web"
|
"github.com/grafana/grafana/pkg/web"
|
||||||
@@ -677,7 +678,7 @@ func getContextHandler(t *testing.T, cfg *setting.Cfg, mockSQLStore *mockstore.S
|
|||||||
tracer := tracing.InitializeTracerForTest()
|
tracer := tracing.InitializeTracerForTest()
|
||||||
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginService, mockSQLStore)
|
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginService, mockSQLStore)
|
||||||
authenticator := &logintest.AuthenticatorFake{ExpectedUser: &user.User{}}
|
authenticator := &logintest.AuthenticatorFake{ExpectedUser: &user.User{}}
|
||||||
return contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, mockSQLStore, tracer, authProxy, loginService, apiKeyService, authenticator)
|
return contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, mockSQLStore, tracer, authProxy, loginService, apiKeyService, authenticator, usertest.NewUserServiceFake())
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeRenderService struct {
|
type fakeRenderService struct {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/rendering"
|
"github.com/grafana/grafana/pkg/services/rendering"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
"github.com/grafana/grafana/pkg/services/user"
|
"github.com/grafana/grafana/pkg/services/user"
|
||||||
|
"github.com/grafana/grafana/pkg/services/user/usertest"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/web"
|
"github.com/grafana/grafana/pkg/web"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -86,7 +87,7 @@ func getContextHandler(t *testing.T) *ContextHandler {
|
|||||||
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginService, &FakeGetSignUserStore{})
|
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginService, &FakeGetSignUserStore{})
|
||||||
authenticator := &fakeAuthenticator{}
|
authenticator := &fakeAuthenticator{}
|
||||||
|
|
||||||
return ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, sqlStore, tracer, authProxy, loginService, nil, authenticator)
|
return ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, sqlStore, tracer, authProxy, loginService, nil, authenticator, &usertest.FakeUserService{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type FakeGetSignUserStore struct {
|
type FakeGetSignUserStore struct {
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ const ServiceName = "ContextHandler"
|
|||||||
func ProvideService(cfg *setting.Cfg, tokenService models.UserTokenService, jwtService models.JWTService,
|
func ProvideService(cfg *setting.Cfg, tokenService models.UserTokenService, jwtService models.JWTService,
|
||||||
remoteCache *remotecache.RemoteCache, renderService rendering.Service, sqlStore sqlstore.Store,
|
remoteCache *remotecache.RemoteCache, renderService rendering.Service, sqlStore sqlstore.Store,
|
||||||
tracer tracing.Tracer, authProxy *authproxy.AuthProxy, loginService login.Service,
|
tracer tracing.Tracer, authProxy *authproxy.AuthProxy, loginService login.Service,
|
||||||
apiKeyService apikey.Service, authenticator loginpkg.Authenticator) *ContextHandler {
|
apiKeyService apikey.Service, authenticator loginpkg.Authenticator, userService user.Service,
|
||||||
|
) *ContextHandler {
|
||||||
return &ContextHandler{
|
return &ContextHandler{
|
||||||
Cfg: cfg,
|
Cfg: cfg,
|
||||||
AuthTokenService: tokenService,
|
AuthTokenService: tokenService,
|
||||||
@@ -55,6 +56,7 @@ func ProvideService(cfg *setting.Cfg, tokenService models.UserTokenService, jwtS
|
|||||||
authenticator: authenticator,
|
authenticator: authenticator,
|
||||||
loginService: loginService,
|
loginService: loginService,
|
||||||
apiKeyService: apiKeyService,
|
apiKeyService: apiKeyService,
|
||||||
|
userService: userService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +73,7 @@ type ContextHandler struct {
|
|||||||
authenticator loginpkg.Authenticator
|
authenticator loginpkg.Authenticator
|
||||||
loginService login.Service
|
loginService login.Service
|
||||||
apiKeyService apikey.Service
|
apiKeyService apikey.Service
|
||||||
|
userService user.Service
|
||||||
// GetTime returns the current time.
|
// GetTime returns the current time.
|
||||||
// Stubbable by tests.
|
// Stubbable by tests.
|
||||||
GetTime func() time.Time
|
GetTime func() time.Time
|
||||||
@@ -160,7 +163,7 @@ func (h *ContextHandler) Middleware(mContext *web.Context) {
|
|||||||
// update last seen every 5min
|
// update last seen every 5min
|
||||||
if reqContext.ShouldUpdateLastSeenAt() {
|
if reqContext.ShouldUpdateLastSeenAt() {
|
||||||
reqContext.Logger.Debug("Updating last user_seen_at", "user_id", reqContext.UserId)
|
reqContext.Logger.Debug("Updating last user_seen_at", "user_id", reqContext.UserId)
|
||||||
if err := h.SQLStore.UpdateUserLastSeenAt(mContext.Req.Context(), &models.UpdateUserLastSeenAtCommand{UserId: reqContext.UserId}); err != nil {
|
if err := h.userService.UpdateLastSeenAt(mContext.Req.Context(), &user.UpdateUserLastSeenAtCommand{UserID: reqContext.UserId}); err != nil {
|
||||||
reqContext.Logger.Error("Failed to update last_seen_at", "error", err)
|
reqContext.Logger.Error("Failed to update last_seen_at", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -358,13 +361,13 @@ func (h *ContextHandler) initContextWithBasicAuth(reqContext *models.ReqContext,
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
user := authQuery.User
|
usr := authQuery.User
|
||||||
|
|
||||||
query := models.GetSignedInUserQuery{UserId: user.ID, OrgId: orgID}
|
query := models.GetSignedInUserQuery{UserId: usr.ID, OrgId: orgID}
|
||||||
if err := h.SQLStore.GetSignedInUserWithCacheCtx(ctx, &query); err != nil {
|
if err := h.SQLStore.GetSignedInUserWithCacheCtx(ctx, &query); err != nil {
|
||||||
reqContext.Logger.Error(
|
reqContext.Logger.Error(
|
||||||
"Failed at user signed in",
|
"Failed at user signed in",
|
||||||
"id", user.ID,
|
"id", usr.ID,
|
||||||
"org", orgID,
|
"org", orgID,
|
||||||
)
|
)
|
||||||
reqContext.JsonApiErr(401, InvalidUsernamePassword, err)
|
reqContext.JsonApiErr(401, InvalidUsernamePassword, err)
|
||||||
|
|||||||
@@ -137,10 +137,6 @@ func (m *SQLStoreMock) CreateUser(ctx context.Context, cmd user.CreateUserComman
|
|||||||
return nil, m.ExpectedError
|
return nil, m.ExpectedError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SQLStoreMock) UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error {
|
|
||||||
return m.ExpectedError
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *SQLStoreMock) SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error {
|
func (m *SQLStoreMock) SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error {
|
||||||
return m.ExpectedSetUsingOrgError
|
return m.ExpectedSetUsingOrgError
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ type Store interface {
|
|||||||
GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error
|
GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error
|
||||||
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
|
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
|
||||||
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error)
|
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error)
|
||||||
UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error
|
|
||||||
SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error
|
SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error
|
||||||
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error
|
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error
|
||||||
GetUserOrgList(ctx context.Context, query *models.GetUserOrgListQuery) error
|
GetUserOrgList(ctx context.Context, query *models.GetUserOrgListQuery) error
|
||||||
|
|||||||
@@ -82,6 +82,10 @@ type ChangeUserPasswordCommand struct {
|
|||||||
UserID int64 `json:"-"`
|
UserID int64 `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateUserLastSeenAtCommand struct {
|
||||||
|
UserID int64
|
||||||
|
}
|
||||||
|
|
||||||
func (u *User) NameOrFallback() string {
|
func (u *User) NameOrFallback() string {
|
||||||
if u.Name != "" {
|
if u.Name != "" {
|
||||||
return u.Name
|
return u.Name
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ type Service interface {
|
|||||||
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
|
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
|
||||||
Update(context.Context, *UpdateUserCommand) error
|
Update(context.Context, *UpdateUserCommand) error
|
||||||
ChangePassword(context.Context, *ChangeUserPasswordCommand) error
|
ChangePassword(context.Context, *ChangeUserPasswordCommand) error
|
||||||
|
UpdateLastSeenAt(context.Context, *UpdateUserLastSeenAtCommand) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -289,3 +289,11 @@ func (s *Service) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswo
|
|||||||
}
|
}
|
||||||
return s.sqlStore.ChangeUserPassword(ctx, q)
|
return s.sqlStore.ChangeUserPassword(ctx, q)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remove wrapper around sqlstore
|
||||||
|
func (s *Service) UpdateLastSeenAt(ctx context.Context, cmd *user.UpdateUserLastSeenAtCommand) error {
|
||||||
|
q := &models.UpdateUserLastSeenAtCommand{
|
||||||
|
UserId: cmd.UserID,
|
||||||
|
}
|
||||||
|
return s.sqlStore.UpdateUserLastSeenAt(ctx, q)
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,3 +42,7 @@ func (f *FakeUserService) Update(ctx context.Context, cmd *user.UpdateUserComman
|
|||||||
func (f *FakeUserService) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswordCommand) error {
|
func (f *FakeUserService) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswordCommand) error {
|
||||||
return f.ExpectedError
|
return f.ExpectedError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FakeUserService) UpdateLastSeenAt(ctx context.Context, cmd *user.UpdateUserLastSeenAtCommand) error {
|
||||||
|
return f.ExpectedError
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user