Git with a cup of tea, painless self-hosted git service Mirror for internal git.with.parts use https://git.with.parts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/routers/user/setting.go

269 lines
7.4 KiB

// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package user
import (
10 years ago
"strings"
"github.com/Unknwon/com"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
11 years ago
"github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting"
)
const (
SETTINGS_PROFILE base.TplName = "user/settings/profile"
SETTINGS_PASSWORD base.TplName = "user/settings/password"
SETTINGS_SSH_KEYS base.TplName = "user/settings/sshkeys"
SETTINGS_SOCIAL base.TplName = "user/settings/social"
SETTINGS_DELETE base.TplName = "user/settings/delete"
NOTIFICATION base.TplName = "user/notification"
SECURITY base.TplName = "user/security"
)
func Settings(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsProfile"] = true
ctx.HTML(200, SETTINGS_PROFILE)
11 years ago
}
func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsProfile"] = true
11 years ago
if ctx.HasError() {
ctx.HTML(200, SETTINGS_PROFILE)
11 years ago
return
}
11 years ago
// Check if user name has been changed.
11 years ago
if ctx.User.Name != form.UserName {
11 years ago
isExist, err := models.IsUserExist(form.UserName)
if err != nil {
ctx.Handle(500, "IsUserExist", err)
11 years ago
return
} else if isExist {
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form)
11 years ago
return
11 years ago
} else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
if err == models.ErrUserNameIllegal {
ctx.Flash.Error(ctx.Tr("form.illegal_username"))
ctx.Redirect(setting.AppSubUrl + "/user/settings")
return
} else {
ctx.Handle(500, "ChangeUserName", err)
}
11 years ago
return
}
log.Trace("User name changed: %s -> %s", ctx.User.Name, form.UserName)
11 years ago
ctx.User.Name = form.UserName
}
11 years ago
ctx.User.FullName = form.FullName
ctx.User.Email = form.Email
ctx.User.Website = form.Website
ctx.User.Location = form.Location
ctx.User.Avatar = base.EncodeMd5(form.Avatar)
ctx.User.AvatarEmail = form.Avatar
if err := models.UpdateUser(ctx.User); err != nil {
ctx.Handle(500, "UpdateUser", err)
return
}
log.Trace("User setting updated: %s", ctx.User.Name)
ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
ctx.Redirect(setting.AppSubUrl + "/user/settings")
}
func SettingsPassword(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsPassword"] = true
ctx.HTML(200, SETTINGS_PASSWORD)
11 years ago
}
func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsPassword"] = true
11 years ago
if ctx.HasError() {
ctx.HTML(200, SETTINGS_PASSWORD)
return
}
11 years ago
11 years ago
tmpUser := &models.User{
Passwd: form.OldPassword,
11 years ago
Salt: ctx.User.Salt,
11 years ago
}
tmpUser.EncodePasswd()
11 years ago
if ctx.User.Passwd != tmpUser.Passwd {
ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
} else if form.Password != form.Retype {
ctx.Flash.Error(ctx.Tr("form.password_not_match"))
11 years ago
} else {
ctx.User.Passwd = form.Password
11 years ago
ctx.User.Salt = models.GetUserSalt()
ctx.User.EncodePasswd()
if err := models.UpdateUser(ctx.User); err != nil {
ctx.Handle(500, "UpdateUser", err)
11 years ago
return
}
log.Trace("User password updated: %s", ctx.User.Name)
ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
11 years ago
}
ctx.Redirect(setting.AppSubUrl + "/user/settings/password")
11 years ago
}
func SettingsSSHKeys(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsSSHKeys"] = true
var err error
ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
if err != nil {
ctx.Handle(500, "ssh.ListPublicKey", err)
return
}
ctx.HTML(200, SETTINGS_SSH_KEYS)
}
func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsSSHKeys"] = true
var err error
ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
if err != nil {
ctx.Handle(500, "ssh.ListPublicKey", err)
return
}
// Delete SSH key.
if ctx.Query("_method") == "DELETE" {
id := com.StrTo(ctx.Query("id")).MustInt64()
if id <= 0 {
return
}
if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil {
ctx.Handle(500, "DeletePublicKey", err)
} else {
log.Trace("SSH key deleted: %s", ctx.User.Name)
ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
}
return
}
// Add new SSH key.
11 years ago
if ctx.Req.Method == "POST" {
11 years ago
if ctx.HasError() {
ctx.HTML(200, SETTINGS_SSH_KEYS)
return
}
// Remove newline characters from form.KeyContent
10 years ago
cleanContent := strings.Replace(form.Content, "\n", "", -1)
if ok, err := models.CheckPublicKeyString(cleanContent); !ok {
if err == models.ErrKeyUnableVerify {
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
} else {
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
return
}
}
k := &models.PublicKey{
OwnerId: ctx.User.Id,
Name: form.SSHTitle,
Content: cleanContent,
}
if err := models.AddPublicKey(k); err != nil {
if err == models.ErrKeyAlreadyExist {
ctx.RenderWithErr(ctx.Tr("form.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form)
return
}
11 years ago
ctx.Handle(500, "ssh.AddPublicKey", err)
return
} else {
log.Trace("SSH key added: %s", ctx.User.Name)
ctx.Flash.Success(ctx.Tr("settings.add_key_success"))
ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
11 years ago
return
}
}
ctx.HTML(200, SETTINGS_SSH_KEYS)
}
func SettingsSocial(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsSocial"] = true
// Unbind social account.
remove, _ := com.StrTo(ctx.Query("remove")).Int64()
if remove > 0 {
if err := models.DeleteOauth2ById(remove); err != nil {
ctx.Handle(500, "DeleteOauth2ById", err)
return
}
ctx.Flash.Success(ctx.Tr("settings.unbind_success"))
ctx.Redirect(setting.AppSubUrl + "/user/settings/social")
return
}
socials, err := models.GetOauthByUserId(ctx.User.Id)
if err != nil {
ctx.Handle(500, "GetOauthByUserId", err)
return
}
ctx.Data["Socials"] = socials
ctx.HTML(200, SETTINGS_SOCIAL)
}
func SettingsDelete(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsDelete"] = true
if ctx.Req.Method == "POST" {
// tmpUser := models.User{
// Passwd: ctx.Query("password"),
// Salt: ctx.User.Salt,
// }
// tmpUser.EncodePasswd()
// if tmpUser.Passwd != ctx.User.Passwd {
// ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
// } else {
if err := models.DeleteUser(ctx.User); err != nil {
switch err {
case models.ErrUserOwnRepos:
ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
default:
ctx.Handle(500, "DeleteUser", err)
}
} else {
log.Trace("Account deleted: %s", ctx.User.Name)
ctx.Redirect(setting.AppSubUrl + "/")
}
return
}
ctx.HTML(200, SETTINGS_DELETE)
}