From 87eba347459c0761e98078c95ae70c10f27c5369 Mon Sep 17 00:00:00 2001 From: Karl Persson Date: Mon, 30 Oct 2023 10:44:26 +0100 Subject: [PATCH] User: remove empty email / username check from update in service (#77347) User: remove empty email / username check from update in service --- pkg/api/user.go | 9 +++++++++ pkg/services/user/userimpl/user.go | 9 --------- pkg/services/user/userimpl/user_test.go | 10 ---------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/pkg/api/user.go b/pkg/api/user.go index 32b7db52597..a034299060f 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -221,6 +221,15 @@ func (hs *HTTPServer) handleUpdateUser(ctx context.Context, cmd user.UpdateUserC return response.Error(http.StatusForbidden, "User info cannot be updated for external Users", nil) } + if len(cmd.Login) == 0 { + cmd.Login = cmd.Email + } + + // if login is still empty both email and login field is missing + if len(cmd.Login) == 0 { + return response.Err(user.ErrEmptyUsernameAndEmail.Errorf("user cannot be created with empty username and email")) + } + if err := hs.userService.Update(ctx, &cmd); err != nil { if errors.Is(err, user.ErrCaseInsensitive) { return response.Error(http.StatusConflict, "Update would result in user login conflict", err) diff --git a/pkg/services/user/userimpl/user.go b/pkg/services/user/userimpl/user.go index fced32139d9..3f363498698 100644 --- a/pkg/services/user/userimpl/user.go +++ b/pkg/services/user/userimpl/user.go @@ -224,15 +224,6 @@ func (s *Service) GetByEmail(ctx context.Context, query *user.GetUserByEmailQuer } func (s *Service) Update(ctx context.Context, cmd *user.UpdateUserCommand) error { - if len(cmd.Login) == 0 { - cmd.Login = cmd.Email - } - - // if login is still empty both email and login field is missing - if len(cmd.Login) == 0 { - return user.ErrEmptyUsernameAndEmail.Errorf("user cannot be created with empty username and email") - } - if s.cfg.CaseInsensitiveLogin { cmd.Login = strings.ToLower(cmd.Login) cmd.Email = strings.ToLower(cmd.Email) diff --git a/pkg/services/user/userimpl/user_test.go b/pkg/services/user/userimpl/user_test.go index dc6bcbbc6e1..549515d458f 100644 --- a/pkg/services/user/userimpl/user_test.go +++ b/pkg/services/user/userimpl/user_test.go @@ -98,16 +98,6 @@ func TestUserService(t *testing.T) { require.NoError(t, err) }) - t.Run("update user should fail with empty username and password", func(t *testing.T) { - err := userService.Update(context.Background(), &user.UpdateUserCommand{ - Email: "", - Login: "", - Name: "name", - }) - - require.ErrorIs(t, err, user.ErrEmptyUsernameAndEmail) - }) - t.Run("GetByID - email conflict", func(t *testing.T) { userService.cfg.CaseInsensitiveLogin = true userStore.ExpectedError = errors.New("email conflict")