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-fork-majority-judgment/routers/user/setting.go

219 lines
5.8 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 (
"strconv"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
10 years ago
"github.com/gogits/gogs/modules/middleware"
)
10 years ago
func Setting(ctx *middleware.Context) {
ctx.Data["Title"] = "Setting"
ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSetting"] = true
ctx.Data["Owner"] = ctx.User
ctx.HTML(200, "user/setting")
}
10 years ago
// Render user setting page (email, website modify)
10 years ago
func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
10 years ago
ctx.Data["Title"] = "Setting"
10 years ago
ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
10 years ago
user := ctx.User
ctx.Data["Owner"] = user
10 years ago
if ctx.HasError() {
10 years ago
ctx.HTML(200, "user/setting")
10 years ago
return
}
10 years ago
// Check if user name has been changed.
if user.Name != form.UserName {
isExist, err := models.IsUserExist(form.UserName)
if err != nil {
10 years ago
ctx.Handle(500, "user.Setting(update: check existence)", err)
10 years ago
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 {
10 years ago
ctx.Handle(500, "user.Setting(change user name)", err)
10 years ago
return
}
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName)
user.Name = form.UserName
}
10 years ago
user.FullName = form.FullName
user.Email = form.Email
user.Website = form.Website
user.Location = form.Location
user.Avatar = base.EncodeMd5(form.Avatar)
user.AvatarEmail = form.Avatar
if err := models.UpdateUser(user); err != nil {
10 years ago
ctx.Handle(500, "setting.Setting", err)
return
}
10 years ago
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
10 years ago
ctx.Flash.Success("Your profile has been successfully updated.")
ctx.Redirect("/user/settings")
}
10 years ago
func SettingSocial(ctx *middleware.Context) {
ctx.Data["Title"] = "Social Account"
ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSettingSocial"] = true
socials, err := models.GetOauthByUserId(ctx.User.Id)
if err != nil {
ctx.Handle(500, "user.SettingSocial", err)
return
}
ctx.Data["Socials"] = socials
ctx.HTML(200, "user/social")
}
10 years ago
func SettingPassword(ctx *middleware.Context) {
ctx.Data["Title"] = "Password"
ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSettingPasswd"] = true
ctx.HTML(200, "user/password")
}
func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
10 years ago
ctx.Data["Title"] = "Password"
ctx.Data["PageIsUserSetting"] = true
10 years ago
ctx.Data["IsUserPageSettingPasswd"] = true
10 years ago
if ctx.HasError() {
10 years ago
ctx.HTML(200, "user/password")
return
}
10 years ago
10 years ago
user := ctx.User
10 years ago
tmpUser := &models.User{
Passwd: form.OldPasswd,
Salt: user.Salt,
}
tmpUser.EncodePasswd()
if user.Passwd != tmpUser.Passwd {
ctx.Flash.Error("Old password is not correct")
10 years ago
} else if form.NewPasswd != form.RetypePasswd {
10 years ago
ctx.Flash.Error("New password and re-type password are not same")
10 years ago
} else {
10 years ago
user.Passwd = form.NewPasswd
user.Salt = models.GetUserSalt()
user.EncodePasswd()
10 years ago
if err := models.UpdateUser(user); err != nil {
10 years ago
ctx.Handle(200, "setting.SettingPassword", err)
10 years ago
return
}
10 years ago
log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.")
10 years ago
}
ctx.Redirect("/user/settings/password")
10 years ago
}
10 years ago
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = "SSH Keys"
// Delete SSH key.
10 years ago
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
if err != nil {
log.Error("ssh.DelPublicKey: %v", err)
10 years ago
ctx.JSON(200, map[string]interface{}{
"ok": false,
"err": err.Error(),
})
return
}
k := &models.PublicKey{
Id: id,
10 years ago
OwnerId: ctx.User.Id,
}
if err = models.DeletePublicKey(k); err != nil {
log.Error("ssh.DelPublicKey: %v", err)
10 years ago
ctx.JSON(200, map[string]interface{}{
"ok": false,
"err": err.Error(),
})
} else {
10 years ago
log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
10 years ago
ctx.JSON(200, map[string]interface{}{
"ok": true,
})
}
return
}
// Add new SSH key.
10 years ago
if ctx.Req.Method == "POST" {
10 years ago
if ctx.HasError() {
10 years ago
ctx.HTML(200, "user/publickey")
return
}
k := &models.PublicKey{
OwnerId: ctx.User.Id,
Name: form.KeyName,
Content: form.KeyContent,
}
if err := models.AddPublicKey(k); err != nil {
if err.Error() == models.ErrKeyAlreadyExist.Error() {
ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
return
}
10 years ago
ctx.Handle(500, "ssh.AddPublicKey", err)
return
} else {
10 years ago
log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
ctx.Flash.Success("New SSH Key has been added!")
ctx.Redirect("/user/settings/ssh")
10 years ago
return
}
}
// List existed SSH keys.
10 years ago
keys, err := models.ListPublicKey(ctx.User.Id)
if err != nil {
10 years ago
ctx.Handle(200, "ssh.ListPublicKey", err)
return
}
10 years ago
ctx.Data["PageIsUserSetting"] = true
10 years ago
ctx.Data["IsUserPageSettingSSH"] = true
10 years ago
ctx.Data["Keys"] = keys
10 years ago
ctx.HTML(200, "user/publickey")
}
10 years ago
func SettingNotification(ctx *middleware.Context) {
10 years ago
// TODO: user setting notification
10 years ago
ctx.Data["Title"] = "Notification"
ctx.Data["PageIsUserSetting"] = true
10 years ago
ctx.Data["IsUserPageSettingNotify"] = true
10 years ago
ctx.HTML(200, "user/notification")
}
10 years ago
func SettingSecurity(ctx *middleware.Context) {
10 years ago
// TODO: user setting security
10 years ago
ctx.Data["Title"] = "Security"
ctx.Data["PageIsUserSetting"] = true
10 years ago
ctx.Data["IsUserPageSettingSecurity"] = true
10 years ago
ctx.HTML(200, "user/security")
}