models/user: better directory handling when change username

Previously, if the user base directory somehow doesn't exist, the
application throws 500 for failure of rename.

Now it detects if the application should rename or just create a
new directory.
master
Unknwon 2017-03-07 14:12:19 -05:00
parent e6dbfd918c
commit bb005f3f9a
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
1 changed files with 7 additions and 1 deletions

View File

@ -682,7 +682,13 @@ func ChangeUserName(u *User, newUserName string) (err error) {
return fmt.Errorf("Delete repository wiki local copy: %v", err)
}
return os.Rename(UserPath(u.Name), UserPath(newUserName))
// Rename or create user base directory
baseDir := UserPath(u.Name)
newBaseDir := UserPath(newUserName)
if com.IsExist(baseDir) {
return os.Rename(baseDir, newBaseDir)
}
return os.MkdirAll(newBaseDir, os.ModePerm)
}
func updateUser(e Engine, u *User) error {