aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/repo.go5
-rw-r--r--models/user.go49
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 {