Fix case change in ownernames (#16045) (#16050)

Backport #16045

If you change the case of a username the change needs to be propagated to their
repositories.

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath 2021-06-03 06:09:43 +01:00 committed by GitHub
parent 0600f7972a
commit ba74fdbda9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 1 deletions

View file

@ -1349,6 +1349,26 @@ func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
return sess.Commit() return sess.Commit()
} }
// UpdateRepositoryOwnerNames updates repository owner_names (this should only be used when the ownerName has changed case)
func UpdateRepositoryOwnerNames(ownerID int64, ownerName string) error {
if ownerID == 0 {
return nil
}
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
if _, err := sess.Where("owner_id = ?", ownerID).Cols("owner_name").Update(&Repository{
OwnerName: ownerName,
}); err != nil {
return err
}
return sess.Commit()
}
// UpdateRepositoryUpdatedTime updates a repository's updated time // UpdateRepositoryUpdatedTime updates a repository's updated time
func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error { func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
_, err := x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", updateTime.Unix(), repoID) _, err := x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", updateTime.Unix(), repoID)

View file

@ -51,6 +51,7 @@ func SettingsPost(ctx *context.Context) {
} }
org := ctx.Org.Organization org := ctx.Org.Organization
nameChanged := org.Name != form.Name
// Check if organization name has been changed. // Check if organization name has been changed.
if org.LowerName != strings.ToLower(form.Name) { if org.LowerName != strings.ToLower(form.Name) {
@ -74,7 +75,9 @@ func SettingsPost(ctx *context.Context) {
// reset ctx.org.OrgLink with new name // reset ctx.org.OrgLink with new name
ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name
log.Trace("Organization name changed: %s -> %s", org.Name, form.Name) log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
nameChanged = false
} }
// In case it's just a case change. // In case it's just a case change.
org.Name = form.Name org.Name = form.Name
org.LowerName = strings.ToLower(form.Name) org.LowerName = strings.ToLower(form.Name)
@ -104,11 +107,17 @@ func SettingsPost(ctx *context.Context) {
return return
} }
for _, repo := range org.Repos { for _, repo := range org.Repos {
repo.OwnerName = org.Name
if err := models.UpdateRepository(repo, true); err != nil { if err := models.UpdateRepository(repo, true); err != nil {
ctx.ServerError("UpdateRepository", err) ctx.ServerError("UpdateRepository", err)
return return
} }
} }
} else if nameChanged {
if err := models.UpdateRepositoryOwnerNames(org.ID, org.Name); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}
} }
log.Trace("Organization setting updated: %s", org.Name) log.Trace("Organization setting updated: %s", org.Name)

View file

@ -67,8 +67,13 @@ func HandleUsernameChange(ctx *context.Context, user *models.User, newName strin
} }
return err return err
} }
log.Trace("User name changed: %s -> %s", user.Name, newName) } else {
if err := models.UpdateRepositoryOwnerNames(user.ID, newName); err != nil {
ctx.ServerError("UpdateRepository", err)
return err
} }
}
log.Trace("User name changed: %s -> %s", user.Name, newName)
return nil return nil
} }
@ -84,6 +89,7 @@ func ProfilePost(ctx *context.Context) {
} }
if len(form.Name) != 0 && ctx.User.Name != form.Name { if len(form.Name) != 0 && ctx.User.Name != form.Name {
log.Debug("Changing name for %s to %s", ctx.User.Name, form.Name)
if err := HandleUsernameChange(ctx, ctx.User, form.Name); err != nil { if err := HandleUsernameChange(ctx, ctx.User, form.Name); err != nil {
ctx.Redirect(setting.AppSubURL + "/user/settings") ctx.Redirect(setting.AppSubURL + "/user/settings")
return return