githaven/routers/user/setting.go

237 lines
6.2 KiB
Go
Raw Normal View History

2014-03-10 08:54:52 +00: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-05-05 20:21:43 +00:00
"strings"
2014-03-11 00:48:58 +00:00
2014-03-10 08:54:52 +00: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 13:17:16 +00:00
"github.com/gogits/gogs/modules/middleware"
2014-03-10 08:54:52 +00:00
)
2014-04-10 20:36:50 +00:00
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")
}
func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
2014-03-15 14:34:33 +00:00
ctx.Data["Title"] = "Setting"
2014-05-05 20:21:43 +00:00
ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSetting"] = true
2014-03-15 14:34:33 +00:00
user := ctx.User
ctx.Data["Owner"] = user
2014-03-14 05:12:07 +00:00
2014-04-10 20:36:50 +00:00
if ctx.HasError() {
2014-03-20 11:50:26 +00:00
ctx.HTML(200, "user/setting")
2014-03-13 07:44:56 +00:00
return
}
2014-04-03 20:33:27 +00:00
// Check if user name has been changed.
if user.Name != form.UserName {
isExist, err := models.IsUserExist(form.UserName)
if err != nil {
2014-04-10 20:36:50 +00:00
ctx.Handle(500, "user.Setting(update: check existence)", err)
2014-04-03 20:33:27 +00:00
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-10 20:36:50 +00:00
ctx.Handle(500, "user.Setting(change user name)", err)
2014-04-03 20:33:27 +00:00
return
}
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName)
user.Name = form.UserName
}
2014-05-01 12:26:41 +00:00
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 05:12:07 +00:00
user.AvatarEmail = form.Avatar
if err := models.UpdateUser(user); err != nil {
2014-04-10 20:36:50 +00:00
ctx.Handle(500, "setting.Setting", err)
return
}
2014-03-19 08:48:45 +00:00
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
2014-04-10 20:36:50 +00:00
ctx.Flash.Success("Your profile has been successfully updated.")
ctx.Redirect("/user/settings")
2014-03-10 08:54:52 +00:00
}
2014-04-14 01:00:12 +00:00
func SettingSocial(ctx *middleware.Context) {
ctx.Data["Title"] = "Social Account"
ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSettingSocial"] = true
2014-05-05 20:21:43 +00:00
// Unbind social account.
remove, _ := base.StrTo(ctx.Query("remove")).Int64()
if remove > 0 {
if err := models.DeleteOauth2ById(remove); err != nil {
ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err)
return
}
ctx.Flash.Success("OAuth2 has been unbinded.")
ctx.Redirect("/user/settings/social")
return
}
2014-04-14 01:00:12 +00:00
socials, err := models.GetOauthByUserId(ctx.User.Id)
if err != nil {
2014-05-05 20:21:43 +00:00
ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err)
2014-04-14 01:00:12 +00:00
return
}
ctx.Data["Socials"] = socials
ctx.HTML(200, "user/social")
}
2014-04-10 22:09:57 +00:00
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 14:34:33 +00:00
ctx.Data["Title"] = "Password"
ctx.Data["PageIsUserSetting"] = true
2014-03-18 22:31:54 +00:00
ctx.Data["IsUserPageSettingPasswd"] = true
2014-03-14 03:24:08 +00:00
2014-04-10 22:09:57 +00:00
if ctx.HasError() {
2014-03-20 11:50:26 +00:00
ctx.HTML(200, "user/password")
2014-03-14 05:12:07 +00:00
return
}
2014-03-13 08:06:35 +00:00
2014-03-15 14:34:33 +00:00
user := ctx.User
2014-04-10 22:09:57 +00:00
tmpUser := &models.User{
Passwd: form.OldPasswd,
Salt: user.Salt,
}
tmpUser.EncodePasswd()
if user.Passwd != tmpUser.Passwd {
2014-05-05 20:21:43 +00:00
ctx.Flash.Error("Old password is not correct.")
2014-03-13 08:06:35 +00:00
} else if form.NewPasswd != form.RetypePasswd {
2014-05-05 20:21:43 +00:00
ctx.Flash.Error("New password and re-type password are not same.")
2014-03-13 08:06:35 +00:00
} else {
2014-04-10 22:09:57 +00:00
user.Passwd = form.NewPasswd
user.Salt = models.GetUserSalt()
user.EncodePasswd()
2014-03-13 08:06:35 +00:00
if err := models.UpdateUser(user); err != nil {
2014-03-15 13:17:16 +00:00
ctx.Handle(200, "setting.SettingPassword", err)
2014-03-13 08:06:35 +00:00
return
}
2014-04-10 22:09:57 +00:00
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 08:06:35 +00:00
}
ctx.Redirect("/user/settings/password")
2014-03-13 08:06:35 +00:00
}
2014-03-15 14:34:33 +00:00
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = "SSH Keys"
2014-05-05 20:21:43 +00:00
ctx.Data["PageIsUserSetting"] = true
ctx.Data["IsUserPageSettingSSH"] = true
2014-03-11 00:48:58 +00:00
// Delete SSH key.
2014-03-15 14:34:33 +00:00
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
2014-05-05 20:21:43 +00:00
id, err := base.StrTo(ctx.Query("id")).Int64()
2014-03-10 13:12:49 +00:00
if err != nil {
log.Error("ssh.DelPublicKey: %v", err)
2014-03-19 13:57:55 +00:00
ctx.JSON(200, map[string]interface{}{
2014-03-10 13:12:49 +00:00
"ok": false,
"err": err.Error(),
})
return
}
k := &models.PublicKey{
Id: id,
2014-03-15 14:34:33 +00:00
OwnerId: ctx.User.Id,
2014-03-10 13:12:49 +00:00
}
2014-03-11 00:48:58 +00:00
if err = models.DeletePublicKey(k); err != nil {
2014-03-10 13:12:49 +00:00
log.Error("ssh.DelPublicKey: %v", err)
2014-03-19 13:57:55 +00:00
ctx.JSON(200, map[string]interface{}{
2014-03-10 13:12:49 +00:00
"ok": false,
"err": err.Error(),
})
} else {
2014-03-19 08:48:45 +00:00
log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
2014-03-19 13:57:55 +00:00
ctx.JSON(200, map[string]interface{}{
2014-03-10 13:12:49 +00:00
"ok": true,
})
}
2014-03-11 03:41:38 +00:00
return
2014-03-10 13:12:49 +00:00
}
2014-03-11 00:48:58 +00:00
2014-05-05 20:21:43 +00:00
// List existed SSH keys.
keys, err := models.ListPublicKey(ctx.User.Id)
if err != nil {
ctx.Handle(500, "ssh.ListPublicKey", err)
return
}
ctx.Data["Keys"] = keys
2014-03-11 00:48:58 +00:00
// Add new SSH key.
2014-03-15 14:34:33 +00:00
if ctx.Req.Method == "POST" {
2014-04-14 01:00:12 +00:00
if ctx.HasError() {
2014-03-20 11:50:26 +00:00
ctx.HTML(200, "user/publickey")
2014-03-11 00:48:58 +00:00
return
}
2014-05-05 20:21:43 +00:00
if len(form.KeyContent) < 100 || !strings.HasPrefix(form.KeyContent, "ssh-rsa") {
ctx.Flash.Error("SSH key content is not valid.")
ctx.Redirect("/user/settings/ssh")
return
}
2014-04-27 21:01:39 +00:00
k := &models.PublicKey{
OwnerId: ctx.User.Id,
2014-03-11 00:48:58 +00:00
Name: form.KeyName,
Content: form.KeyContent,
2014-03-10 08:54:52 +00:00
}
2014-03-11 00:48:58 +00: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-04-14 01:00:12 +00:00
ctx.Handle(500, "ssh.AddPublicKey", err)
2014-03-10 08:54:52 +00:00
return
} else {
2014-04-14 01:00:12 +00:00
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 01:00:12 +00:00
return
2014-03-10 08:54:52 +00:00
}
}
2014-03-11 00:48:58 +00:00
2014-03-20 11:50:26 +00:00
ctx.HTML(200, "user/publickey")
2014-03-10 08:54:52 +00:00
}
2014-03-14 09:12:28 +00:00
2014-03-15 14:34:33 +00:00
func SettingNotification(ctx *middleware.Context) {
2014-03-18 22:31:54 +00:00
// TODO: user setting notification
2014-03-15 14:34:33 +00:00
ctx.Data["Title"] = "Notification"
ctx.Data["PageIsUserSetting"] = true
2014-03-18 22:31:54 +00:00
ctx.Data["IsUserPageSettingNotify"] = true
2014-03-20 11:50:26 +00:00
ctx.HTML(200, "user/notification")
2014-03-14 09:12:28 +00:00
}
2014-03-15 14:34:33 +00:00
func SettingSecurity(ctx *middleware.Context) {
2014-03-18 22:31:54 +00:00
// TODO: user setting security
2014-03-15 14:34:33 +00:00
ctx.Data["Title"] = "Security"
ctx.Data["PageIsUserSetting"] = true
2014-03-18 22:31:54 +00:00
ctx.Data["IsUserPageSettingSecurity"] = true
2014-03-20 11:50:26 +00:00
ctx.HTML(200, "user/security")
2014-03-14 09:12:28 +00:00
}