Browse Source

Add: rename user

for-closed-social
Unknown 10 years ago
parent
commit
e9c4156c87
7 changed files with 84 additions and 16 deletions
  1. +1
    -1
      gogs.go
  2. +2
    -3
      models/repo.go
  3. +49
    -0
      models/user.go
  4. +2
    -0
      modules/auth/user.go
  5. +1
    -1
      routers/repo/repo.go
  6. +17
    -6
      routers/user/setting.go
  7. +12
    -5
      templates/user/setting.tmpl

+ 1
- 1
gogs.go View File

@ -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

+ 2
- 3
models/repo.go View File

@ -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 {

+ 49
- 0
models/user.go View File

@ -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 {

+ 2
- 0
modules/auth/user.go View File

@ -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",

+ 1
- 1
routers/repo/repo.go View File

@ -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)

+ 17
- 6
routers/user/setting.go View File

@ -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)
}

+ 12
- 5
templates/user/setting.tmpl 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}}
<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">
<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">
<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 class="form-group">
<label class="col-md-2 control-label">Website</label>
<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 class="form-group">
<label class="col-md-2 control-label">Location</label>
<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 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>
<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>

Loading…
Cancel
Save