#1070 Clearer error message for illegal characters

This commit is contained in:
Unknwon 2015-03-26 17:11:47 -04:00
parent d76f11c6f1
commit 698b9e2acc
15 changed files with 275 additions and 167 deletions

View file

@ -157,9 +157,6 @@ org_name_been_taken = Organization name has been already taken.
team_name_been_taken = Team name has been already taken.
email_been_used = E-mail address has been already used.
ssh_key_been_used = Public key name or content has been used.
illegal_username = Your username contains illegal characters.
illegal_repo_name = Repository name contains illegal characters.
illegal_org_name = Organization name contains illegal characters.
illegal_team_name = Team name contains illegal characters.
username_password_incorrect = Username or password is not correct.
enterred_invalid_repo_name = Please make sure that the repository name you entered is correct.
@ -190,6 +187,9 @@ followers = Followers
starred = Starred
following = Following
form.name_reserved = Username '%s' is reserved.
form.name_pattern_not_allowed = Username pattern '%s' is not allowed.
[settings]
profile = Profile
password = Password
@ -290,6 +290,9 @@ create_repo = Create Repository
default_branch = Default Branch
mirror_interval = Mirror Interval (hour)
form.name_reserved = Repository name '%s' is reserved.
form.name_pattern_not_allowed = Repository name pattern '%s' is not allowed.
need_auth = Need Authorization
migrate_type = Migration Type
migrate_type_helper = This repository will be a <span class="label label-blue label-radius">mirror</span>
@ -440,6 +443,9 @@ team_name_helper = You'll use this name to mention this team in conversations.
team_desc_helper = What is this team all about?
team_permission_desc = What permission level should this team have?
form.name_reserved = Organization name '%s' is reserved.
form.name_pattern_not_allowed = Organization name pattern '%s' is not allowed.
settings = Settings
settings.options = Options
settings.full_name = Full Name

View file

