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.

170 lines
4.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. data["Owner"] = user
  20. if req.Method == "GET" {
  21. r.HTML(200, "user/setting", data)
  22. return
  23. }
  24. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  25. r.HTML(200, "user/setting", data)
  26. return
  27. }
  28. user.Email = form.Email
  29. user.Website = form.Website
  30. user.Location = form.Location
  31. user.Avatar = base.EncodeMd5(form.Avatar)
  32. user.AvatarEmail = form.Avatar
  33. if err := models.UpdateUser(user); err != nil {
  34. log.Handle(200, "setting.Setting", data, r, err)
  35. return
  36. }
  37. data["IsSuccess"] = true
  38. r.HTML(200, "user/setting", data)
  39. }
  40. func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.TmplData, session sessions.Session, req *http.Request) {
  41. data["Title"] = "Password"
  42. data["PageIsUserSetting"] = true
  43. if req.Method == "GET" {
  44. r.HTML(200, "user/password", data)
  45. return
  46. }
  47. user := auth.SignedInUser(session)
  48. newUser := &models.User{Passwd: form.NewPasswd}
  49. if err := newUser.EncodePasswd(); err != nil {
  50. log.Handle(200, "setting.SettingPassword", data, r, err)
  51. return
  52. }
  53. if user.Passwd != newUser.Passwd {
  54. data["HasError"] = true
  55. data["ErrorMsg"] = "Old password is not correct"
  56. } else if form.NewPasswd != form.RetypePasswd {
  57. data["HasError"] = true
  58. data["ErrorMsg"] = "New password and re-type password are not same"
  59. } else {
  60. user.Passwd = newUser.Passwd
  61. if err := models.UpdateUser(user); err != nil {
  62. log.Handle(200, "setting.SettingPassword", data, r, err)
  63. return
  64. }
  65. data["IsSuccess"] = true
  66. }
  67. data["Owner"] = user
  68. r.HTML(200, "user/password", data)
  69. }
  70. func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
  71. data["Title"] = "SSH Keys"
  72. // Delete SSH key.
  73. if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" {
  74. println(1)
  75. id, err := strconv.ParseInt(req.FormValue("id"), 10, 64)
  76. if err != nil {
  77. data["ErrorMsg"] = err
  78. log.Error("ssh.DelPublicKey: %v", err)
  79. r.JSON(200, map[string]interface{}{
  80. "ok": false,
  81. "err": err.Error(),
  82. })
  83. return
  84. }
  85. k := &models.PublicKey{
  86. Id: id,
  87. OwnerId: auth.SignedInId(session),
  88. }
  89. if err = models.DeletePublicKey(k); err != nil {
  90. data["ErrorMsg"] = err
  91. log.Error("ssh.DelPublicKey: %v", err)
  92. r.JSON(200, map[string]interface{}{
  93. "ok": false,
  94. "err": err.Error(),
  95. })
  96. } else {
  97. r.JSON(200, map[string]interface{}{
  98. "ok": true,
  99. })
  100. }
  101. return
  102. }
  103. // Add new SSH key.
  104. if req.Method == "POST" {
  105. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  106. r.HTML(200, "user/publickey", data)
  107. return
  108. }
  109. k := &models.PublicKey{OwnerId: auth.SignedInId(session),
  110. Name: form.KeyName,
  111. Content: form.KeyContent,
  112. }
  113. if err := models.AddPublicKey(k); err != nil {
  114. data["ErrorMsg"] = err
  115. log.Error("ssh.AddPublicKey: %v", err)
  116. r.HTML(200, "base/error", data)
  117. return
  118. } else {
  119. data["AddSSHKeySuccess"] = true
  120. }
  121. }
  122. // List existed SSH keys.
  123. keys, err := models.ListPublicKey(auth.SignedInId(session))
  124. if err != nil {
  125. data["ErrorMsg"] = err
  126. log.Error("ssh.ListPublicKey: %v", err)
  127. r.HTML(200, "base/error", data)
  128. return
  129. }
  130. data["PageIsUserSetting"] = true
  131. data["Keys"] = keys
  132. r.HTML(200, "user/publickey", data)
  133. }
  134. func SettingNotification(r render.Render, data base.TmplData) {
  135. // todo user setting notification
  136. data["Title"] = "Notification"
  137. data["PageIsUserSetting"] = true
  138. r.HTML(200, "user/notification", data)
  139. }
  140. func SettingSecurity(r render.Render, data base.TmplData) {
  141. // todo user setting security
  142. data["Title"] = "Security"
  143. data["PageIsUserSetting"] = true
  144. r.HTML(200, "user/security", data)
  145. }