|
|
@ -8,6 +8,7 @@ import ( |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/Unknwon/com" |
|
|
|
"github.com/Unknwon/com" |
|
|
@ -39,12 +40,13 @@ func Settings(ctx *middleware.Context) { |
|
|
|
ctx.HTML(200, SETTINGS_PROFILE) |
|
|
|
ctx.HTML(200, SETTINGS_PROFILE) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func handlerUsernameChange(ctx *middleware.Context, newName string) { |
|
|
|
func handleUsernameChange(ctx *middleware.Context, newName string) { |
|
|
|
|
|
|
|
// Non-local users are not allowed to change their username.
|
|
|
|
if len(newName) == 0 || !ctx.User.IsLocal() { |
|
|
|
if len(newName) == 0 || !ctx.User.IsLocal() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Check if user name has been changed.
|
|
|
|
// Check if user name has been changed
|
|
|
|
if ctx.User.LowerName != strings.ToLower(newName) { |
|
|
|
if ctx.User.LowerName != strings.ToLower(newName) { |
|
|
|
if err := models.ChangeUserName(ctx.User, newName); err != nil { |
|
|
|
if err := models.ChangeUserName(ctx.User, newName); err != nil { |
|
|
|
switch { |
|
|
|
switch { |
|
|
@ -67,7 +69,8 @@ func handlerUsernameChange(ctx *middleware.Context, newName string) { |
|
|
|
} |
|
|
|
} |
|
|
|
log.Trace("User name changed: %s -> %s", ctx.User.Name, newName) |
|
|
|
log.Trace("User name changed: %s -> %s", ctx.User.Name, newName) |
|
|
|
} |
|
|
|
} |
|
|
|
// In case it's just a case change.
|
|
|
|
|
|
|
|
|
|
|
|
// In case it's just a case change
|
|
|
|
ctx.User.Name = newName |
|
|
|
ctx.User.Name = newName |
|
|
|
ctx.User.LowerName = strings.ToLower(newName) |
|
|
|
ctx.User.LowerName = strings.ToLower(newName) |
|
|
|
} |
|
|
|
} |
|
|
@ -81,7 +84,7 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
handlerUsernameChange(ctx, form.Name) |
|
|
|
handleUsernameChange(ctx, form.Name) |
|
|
|
if ctx.Written() { |
|
|
|
if ctx.Written() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -98,7 +101,8 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { |
|
|
|
ctx.Handle(500, "UpdateUser", err) |
|
|
|
ctx.Handle(500, "UpdateUser", err) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
log.Trace("User setting updated: %s", ctx.User.Name) |
|
|
|
|
|
|
|
|
|
|
|
log.Trace("User settings updated: %s", ctx.User.Name) |
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) |
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) |
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/settings") |
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/settings") |
|
|
|
} |
|
|
|
} |
|
|
@ -112,10 +116,11 @@ func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ct |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("Avatar.Open: %v", err) |
|
|
|
return fmt.Errorf("Avatar.Open: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
defer fr.Close() |
|
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(fr) |
|
|
|
data, err := ioutil.ReadAll(fr) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("ReadAll: %v", err) |
|
|
|
return fmt.Errorf("ioutil.ReadAll: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
if _, ok := base.IsImageFile(data); !ok { |
|
|
|
if _, ok := base.IsImageFile(data); !ok { |
|
|
|
return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) |
|
|
|
return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) |
|
|
@ -124,9 +129,12 @@ func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ct |
|
|
|
return fmt.Errorf("UploadAvatar: %v", err) |
|
|
|
return fmt.Errorf("UploadAvatar: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// In case no avatar at all.
|
|
|
|
// No avatar is uploaded but setting has been changed to enable,
|
|
|
|
if form.Enable && !com.IsFile(ctx.User.CustomAvatarPath()) { |
|
|
|
// generate a random one when needed.
|
|
|
|
return errors.New(ctx.Tr("settings.no_custom_avatar_available")) |
|
|
|
if form.Enable && !com.IsFile(ctxUser.CustomAvatarPath()) { |
|
|
|
|
|
|
|
if err := ctxUser.GenerateRandomAvatar(); err != nil { |
|
|
|
|
|
|
|
log.Error(4, "GenerateRandomAvatar[%d]: %v", ctxUser.Id, err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -147,6 +155,16 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) { |
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/settings") |
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/settings") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func SettingsDeleteAvatar(ctx *middleware.Context) { |
|
|
|
|
|
|
|
os.Remove(ctx.User.CustomAvatarPath()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.User.UseCustomAvatar = false |
|
|
|
|
|
|
|
if err := models.UpdateUser(ctx.User); err != nil { |
|
|
|
|
|
|
|
ctx.Flash.Error(fmt.Sprintf("UpdateUser: %v", err)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubUrl + "/user/settings") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func SettingsPassword(ctx *middleware.Context) { |
|
|
|
func SettingsPassword(ctx *middleware.Context) { |
|
|
|
ctx.Data["Title"] = ctx.Tr("settings") |
|
|
|
ctx.Data["Title"] = ctx.Tr("settings") |
|
|
|
ctx.Data["PageIsSettingsPassword"] = true |
|
|
|
ctx.Data["PageIsSettingsPassword"] = true |
|
|
|