From e9c4156c874ceeecc81fdf7fe00ff2f582110ecd Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 3 Apr 2014 16:33:27 -0400 Subject: [PATCH] Add: rename user --- gogs.go | 2 +- models/repo.go | 5 ++-- models/user.go | 49 +++++++++++++++++++++++++++++++++++++ modules/auth/user.go | 2 ++ routers/repo/repo.go | 2 +- routers/user/setting.go | 23 ++++++++++++----- templates/user/setting.tmpl | 17 +++++++++---- 7 files changed, 84 insertions(+), 16 deletions(-) diff --git a/gogs.go b/gogs.go index 88f53eeb8..034e131bc 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.2.0.0402 Alpha" +const APP_VER = "0.2.0.0403 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/repo.go b/models/repo.go index 11e23ecd0..56f289097 100644 --- a/models/repo.go +++ b/models/repo.go @@ -372,9 +372,8 @@ func RepoPath(userName, repoName string) string { // ChangeRepositoryName changes all corresponding setting from old repository name to new one. func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) { // Update accesses. - accesses := make([]Access, 0, 5) - err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}) - if err != nil { + accesses := make([]Access, 0, 10) + if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil { return err } for i := range accesses { diff --git a/models/user.go b/models/user.go index 4908552f2..c5e46b480 100644 --- a/models/user.go +++ b/models/user.go @@ -203,8 +203,52 @@ func VerifyUserActiveCode(code string) (user *User) { return nil } +// ChangeUserName changes all corresponding setting from old user name to new one. +func ChangeUserName(user *User, newUserName string) (err error) { + newUserName = strings.ToLower(newUserName) + + // Update accesses of user. + accesses := make([]Access, 0, 10) + if err = orm.Find(&accesses, &Access{UserName: user.LowerName}); err != nil { + return err + } + for i := range accesses { + accesses[i].UserName = newUserName + if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") { + accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1) + if err = UpdateAccess(&accesses[i]); err != nil { + return err + } + } + } + + repos, err := GetRepositories(user) + if err != nil { + return err + } + for i := range repos { + accesses = make([]Access, 0, 10) + // Update accesses of user repository. + if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repos[i].LowerName}); err != nil { + return err + } + + for j := range accesses { + accesses[j].RepoName = newUserName + "/" + repos[i].LowerName + if err = UpdateAccess(&accesses[j]); err != nil { + return err + } + } + } + + // Change user directory name. + return os.Rename(UserPath(user.LowerName), UserPath(newUserName)) +} + // UpdateUser updates user's information. func UpdateUser(user *User) (err error) { + user.LowerName = strings.ToLower(user.Name) + if len(user.Location) > 255 { user.Location = user.Location[:255] } @@ -233,6 +277,11 @@ func DeleteUser(user *User) error { return err } + // Delete all accesses. + if _, err = orm.Delete(&Access{UserName: user.LowerName}); err != nil { + return err + } + // Delete all SSH keys. keys := make([]PublicKey, 0, 10) if err = orm.Find(&keys, &PublicKey{OwnerId: user.Id}); err != nil { diff --git a/modules/auth/user.go b/modules/auth/user.go index 2d3c29fde..015059f7d 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -75,6 +75,7 @@ type FeedsForm struct { } type UpdateProfileForm struct { + UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"` Email string `form:"email" binding:"Required;Email;MaxSize(50)"` Website string `form:"website" binding:"MaxSize(50)"` Location string `form:"location" binding:"MaxSize(50)"` @@ -83,6 +84,7 @@ type UpdateProfileForm struct { func (f *UpdateProfileForm) Name(field string) string { names := map[string]string{ + "UserName": "Username", "Email": "E-mail address", "Website": "Website", "Location": "Location", diff --git a/routers/repo/repo.go b/routers/repo/repo.go index a55647a31..ae51c5513 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -291,7 +291,7 @@ func SettingPost(ctx *middleware.Context) { ctx.RenderWithErr("Repository name has been taken in your repositories.", "repo/setting", nil) return } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil { - ctx.Handle(404, "repo.SettingPost(update)", err) + ctx.Handle(404, "repo.SettingPost(change repository name)", err) return } log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName) diff --git a/routers/user/setting.go b/routers/user/setting.go index 75adf2b81..4b6d88a36 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -23,15 +23,27 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { user := ctx.User ctx.Data["Owner"] = user - if ctx.Req.Method == "GET" { + if ctx.Req.Method == "GET" || ctx.HasError() { ctx.HTML(200, "user/setting") return } - // below is for POST requests - if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { - ctx.HTML(200, "user/setting") - return + // Check if user name has been changed. + if user.Name != form.UserName { + isExist, err := models.IsUserExist(form.UserName) + if err != nil { + ctx.Handle(404, "user.Setting(update: check existence)", err) + return + } else if isExist { + ctx.RenderWithErr("User name has been taken.", "user/setting", &form) + return + } else if err = models.ChangeUserName(user, form.UserName); err != nil { + ctx.Handle(404, "user.Setting(change user name)", err) + return + } + log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName) + + user.Name = form.UserName } user.Email = form.Email @@ -46,7 +58,6 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.Data["IsSuccess"] = true ctx.HTML(200, "user/setting") - log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) } diff --git a/templates/user/setting.tmpl b/templates/user/setting.tmpl index 283a8df8b..f44218cde 100644 --- a/templates/user/setting.tmpl +++ b/templates/user/setting.tmpl @@ -10,30 +10,37 @@ {{if .IsSuccess}}

Your profile has been successfully updated.

{{else if .HasError}}

{{.ErrorMsg}}

{{end}}

Your Email will be public and used for Account related notifications and any web based operations made via the web.

- +
- + +
+
+ +
+ +
+
- +
- +
- +