diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/admin/users.go | 15 | ||||
-rw-r--r-- | routers/api/v1/repo.go | 3 | ||||
-rw-r--r-- | routers/install.go | 2 | ||||
-rw-r--r-- | routers/org/org.go | 17 | ||||
-rw-r--r-- | routers/org/setting.go | 2 | ||||
-rw-r--r-- | routers/repo/repo.go | 70 | ||||
-rw-r--r-- | routers/repo/setting.go | 19 | ||||
-rw-r--r-- | routers/user/auth.go | 13 | ||||
-rw-r--r-- | routers/user/setting.go | 25 |
9 files changed, 97 insertions, 69 deletions
diff --git a/routers/admin/users.go b/routers/admin/users.go index ddd12a8b22..ddcca6390e 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -106,16 +106,19 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { } if err := models.CreateUser(u); err != nil { - switch err { - case models.ErrUserAlreadyExist: + switch { + case models.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form) - case models.ErrEmailAlreadyUsed: + case models.IsErrEmailAlreadyUsed(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form) - case models.ErrUserNameIllegal: + case models.IsErrNameReserved(err): + ctx.Data["Err_UserName"] = true + ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &form) + case models.IsErrNamePatternNotAllowed(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_username"), USER_NEW, &form) + ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &form) default: ctx.Handle(500, "CreateUser", err) } @@ -195,7 +198,7 @@ func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) { u.AllowGitHook = form.AllowGitHook if err := models.UpdateUser(u); err != nil { - if err == models.ErrEmailAlreadyUsed { + if models.IsErrEmailAlreadyUsed(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form) } else { diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index d7cc5955ab..170c2e90ba 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -105,7 +105,8 @@ func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoO opt.Gitignore, opt.License, opt.Private, false, opt.AutoInit) if err != nil { if err == models.ErrRepoAlreadyExist || - err == models.ErrRepoNameIllegal { + models.IsErrNameReserved(err) || + models.IsErrNamePatternNotAllowed(err) { ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL}) } else { log.Error(4, "CreateRepository: %v", err) diff --git a/routers/install.go b/routers/install.go index f5dc0c580b..6da611286f 100644 --- a/routers/install.go +++ b/routers/install.go @@ -239,7 +239,7 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { // Create admin account. if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd, IsAdmin: true, IsActive: true}); err != nil { - if err != models.ErrUserAlreadyExist { + if !models.IsErrUserAlreadyExist(err) { setting.InstallLock = false ctx.Data["Err_AdminName"] = true ctx.Data["Err_AdminEmail"] = true diff --git a/routers/org/org.go b/routers/org/org.go index ab589832d1..ed4f2abdea 100644 --- a/routers/org/org.go +++ b/routers/org/org.go @@ -65,19 +65,22 @@ func CreatePost(ctx *middleware.Context, form auth.CreateOrgForm) { } var err error - if org, err = models.CreateOrganization(org, ctx.User); err != nil { - switch err { - case models.ErrUserAlreadyExist: + if err = models.CreateOrganization(org, ctx.User); err != nil { + switch { + case models.IsErrUserAlreadyExist(err): ctx.Data["Err_OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form) - case models.ErrEmailAlreadyUsed: + case models.IsErrEmailAlreadyUsed(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), CREATE, &form) - case models.ErrUserNameIllegal: + case models.IsErrNameReserved(err): ctx.Data["Err_OrgName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_org_name"), CREATE, &form) + ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form) + case models.IsErrNamePatternNotAllowed(err): + ctx.Data["Err_OrgName"] = true + ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form) default: - ctx.Handle(500, "CreateUser", err) + ctx.Handle(500, "CreateOrganization", err) } return } diff --git a/routers/org/setting.go b/routers/org/setting.go index 8bc6a2a96a..7a76199502 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -68,7 +68,7 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) { org.Avatar = base.EncodeMd5(form.Avatar) org.AvatarEmail = form.Avatar if err := models.UpdateUser(org); err != nil { - if err == models.ErrEmailAlreadyUsed { + if models.IsErrEmailAlreadyUsed(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_OPTIONS, &form) } else { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 73c9277d94..5e6a2e0d20 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -110,14 +110,6 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return - } else if err == models.ErrRepoAlreadyExist { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) - return - } else if err == models.ErrRepoNameIllegal { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form) - return } if repo != nil { @@ -125,7 +117,20 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { log.Error(4, "DeleteRepository: %v", errDelete) } } - ctx.Handle(500, "CreatePost", err) + + switch { + case err == models.ErrRepoAlreadyExist: + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) + case models.IsErrNameReserved(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form) + case models.IsErrNamePatternNotAllowed(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form) + default: + ctx.Handle(500, "CreatePost", err) + } } func Migrate(ctx *middleware.Context) { @@ -209,14 +214,6 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName) return - } else if err == models.ErrRepoAlreadyExist { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form) - return - } else if err == models.ErrRepoNameIllegal { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), MIGRATE, &form) - return } if repo != nil { @@ -230,7 +227,20 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.RenderWithErr(ctx.Tr("form.auth_failed", err), MIGRATE, &form) return } - ctx.Handle(500, "MigratePost", err) + + switch { + case err == models.ErrRepoAlreadyExist: + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form) + case models.IsErrNameReserved(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), MIGRATE, &form) + case models.IsErrNamePatternNotAllowed(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), MIGRATE, &form) + default: + ctx.Handle(500, "MigratePost", err) + } } func getForkRepository(ctx *middleware.Context) (*models.Repository, error) { @@ -323,14 +333,6 @@ func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) { log.Trace("Repository forked: %s/%s", ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return - } else if err == models.ErrRepoAlreadyExist { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), FORK, &form) - return - } else if err == models.ErrRepoNameIllegal { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form) - return } if repo != nil { @@ -338,7 +340,21 @@ func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) { log.Error(4, "DeleteRepository: %v", errDelete) } } - ctx.Handle(500, "ForkPost", err) + + // FIXME: merge this with other 2 error handling in to one. + switch { + case err == models.ErrRepoAlreadyExist: + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), FORK, &form) + case models.IsErrNameReserved(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form) + case models.IsErrNamePatternNotAllowed(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form) + default: + ctx.Handle(500, "ForkPost", err) + } } func Action(ctx *middleware.Context) { diff --git a/routers/repo/setting.go b/routers/repo/setting.go index be21405bf6..cdcade67c0 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -53,15 +53,18 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) { newRepoName := form.RepoName // Check if repository name has been changed. if ctx.Repo.Repository.Name != newRepoName { - if models.IsRepositoryExist(ctx.Repo.Owner, newRepoName) { - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, nil) - return - } else if err := models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil { - if err == models.ErrRepoNameIllegal { + if err := models.ChangeRepositoryName(ctx.Repo.Owner, ctx.Repo.Repository.Name, newRepoName); err != nil { + switch { + case err == models.ErrRepoAlreadyExist: + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form) + case models.IsErrNameReserved(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &form) + case models.IsErrNamePatternNotAllowed(err): ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), SETTINGS_OPTIONS, nil) - } else { + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &form) + default: ctx.Handle(500, "ChangeRepositoryName", err) } return diff --git a/routers/user/auth.go b/routers/user/auth.go index 5dacaf8c79..52675a777d 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -249,16 +249,19 @@ func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.Registe } if err := models.CreateUser(u); err != nil { - switch err { - case models.ErrUserAlreadyExist: + switch { + case models.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form) - case models.ErrEmailAlreadyUsed: + case models.IsErrEmailAlreadyUsed(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form) - case models.ErrUserNameIllegal: + case models.IsErrNameReserved(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SIGNUP, &form) + ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), SIGNUP, &form) + case models.IsErrNamePatternNotAllowed(err): + ctx.Data["Err_UserName"] = true + ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SIGNUP, &form) default: ctx.Handle(500, "CreateUser", err) } diff --git a/routers/user/setting.go b/routers/user/setting.go index 20e6c06035..b31e93a63f 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -50,21 +50,20 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { // Check if user name has been changed. if ctx.User.Name != form.UserName { - isExist, err := models.IsUserExist(ctx.User.Id, form.UserName) - if err != nil { - ctx.Handle(500, "IsUserExist", err) - return - } else if isExist { - ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form) - return - } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil { - switch err { - case models.ErrUserNameIllegal: - ctx.Flash.Error(ctx.Tr("form.illegal_username")) + if err := models.ChangeUserName(ctx.User, form.UserName); err != nil { + switch { + case models.IsErrUserAlreadyExist(err): + ctx.Flash.Error(ctx.Tr("form.username_been_taken")) ctx.Redirect(setting.AppSubUrl + "/user/settings") - case models.ErrEmailAlreadyUsed: + case models.IsErrEmailAlreadyUsed(err): ctx.Flash.Error(ctx.Tr("form.email_been_used")) ctx.Redirect(setting.AppSubUrl + "/user/settings") + case models.IsErrNameReserved(err): + ctx.Flash.Error(ctx.Tr("user.form.name_reserved")) + ctx.Redirect(setting.AppSubUrl + "/user/settings") + case models.IsErrNamePatternNotAllowed(err): + ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed")) + ctx.Redirect(setting.AppSubUrl + "/user/settings") default: ctx.Handle(500, "ChangeUserName", err) } @@ -204,7 +203,7 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) { } if err := models.AddEmailAddress(e); err != nil { - if err == models.ErrEmailAlreadyUsed { + if models.IsErrEmailAlreadyUsed(err) { ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form) return } |