Skip to content

Commit 6e2ece4

Browse files
committed
Refactored user creation
There was common code to create a user and display the correct error message. And after the creation the only user should be an admin and if enabled a confirmation email should be sent. This common code is now abstracted into two functions and a helper function to call both. Signed-off-by: Martin Michaelis <[email protected]>
1 parent a3366c4 commit 6e2ece4

File tree

2 files changed

+50
-92
lines changed

2 files changed

+50
-92
lines changed

routers/user/auth.go

+46-51
Original file line numberDiff line numberDiff line change
@@ -810,49 +810,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
810810
LoginName: gothUser.(goth.User).UserID,
811811
}
812812

813-
if err := models.CreateUser(u); err != nil {
814-
switch {
815-
case models.IsErrUserAlreadyExist(err):
816-
ctx.Data["Err_UserName"] = true
817-
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplLinkAccount, &form)
818-
case models.IsErrEmailAlreadyUsed(err):
819-
ctx.Data["Err_Email"] = true
820-
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
821-
case models.IsErrNameReserved(err):
822-
ctx.Data["Err_UserName"] = true
823-
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
824-
case models.IsErrNamePatternNotAllowed(err):
825-
ctx.Data["Err_UserName"] = true
826-
ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplLinkAccount, &form)
827-
default:
828-
ctx.ServerError("CreateUser", err)
829-
}
830-
return
831-
}
832-
log.Trace("Account created: %s", u.Name)
833-
834-
// Auto-set admin for the only user.
835-
if models.CountUsers() == 1 {
836-
u.IsAdmin = true
837-
u.IsActive = true
838-
u.SetLastLogin()
839-
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
840-
ctx.ServerError("UpdateUser", err)
841-
return
842-
}
843-
}
844-
845-
// Send confirmation email
846-
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
847-
models.SendActivateAccountMail(ctx.Context, u)
848-
ctx.Data["IsSendRegisterMail"] = true
849-
ctx.Data["Email"] = u.Email
850-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
851-
ctx.HTML(200, TplActivate)
852-
853-
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
854-
log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
855-
}
813+
if !createAndHandleCreatedUser(ctx, tplLinkAccount, form, u) {
814+
// error already handled
856815
return
857816
}
858817

@@ -943,27 +902,64 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
943902
Passwd: form.Password,
944903
IsActive: !setting.Service.RegisterEmailConfirm,
945904
}
905+
906+
if !createAndHandleCreatedUser(ctx, tplSignUp, form, u) {
907+
// error already handled
908+
return
909+
}
910+
911+
ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
912+
handleSignInFull(ctx, u, false, true)
913+
}
914+
915+
// CreateAndHandleCreatedUser calls createUserInContext and
916+
// then handleUserCreated.
917+
func createAndHandleCreatedUser(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User) (ok bool) {
918+
ok = createUserInContext(ctx, tpl, form, u)
919+
if !ok {
920+
return
921+
}
922+
ok = handleUserCreated(ctx, u)
923+
return
924+
}
925+
926+
// CreateUserInContext creates a user and handles errors within a given context.
927+
// Optionaly a template can be specified.
928+
func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User) (ok bool) {
946929
if err := models.CreateUser(u); err != nil {
930+
// handle error without template
931+
if len(tpl) == 0 {
932+
ctx.ServerError("CreateUser", err)
933+
return
934+
}
935+
936+
// handle error with template
947937
switch {
948938
case models.IsErrUserAlreadyExist(err):
949939
ctx.Data["Err_UserName"] = true
950-
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUp, &form)
940+
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tpl, &form)
951941
case models.IsErrEmailAlreadyUsed(err):
952942
ctx.Data["Err_Email"] = true
953-
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
943+
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tpl, &form)
954944
case models.IsErrNameReserved(err):
955945
ctx.Data["Err_UserName"] = true
956-
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)
946+
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, &form)
957947
case models.IsErrNamePatternNotAllowed(err):
958948
ctx.Data["Err_UserName"] = true
959-
ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUp, &form)
949+
ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, &form)
960950
default:
961951
ctx.ServerError("CreateUser", err)
962952
}
963953
return
964954
}
965955
log.Trace("Account created: %s", u.Name)
956+
return true
957+
}
966958

959+
// HandleUserCreated does additional steps after a new user is created.
960+
// It auto-sets admin for the only user and
961+
// sends a confirmation email if required.
962+
func handleUserCreated(ctx *context.Context, u *models.User) (ok bool) {
967963
// Auto-set admin for the only user.
968964
if models.CountUsers() == 1 {
969965
u.IsAdmin = true
@@ -975,8 +971,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
975971
}
976972
}
977973

978-
// Send confirmation email, no need for social account.
979-
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
974+
// Send confirmation email
975+
if !u.IsActive && u.ID > 1 {
980976
models.SendActivateAccountMail(ctx.Context, u)
981977
ctx.Data["IsSendRegisterMail"] = true
982978
ctx.Data["Email"] = u.Email
@@ -989,8 +985,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
989985
return
990986
}
991987

992-
ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
993-
handleSignInFull(ctx, u, false, true)
988+
return true
994989
}
995990

996991
// Activate render activate user page

routers/user/auth_openid.go

+4-41
Original file line numberDiff line numberDiff line change
@@ -366,33 +366,16 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
366366
return
367367
}
368368

369-
// TODO: abstract a finalizeSignUp function ?
370369
u := &models.User{
371370
Name: form.UserName,
372371
Email: form.Email,
373372
Passwd: password,
374373
IsActive: !setting.Service.RegisterEmailConfirm,
375374
}
376-
if err := models.CreateUser(u); err != nil {
377-
switch {
378-
case models.IsErrUserAlreadyExist(err):
379-
ctx.Data["Err_UserName"] = true
380-
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSignUpOID, &form)
381-
case models.IsErrEmailAlreadyUsed(err):
382-
ctx.Data["Err_Email"] = true
383-
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUpOID, &form)
384-
case models.IsErrNameReserved(err):
385-
ctx.Data["Err_UserName"] = true
386-
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUpOID, &form)
387-
case models.IsErrNamePatternNotAllowed(err):
388-
ctx.Data["Err_UserName"] = true
389-
ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSignUpOID, &form)
390-
default:
391-
ctx.ServerError("CreateUser", err)
392-
}
375+
if !createUserInContext(ctx, tplSignUpOID, form, u) {
376+
// error already handled
393377
return
394378
}
395-
log.Trace("Account created: %s", u.Name)
396379

397380
// add OpenID for the user
398381
userOID := &models.UserOpenID{UID: u.ID, URI: oid}
@@ -405,28 +388,8 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
405388
return
406389
}
407390

408-
// Auto-set admin for the only user.
409-
if models.CountUsers() == 1 {
410-
u.IsAdmin = true
411-
u.IsActive = true
412-
u.SetLastLogin()
413-
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
414-
ctx.ServerError("UpdateUser", err)
415-
return
416-
}
417-
}
418-
419-
// Send confirmation email, no need for social account.
420-
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
421-
models.SendActivateAccountMail(ctx.Context, u)
422-
ctx.Data["IsSendRegisterMail"] = true
423-
ctx.Data["Email"] = u.Email
424-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
425-
ctx.HTML(200, TplActivate)
426-
427-
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
428-
log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
429-
}
391+
if !handleUserCreated(ctx, u) {
392+
// error already handled
430393
return
431394
}
432395

0 commit comments

Comments
 (0)