forgejo-federation/routers/user/setting.go

219 lines
5.8 KiB
Go
Raw Normal View History

2014-03-10 14:24:52 +05:30
// 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 06:18:58 +05:30
"strconv"
2014-03-10 14:24:52 +05:30
"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 18:47:16 +05:30
"github.com/gogits/gogs/modules/middleware"
2014-03-10 14:24:52 +05:30
)
2014-04-11 02:06:50 +05:30
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")
}
2014-03-19 04:01:54 +05:30
// Render user setting page (email, website modify)
2014-04-11 02:06:50 +05:30
func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
2014-03-15 20:04:33 +05:30
ctx.Data["Title"] = "Setting"
2014-03-19 04:01:54 +05:30
ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
2014-03-15 20:04:33 +05:30
user := ctx.User
ctx.Data["Owner"] = user
2014-03-14 10:42:07 +05:30
2014-04-11 02:06:50 +05:30
if ctx.HasError() {
2014-03-20 17:20:26 +05:30
ctx.HTML(200, "user/setting")
2014-03-13 13:14:56 +05:30
return
}
2014-04-04 02:03:27 +05:30
// Check if user name has been changed.
if user.Name != form.UserName {
isExist, err := models.IsUserExist(form.UserName)
if err != nil {
2014-04-11 02:06:50 +05:30
ctx.Handle(500, "user.Setting(update: check existence)", err)
2014-04-04 02:03:27 +05:30
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 {
2014-04-11 02:06:50 +05:30
ctx.Handle(500, "user.Setting(change user name)", err)
2014-04-04 02:03:27 +05:30
return
}
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName)
user.Name = form.UserName
}
2014-05-01 17:56:41 +05:30
user.FullName = form.FullName
user.Email = form.Email
user.Website = form.Website
user.Location = form.Location
user.Avatar = base.EncodeMd5(form.Avatar)
2014-03-14 10:42:07 +05:30
user.AvatarEmail = form.Avatar
if err := models.UpdateUser(user); err != nil {
2014-04-11 02:06:50 +05:30
ctx.Handle(500, "setting.Setting", err)
return
}
2014-03-19 14:18:45 +05:30
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
2014-04-11 02:06:50 +05:30
ctx.Flash.Success("Your profile has been successfully updated.")
ctx.Redirect("/user/settings")
2014-03-10 14:24:52 +05:30
}
2014-04-14 06:30:12 +05:30
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")
}
2014-04-11 03:39:57 +05:30
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) {
2014-03-15 20:04:33 +05:30
ctx.Data["Title"] = "Password"
ctx.Data["PageIsUserSetting"] = true
2014-03-19 04:01:54 +05:30
ctx.Data["IsUserPageSettingPasswd"] = true
2014-03-14 08:54:08 +05:30
2014-04-11 03:39:57 +05:30
if ctx.HasError() {
2014-03-20 17:20:26 +05:30
ctx.HTML(200, "user/password")
2014-03-14 10:42:07 +05:30
return
}
2014-03-13 13:36:35 +05:30
2014-03-15 20:04:33 +05:30
user := ctx.User
2014-04-11 03:39:57 +05:30
tmpUser := &models.User{
Passwd: form.OldPasswd,
Salt: user.Salt,
}
tmpUser.EncodePasswd()
if user.Passwd != tmpUser.Passwd {
ctx.Flash.Error("Old password is not correct")
2014-03-13 13:36:35 +05:30
} else if form.NewPasswd != form.RetypePasswd {
2014-04-11 03:39:57 +05:30
ctx.Flash.Error("New password and re-type password are not same")
2014-03-13 13:36:35 +05:30
} else {
2014-04-11 03:39:57 +05:30
user.Passwd = form.NewPasswd
user.Salt = models.GetUserSalt()
user.EncodePasswd()
2014-03-13 13:36:35 +05:30
if err := models.UpdateUser(user); err != nil {
2014-03-15 18:47:16 +05:30
ctx.Handle(200, "setting.SettingPassword", err)
2014-03-13 13:36:35 +05:30
return
}
2014-04-11 03:39:57 +05:30
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.")
2014-03-13 13:36:35 +05:30
}
ctx.Redirect("/user/settings/password")
2014-03-13 13:36:35 +05:30
}
2014-03-15 20:04:33 +05:30
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = "SSH Keys"
2014-03-11 06:18:58 +05:30
// Delete SSH key.
2014-03-15 20:04:33 +05:30
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
2014-03-10 18:42:49 +05:30
if err != nil {
log.Error("ssh.DelPublicKey: %v", err)
2014-03-19 19:27:55 +05:30
ctx.JSON(200, map[string]interface{}{
2014-03-10 18:42:49 +05:30
"ok": false,
"err": err.Error(),
})
return
}
k := &models.PublicKey{
Id: id,
2014-03-15 20:04:33 +05:30
OwnerId: ctx.User.Id,
2014-03-10 18:42:49 +05:30
}
2014-03-11 06:18:58 +05:30
if err = models.DeletePublicKey(k); err != nil {
2014-03-10 18:42:49 +05:30
log.Error("ssh.DelPublicKey: %v", err)
2014-03-19 19:27:55 +05:30
ctx.JSON(200, map[string]interface{}{
2014-03-10 18:42:49 +05:30
"ok": false,
"err": err.Error(),
})
} else {
2014-03-19 14:18:45 +05:30
log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
2014-03-19 19:27:55 +05:30
ctx.JSON(200, map[string]interface{}{
2014-03-10 18:42:49 +05:30
"ok": true,
})
}
2014-03-11 09:11:38 +05:30
return
2014-03-10 18:42:49 +05:30
}
2014-03-11 06:18:58 +05:30
// Add new SSH key.
2014-03-15 20:04:33 +05:30
if ctx.Req.Method == "POST" {
2014-04-14 06:30:12 +05:30
if ctx.HasError() {
2014-03-20 17:20:26 +05:30
ctx.HTML(200, "user/publickey")
2014-03-11 06:18:58 +05:30
return
}
2014-04-28 02:31:39 +05:30
k := &models.PublicKey{
OwnerId: ctx.User.Id,
2014-03-11 06:18:58 +05:30
Name: form.KeyName,
Content: form.KeyContent,
2014-03-10 14:24:52 +05:30
}
2014-03-11 06:18:58 +05:30
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-04-14 06:30:12 +05:30
ctx.Handle(500, "ssh.AddPublicKey", err)
2014-03-10 14:24:52 +05:30
return
} else {
2014-04-14 06:30:12 +05:30
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")
2014-04-14 06:30:12 +05:30
return
2014-03-10 14:24:52 +05:30
}
}
2014-03-11 06:18:58 +05:30
// List existed SSH keys.
2014-03-15 20:04:33 +05:30
keys, err := models.ListPublicKey(ctx.User.Id)
2014-03-10 14:24:52 +05:30
if err != nil {
2014-03-15 20:22:14 +05:30
ctx.Handle(200, "ssh.ListPublicKey", err)
2014-03-10 14:24:52 +05:30
return
}
2014-03-15 20:04:33 +05:30
ctx.Data["PageIsUserSetting"] = true
2014-03-19 04:01:54 +05:30
ctx.Data["IsUserPageSettingSSH"] = true
2014-03-15 20:04:33 +05:30
ctx.Data["Keys"] = keys
2014-03-20 17:20:26 +05:30
ctx.HTML(200, "user/publickey")
2014-03-10 14:24:52 +05:30
}
2014-03-14 14:42:28 +05:30
2014-03-15 20:04:33 +05:30
func SettingNotification(ctx *middleware.Context) {
2014-03-19 04:01:54 +05:30
// TODO: user setting notification
2014-03-15 20:04:33 +05:30
ctx.Data["Title"] = "Notification"
ctx.Data["PageIsUserSetting"] = true
2014-03-19 04:01:54 +05:30
ctx.Data["IsUserPageSettingNotify"] = true
2014-03-20 17:20:26 +05:30
ctx.HTML(200, "user/notification")
2014-03-14 14:42:28 +05:30
}
2014-03-15 20:04:33 +05:30
func SettingSecurity(ctx *middleware.Context) {
2014-03-19 04:01:54 +05:30
// TODO: user setting security
2014-03-15 20:04:33 +05:30
ctx.Data["Title"] = "Security"
ctx.Data["PageIsUserSetting"] = true
2014-03-19 04:01:54 +05:30
ctx.Data["IsUserPageSettingSecurity"] = true
2014-03-20 17:20:26 +05:30
ctx.HTML(200, "user/security")
2014-03-14 14:42:28 +05:30
}