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

View File

@ -8,6 +8,32 @@ import (
"fmt" "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 { type ErrUserOwnRepos struct {
UID int64 UID int64
} }

View File

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

View File

@ -37,7 +37,6 @@ const (
var ( var (
ErrRepoAlreadyExist = errors.New("Repository already exist") ErrRepoAlreadyExist = errors.New("Repository already exist")
ErrRepoFileNotExist = errors.New("Repository file does not exist") ErrRepoFileNotExist = errors.New("Repository file does not exist")
ErrRepoNameIllegal = errors.New("Repository name contains illegal characters")
ErrRepoFileNotLoaded = errors.New("Repository file not loaded") ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
ErrMirrorNotExist = errors.New("Mirror does not exist") ErrMirrorNotExist = errors.New("Mirror does not exist")
ErrInvalidReference = errors.New("Invalid reference specified") 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. // IsRepositoryExist returns true if the repository with given name under user has already existed.
func IsRepositoryExist(u *User, repoName string) bool { func IsRepositoryExist(u *User, repoName string) (bool, error) {
has, _ := x.Get(&Repository{ has, err := x.Get(&Repository{
OwnerId: u.Id, OwnerId: u.Id,
LowerName: strings.ToLower(repoName), 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. // CloneLink represents different types of clone URLs of repository.
@ -253,24 +252,27 @@ func (repo *Repository) CloneLink() (cl CloneLink, err error) {
} }
var ( var (
illegalEquals = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"} reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"}
illegalSuffixs = []string{".git", ".keys"} reservedPatterns = []string{"*.git", "*.keys"}
) )
// IsLegalName returns false if name contains illegal characters. // IsUsableName checks if name is reserved or pattern of name is not allowed.
func IsLegalName(repoName string) bool { func IsUsableName(name string) error {
repoName = strings.ToLower(repoName) name = strings.ToLower(name)
for _, char := range illegalEquals { for i := range reservedNames {
if repoName == char { if name == reservedNames[i] {
return false return ErrNameReserved{name}
} }
} }
for _, char := range illegalSuffixs {
if strings.HasSuffix(repoName, char) { for _, pat := range reservedPatterns {
return false 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. // 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. // 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) { func CreateRepository(u *User, name, desc, lang, license string, isPrivate, isMirror, initReadme bool) (_ *Repository, err error) {
if !IsLegalName(name) { if err = IsUsableName(name); err != nil {
return nil, ErrRepoNameIllegal 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 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. // 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 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. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) { func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
userName = strings.ToLower(userName)
oldRepoName = strings.ToLower(oldRepoName) oldRepoName = strings.ToLower(oldRepoName)
newRepoName = strings.ToLower(newRepoName) newRepoName = strings.ToLower(newRepoName)
if !IsLegalName(newRepoName) { if err = IsUsableName(newRepoName); err != nil {
return ErrRepoNameIllegal 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. // 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) { 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) { 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 return nil, ErrRepoAlreadyExist
} }

View File

@ -36,10 +36,8 @@ const (
) )
var ( var (
ErrUserAlreadyExist = errors.New("User already exist")
ErrUserNotExist = errors.New("User does not exist") ErrUserNotExist = errors.New("User does not exist")
ErrUserNotKeyOwner = errors.New("User does not the owner of public key") 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") ErrEmailNotExist = errors.New("E-mail does not exist")
ErrEmailNotActivated = errors.New("E-mail address has not been activated") ErrEmailNotActivated = errors.New("E-mail address has not been activated")
ErrUserNameIllegal = errors.New("User name contains illegal characters") ErrUserNameIllegal = errors.New("User name contains illegal characters")
@ -273,23 +271,23 @@ func GetUserSalt() string {
} }
// CreateUser creates record of a new user. // CreateUser creates record of a new user.
func CreateUser(u *User) error { func CreateUser(u *User) (err error) {
if !IsLegalName(u.Name) { if err = IsUsableName(u.Name); err != nil {
return ErrUserNameIllegal return err
} }
isExist, err := IsUserExist(0, u.Name) isExist, err := IsUserExist(0, u.Name)
if err != nil { if err != nil {
return err return err
} else if isExist { } else if isExist {
return ErrUserAlreadyExist return ErrUserAlreadyExist{u.Name}
} }
isExist, err = IsEmailUsed(u.Email) isExist, err = IsEmailUsed(u.Email)
if err != nil { if err != nil {
return err return err
} else if isExist { } else if isExist {
return ErrEmailAlreadyUsed return ErrEmailAlreadyUsed{u.Email}
} }
u.LowerName = strings.ToLower(u.Name) 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. // ChangeUserName changes all corresponding setting from old user name to new one.
func ChangeUserName(u *User, newUserName string) (err error) { func ChangeUserName(u *User, newUserName string) (err error) {
if !IsLegalName(newUserName) { if err = IsUsableName(newUserName); err != nil {
return ErrUserNameIllegal 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)) return os.Rename(UserPath(u.LowerName), UserPath(newUserName))
@ -405,7 +410,7 @@ func UpdateUser(u *User) error {
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
return ErrEmailAlreadyUsed return ErrEmailAlreadyUsed{u.Email}
} }
u.LowerName = strings.ToLower(u.Name) u.LowerName = strings.ToLower(u.Name)
@ -641,7 +646,7 @@ func AddEmailAddress(email *EmailAddress) error {
if err != nil { if err != nil {
return err return err
} else if used { } else if used {
return ErrEmailAlreadyUsed return ErrEmailAlreadyUsed{email.Email}
} }
_, err = x.Insert(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 { if err := models.CreateUser(u); err != nil {
switch err { switch {
case models.ErrUserAlreadyExist: case models.IsErrUserAlreadyExist(err):
ctx.Data["Err_UserName"] = true ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form) ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form)
case models.ErrEmailAlreadyUsed: case models.IsErrEmailAlreadyUsed(err):
ctx.Data["Err_Email"] = true ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form)
case models.ErrUserNameIllegal: case models.IsErrNameReserved(err):
ctx.Data["Err_UserName"] = true 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: default:
ctx.Handle(500, "CreateUser", err) ctx.Handle(500, "CreateUser", err)
} }
@ -195,7 +198,7 @@ func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) {
u.AllowGitHook = form.AllowGitHook u.AllowGitHook = form.AllowGitHook
if err := models.UpdateUser(u); err != nil { if err := models.UpdateUser(u); err != nil {
if err == models.ErrEmailAlreadyUsed { if models.IsErrEmailAlreadyUsed(err) {
ctx.Data["Err_Email"] = true ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form)
} else { } 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) opt.Gitignore, opt.License, opt.Private, false, opt.AutoInit)
if err != nil { if err != nil {
if err == models.ErrRepoAlreadyExist || if err == models.ErrRepoAlreadyExist ||
err == models.ErrRepoNameIllegal { models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL}) ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL})
} else { } else {
log.Error(4, "CreateRepository: %v", err) log.Error(4, "CreateRepository: %v", err)

View File

@ -239,7 +239,7 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
// Create admin account. // Create admin account.
if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd, if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
IsAdmin: true, IsActive: true}); err != nil { IsAdmin: true, IsActive: true}); err != nil {
if err != models.ErrUserAlreadyExist { if !models.IsErrUserAlreadyExist(err) {
setting.InstallLock = false setting.InstallLock = false
ctx.Data["Err_AdminName"] = true ctx.Data["Err_AdminName"] = true
ctx.Data["Err_AdminEmail"] = true ctx.Data["Err_AdminEmail"] = true

View File

@ -65,19 +65,22 @@ func CreatePost(ctx *middleware.Context, form auth.CreateOrgForm) {
} }
var err error var err error
if org, err = models.CreateOrganization(org, ctx.User); err != nil { if err = models.CreateOrganization(org, ctx.User); err != nil {
switch err { switch {
case models.ErrUserAlreadyExist: case models.IsErrUserAlreadyExist(err):
ctx.Data["Err_OrgName"] = true ctx.Data["Err_OrgName"] = true
ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form) ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form)
case models.ErrEmailAlreadyUsed: case models.IsErrEmailAlreadyUsed(err):
ctx.Data["Err_Email"] = true ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), CREATE, &form) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), CREATE, &form)
case models.ErrUserNameIllegal: case models.IsErrNameReserved(err):
ctx.Data["Err_OrgName"] = true 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: default:
ctx.Handle(500, "CreateUser", err) ctx.Handle(500, "CreateOrganization", err)
} }
return return
} }

View File

@ -68,7 +68,7 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
org.Avatar = base.EncodeMd5(form.Avatar) org.Avatar = base.EncodeMd5(form.Avatar)
org.AvatarEmail = form.Avatar org.AvatarEmail = form.Avatar
if err := models.UpdateUser(org); err != nil { if err := models.UpdateUser(org); err != nil {
if err == models.ErrEmailAlreadyUsed { if models.IsErrEmailAlreadyUsed(err) {
ctx.Data["Err_Email"] = true ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_OPTIONS, &form) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_OPTIONS, &form)
} else { } 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) log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
return 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 { if repo != nil {
@ -125,8 +117,21 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
log.Error(4, "DeleteRepository: %v", errDelete) 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) ctx.Handle(500, "CreatePost", err)
} }
}
func Migrate(ctx *middleware.Context) { func Migrate(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("new_migrate") 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) log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
return 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 { 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) ctx.RenderWithErr(ctx.Tr("form.auth_failed", err), MIGRATE, &form)
return 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) ctx.Handle(500, "MigratePost", err)
} }
}
func getForkRepository(ctx *middleware.Context) (*models.Repository, error) { func getForkRepository(ctx *middleware.Context) (*models.Repository, error) {
forkId := ctx.QueryInt64("fork_id") 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) log.Trace("Repository forked: %s/%s", ctxUser.Name, repo.Name)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
return 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 { if repo != nil {
@ -338,8 +340,22 @@ func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
log.Error(4, "DeleteRepository: %v", errDelete) 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) ctx.Handle(500, "ForkPost", err)
} }
}
func Action(ctx *middleware.Context) { func Action(ctx *middleware.Context) {
var err error var err error

View File

@ -53,15 +53,18 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
newRepoName := form.RepoName newRepoName := form.RepoName
// Check if repository name has been changed. // Check if repository name has been changed.
if ctx.Repo.Repository.Name != newRepoName { 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.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, nil) ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form)
return case models.IsErrNameReserved(err):
} else if err := models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
if err == models.ErrRepoNameIllegal {
ctx.Data["Err_RepoName"] = true ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), SETTINGS_OPTIONS, nil) ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &form)
} else { 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) ctx.Handle(500, "ChangeRepositoryName", err)
} }
return 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 { if err := models.CreateUser(u); err != nil {
switch err { switch {
case models.ErrUserAlreadyExist: case models.IsErrUserAlreadyExist(err):
ctx.Data["Err_UserName"] = true ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form) ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form)
case models.ErrEmailAlreadyUsed: case models.IsErrEmailAlreadyUsed(err):
ctx.Data["Err_Email"] = true ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form)
case models.ErrUserNameIllegal: case models.IsErrNameReserved(err):
ctx.Data["Err_UserName"] = true 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: default:
ctx.Handle(500, "CreateUser", err) 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. // Check if user name has been changed.
if ctx.User.Name != form.UserName { if ctx.User.Name != form.UserName {
isExist, err := models.IsUserExist(ctx.User.Id, form.UserName) if err := models.ChangeUserName(ctx.User, form.UserName); err != nil {
if err != nil { switch {
ctx.Handle(500, "IsUserExist", err) case models.IsErrUserAlreadyExist(err):
return ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
} 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"))
ctx.Redirect(setting.AppSubUrl + "/user/settings") ctx.Redirect(setting.AppSubUrl + "/user/settings")
case models.ErrEmailAlreadyUsed: case models.IsErrEmailAlreadyUsed(err):
ctx.Flash.Error(ctx.Tr("form.email_been_used")) ctx.Flash.Error(ctx.Tr("form.email_been_used"))
ctx.Redirect(setting.AppSubUrl + "/user/settings") 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: default:
ctx.Handle(500, "ChangeUserName", err) 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.AddEmailAddress(e); err != nil {
if err == models.ErrEmailAlreadyUsed { if models.IsErrEmailAlreadyUsed(err) {
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form) ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form)
return return
} }