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.

96 lines
2.3 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(r render.Render, data base.TmplData, session sessions.Session) {
  16. data["Title"] = "Setting"
  17. data["PageIsUserSetting"] = true
  18. r.HTML(200, "user/setting", data)
  19. }
  20. func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
  21. data["Title"] = "SSH Keys"
  22. // Delete SSH key.
  23. if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" {
  24. println(1)
  25. id, err := strconv.ParseInt(req.FormValue("id"), 10, 64)
  26. if err != nil {
  27. data["ErrorMsg"] = err
  28. log.Error("ssh.DelPublicKey: %v", err)
  29. r.JSON(200, map[string]interface{}{
  30. "ok": false,
  31. "err": err.Error(),
  32. })
  33. return
  34. }
  35. k := &models.PublicKey{
  36. Id: id,
  37. OwnerId: auth.SignedInId(session),
  38. }
  39. if err = models.DeletePublicKey(k); err != nil {
  40. data["ErrorMsg"] = err
  41. log.Error("ssh.DelPublicKey: %v", err)
  42. r.JSON(200, map[string]interface{}{
  43. "ok": false,
  44. "err": err.Error(),
  45. })
  46. } else {
  47. r.JSON(200, map[string]interface{}{
  48. "ok": true,
  49. })
  50. }
  51. return
  52. }
  53. // Add new SSH key.
  54. if req.Method == "POST" {
  55. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  56. r.HTML(200, "user/publickey", data)
  57. return
  58. }
  59. k := &models.PublicKey{OwnerId: auth.SignedInId(session),
  60. Name: form.KeyName,
  61. Content: form.KeyContent,
  62. }
  63. if err := models.AddPublicKey(k); err != nil {
  64. data["ErrorMsg"] = err
  65. log.Error("ssh.AddPublicKey: %v", err)
  66. r.HTML(200, "base/error", data)
  67. return
  68. } else {
  69. data["AddSSHKeySuccess"] = true
  70. }
  71. }
  72. // List existed SSH keys.
  73. keys, err := models.ListPublicKey(auth.SignedInId(session))
  74. if err != nil {
  75. data["ErrorMsg"] = err
  76. log.Error("ssh.ListPublicKey: %v", err)
  77. r.HTML(200, "base/error", data)
  78. return
  79. }
  80. data["PageIsUserSetting"] = true
  81. data["Keys"] = keys
  82. r.HTML(200, "user/publickey", data)
  83. }