You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

setting.go 5.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "strconv"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/auth"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. func Setting(ctx *middleware.Context) {
  14. ctx.Data["Title"] = "Setting"
  15. ctx.Data["PageIsUserSetting"] = true
  16. ctx.Data["IsUserPageSetting"] = true
  17. ctx.Data["Owner"] = ctx.User
  18. ctx.HTML(200, "user/setting")
  19. }
  20. // Render user setting page (email, website modify)
  21. func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
  22. ctx.Data["Title"] = "Setting"
  23. ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
  24. ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
  25. user := ctx.User
  26. ctx.Data["Owner"] = user
  27. if ctx.HasError() {
  28. ctx.HTML(200, "user/setting")
  29. return
  30. }
  31. // Check if user name has been changed.
  32. if user.Name != form.UserName {
  33. isExist, err := models.IsUserExist(form.UserName)
  34. if err != nil {
  35. ctx.Handle(500, "user.Setting(update: check existence)", err)
  36. return
  37. } else if isExist {
  38. ctx.RenderWithErr("User name has been taken.", "user/setting", &form)
  39. return
  40. } else if err = models.ChangeUserName(user, form.UserName); err != nil {
  41. ctx.Handle(500, "user.Setting(change user name)", err)
  42. return
  43. }
  44. log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName)
  45. user.Name = form.UserName
  46. }
  47. user.Email = form.Email
  48. user.Website = form.Website
  49. user.Location = form.Location
  50. user.Avatar = base.EncodeMd5(form.Avatar)
  51. user.AvatarEmail = form.Avatar
  52. if err := models.UpdateUser(user); err != nil {
  53. ctx.Handle(500, "setting.Setting", err)
  54. return
  55. }
  56. log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  57. ctx.Flash.Success("Your profile has been successfully updated.")
  58. ctx.Redirect("/user/settings")
  59. }
  60. func SettingSocial(ctx *middleware.Context) {
  61. ctx.Data["Title"] = "Social Account"
  62. ctx.Data["PageIsUserSetting"] = true
  63. ctx.Data["IsUserPageSettingSocial"] = true
  64. socials, err := models.GetOauthByUserId(ctx.User.Id)
  65. if err != nil {
  66. ctx.Handle(500, "user.SettingSocial", err)
  67. return
  68. }
  69. ctx.Data["Socials"] = socials
  70. ctx.HTML(200, "user/social")
  71. }
  72. func SettingPassword(ctx *middleware.Context) {
  73. ctx.Data["Title"] = "Password"
  74. ctx.Data["PageIsUserSetting"] = true
  75. ctx.Data["IsUserPageSettingPasswd"] = true
  76. ctx.HTML(200, "user/password")
  77. }
  78. func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
  79. ctx.Data["Title"] = "Password"
  80. ctx.Data["PageIsUserSetting"] = true
  81. ctx.Data["IsUserPageSettingPasswd"] = true
  82. if ctx.HasError() {
  83. ctx.HTML(200, "user/password")
  84. return
  85. }
  86. user := ctx.User
  87. tmpUser := &models.User{
  88. Passwd: form.OldPasswd,
  89. Salt: user.Salt,
  90. }
  91. tmpUser.EncodePasswd()
  92. if user.Passwd != tmpUser.Passwd {
  93. ctx.Flash.Error("Old password is not correct")
  94. } else if form.NewPasswd != form.RetypePasswd {
  95. ctx.Flash.Error("New password and re-type password are not same")
  96. } else {
  97. user.Passwd = form.NewPasswd
  98. user.Salt = models.GetUserSalt()
  99. user.EncodePasswd()
  100. if err := models.UpdateUser(user); err != nil {
  101. ctx.Handle(200, "setting.SettingPassword", err)
  102. return
  103. }
  104. log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  105. ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.")
  106. }
  107. ctx.Redirect("/user/settings/password")
  108. }
  109. func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  110. ctx.Data["Title"] = "SSH Keys"
  111. // Delete SSH key.
  112. if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
  113. id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
  114. if err != nil {
  115. log.Error("ssh.DelPublicKey: %v", err)
  116. ctx.JSON(200, map[string]interface{}{
  117. "ok": false,
  118. "err": err.Error(),
  119. })
  120. return
  121. }
  122. k := &models.PublicKey{
  123. Id: id,
  124. OwnerId: ctx.User.Id,
  125. }
  126. if err = models.DeletePublicKey(k); err != nil {
  127. log.Error("ssh.DelPublicKey: %v", err)
  128. ctx.JSON(200, map[string]interface{}{
  129. "ok": false,
  130. "err": err.Error(),
  131. })
  132. } else {
  133. log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  134. ctx.JSON(200, map[string]interface{}{
  135. "ok": true,
  136. })
  137. }
  138. return
  139. }
  140. // Add new SSH key.
  141. if ctx.Req.Method == "POST" {
  142. if ctx.HasError() {
  143. ctx.HTML(200, "user/publickey")
  144. return
  145. }
  146. k := &models.PublicKey{
  147. OwnerId: ctx.User.Id,
  148. Name: form.KeyName,
  149. Content: form.KeyContent,
  150. }
  151. if err := models.AddPublicKey(k); err != nil {
  152. if err.Error() == models.ErrKeyAlreadyExist.Error() {
  153. ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
  154. return
  155. }
  156. ctx.Handle(500, "ssh.AddPublicKey", err)
  157. return
  158. } else {
  159. log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  160. ctx.Flash.Success("New SSH Key has been added!")
  161. ctx.Redirect("/user/settings/ssh")
  162. return
  163. }
  164. }
  165. // List existed SSH keys.
  166. keys, err := models.ListPublicKey(ctx.User.Id)
  167. if err != nil {
  168. ctx.Handle(200, "ssh.ListPublicKey", err)
  169. return
  170. }
  171. ctx.Data["PageIsUserSetting"] = true
  172. ctx.Data["IsUserPageSettingSSH"] = true
  173. ctx.Data["Keys"] = keys
  174. ctx.HTML(200, "user/publickey")
  175. }
  176. func SettingNotification(ctx *middleware.Context) {
  177. // TODO: user setting notification
  178. ctx.Data["Title"] = "Notification"
  179. ctx.Data["PageIsUserSetting"] = true
  180. ctx.Data["IsUserPageSettingNotify"] = true
  181. ctx.HTML(200, "user/notification")
  182. }
  183. func SettingSecurity(ctx *middleware.Context) {
  184. // TODO: user setting security
  185. ctx.Data["Title"] = "Security"
  186. ctx.Data["PageIsUserSetting"] = true
  187. ctx.Data["IsUserPageSettingSecurity"] = true
  188. ctx.HTML(200, "user/security")
  189. }