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

178 lines
4.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"
11 years ago
"github.com/gogits/gogs/modules/middleware"
)
11 years ago
// Render user setting page (email, website modify)
11 years ago
func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.Data["Title"] = "Setting"
11 years ago
ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
11 years ago
user := ctx.User
ctx.Data["Owner"] = user
11 years ago
if ctx.Req.Method == "GET" {
ctx.Render.HTML(200, "user/setting", ctx.Data)
11 years ago
return
}
11 years ago
// below is for POST requests
11 years ago
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
ctx.Render.HTML(200, "user/setting", ctx.Data)
return
}
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 {
11 years ago
ctx.Handle(200, "setting.Setting", err)
return
}
11 years ago
ctx.Data["IsSuccess"] = true
ctx.Render.HTML(200, "user/setting", ctx.Data)
11 years ago
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
}
11 years ago
func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) {
ctx.Data["Title"] = "Password"
ctx.Data["PageIsUserSetting"] = true
11 years ago
ctx.Data["IsUserPageSettingPasswd"] = true
11 years ago
if ctx.Req.Method == "GET" {
ctx.Render.HTML(200, "user/password", ctx.Data)
return
}
11 years ago
11 years ago
user := ctx.User
newUser := &models.User{Passwd: form.NewPasswd}
11 years ago
if err := newUser.EncodePasswd(); err != nil {
11 years ago
ctx.Handle(200, "setting.SettingPassword", err)
11 years ago
return
}
if user.Passwd != newUser.Passwd {
11 years ago
ctx.Data["HasError"] = true
ctx.Data["ErrorMsg"] = "Old password is not correct"
11 years ago
} else if form.NewPasswd != form.RetypePasswd {
11 years ago
ctx.Data["HasError"] = true
ctx.Data["ErrorMsg"] = "New password and re-type password are not same"
11 years ago
} else {
user.Passwd = newUser.Passwd
if err := models.UpdateUser(user); err != nil {
11 years ago
ctx.Handle(200, "setting.SettingPassword", err)
11 years ago
return
}
11 years ago
ctx.Data["IsSuccess"] = true
11 years ago
}
11 years ago
ctx.Data["Owner"] = user
ctx.Render.HTML(200, "user/password", ctx.Data)
11 years ago
log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
11 years ago
}
11 years ago
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = "SSH Keys"
// Delete SSH key.
11 years ago
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
if err != nil {
11 years ago
ctx.Data["ErrorMsg"] = err
log.Error("ssh.DelPublicKey: %v", err)
ctx.Render.JSON(200, map[string]interface{}{
"ok": false,
"err": err.Error(),
})
return
}
k := &models.PublicKey{
Id: id,
11 years ago
OwnerId: ctx.User.Id,
}
if err = models.DeletePublicKey(k); err != nil {
11 years ago
ctx.Data["ErrorMsg"] = err
log.Error("ssh.DelPublicKey: %v", err)
ctx.Render.JSON(200, map[string]interface{}{
"ok": false,
"err": err.Error(),
})
} else {
11 years ago
log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
ctx.Render.JSON(200, map[string]interface{}{
"ok": true,
})
}
return
}
// Add new SSH key.
11 years ago
if ctx.Req.Method == "POST" {
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
ctx.Render.HTML(200, "user/publickey", ctx.Data)
return
}
11 years ago
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
}
11 years ago
ctx.Handle(200, "ssh.AddPublicKey", err)
11 years ago
log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
return
} else {
11 years ago
ctx.Data["AddSSHKeySuccess"] = true
}
}
// List existed SSH keys.
11 years ago
keys, err := models.ListPublicKey(ctx.User.Id)
if err != nil {
11 years ago
ctx.Handle(200, "ssh.ListPublicKey", err)
return
}
11 years ago
ctx.Data["PageIsUserSetting"] = true
11 years ago
ctx.Data["IsUserPageSettingSSH"] = true
11 years ago
ctx.Data["Keys"] = keys
ctx.Render.HTML(200, "user/publickey", ctx.Data)
}
11 years ago
func SettingNotification(ctx *middleware.Context) {
11 years ago
// TODO: user setting notification
11 years ago
ctx.Data["Title"] = "Notification"
ctx.Data["PageIsUserSetting"] = true
11 years ago
ctx.Data["IsUserPageSettingNotify"] = true
ctx.Render.HTML(200, "user/notification", ctx.Data)
}
11 years ago
func SettingSecurity(ctx *middleware.Context) {
11 years ago
// TODO: user setting security
11 years ago
ctx.Data["Title"] = "Security"
ctx.Data["PageIsUserSetting"] = true
11 years ago
ctx.Data["IsUserPageSettingSecurity"] = true
ctx.Render.HTML(200, "user/security", ctx.Data)
}