@ -8,6 +8,32 @@ import (
"fmt"
)
type ErrNameReserved struct {
Name string
}
func IsErrNameReserved(err error) bool {
_, ok := err.(ErrNameReserved)
return ok
}
func (err ErrNameReserved) Error() string {
return fmt.Sprintf("name is reserved: [name: %s]", err.Name)
}
type ErrNamePatternNotAllowed struct {
Pattern string
}
func IsErrNamePatternNotAllowed(err error) bool {
_, ok := err.(ErrNamePatternNotAllowed)
return ok
}
func (err ErrNamePatternNotAllowed) Error() string {
return fmt.Sprintf("name pattern is not allowed: [pattern: %s]", err.Pattern)
}
// ____ ___
// | | \______ ___________
// | | / ___// __ \_ __ \
@ -15,6 +41,32 @@ import (
// |______//____ >\___ >__|
// \/ \/
type ErrUserAlreadyExist struct {
Name string
}
func IsErrUserAlreadyExist(err error) bool {
_, ok := err.(ErrUserAlreadyExist)
return ok
}
func (err ErrUserAlreadyExist) Error() string {
return fmt.Sprintf("user already exists: [name: %s]", err.Name)
}
type ErrEmailAlreadyUsed struct {
Email string
}
func IsErrEmailAlreadyUsed(err error) bool {
_, ok := err.(ErrEmailAlreadyUsed)
return ok
}
func (err ErrEmailAlreadyUsed) Error() string {
return fmt.Sprintf("e-mail has been used: [email: %s]", err.Email)
}
type ErrUserOwnRepos struct {
UID int64
}

View file

@ -105,23 +105,23 @@ func IsOrgEmailUsed(email string) (bool, error) {
}
// CreateOrganization creates record of a new organization.
func CreateOrganization(org, owner *User) (*User, error) {
if !IsLegalName(org.Name) {
return nil, ErrUserNameIllegal
func CreateOrganization(org, owner *User) (err error) {
if err = IsUsableName(org.Name); err != nil {
return err
}
isExist, err := IsUserExist(0, org.Name)
if err != nil {
return nil, err
return err
} else if isExist {
return nil, ErrUserAlreadyExist
return ErrUserAlreadyExist{org.Name}
}
isExist, err = IsOrgEmailUsed(org.Email)
if err != nil {
return nil, err
return err
} else if isExist {
return nil, ErrEmailAlreadyUsed
return ErrEmailAlreadyUsed{org.Email}
}
org.LowerName = strings.ToLower(org.Name)
@ -135,11 +135,11 @@ func CreateOrganization(org, owner *User) (*User, error) {
sess := x.NewSession()
defer sessionRelease(sess)
if err = sess.Begin(); err != nil {
return nil, err
return err
}
if _, err = sess.Insert(org); err != nil {
return nil, err
return fmt.Errorf("insert organization: %v", err)
}
// Create default owner team.
@ -151,7 +151,7 @@ func CreateOrganization(org, owner *User) (*User, error) {
NumMembers: 1,
}
if _, err = sess.Insert(t); err != nil {
return nil, err
return fmt.Errorf("insert owner team: %v", err)
}
// Add initial creator to organization and owner team.
@ -162,7 +162,7 @@ func CreateOrganization(org, owner *User) (*User, error) {
NumTeams: 1,
}
if _, err = sess.Insert(ou); err != nil {
return nil, err
return fmt.Errorf("insert org-user relation: %v", err)
}
tu := &TeamUser{
@ -171,14 +171,14 @@ func CreateOrganization(org, owner *User) (*User, error) {
TeamID: t.ID,
}
if _, err = sess.Insert(tu); err != nil {
return nil, err
return fmt.Errorf("insert team-user relation: %v", err)
}
if err = os.MkdirAll(UserPath(org.Name), os.ModePerm); err != nil {
return nil, err
return fmt.Errorf("create directory: %v", err)
}
return org, sess.Commit()
return sess.Commit()
}
// GetOrgByName returns organization by given name.
@ -594,9 +594,9 @@ func (t *Team) RemoveRepository(repoID int64) error {
// NewTeam creates a record of new team.
// It's caller's responsibility to assign organization ID.
func NewTeam(t *Team) error {
if !IsLegalName(t.Name) {
return ErrTeamNameIllegal
func NewTeam(t *Team) (err error) {
if err = IsUsableName(t.Name); err != nil {
return err
}
has, err := x.Id(t.OrgID).Get(new(User))
@ -670,8 +670,8 @@ func GetTeamById(teamId int64) (*Team, error) {
// UpdateTeam updates information of team.
func UpdateTeam(t *Team, authChanged bool) (err error) {
if !IsLegalName(t.Name) {
return ErrTeamNameIllegal
if err = IsUsableName(t.Name); err != nil {
return err
}
if len(t.Description) > 255 {

View file

@ -37,7 +37,6 @@ const (
var (
ErrRepoAlreadyExist = errors.New("Repository already exist")
ErrRepoFileNotExist = errors.New("Repository file does not exist")
ErrRepoNameIllegal = errors.New("Repository name contains illegal characters")
ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
ErrMirrorNotExist = errors.New("Mirror does not exist")
ErrInvalidReference = errors.New("Invalid reference specified")
@ -223,12 +222,12 @@ func (repo *Repository) DescriptionHtml() template.HTML {
}
// IsRepositoryExist returns true if the repository with given name under user has already existed.
func IsRepositoryExist(u *User, repoName string) bool {
has, _ := x.Get(&Repository{
func IsRepositoryExist(u *User, repoName string) (bool, error) {
has, err := x.Get(&Repository{
OwnerId: u.Id,
LowerName: strings.ToLower(repoName),
})
return has && com.IsDir(RepoPath(u.Name, repoName))
return has && com.IsDir(RepoPath(u.Name, repoName)), err
}
// CloneLink represents different types of clone URLs of repository.
@ -253,24 +252,27 @@ func (repo *Repository) CloneLink() (cl CloneLink, err error) {
}
var (
illegalEquals = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"}
illegalSuffixs = []string{".git", ".keys"}
reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"}
reservedPatterns = []string{"*.git", "*.keys"}
)
// IsLegalName returns false if name contains illegal characters.
func IsLegalName(repoName string) bool {
repoName = strings.ToLower(repoName)
for _, char := range illegalEquals {
if repoName == char {
return false
// IsUsableName checks if name is reserved or pattern of name is not allowed.
func IsUsableName(name string) error {
name = strings.ToLower(name)
for i := range reservedNames {
if name == reservedNames[i] {
return ErrNameReserved{name}
}
}
for _, char := range illegalSuffixs {
if strings.HasSuffix(repoName, char) {
return false
for _, pat := range reservedPatterns {
if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) ||
(pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) {
return ErrNamePatternNotAllowed{pat}
}
}
return true
return nil
}
// Mirror represents a mirror information of repository.
@ -504,11 +506,14 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, initRe
// CreateRepository creates a repository for given user or organization.
func CreateRepository(u *User, name, desc, lang, license string, isPrivate, isMirror, initReadme bool) (_ *Repository, err error) {
if !IsLegalName(name) {
return nil, ErrRepoNameIllegal
if err = IsUsableName(name); err != nil {
return nil, err
}
if IsRepositoryExist(u, name) {
has, err := IsRepositoryExist(u, name)
if err != nil {
return nil, fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
return nil, ErrRepoAlreadyExist
}
@ -619,7 +624,10 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
}
// Check if new owner has repository with same name.
if IsRepositoryExist(newOwner, repo.Name) {
has, err := IsRepositoryExist(newOwner, repo.Name)
if err != nil {
return fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
return ErrRepoAlreadyExist
}
@ -727,16 +735,22 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
}
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) {
userName = strings.ToLower(userName)
func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
oldRepoName = strings.ToLower(oldRepoName)
newRepoName = strings.ToLower(newRepoName)
if !IsLegalName(newRepoName) {
return ErrRepoNameIllegal
if err = IsUsableName(newRepoName); err != nil {
return err
}
has, err := IsRepositoryExist(u, newRepoName)
if err != nil {
return fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
return ErrRepoAlreadyExist
}
// Change repository directory name.
return os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName))
return os.Rename(RepoPath(u.LowerName, oldRepoName), RepoPath(u.LowerName, newRepoName))
}
func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err error) {
@ -1340,7 +1354,10 @@ func IsStaring(uid, repoId int64) bool {
// \/ \/
func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
if IsRepositoryExist(u, name) {
has, err := IsRepositoryExist(u, name)
if err != nil {
return nil, fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
return nil, ErrRepoAlreadyExist
}

View file

@ -36,10 +36,8 @@ const (
)
var (
ErrUserAlreadyExist = errors.New("User already exist")
ErrUserNotExist = errors.New("User does not exist")
ErrUserNotKeyOwner = errors.New("User does not the owner of public key")
ErrEmailAlreadyUsed = errors.New("E-mail already used")
ErrEmailNotExist = errors.New("E-mail does not exist")
ErrEmailNotActivated = errors.New("E-mail address has not been activated")
ErrUserNameIllegal = errors.New("User name contains illegal characters")
@ -273,23 +271,23 @@ func GetUserSalt() string {
}
// CreateUser creates record of a new user.
func CreateUser(u *User) error {
if !IsLegalName(u.Name) {
return ErrUserNameIllegal
func CreateUser(u *User) (err error) {
if err = IsUsableName(u.Name); err != nil {
return err
}
isExist, err := IsUserExist(0, u.Name)
if err != nil {
return err
} else if isExist {
return ErrUserAlreadyExist
return ErrUserAlreadyExist{u.Name}
}
isExist, err = IsEmailUsed(u.Email)
if err != nil {
return err
} else if isExist {
return ErrEmailAlreadyUsed
return ErrEmailAlreadyUsed{u.Email}
}
u.LowerName = strings.ToLower(u.Name)
@ -392,8 +390,15 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress {
// ChangeUserName changes all corresponding setting from old user name to new one.
func ChangeUserName(u *User, newUserName string) (err error) {
if !IsLegalName(newUserName) {
return ErrUserNameIllegal
if err = IsUsableName(newUserName); err != nil {
return err
}
isExist, err := IsUserExist(0, newUserName)
if err != nil {
return err
} else if isExist {
return ErrUserAlreadyExist{newUserName}
}
return os.Rename(UserPath(u.LowerName), UserPath(newUserName))
@ -405,7 +410,7 @@ func UpdateUser(u *User) error {
if err != nil {
return err
} else if has {
return ErrEmailAlreadyUsed
return ErrEmailAlreadyUsed{u.Email}
}
u.LowerName = strings.ToLower(u.Name)
@ -641,7 +646,7 @@ func AddEmailAddress(email *EmailAddress) error {
if err != nil {
return err
} else if used {
return ErrEmailAlreadyUsed
return ErrEmailAlreadyUsed{email.Email}
}
_, err = x.Insert(email)

File diff suppressed because one or more lines are too long

View file

@ -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("form.illegal_username"), USER_NEW, &form)
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("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 {

View file

@ -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)

View file

@ -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

View file

@ -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
}

View file

@ -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 {

View file

@ -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,8 +117,21 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
log.Error(4, "DeleteRepository: %v", errDelete)
}
}
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) {
ctx.Data["Title"] = ctx.Tr("new_migrate")
@ -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,8 +227,21 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
ctx.RenderWithErr(ctx.Tr("form.auth_failed", err), MIGRATE, &form)
return
}
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) {
forkId := ctx.QueryInt64("fork_id")
@ -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,8 +340,22 @@ func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
log.Error(4, "DeleteRepository: %v", errDelete)
}
}
// 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) {
var err error

View file

@ -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) {
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, nil)
return
} else if err := models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
if err == models.ErrRepoNameIllegal {
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("form.illegal_repo_name"), SETTINGS_OPTIONS, nil)
} else {
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("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &form)
default:
ctx.Handle(500, "ChangeRepositoryName", err)
}
return

View file

@ -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)
}

View file

@ -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
}