]> source.dussan.org Git - gitea.git/commitdiff
Fix case change in ownernames (#16045)
authorzeripath <art27@cantab.net>
Wed, 2 Jun 2021 12:03:59 +0000 (13:03 +0100)
committerGitHub <noreply@github.com>
Wed, 2 Jun 2021 12:03:59 +0000 (13:03 +0100)
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>
models/repo.go
routers/org/setting.go
routers/user/setting/profile.go

index daa94c0d508b989f14e232c4a890aa44c064a68f..58a393ae708e7a14681bad259beaa1077393ff62 100644 (file)
@@ -1350,6 +1350,26 @@ func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
        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
 func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
        _, err := x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", updateTime.Unix(), repoID)
index e7995fe8fa59012582b8f0af2414e7ee61d0618e..0e28a93acef95b1bc310aec301d42ac06ff25a0c 100644 (file)
@@ -52,6 +52,7 @@ func SettingsPost(ctx *context.Context) {
        }
 
        org := ctx.Org.Organization
+       nameChanged := org.Name != form.Name
 
        // Check if organization name has been changed.
        if org.LowerName != strings.ToLower(form.Name) {
@@ -75,7 +76,9 @@ func SettingsPost(ctx *context.Context) {
                // reset ctx.org.OrgLink with new name
                ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name
                log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
+               nameChanged = false
        }
+
        // In case it's just a case change.
        org.Name = form.Name
        org.LowerName = strings.ToLower(form.Name)
@@ -105,11 +108,17 @@ func SettingsPost(ctx *context.Context) {
                        return
                }
                for _, repo := range org.Repos {
+                       repo.OwnerName = org.Name
                        if err := models.UpdateRepository(repo, true); err != nil {
                                ctx.ServerError("UpdateRepository", err)
                                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)
index 0bc2b4ee36b9946cf52f8776a726787c8b5700ac..8cde81f2954b9e340e967580b50ee36af266898e 100644 (file)
@@ -68,8 +68,13 @@ func HandleUsernameChange(ctx *context.Context, user *models.User, newName strin
                        }
                        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
 }
 
@@ -85,6 +90,7 @@ func ProfilePost(ctx *context.Context) {
        }
 
        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 {
                        ctx.Redirect(setting.AppSubURL + "/user/settings")
                        return