diff --git a/pkg/services/loginattempt/loginattemptimpl/login_attempt.go b/pkg/services/loginattempt/loginattemptimpl/login_attempt.go index 76a07d6305e..e6c5366a860 100644 --- a/pkg/services/loginattempt/loginattemptimpl/login_attempt.go +++ b/pkg/services/loginattempt/loginattemptimpl/login_attempt.go @@ -53,10 +53,11 @@ func (s *Service) Add(ctx context.Context, username, IPAddress string) error { return nil } - return s.store.CreateLoginAttempt(ctx, CreateLoginAttemptCommand{ + _, err := s.store.CreateLoginAttempt(ctx, CreateLoginAttemptCommand{ Username: username, IpAddress: IPAddress, }) + return err } func (s *Service) Reset(ctx context.Context, username string) error { diff --git a/pkg/services/loginattempt/loginattemptimpl/login_attempt_test.go b/pkg/services/loginattempt/loginattemptimpl/login_attempt_test.go index e6ba7c31fe4..8ec85223c10 100644 --- a/pkg/services/loginattempt/loginattemptimpl/login_attempt_test.go +++ b/pkg/services/loginattempt/loginattemptimpl/login_attempt_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" + "github.com/grafana/grafana/pkg/services/loginattempt" "github.com/grafana/grafana/pkg/setting" ) @@ -90,8 +91,8 @@ func (f fakeStore) GetUserLoginAttemptCount(ctx context.Context, query GetUserLo return f.ExpectedCount, f.ExpectedErr } -func (f fakeStore) CreateLoginAttempt(ctx context.Context, command CreateLoginAttemptCommand) error { - return f.ExpectedErr +func (f fakeStore) CreateLoginAttempt(ctx context.Context, command CreateLoginAttemptCommand) (loginattempt.LoginAttempt, error) { + return loginattempt.LoginAttempt{}, f.ExpectedErr } func (f fakeStore) DeleteOldLoginAttempts(ctx context.Context, command DeleteOldLoginAttemptsCommand) (int64, error) { diff --git a/pkg/services/loginattempt/loginattemptimpl/models.go b/pkg/services/loginattempt/loginattemptimpl/models.go index 85fc944f577..f235cd3d308 100644 --- a/pkg/services/loginattempt/loginattemptimpl/models.go +++ b/pkg/services/loginattempt/loginattemptimpl/models.go @@ -2,15 +2,11 @@ package loginattemptimpl import ( "time" - - "github.com/grafana/grafana/pkg/services/loginattempt" ) type CreateLoginAttemptCommand struct { Username string IpAddress string - - Result loginattempt.LoginAttempt } type GetUserLoginAttemptCountQuery struct { diff --git a/pkg/services/loginattempt/loginattemptimpl/store.go b/pkg/services/loginattempt/loginattemptimpl/store.go index 75456d7a9f8..f273e6f5ef0 100644 --- a/pkg/services/loginattempt/loginattemptimpl/store.go +++ b/pkg/services/loginattempt/loginattemptimpl/store.go @@ -14,14 +14,14 @@ type xormStore struct { } type store interface { - CreateLoginAttempt(ctx context.Context, cmd CreateLoginAttemptCommand) error + CreateLoginAttempt(ctx context.Context, cmd CreateLoginAttemptCommand) (loginattempt.LoginAttempt, error) DeleteOldLoginAttempts(ctx context.Context, cmd DeleteOldLoginAttemptsCommand) (int64, error) DeleteLoginAttempts(ctx context.Context, cmd DeleteLoginAttemptsCommand) error GetUserLoginAttemptCount(ctx context.Context, query GetUserLoginAttemptCountQuery) (int64, error) } -func (xs *xormStore) CreateLoginAttempt(ctx context.Context, cmd CreateLoginAttemptCommand) error { - return xs.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error { +func (xs *xormStore) CreateLoginAttempt(ctx context.Context, cmd CreateLoginAttemptCommand) (result loginattempt.LoginAttempt, err error) { + err = xs.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error { loginAttempt := loginattempt.LoginAttempt{ Username: cmd.Username, IpAddress: cmd.IpAddress, @@ -32,10 +32,11 @@ func (xs *xormStore) CreateLoginAttempt(ctx context.Context, cmd CreateLoginAtte return err } - cmd.Result = loginAttempt + result = loginAttempt return nil }) + return result, err } func (xs *xormStore) DeleteOldLoginAttempts(ctx context.Context, cmd DeleteOldLoginAttemptsCommand) (int64, error) { diff --git a/pkg/services/loginattempt/loginattemptimpl/store_test.go b/pkg/services/loginattempt/loginattemptimpl/store_test.go index 12f4ed58bfb..45c27c0c90f 100644 --- a/pkg/services/loginattempt/loginattemptimpl/store_test.go +++ b/pkg/services/loginattempt/loginattemptimpl/store_test.go @@ -53,21 +53,21 @@ func TestIntegrationLoginAttemptsQuery(t *testing.T) { now: func() time.Time { return mockTime }, } - err := s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ + _, err := s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ Username: user, IpAddress: "192.168.0.1", }) require.Nil(t, err) mockTime = timePlusOneMinute - err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ + _, err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ Username: user, IpAddress: "192.168.0.1", }) require.Nil(t, err) mockTime = timePlusTwoMinutes - err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ + _, err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ Username: user, IpAddress: "192.168.0.1", }) @@ -118,21 +118,21 @@ func TestIntegrationLoginAttemptsDelete(t *testing.T) { now: func() time.Time { return mockTime }, } - err := s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ + _, err := s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ Username: user, IpAddress: "192.168.0.1", }) require.Nil(t, err) mockTime = timePlusOneMinute - err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ + _, err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ Username: user, IpAddress: "192.168.0.1", }) require.Nil(t, err) mockTime = timePlusTwoMinutes - err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ + _, err = s.CreateLoginAttempt(context.Background(), CreateLoginAttemptCommand{ Username: user, IpAddress: "192.168.0.1", })