forgejo/routers/user/setting.go

174 lines
4.5 KiB
Go
Raw Normal View History

2014-03-10 17:54:52 +09:00
// 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 (
2014-03-11 09:48:58 +09:00
"strconv"
2014-03-10 17:54:52 +09:00
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
2014-03-15 22:17:16 +09:00
"github.com/gogits/gogs/modules/middleware"
2014-03-10 17:54:52 +09:00
)
2014-03-19 07:31:54 +09:00
// Render user setting page (email, website modify)
2014-03-15 23:34:33 +09:00
func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.Data["Title"] = "Setting"
2014-03-19 07:31:54 +09:00
ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
2014-03-15 23:34:33 +09:00
user := ctx.User
ctx.Data["Owner"] = user
2014-03-14 14:12:07 +09:00
2014-03-15 23:34:33 +09:00
if ctx.Req.Method == "GET" {
ctx.Render.HTML(200, "user/setting", ctx.Data)
2014-03-13 16:44:56 +09:00
return
}
2014-03-18 22:58:58 +09:00
// below is for POST requests
2014-03-15 23:34:33 +09:00
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)
2014-03-14 14:12:07 +09:00
user.AvatarEmail = form.Avatar
if err := models.UpdateUser(user); err != nil {
2014-03-15 22:17:16 +09:00
ctx.Handle(200, "setting.Setting", err)
return
}
2014-03-15 23:34:33 +09:00
ctx.Data["IsSuccess"] = true
ctx.Render.HTML(200, "user/setting", ctx.Data)
2014-03-10 17:54:52 +09:00
}
2014-03-15 23:34:33 +09:00
func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) {
ctx.Data["Title"] = "Password"
ctx.Data["PageIsUserSetting"] = true
2014-03-19 07:31:54 +09:00
ctx.Data["IsUserPageSettingPasswd"] = true
2014-03-14 12:24:08 +09:00
2014-03-15 23:34:33 +09:00
if ctx.Req.Method == "GET" {
ctx.Render.HTML(200, "user/password", ctx.Data)
2014-03-14 14:12:07 +09:00
return
}
2014-03-13 17:06:35 +09:00
2014-03-15 23:34:33 +09:00
user := ctx.User
2014-03-14 14:12:07 +09:00
newUser := &models.User{Passwd: form.NewPasswd}
2014-03-13 17:06:35 +09:00
if err := newUser.EncodePasswd(); err != nil {
2014-03-15 22:17:16 +09:00
ctx.Handle(200, "setting.SettingPassword", err)
2014-03-13 17:06:35 +09:00
return
}
if user.Passwd != newUser.Passwd {
2014-03-15 23:34:33 +09:00
ctx.Data["HasError"] = true
ctx.Data["ErrorMsg"] = "Old password is not correct"
2014-03-13 17:06:35 +09:00
} else if form.NewPasswd != form.RetypePasswd {
2014-03-15 23:34:33 +09:00
ctx.Data["HasError"] = true
ctx.Data["ErrorMsg"] = "New password and re-type password are not same"
2014-03-13 17:06:35 +09:00
} else {
user.Passwd = newUser.Passwd
if err := models.UpdateUser(user); err != nil {
2014-03-15 22:17:16 +09:00
ctx.Handle(200, "setting.SettingPassword", err)
2014-03-13 17:06:35 +09:00
return
}
2014-03-15 23:34:33 +09:00
ctx.Data["IsSuccess"] = true
2014-03-13 17:06:35 +09:00
}
2014-03-15 23:34:33 +09:00
ctx.Data["Owner"] = user
ctx.Render.HTML(200, "user/password", ctx.Data)
2014-03-13 17:06:35 +09:00
}
2014-03-15 23:34:33 +09:00
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = "SSH Keys"
2014-03-11 09:48:58 +09:00
// Delete SSH key.
2014-03-15 23:34:33 +09:00
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
2014-03-10 22:12:49 +09:00
if err != nil {
2014-03-15 23:34:33 +09:00
ctx.Data["ErrorMsg"] = err
2014-03-10 22:12:49 +09:00
log.Error("ssh.DelPublicKey: %v", err)
2014-03-15 23:34:33 +09:00
ctx.Render.JSON(200, map[string]interface{}{
2014-03-10 22:12:49 +09:00
"ok": false,
"err": err.Error(),
})
return
}
k := &models.PublicKey{
Id: id,
2014-03-15 23:34:33 +09:00
OwnerId: ctx.User.Id,
2014-03-10 22:12:49 +09:00
}
2014-03-11 09:48:58 +09:00
if err = models.DeletePublicKey(k); err != nil {
2014-03-15 23:34:33 +09:00
ctx.Data["ErrorMsg"] = err
2014-03-10 22:12:49 +09:00
log.Error("ssh.DelPublicKey: %v", err)
2014-03-15 23:34:33 +09:00
ctx.Render.JSON(200, map[string]interface{}{
2014-03-10 22:12:49 +09:00
"ok": false,
"err": err.Error(),
})
} else {
2014-03-15 23:34:33 +09:00
ctx.Render.JSON(200, map[string]interface{}{
2014-03-10 22:12:49 +09:00
"ok": true,
})
}
2014-03-11 12:41:38 +09:00
return
2014-03-10 22:12:49 +09:00
}
2014-03-11 09:48:58 +09:00
// Add new SSH key.
2014-03-15 23:34:33 +09:00
if ctx.Req.Method == "POST" {
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
ctx.Render.HTML(200, "user/publickey", ctx.Data)
2014-03-11 09:48:58 +09:00
return
}
2014-03-15 23:34:33 +09:00
k := &models.PublicKey{OwnerId: ctx.User.Id,
2014-03-11 09:48:58 +09:00
Name: form.KeyName,
Content: form.KeyContent,
2014-03-10 17:54:52 +09:00
}
2014-03-11 09:48:58 +09:00
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
}
2014-03-15 23:52:14 +09:00
ctx.Handle(200, "ssh.AddPublicKey", err)
2014-03-10 17:54:52 +09:00
return
} else {
2014-03-15 23:34:33 +09:00
ctx.Data["AddSSHKeySuccess"] = true
2014-03-10 17:54:52 +09:00
}
}
2014-03-11 09:48:58 +09:00
// List existed SSH keys.
2014-03-15 23:34:33 +09:00
keys, err := models.ListPublicKey(ctx.User.Id)
2014-03-10 17:54:52 +09:00
if err != nil {
2014-03-15 23:52:14 +09:00
ctx.Handle(200, "ssh.ListPublicKey", err)
2014-03-10 17:54:52 +09:00
return
}
2014-03-15 23:34:33 +09:00
ctx.Data["PageIsUserSetting"] = true
2014-03-19 07:31:54 +09:00
ctx.Data["IsUserPageSettingSSH"] = true
2014-03-15 23:34:33 +09:00
ctx.Data["Keys"] = keys
ctx.Render.HTML(200, "user/publickey", ctx.Data)
2014-03-10 17:54:52 +09:00
}
2014-03-14 18:12:28 +09:00
2014-03-15 23:34:33 +09:00
func SettingNotification(ctx *middleware.Context) {
2014-03-19 07:31:54 +09:00
// TODO: user setting notification
2014-03-15 23:34:33 +09:00
ctx.Data["Title"] = "Notification"
ctx.Data["PageIsUserSetting"] = true
2014-03-19 07:31:54 +09:00
ctx.Data["IsUserPageSettingNotify"] = true
2014-03-15 23:34:33 +09:00
ctx.Render.HTML(200, "user/notification", ctx.Data)
2014-03-14 18:12:28 +09:00
}
2014-03-15 23:34:33 +09:00
func SettingSecurity(ctx *middleware.Context) {
2014-03-19 07:31:54 +09:00
// TODO: user setting security
2014-03-15 23:34:33 +09:00
ctx.Data["Title"] = "Security"
ctx.Data["PageIsUserSetting"] = true
2014-03-19 07:31:54 +09:00
ctx.Data["IsUserPageSettingSecurity"] = true
2014-03-15 23:34:33 +09:00
ctx.Render.HTML(200, "user/security", ctx.Data)
2014-03-14 18:12:28 +09:00
}