@@ -15,7 +15,6 @@ import (
15
15
"github.com/grafana/grafana/pkg/services/auth/identity"
16
16
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
17
17
"github.com/grafana/grafana/pkg/services/login"
18
- "github.com/grafana/grafana/pkg/services/notifications"
19
18
"github.com/grafana/grafana/pkg/services/org"
20
19
"github.com/grafana/grafana/pkg/services/team"
21
20
tempuser "github.com/grafana/grafana/pkg/services/temp_user"
@@ -245,25 +244,22 @@ func (hs *HTTPServer) handleUpdateUser(ctx context.Context, cmd user.UpdateUserC
245
244
usr , err := hs .userService .GetByID (ctx , & query )
246
245
if err != nil {
247
246
if errors .Is (err , user .ErrUserNotFound ) {
248
- return response .Error (http .StatusNotFound , user .ErrUserNotFound .Error (), nil )
247
+ return response .Error (http .StatusNotFound , user .ErrUserNotFound .Error (), err )
249
248
}
250
249
return response .Error (http .StatusInternalServerError , "Failed to get user" , err )
251
250
}
252
251
253
252
if len (cmd .Email ) != 0 && usr .Email != cmd .Email {
254
- // Email is being updated
255
- newEmail , err := ValidateAndNormalizeEmail (cmd .Email )
253
+ normalized , err := ValidateAndNormalizeEmail (cmd .Email )
256
254
if err != nil {
257
255
return response .Error (http .StatusBadRequest , "Invalid email address" , err )
258
256
}
259
-
260
- return hs .verifyEmailUpdate (ctx , newEmail , user .EmailUpdateAction , usr )
257
+ return hs .verifyEmailUpdate (ctx , normalized , user .EmailUpdateAction , usr )
261
258
}
262
259
if len (cmd .Login ) != 0 && usr .Login != cmd .Login {
263
- // Username is being updated. If it's an email, go through the email verification flow
264
- newEmailLogin , err := ValidateAndNormalizeEmail (cmd .Login )
265
- if err == nil && newEmailLogin != usr .Email {
266
- return hs .verifyEmailUpdate (ctx , newEmailLogin , user .LoginUpdateAction , usr )
260
+ normalized , err := ValidateAndNormalizeEmail (cmd .Login )
261
+ if err == nil && usr .Email != normalized {
262
+ return hs .verifyEmailUpdate (ctx , cmd .Login , user .LoginUpdateAction , usr )
267
263
}
268
264
}
269
265
}
@@ -279,55 +275,12 @@ func (hs *HTTPServer) handleUpdateUser(ctx context.Context, cmd user.UpdateUserC
279
275
}
280
276
281
277
func (hs * HTTPServer ) verifyEmailUpdate (ctx context.Context , email string , field user.UpdateEmailActionType , usr * user.User ) response.Response {
282
- // Verify that email is not already being used
283
- query := user.GetUserByLoginQuery {LoginOrEmail : email }
284
- existingUsr , err := hs .userService .GetByLogin (ctx , & query )
285
- if err != nil && ! errors .Is (err , user .ErrUserNotFound ) {
286
- return response .Error (http .StatusInternalServerError , "Failed to validate if email is already in use" , err )
287
- }
288
- if existingUsr != nil {
289
- return response .Error (http .StatusConflict , "Email is already being used" , nil )
290
- }
291
-
292
- // Invalidate any pending verifications for this user
293
- expireCmd := tempuser.ExpirePreviousVerificationsCommand {InvitedByUserID : usr .ID }
294
- err = hs .tempUserService .ExpirePreviousVerifications (ctx , & expireCmd )
295
- if err != nil {
296
- return response .Error (http .StatusInternalServerError , "Could not invalidate pending email verifications" , err )
297
- }
298
-
299
- code , err := util .GetRandomString (20 )
300
- if err != nil {
301
- return response .Error (http .StatusInternalServerError , "Failed to generate random string" , err )
302
- }
303
-
304
- tempCmd := tempuser.CreateTempUserCommand {
305
- OrgID : - 1 ,
278
+ if err := hs .userVerifier .VerifyEmail (ctx , user.VerifyEmailCommand {
279
+ User : * usr ,
306
280
Email : email ,
307
- Code : code ,
308
- Status : tempuser .TmpUserEmailUpdateStarted ,
309
- // used to fetch the User in the second step of the verification flow
310
- InvitedByUserID : usr .ID ,
311
- // used to determine if the user was updating their email or username in the second step of the verification flow
312
- Name : string (field ),
313
- }
314
-
315
- tempUser , err := hs .tempUserService .CreateTempUser (ctx , & tempCmd )
316
- if err != nil {
317
- return response .Error (http .StatusInternalServerError , "Failed to create email change" , err )
318
- }
319
-
320
- emailCmd := notifications.SendVerifyEmailCommand {Email : tempUser .Email , Code : tempUser .Code , User : usr }
321
- err = hs .NotificationService .SendVerificationEmail (ctx , & emailCmd )
322
- if err != nil {
323
- return response .Error (http .StatusInternalServerError , "Failed to send verification email" , err )
324
- }
325
-
326
- // Record email as sent
327
- emailSentCmd := tempuser.UpdateTempUserWithEmailSentCommand {Code : tempUser .Code }
328
- err = hs .tempUserService .UpdateTempUserWithEmailSent (ctx , & emailSentCmd )
329
- if err != nil {
330
- return response .Error (http .StatusInternalServerError , "Failed to record verification email" , err )
281
+ Action : field ,
282
+ }); err != nil {
283
+ return response .ErrOrFallback (http .StatusInternalServerError , "Failed to generate email verification" , err )
331
284
}
332
285
333
286
return response .Success ("Email sent for verification" )
0 commit comments