2014-03-10 03:54:52 -05: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-07-20 11:02:59 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-05-05 15:21:43 -05:00
|
|
|
"strings"
|
2014-03-10 19:48:58 -05:00
|
|
|
|
2014-03-10 03:54:52 -05: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 08:17:16 -05:00
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
2014-07-20 11:02:59 -05:00
|
|
|
"github.com/gogits/gogs/modules/process"
|
2014-03-10 03:54:52 -05:00
|
|
|
)
|
|
|
|
|
2014-06-22 22:11:12 -05:00
|
|
|
const (
|
|
|
|
SETTING base.TplName = "user/setting"
|
|
|
|
SOCIAL base.TplName = "user/social"
|
|
|
|
PASSWORD base.TplName = "user/password"
|
|
|
|
PUBLICKEY base.TplName = "user/publickey"
|
|
|
|
NOTIFICATION base.TplName = "user/notification"
|
|
|
|
SECURITY base.TplName = "user/security"
|
|
|
|
)
|
|
|
|
|
2014-07-20 11:02:59 -05:00
|
|
|
var (
|
|
|
|
MinimumKeySize = map[string]int{
|
|
|
|
"(ED25519)": 256,
|
|
|
|
"(ECDSA)": 256,
|
|
|
|
"(NTRU)": 1087,
|
|
|
|
"(MCE)": 1702,
|
|
|
|
"(McE)": 1702,
|
|
|
|
"(RSA)": 2048,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-04-10 15:36:50 -05:00
|
|
|
func Setting(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "Setting"
|
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
|
|
|
ctx.Data["IsUserPageSetting"] = true
|
|
|
|
ctx.Data["Owner"] = ctx.User
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, SETTING)
|
2014-04-10 15:36:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
|
2014-03-15 09:34:33 -05:00
|
|
|
ctx.Data["Title"] = "Setting"
|
2014-05-05 15:21:43 -05:00
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
|
|
|
ctx.Data["IsUserPageSetting"] = true
|
2014-03-13 02:39:18 -05:00
|
|
|
|
2014-04-10 15:36:50 -05:00
|
|
|
if ctx.HasError() {
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, SETTING)
|
2014-03-13 02:44:56 -05:00
|
|
|
return
|
2014-03-13 02:39:18 -05:00
|
|
|
}
|
|
|
|
|
2014-05-24 14:28:31 -05:00
|
|
|
ctx.Data["Owner"] = ctx.User
|
|
|
|
|
2014-04-03 15:33:27 -05:00
|
|
|
// Check if user name has been changed.
|
2014-05-24 14:28:31 -05:00
|
|
|
if ctx.User.Name != form.UserName {
|
2014-04-03 15:33:27 -05:00
|
|
|
isExist, err := models.IsUserExist(form.UserName)
|
|
|
|
if err != nil {
|
2014-07-11 23:55:19 -05:00
|
|
|
ctx.Handle(500, "user.SettingPost(update: check existence)", err)
|
2014-04-03 15:33:27 -05:00
|
|
|
return
|
|
|
|
} else if isExist {
|
2014-07-11 23:55:19 -05:00
|
|
|
ctx.RenderWithErr("User name has been taken.", SETTING, &form)
|
2014-04-03 15:33:27 -05:00
|
|
|
return
|
2014-05-24 14:28:31 -05:00
|
|
|
} else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
|
2014-07-11 23:55:19 -05:00
|
|
|
ctx.Handle(500, "user.SettingPost(change user name)", err)
|
2014-04-03 15:33:27 -05:00
|
|
|
return
|
|
|
|
}
|
2014-05-24 14:28:31 -05:00
|
|
|
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, ctx.User.Name, form.UserName)
|
2014-04-03 15:33:27 -05:00
|
|
|
|
2014-05-24 14:28:31 -05:00
|
|
|
ctx.User.Name = form.UserName
|
2014-03-13 02:39:18 -05:00
|
|
|
}
|
|
|
|
|
2014-05-24 14:28:31 -05:00
|
|
|
ctx.User.FullName = form.FullName
|
|
|
|
ctx.User.Email = form.Email
|
|
|
|
ctx.User.Website = form.Website
|
|
|
|
ctx.User.Location = form.Location
|
|
|
|
ctx.User.Avatar = base.EncodeMd5(form.Avatar)
|
|
|
|
ctx.User.AvatarEmail = form.Avatar
|
|
|
|
if err := models.UpdateUser(ctx.User); err != nil {
|
2014-07-11 23:55:19 -05:00
|
|
|
ctx.Handle(500, "setting.SettingPost(UpdateUser)", err)
|
2014-03-13 02:39:18 -05:00
|
|
|
return
|
|
|
|
}
|
2014-03-19 03:48:45 -05:00
|
|
|
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
2014-04-10 15:36:50 -05:00
|
|
|
ctx.Flash.Success("Your profile has been successfully updated.")
|
2014-04-24 13:50:24 -05:00
|
|
|
ctx.Redirect("/user/settings")
|
2014-03-10 03:54:52 -05:00
|
|
|
}
|
|
|
|
|
2014-04-13 20:00:12 -05:00
|
|
|
func SettingSocial(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "Social Account"
|
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
|
|
|
ctx.Data["IsUserPageSettingSocial"] = true
|
2014-05-05 15:21:43 -05: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-05-24 14:28:31 -05:00
|
|
|
var err error
|
|
|
|
ctx.Data["Socials"], err = models.GetOauthByUserId(ctx.User.Id)
|
2014-04-13 20:00:12 -05:00
|
|
|
if err != nil {
|
2014-05-05 15:21:43 -05:00
|
|
|
ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err)
|
2014-04-13 20:00:12 -05:00
|
|
|
return
|
|
|
|
}
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, SOCIAL)
|
2014-04-13 20:00:12 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 17:09:57 -05:00
|
|
|
func SettingPassword(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "Password"
|
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
|
|
|
ctx.Data["IsUserPageSettingPasswd"] = true
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, PASSWORD)
|
2014-04-10 17:09:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
|
2014-03-15 09:34:33 -05:00
|
|
|
ctx.Data["Title"] = "Password"
|
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
2014-03-18 17:31:54 -05:00
|
|
|
ctx.Data["IsUserPageSettingPasswd"] = true
|
2014-03-13 22:24:08 -05:00
|
|
|
|
2014-04-10 17:09:57 -05:00
|
|
|
if ctx.HasError() {
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, PASSWORD)
|
2014-03-14 00:12:07 -05:00
|
|
|
return
|
|
|
|
}
|
2014-03-13 03:06:35 -05:00
|
|
|
|
2014-04-10 17:09:57 -05:00
|
|
|
tmpUser := &models.User{
|
|
|
|
Passwd: form.OldPasswd,
|
2014-05-24 14:28:31 -05:00
|
|
|
Salt: ctx.User.Salt,
|
2014-04-10 17:09:57 -05:00
|
|
|
}
|
|
|
|
tmpUser.EncodePasswd()
|
2014-05-24 14:28:31 -05:00
|
|
|
if ctx.User.Passwd != tmpUser.Passwd {
|
2014-05-05 15:21:43 -05:00
|
|
|
ctx.Flash.Error("Old password is not correct.")
|
2014-03-13 03:06:35 -05:00
|
|
|
} else if form.NewPasswd != form.RetypePasswd {
|
2014-05-05 15:21:43 -05:00
|
|
|
ctx.Flash.Error("New password and re-type password are not same.")
|
2014-03-13 03:06:35 -05:00
|
|
|
} else {
|
2014-05-24 14:28:31 -05:00
|
|
|
ctx.User.Passwd = form.NewPasswd
|
|
|
|
ctx.User.Salt = models.GetUserSalt()
|
|
|
|
ctx.User.EncodePasswd()
|
|
|
|
if err := models.UpdateUser(ctx.User); err != nil {
|
2014-03-15 08:17:16 -05:00
|
|
|
ctx.Handle(200, "setting.SettingPassword", err)
|
2014-03-13 03:06:35 -05:00
|
|
|
return
|
|
|
|
}
|
2014-04-10 17:09:57 -05: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 03:06:35 -05:00
|
|
|
}
|
2014-04-24 13:50:24 -05:00
|
|
|
ctx.Redirect("/user/settings/password")
|
2014-03-13 03:06:35 -05:00
|
|
|
}
|
|
|
|
|
2014-07-20 11:02:59 -05:00
|
|
|
// Checks if the given public key string is recognized by SSH.
|
|
|
|
func CheckPublicKeyString(keyContent string) (ok bool, err error) {
|
|
|
|
if strings.ContainsAny(keyContent, "\n\r") {
|
|
|
|
return false, errors.New("Only a single line with a single key please")
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the key to a file…
|
|
|
|
tmpFile, err := ioutil.TempFile(os.TempDir(), "keytest")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
tmpPath := tmpFile.Name()
|
|
|
|
defer os.Remove(tmpPath)
|
|
|
|
tmpFile.WriteString(keyContent)
|
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
// … see if ssh-keygen recognizes its contents
|
|
|
|
stdout, stderr, err := process.Exec("CheckPublicKeyString", "ssh-keygen", "-l", "-f", tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("ssh-keygen -l -f: " + stderr)
|
|
|
|
} else if len(stdout) < 2 {
|
|
|
|
return false, errors.New("ssh-keygen returned not enough output to evaluate the key")
|
|
|
|
}
|
|
|
|
sshKeygenOutput := strings.Split(stdout, " ")
|
|
|
|
if len(sshKeygenOutput) < 4 {
|
|
|
|
return false, errors.New("Not enough fields returned by ssh-keygen -l -f")
|
|
|
|
}
|
|
|
|
keySize, err := strconv.Atoi(sshKeygenOutput[0])
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("Cannot get key size of the given key")
|
|
|
|
}
|
|
|
|
keyType := strings.TrimSpace(sshKeygenOutput[len(sshKeygenOutput)-1])
|
|
|
|
|
|
|
|
if minimumKeySize := MinimumKeySize[keyType]; minimumKeySize == 0 {
|
|
|
|
return false, errors.New("Sorry, unrecognized public key type")
|
|
|
|
} else {
|
|
|
|
if keySize < minimumKeySize {
|
|
|
|
return false, fmt.Errorf("The minimum accepted size of a public key %s is %d", keyType, minimumKeySize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-03-15 09:34:33 -05:00
|
|
|
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
|
|
|
|
ctx.Data["Title"] = "SSH Keys"
|
2014-05-05 15:21:43 -05:00
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
|
|
|
ctx.Data["IsUserPageSettingSSH"] = true
|
2014-03-10 19:48:58 -05:00
|
|
|
|
|
|
|
// Delete SSH key.
|
2014-03-15 09:34:33 -05:00
|
|
|
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
|
2014-05-05 15:21:43 -05:00
|
|
|
id, err := base.StrTo(ctx.Query("id")).Int64()
|
2014-03-10 08:12:49 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("ssh.DelPublicKey: %v", err)
|
2014-03-19 08:57:55 -05:00
|
|
|
ctx.JSON(200, map[string]interface{}{
|
2014-03-10 08:12:49 -05:00
|
|
|
"ok": false,
|
|
|
|
"err": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2014-03-10 19:48:58 -05:00
|
|
|
|
2014-05-06 15:28:52 -05:00
|
|
|
if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil {
|
2014-03-10 08:12:49 -05:00
|
|
|
log.Error("ssh.DelPublicKey: %v", err)
|
2014-03-19 08:57:55 -05:00
|
|
|
ctx.JSON(200, map[string]interface{}{
|
2014-03-10 08:12:49 -05:00
|
|
|
"ok": false,
|
|
|
|
"err": err.Error(),
|
|
|
|
})
|
|
|
|
} else {
|
2014-03-19 03:48:45 -05:00
|
|
|
log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
2014-03-19 08:57:55 -05:00
|
|
|
ctx.JSON(200, map[string]interface{}{
|
2014-03-10 08:12:49 -05:00
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
2014-03-10 22:41:38 -05:00
|
|
|
return
|
2014-03-10 08:12:49 -05:00
|
|
|
}
|
2014-03-10 19:48:58 -05:00
|
|
|
|
2014-05-24 14:28:31 -05:00
|
|
|
var err error
|
2014-05-05 15:21:43 -05:00
|
|
|
// List existed SSH keys.
|
2014-05-24 14:28:31 -05:00
|
|
|
ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
|
2014-05-05 15:21:43 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "ssh.ListPublicKey", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:48:58 -05:00
|
|
|
// Add new SSH key.
|
2014-03-15 09:34:33 -05:00
|
|
|
if ctx.Req.Method == "POST" {
|
2014-04-13 20:00:12 -05:00
|
|
|
if ctx.HasError() {
|
2014-03-20 06:50:26 -05:00
|
|
|
ctx.HTML(200, "user/publickey")
|
2014-03-10 19:48:58 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-20 11:02:59 -05:00
|
|
|
if ok, err := CheckPublicKeyString(form.KeyContent); !ok {
|
|
|
|
ctx.Flash.Error(err.Error())
|
2014-05-05 15:21:43 -05:00
|
|
|
ctx.Redirect("/user/settings/ssh")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-27 16:01:39 -05:00
|
|
|
k := &models.PublicKey{
|
|
|
|
OwnerId: ctx.User.Id,
|
2014-03-10 19:48:58 -05:00
|
|
|
Name: form.KeyName,
|
|
|
|
Content: form.KeyContent,
|
2014-03-10 03:54:52 -05:00
|
|
|
}
|
2014-03-10 19:48:58 -05:00
|
|
|
|
|
|
|
if err := models.AddPublicKey(k); err != nil {
|
2014-03-16 05:25:16 -05:00
|
|
|
if err.Error() == models.ErrKeyAlreadyExist.Error() {
|
|
|
|
ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
|
|
|
|
return
|
|
|
|
}
|
2014-04-13 20:00:12 -05:00
|
|
|
ctx.Handle(500, "ssh.AddPublicKey", err)
|
2014-03-10 03:54:52 -05:00
|
|
|
return
|
|
|
|
} else {
|
2014-04-13 20:00:12 -05:00
|
|
|
log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
|
|
|
ctx.Flash.Success("New SSH Key has been added!")
|
2014-04-24 13:50:24 -05:00
|
|
|
ctx.Redirect("/user/settings/ssh")
|
2014-04-13 20:00:12 -05:00
|
|
|
return
|
2014-03-10 03:54:52 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 19:48:58 -05:00
|
|
|
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, PUBLICKEY)
|
2014-03-10 03:54:52 -05:00
|
|
|
}
|
2014-03-14 04:12:28 -05:00
|
|
|
|
2014-03-15 09:34:33 -05:00
|
|
|
func SettingNotification(ctx *middleware.Context) {
|
2014-03-18 17:31:54 -05:00
|
|
|
// TODO: user setting notification
|
2014-03-15 09:34:33 -05:00
|
|
|
ctx.Data["Title"] = "Notification"
|
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
2014-03-18 17:31:54 -05:00
|
|
|
ctx.Data["IsUserPageSettingNotify"] = true
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, NOTIFICATION)
|
2014-03-14 04:12:28 -05:00
|
|
|
}
|
|
|
|
|
2014-03-15 09:34:33 -05:00
|
|
|
func SettingSecurity(ctx *middleware.Context) {
|
2014-03-18 17:31:54 -05:00
|
|
|
// TODO: user setting security
|
2014-03-15 09:34:33 -05:00
|
|
|
ctx.Data["Title"] = "Security"
|
|
|
|
ctx.Data["PageIsUserSetting"] = true
|
2014-03-18 17:31:54 -05:00
|
|
|
ctx.Data["IsUserPageSettingSecurity"] = true
|
2014-06-22 22:11:12 -05:00
|
|
|
ctx.HTML(200, SECURITY)
|
2014-03-14 04:12:28 -05:00
|
|
|
}
|