diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-11 15:03:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-11 15:03:30 +0800 |
commit | 90eb9fb889e5d3a5845576dbc63e3792f3da33f2 (patch) | |
tree | 4107df9dd446fe9a93e49cba1e59d0f721e70351 /routers | |
parent | 492e1c2fbd1b646f4428207942a9f89b56f7b6a9 (diff) | |
download | gitea-90eb9fb889e5d3a5845576dbc63e3792f3da33f2.tar.gz gitea-90eb9fb889e5d3a5845576dbc63e3792f3da33f2.zip |
Move EmailAddress & UserRedirect into models/user/ (#17607)
* Move EmailAddress into models/user/
* Fix test
* rename user_mail to user_email
* Fix test
* Move UserRedirect into models/user/
* Fix lint & test
* Fix lint
* Fix lint
* remove nolint comment
* Fix lint
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/admin/user.go | 7 | ||||
-rw-r--r-- | routers/api/v1/api.go | 9 | ||||
-rw-r--r-- | routers/api/v1/user/email.go | 26 | ||||
-rw-r--r-- | routers/api/v1/user/helper.go | 3 | ||||
-rw-r--r-- | routers/web/admin/emails.go | 3 | ||||
-rw-r--r-- | routers/web/admin/users.go | 9 | ||||
-rw-r--r-- | routers/web/repo/http.go | 3 | ||||
-rw-r--r-- | routers/web/user/auth.go | 11 | ||||
-rw-r--r-- | routers/web/user/profile.go | 3 | ||||
-rw-r--r-- | routers/web/user/setting/account.go | 19 | ||||
-rw-r--r-- | routers/web/user/setting/profile.go | 5 |
11 files changed, 54 insertions, 44 deletions
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 2d585b6040..1e4a2851c2 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/log" @@ -109,10 +110,10 @@ func CreateUser(ctx *context.APIContext) { if err := models.CreateUser(u, overwriteDefault); err != nil { if models.IsErrUserAlreadyExist(err) || - models.IsErrEmailAlreadyUsed(err) || + user_model.IsErrEmailAlreadyUsed(err) || models.IsErrNameReserved(err) || models.IsErrNameCharsNotAllowed(err) || - models.IsErrEmailInvalid(err) || + user_model.IsErrEmailInvalid(err) || models.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { @@ -245,7 +246,7 @@ func EditUser(ctx *context.APIContext) { } if err := models.UpdateUser(u); err != nil { - if models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailInvalid(err) { + if user_model.IsErrEmailAlreadyUsed(err) || user_model.IsErrEmailInvalid(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "UpdateUser", err) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 8f852a96ce..f312df23db 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -71,6 +71,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -138,9 +139,9 @@ func repoAssignment() func(ctx *context.APIContext) { owner, err = models.GetUserByName(userName) if err != nil { if models.IsErrUserNotExist(err) { - if redirectUserID, err := models.LookupUserRedirect(userName); err == nil { + if redirectUserID, err := user_model.LookupUserRedirect(userName); err == nil { context.RedirectToUser(ctx.Context, userName, redirectUserID) - } else if models.IsErrUserRedirectNotExist(err) { + } else if user_model.IsErrUserRedirectNotExist(err) { ctx.NotFound("GetUserByName", err) } else { ctx.Error(http.StatusInternalServerError, "LookupUserRedirect", err) @@ -421,10 +422,10 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) { ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":org")) if err != nil { if models.IsErrOrgNotExist(err) { - redirectUserID, err := models.LookupUserRedirect(ctx.Params(":org")) + redirectUserID, err := user_model.LookupUserRedirect(ctx.Params(":org")) if err == nil { context.RedirectToUser(ctx.Context, ctx.Params(":org"), redirectUserID) - } else if models.IsErrUserRedirectNotExist(err) { + } else if user_model.IsErrUserRedirectNotExist(err) { ctx.NotFound("GetOrgByName", err) } else { ctx.Error(http.StatusInternalServerError, "LookupUserRedirect", err) diff --git a/routers/api/v1/user/email.go b/routers/api/v1/user/email.go index 9dd35f91b6..6887c306cc 100644 --- a/routers/api/v1/user/email.go +++ b/routers/api/v1/user/email.go @@ -8,7 +8,7 @@ import ( "fmt" "net/http" - "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" @@ -28,7 +28,7 @@ func ListEmails(ctx *context.APIContext) { // "200": // "$ref": "#/responses/EmailList" - emails, err := models.GetEmailAddresses(ctx.User.ID) + emails, err := user_model.GetEmailAddresses(ctx.User.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err) return @@ -68,20 +68,20 @@ func AddEmail(ctx *context.APIContext) { return } - emails := make([]*models.EmailAddress, len(form.Emails)) + emails := make([]*user_model.EmailAddress, len(form.Emails)) for i := range form.Emails { - emails[i] = &models.EmailAddress{ + emails[i] = &user_model.EmailAddress{ UID: ctx.User.ID, Email: form.Emails[i], IsActivated: !setting.Service.RegisterEmailConfirm, } } - if err := models.AddEmailAddresses(emails); err != nil { - if models.IsErrEmailAlreadyUsed(err) { - ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email) - } else if models.IsErrEmailInvalid(err) { - errMsg := fmt.Sprintf("Email address %s invalid", err.(models.ErrEmailInvalid).Email) + if err := user_model.AddEmailAddresses(emails); err != nil { + if user_model.IsErrEmailAlreadyUsed(err) { + ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email) + } else if user_model.IsErrEmailInvalid(err) { + errMsg := fmt.Sprintf("Email address %s invalid", err.(user_model.ErrEmailInvalid).Email) ctx.Error(http.StatusUnprocessableEntity, "", errMsg) } else { ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err) @@ -119,16 +119,16 @@ func DeleteEmail(ctx *context.APIContext) { return } - emails := make([]*models.EmailAddress, len(form.Emails)) + emails := make([]*user_model.EmailAddress, len(form.Emails)) for i := range form.Emails { - emails[i] = &models.EmailAddress{ + emails[i] = &user_model.EmailAddress{ Email: form.Emails[i], UID: ctx.User.ID, } } - if err := models.DeleteEmailAddresses(emails); err != nil { - if models.IsErrEmailAddressNotExist(err) { + if err := user_model.DeleteEmailAddresses(emails); err != nil { + if user_model.IsErrEmailAddressNotExist(err) { ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err) return } diff --git a/routers/api/v1/user/helper.go b/routers/api/v1/user/helper.go index a3500e0ee6..a7c2e61405 100644 --- a/routers/api/v1/user/helper.go +++ b/routers/api/v1/user/helper.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" ) @@ -17,7 +18,7 @@ func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { user, err := models.GetUserByName(username) if err != nil { if models.IsErrUserNotExist(err) { - if redirectUserID, err2 := models.LookupUserRedirect(username); err2 == nil { + if redirectUserID, err2 := user_model.LookupUserRedirect(username); err2 == nil { context.RedirectToUser(ctx.Context, username, redirectUserID) } else { ctx.NotFound("GetUserByName", err) diff --git a/routers/web/admin/emails.go b/routers/web/admin/emails.go index 5cbe70020b..4872ecb920 100644 --- a/routers/web/admin/emails.go +++ b/routers/web/admin/emails.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -128,7 +129,7 @@ func ActivateEmail(ctx *context.Context) { if err := models.ActivateUserEmail(uid, email, activate); err != nil { log.Error("ActivateUserEmail(%v,%v,%v): %v", uid, email, activate, err) - if models.IsErrEmailAlreadyUsed(err) { + if user_model.IsErrEmailAlreadyUsed(err) { ctx.Flash.Error(ctx.Tr("admin.emails.duplicate_active")) } else { ctx.Flash.Error(ctx.Tr("admin.emails.not_updated", err)) diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index 0041f3d07c..db7fe7b36f 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -159,10 +160,10 @@ func NewUserPost(ctx *context.Context) { case models.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplUserNew, &form) - case models.IsErrEmailAlreadyUsed(err): + case user_model.IsErrEmailAlreadyUsed(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserNew, &form) - case models.IsErrEmailInvalid(err): + case user_model.IsErrEmailInvalid(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplUserNew, &form) case models.IsErrNameReserved(err): @@ -351,10 +352,10 @@ func EditUserPost(ctx *context.Context) { } if err := models.UpdateUser(u); err != nil { - if models.IsErrEmailAlreadyUsed(err) { + if user_model.IsErrEmailAlreadyUsed(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserEdit, &form) - } else if models.IsErrEmailInvalid(err) { + } else if user_model.IsErrEmailInvalid(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplUserEdit, &form) } else { diff --git a/routers/web/repo/http.go b/routers/web/repo/http.go index 7c5dcd42be..0626d08a7c 100644 --- a/routers/web/repo/http.go +++ b/routers/web/repo/http.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -112,7 +113,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) { owner, err := models.GetUserByName(username) if err != nil { if models.IsErrUserNotExist(err) { - if redirectUserID, err := models.LookupUserRedirect(username); err == nil { + if redirectUserID, err := user_model.LookupUserRedirect(username); err == nil { context.RedirectToUser(ctx, username, redirectUserID) } else { ctx.NotFound(fmt.Sprintf("User %s does not exist", username), nil) diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go index 55f304a7cb..ba37c8e3ed 100644 --- a/routers/web/user/auth.go +++ b/routers/web/user/auth.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/eventsource" @@ -181,7 +182,7 @@ func SignInPost(ctx *context.Context) { if models.IsErrUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form) log.Info("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err) - } else if models.IsErrEmailAlreadyUsed(err) { + } else if user_model.IsErrEmailAlreadyUsed(err) { ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form) log.Info("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err) } else if models.IsErrUserProhibitLogin(err) { @@ -1273,7 +1274,7 @@ func createAndHandleCreatedUser(ctx *context.Context, tpl base.TplName, form int // Optionally a template can be specified. func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User, gothUser *goth.User, allowLink bool) (ok bool) { if err := models.CreateUser(u); err != nil { - if allowLink && (models.IsErrUserAlreadyExist(err) || models.IsErrEmailAlreadyUsed(err)) { + if allowLink && (models.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err)) { if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingAuto { var user *models.User user = &models.User{Name: u.Name} @@ -1307,10 +1308,10 @@ func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{ case models.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tpl, form) - case models.IsErrEmailAlreadyUsed(err): + case user_model.IsErrEmailAlreadyUsed(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tpl, form) - case models.IsErrEmailInvalid(err): + case user_model.IsErrEmailInvalid(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tpl, form) case models.IsErrNameReserved(err): @@ -1499,7 +1500,7 @@ func ActivateEmail(ctx *context.Context) { // Verify code. if email := models.VerifyActiveEmailCode(code, emailStr); email != nil { - if err := email.Activate(); err != nil { + if err := models.ActivateEmail(email); err != nil { ctx.ServerError("ActivateEmail", err) } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index d2a8d83faa..f8fcbf6565 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -27,7 +28,7 @@ func GetUserByName(ctx *context.Context, name string) *models.User { user, err := models.GetUserByName(name) if err != nil { if models.IsErrUserNotExist(err) { - if redirectUserID, err := models.LookupUserRedirect(name); err == nil { + if redirectUserID, err := user_model.LookupUserRedirect(name); err == nil { context.RedirectToUser(ctx, name, redirectUserID) } else { ctx.NotFound("GetUserByName", err) diff --git a/routers/web/user/setting/account.go b/routers/web/user/setting/account.go index 47014dc814..5ef1c3bdc8 100644 --- a/routers/web/user/setting/account.go +++ b/routers/web/user/setting/account.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -91,7 +92,7 @@ func EmailPost(ctx *context.Context) { // Make emailaddress primary. if ctx.FormString("_method") == "PRIMARY" { - if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.FormInt64("id")}); err != nil { + if err := models.MakeEmailPrimary(&user_model.EmailAddress{ID: ctx.FormInt64("id")}); err != nil { ctx.ServerError("MakeEmailPrimary", err) return } @@ -110,7 +111,7 @@ func EmailPost(ctx *context.Context) { } id := ctx.FormInt64("id") - email, err := models.GetEmailAddressByID(ctx.User.ID, id) + email, err := user_model.GetEmailAddressByID(ctx.User.ID, id) if err != nil { log.Error("GetEmailAddressByID(%d,%d) error: %v", ctx.User.ID, id, err) ctx.Redirect(setting.AppSubURL + "/user/settings/account") @@ -174,18 +175,18 @@ func EmailPost(ctx *context.Context) { return } - email := &models.EmailAddress{ + email := &user_model.EmailAddress{ UID: ctx.User.ID, Email: form.Email, IsActivated: !setting.Service.RegisterEmailConfirm, } - if err := models.AddEmailAddress(email); err != nil { - if models.IsErrEmailAlreadyUsed(err) { + if err := user_model.AddEmailAddress(email); err != nil { + if user_model.IsErrEmailAlreadyUsed(err) { loadAccountData(ctx) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form) return - } else if models.IsErrEmailInvalid(err) { + } else if user_model.IsErrEmailInvalid(err) { loadAccountData(ctx) ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSettingsAccount, &form) @@ -212,7 +213,7 @@ func EmailPost(ctx *context.Context) { // DeleteEmail response for delete user's email func DeleteEmail(ctx *context.Context) { - if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.FormInt64("id"), UID: ctx.User.ID}); err != nil { + if err := user_model.DeleteEmailAddress(&user_model.EmailAddress{ID: ctx.FormInt64("id"), UID: ctx.User.ID}); err != nil { ctx.ServerError("DeleteEmail", err) return } @@ -258,13 +259,13 @@ func DeleteAccount(ctx *context.Context) { } func loadAccountData(ctx *context.Context) { - emlist, err := models.GetEmailAddresses(ctx.User.ID) + emlist, err := user_model.GetEmailAddresses(ctx.User.ID) if err != nil { ctx.ServerError("GetEmailAddresses", err) return } type UserEmail struct { - models.EmailAddress + user_model.EmailAddress CanBePrimary bool } pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index d181ae1720..4a09006150 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -60,7 +61,7 @@ func HandleUsernameChange(ctx *context.Context, user *models.User, newName strin switch { case models.IsErrUserAlreadyExist(err): ctx.Flash.Error(ctx.Tr("form.username_been_taken")) - case models.IsErrEmailAlreadyUsed(err): + case user_model.IsErrEmailAlreadyUsed(err): ctx.Flash.Error(ctx.Tr("form.email_been_used")) case models.IsErrNameReserved(err): ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName)) @@ -120,7 +121,7 @@ func ProfilePost(ctx *context.Context) { ctx.User.KeepActivityPrivate = form.KeepActivityPrivate ctx.User.Visibility = form.Visibility if err := models.UpdateUserSetting(ctx.User); err != nil { - if _, ok := err.(models.ErrEmailAlreadyUsed); ok { + if _, ok := err.(user_model.ErrEmailAlreadyUsed); ok { ctx.Flash.Error(ctx.Tr("form.email_been_used")) ctx.Redirect(setting.AppSubURL + "/user/settings") return |