diff options
author | Unknown <joe2010xtmf@163.com> | 2014-04-03 16:33:27 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-04-03 16:33:27 -0400 |
commit | e9c4156c874ceeecc81fdf7fe00ff2f582110ecd (patch) | |
tree | 091d17fbfa89444ab16c97d4c4b8da361a1c6ad8 /models | |
parent | 79a610592ea7c2e93ab2f91190af1782c4aa8f8d (diff) | |
download | gitea-e9c4156c874ceeecc81fdf7fe00ff2f582110ecd.tar.gz gitea-e9c4156c874ceeecc81fdf7fe00ff2f582110ecd.zip |
Add: rename user
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 5 | ||||
-rw-r--r-- | models/user.go | 49 |
2 files changed, 51 insertions, 3 deletions
diff --git a/models/repo.go b/models/repo.go index 11e23ecd0f..56f2890977 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 4908552f29..c5e46b4804 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 { |