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.

118 lines
2.8 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "net/http"
  7. "strconv"
  8. "github.com/martini-contrib/render"
  9. "github.com/martini-contrib/sessions"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. )
  15. func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
  16. data["Title"] = "Setting"
  17. data["PageIsUserSetting"] = true
  18. user := auth.SignedInUser(session)
  19. if req.Method == "GET" {
  20. data["Owner"] = user
  21. }
  22. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  23. r.HTML(200, "user/setting", data)
  24. return
  25. }
  26. user.Email = form.Email
  27. user.Website = form.Website
  28. user.Location = form.Location
  29. user.Avatar = base.EncodeMd5(form.Avatar)
  30. if err := models.UpdateUser(user); err != nil {
  31. data["ErrorMsg"] = err
  32. log.Error("setting.Setting: %v", err)
  33. r.HTML(200, "base/error", data)
  34. return
  35. }
  36. r.HTML(200, "user/setting", data)
  37. }
  38. func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
  39. data["Title"] = "SSH Keys"
  40. // Delete SSH key.
  41. if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" {
  42. println(1)
  43. id, err := strconv.ParseInt(req.FormValue("id"), 10, 64)
  44. if err != nil {
  45. data["ErrorMsg"] = err
  46. log.Error("ssh.DelPublicKey: %v", err)
  47. r.JSON(200, map[string]interface{}{
  48. "ok": false,
  49. "err": err.Error(),
  50. })
  51. return
  52. }
  53. k := &models.PublicKey{
  54. Id: id,
  55. OwnerId: auth.SignedInId(session),
  56. }
  57. if err = models.DeletePublicKey(k); err != nil {
  58. data["ErrorMsg"] = err
  59. log.Error("ssh.DelPublicKey: %v", err)
  60. r.JSON(200, map[string]interface{}{
  61. "ok": false,
  62. "err": err.Error(),
  63. })
  64. } else {
  65. r.JSON(200, map[string]interface{}{
  66. "ok": true,
  67. })
  68. }
  69. return
  70. }
  71. // Add new SSH key.
  72. if req.Method == "POST" {
  73. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  74. r.HTML(200, "user/publickey", data)
  75. return
  76. }
  77. k := &models.PublicKey{OwnerId: auth.SignedInId(session),
  78. Name: form.KeyName,
  79. Content: form.KeyContent,
  80. }
  81. if err := models.AddPublicKey(k); err != nil {
  82. data["ErrorMsg"] = err
  83. log.Error("ssh.AddPublicKey: %v", err)
  84. r.HTML(200, "base/error", data)
  85. return
  86. } else {
  87. data["AddSSHKeySuccess"] = true
  88. }
  89. }
  90. // List existed SSH keys.
  91. keys, err := models.ListPublicKey(auth.SignedInId(session))
  92. if err != nil {
  93. data["ErrorMsg"] = err
  94. log.Error("ssh.ListPublicKey: %v", err)
  95. r.HTML(200, "base/error", data)
  96. return
  97. }
  98. data["PageIsUserSetting"] = true
  99. data["Keys"] = keys
  100. r.HTML(200, "user/publickey", data)
  101. }