Add: rename user

This commit is contained in:
Unknown 2014-04-03 16:33:27 -04:00
parent 79a610592e
commit e9c4156c87
7 changed files with 84 additions and 16 deletions

View File

@ -19,7 +19,7 @@ import (
// Test that go1.2 tag above is included in builds. main.go refers to this definition. // Test that go1.2 tag above is included in builds. main.go refers to this definition.
const go12tag = true const go12tag = true
const APP_VER = "0.2.0.0402 Alpha" const APP_VER = "0.2.0.0403 Alpha"
func init() { func init() {
base.AppVer = APP_VER base.AppVer = APP_VER

View File

@ -372,9 +372,8 @@ func RepoPath(userName, repoName string) string {
// ChangeRepositoryName changes all corresponding setting from old repository name to new one. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) { func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) {
// Update accesses. // Update accesses.
accesses := make([]Access, 0, 5) accesses := make([]Access, 0, 10)
err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}) if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil {
if err != nil {
return err return err
} }
for i := range accesses { for i := range accesses {

View File

@ -203,8 +203,52 @@ func VerifyUserActiveCode(code string) (user *User) {
return nil 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. // UpdateUser updates user's information.
func UpdateUser(user *User) (err error) { func UpdateUser(user *User) (err error) {
user.LowerName = strings.ToLower(user.Name)
if len(user.Location) > 255 { if len(user.Location) > 255 {
user.Location = user.Location[:255] user.Location = user.Location[:255]
} }
@ -233,6 +277,11 @@ func DeleteUser(user *User) error {
return err return err
} }
// Delete all accesses.
if _, err = orm.Delete(&Access{UserName: user.LowerName}); err != nil {
return err
}
// Delete all SSH keys. // Delete all SSH keys.
keys := make([]PublicKey, 0, 10) keys := make([]PublicKey, 0, 10)
if err = orm.Find(&keys, &PublicKey{OwnerId: user.Id}); err != nil { if err = orm.Find(&keys, &PublicKey{OwnerId: user.Id}); err != nil {

View File

@ -75,6 +75,7 @@ type FeedsForm struct {
} }
type UpdateProfileForm struct { type UpdateProfileForm struct {
UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"`
Email string `form:"email" binding:"Required;Email;MaxSize(50)"` Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
Website string `form:"website" binding:"MaxSize(50)"` Website string `form:"website" binding:"MaxSize(50)"`
Location string `form:"location" binding:"MaxSize(50)"` Location string `form:"location" binding:"MaxSize(50)"`
@ -83,6 +84,7 @@ type UpdateProfileForm struct {
func (f *UpdateProfileForm) Name(field string) string { func (f *UpdateProfileForm) Name(field string) string {
names := map[string]string{ names := map[string]string{
"UserName": "Username",
"Email": "E-mail address", "Email": "E-mail address",
"Website": "Website", "Website": "Website",
"Location": "Location", "Location": "Location",

View File

@ -291,7 +291,7 @@ func SettingPost(ctx *middleware.Context) {
ctx.RenderWithErr("Repository name has been taken in your repositories.", "repo/setting", nil) ctx.RenderWithErr("Repository name has been taken in your repositories.", "repo/setting", nil)
return return
} else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil { } 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 return
} }
log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName) log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName)

View File

@ -23,15 +23,27 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
user := ctx.User user := ctx.User
ctx.Data["Owner"] = user ctx.Data["Owner"] = user
if ctx.Req.Method == "GET" { if ctx.Req.Method == "GET" || ctx.HasError() {
ctx.HTML(200, "user/setting") ctx.HTML(200, "user/setting")
return return
} }
// below is for POST requests // Check if user name has been changed.
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { if user.Name != form.UserName {
ctx.HTML(200, "user/setting") isExist, err := models.IsUserExist(form.UserName)
return 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 user.Email = form.Email
@ -46,7 +58,6 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.Data["IsSuccess"] = true ctx.Data["IsSuccess"] = true
ctx.HTML(200, "user/setting") ctx.HTML(200, "user/setting")
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
} }

View File

@ -10,30 +10,37 @@
{{if .IsSuccess}}<p class="alert alert-success">Your profile has been successfully updated.</p>{{else if .HasError}}<p class="alert alert-danger form-error">{{.ErrorMsg}}</p>{{end}} {{if .IsSuccess}}<p class="alert alert-success">Your profile has been successfully updated.</p>{{else if .HasError}}<p class="alert alert-danger form-error">{{.ErrorMsg}}</p>{{end}}
<p>Your Email will be public and used for Account related notifications and any web based operations made via the web.</p> <p>Your Email will be public and used for Account related notifications and any web based operations made via the web.</p>
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">Email</label> <label class="col-md-2 control-label">Username<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
<input type="text" name="email" class="form-control" placeholder="Type your e-mail address" value="{{.Owner.Email}}"> <input name="username" class="form-control" placeholder="Type your user name" required="required" value="{{.SignedUser.Name}}">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Email<strong class="text-danger">*</strong></label>
<div class="col-md-8">
<input name="email" class="form-control" placeholder="Type your e-mail address" required="required" value="{{.SignedUser.Email}}">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">Website</label> <label class="col-md-2 control-label">Website</label>
<div class="col-md-8"> <div class="col-md-8">
<input type="text" name="website" class="form-control" placeholder="Type your website URL" value="{{.Owner.Website}}"> <input name="website" class="form-control" placeholder="Type your website URL" value="{{.SignedUser.Website}}">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">Location</label> <label class="col-md-2 control-label">Location</label>
<div class="col-md-8"> <div class="col-md-8">
<input type="text" name="location" class="form-control" placeholder="Type your current location" value="{{.Owner.Location}}"> <input name="location" class="form-control" placeholder="Type your current location" value="{{.SignedUser.Location}}">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Avatar}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Avatar}}has-error has-feedback{{end}}">
<label class="col-md-2 control-label">Gravatar Email<strong class="text-danger">*</strong></label> <label class="col-md-2 control-label">Gravatar Email<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
<input type="text" name="avatar" class="form-control" placeholder="Type your Gravatar e-mail address" required="required" value="{{.Owner.AvatarEmail}}"> <input name="avatar" class="form-control" placeholder="Type your Gravatar e-mail address" required="required" value="{{.SignedUser.AvatarEmail}}">
</div> </div>
</div> </div>