models: able to rename user with diff letter cases #981

- templates/org: mirror fix on name output
- routers: add missing error check
This commit is contained in:
Unknwon 2015-02-22 18:24:49 -05:00
parent 74a8bb93d8
commit 04164eada3
8 changed files with 97 additions and 83 deletions

View file

@ -93,7 +93,7 @@ func CreateOrganization(org, owner *User) (*User, error) {
return nil, ErrUserNameIllegal return nil, ErrUserNameIllegal
} }
isExist, err := IsUserExist(org.Name) isExist, err := IsUserExist(0, org.Name)
if err != nil { if err != nil {
return nil, err return nil, err
} else if isExist { } else if isExist {

View file

@ -249,11 +249,13 @@ func (u *User) GetFullNameFallback() string {
// IsUserExist checks if given user name exist, // IsUserExist checks if given user name exist,
// the user name should be noncased unique. // the user name should be noncased unique.
func IsUserExist(name string) (bool, error) { // If uid is presented, then check will rule out that one,
// it is used when update a user name in settings page.
func IsUserExist(uid int64, name string) (bool, error) {
if len(name) == 0 { if len(name) == 0 {
return false, nil return false, nil
} }
return x.Get(&User{LowerName: strings.ToLower(name)}) return x.Where("id!=?", uid).Get(&User{LowerName: strings.ToLower(name)})
} }
// IsEmailUsed returns true if the e-mail has been used. // IsEmailUsed returns true if the e-mail has been used.
@ -278,7 +280,7 @@ func CreateUser(u *User) error {
return ErrUserNameIllegal return ErrUserNameIllegal
} }
isExist, err := IsUserExist(u.Name) isExist, err := IsUserExist(0, u.Name)
if err != nil { if err != nil {
return err return err
} else if isExist { } else if isExist {
@ -397,6 +399,10 @@ func ChangeUserName(u *User, newUserName string) (err error) {
} }
newUserName = strings.ToLower(newUserName) newUserName = strings.ToLower(newUserName)
if u.LowerName == newUserName {
// User only change letter cases.
return nil
}
// Update accesses of user. // Update accesses of user.
accesses := make([]Access, 0, 10) accesses := make([]Access, 0, 10)
@ -453,7 +459,7 @@ func ChangeUserName(u *User, newUserName string) (err error) {
// UpdateUser updates user's information. // UpdateUser updates user's information.
func UpdateUser(u *User) error { func UpdateUser(u *User) error {
has, err := x.Where("id!=?", u.Id).And("type=?", INDIVIDUAL).And("email=?", u.Email).Get(new(User)) has, err := x.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {

View file

@ -39,18 +39,18 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
// Check if organization name has been changed. // Check if organization name has been changed.
if org.Name != form.OrgUserName { if org.Name != form.OrgUserName {
isExist, err := models.IsUserExist(form.OrgUserName) isExist, err := models.IsUserExist(org.Id, form.OrgUserName)
if err != nil { if err != nil {
ctx.Handle(500, "IsUserExist", err) ctx.Handle(500, "IsUserExist", err)
return return
} else if isExist { } else if isExist {
ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form) ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
return return
} else if err = models.ChangeUserName(org, form.OrgUserName); err != nil { } else if err = models.ChangeUserName(org, form.OrgUserName); err != nil {
if err == models.ErrUserNameIllegal { if err == models.ErrUserNameIllegal {
ctx.Flash.Error(ctx.Tr("form.illegal_username")) ctx.Data["Err_UserName"] = true
ctx.Redirect(setting.AppSubUrl + "/org/" + org.LowerName + "/settings") ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &form)
return
} else { } else {
ctx.Handle(500, "ChangeUserName", err) ctx.Handle(500, "ChangeUserName", err)
} }
@ -68,7 +68,12 @@ 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 {
ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_OPTIONS, &form)
} else {
ctx.Handle(500, "UpdateUser", err) ctx.Handle(500, "UpdateUser", err)
}
return return
} }
log.Trace("Organization setting updated: %s", org.Name) log.Trace("Organization setting updated: %s", org.Name)

View file

@ -109,7 +109,7 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
} }
newOwner := ctx.Query("new_owner_name") newOwner := ctx.Query("new_owner_name")
isExist, err := models.IsUserExist(newOwner) isExist, err := models.IsUserExist(0, newOwner)
if err != nil { if err != nil {
ctx.Handle(500, "IsUserExist", err) ctx.Handle(500, "IsUserExist", err)
return return

View file

@ -50,7 +50,7 @@ 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(form.UserName) isExist, err := models.IsUserExist(ctx.User.Id, form.UserName)
if err != nil { if err != nil {
ctx.Handle(500, "IsUserExist", err) ctx.Handle(500, "IsUserExist", err)
return return
@ -58,11 +58,14 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form) ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form)
return return
} else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil { } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
if err == models.ErrUserNameIllegal { switch err {
case models.ErrUserNameIllegal:
ctx.Flash.Error(ctx.Tr("form.illegal_username")) ctx.Flash.Error(ctx.Tr("form.illegal_username"))
ctx.Redirect(setting.AppSubUrl + "/user/settings") ctx.Redirect(setting.AppSubUrl + "/user/settings")
return case models.ErrEmailAlreadyUsed:
} else { ctx.Flash.Error(ctx.Tr("form.email_been_used"))
ctx.Redirect(setting.AppSubUrl + "/user/settings")
default:
ctx.Handle(500, "ChangeUserName", err) ctx.Handle(500, "ChangeUserName", err)
} }
return return

View file

@ -2,7 +2,7 @@
<div class="container"> <div class="container">
<a class="text-black left" href="{{AppSubUrl}}/org/{{.Org.LowerName}}"> <a class="text-black left" href="{{AppSubUrl}}/org/{{.Org.LowerName}}">
<img class="avatar-48 left" src="{{.Org.AvatarLink}}?s=100"> <img class="avatar-48 left" src="{{.Org.AvatarLink}}?s=100">
<span class="org-name">{{.Org.FullName}}</span> <span class="org-name">{{if .Org.FullName}}{{.Org.FullName}}{{else}}{{.Org.Name}}{{end}}</span>
</a> </a>
<ul class="menu menu-line container"> <ul class="menu menu-line container">
<li class="right"> <li class="right">

View file

@ -4,9 +4,9 @@
</div> </div>
<div class="panel-body"> <div class="panel-body">
<ul class="menu menu-vertical switching-list grid-1-5 left"> <ul class="menu menu-vertical switching-list grid-1-5 left">
<li {{if .PageIsSettingsOptions}}class="current"{{end}}><a href="{{AppSubUrl}}/org/{{.Org.LowerName}}/settings">{{.i18n.Tr "org.settings.options"}}</a></li> <li {{if .PageIsSettingsOptions}}class="current"{{end}}><a href="{{AppSubUrl}}/org/{{.Org.Name}}/settings">{{.i18n.Tr "org.settings.options"}}</a></li>
<li {{if .PageIsSettingsHooks}}class="current"{{end}}><a href="{{AppSubUrl}}/org/{{.Org.LowerName}}/settings/hooks">{{.i18n.Tr "repo.settings.hooks"}}</a></li> <li {{if .PageIsSettingsHooks}}class="current"{{end}}><a href="{{AppSubUrl}}/org/{{.Org.Name}}/settings/hooks">{{.i18n.Tr "repo.settings.hooks"}}</a></li>
<li {{if .PageIsSettingsDelete}}class="current"{{end}}><a href="{{AppSubUrl}}/org/{{.Org.LowerName}}/settings/delete">{{.i18n.Tr "org.settings.delete"}}</a></li> <li {{if .PageIsSettingsDelete}}class="current"{{end}}><a href="{{AppSubUrl}}/org/{{.Org.Name}}/settings/delete">{{.i18n.Tr "org.settings.delete"}}</a></li>
</ul> </ul>
</div> </div>
</div> </div>