setting.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/setting")
  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/setting/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{OwnerId: ctx.User.Id,
  147. Name: form.KeyName,
  148. Content: form.KeyContent,
  149. }
  150. if err := models.AddPublicKey(k); err != nil {
  151. if err.Error() == models.ErrKeyAlreadyExist.Error() {
  152. ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
  153. return
  154. }
  155. ctx.Handle(500, "ssh.AddPublicKey", err)
  156. return
  157. } else {
  158. log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  159. ctx.Flash.Success("New SSH Key has been added!")
  160. ctx.Redirect("/user/setting/ssh")
  161. return
  162. }
  163. }
  164. // List existed SSH keys.
  165. keys, err := models.ListPublicKey(ctx.User.Id)
  166. if err != nil {
  167. ctx.Handle(200, "ssh.ListPublicKey", err)
  168. return
  169. }
  170. ctx.Data["PageIsUserSetting"] = true
  171. ctx.Data["IsUserPageSettingSSH"] = true
  172. ctx.Data["Keys"] = keys
  173. ctx.HTML(200, "user/publickey")
  174. }
  175. func SettingNotification(ctx *middleware.Context) {
  176. // TODO: user setting notification
  177. ctx.Data["Title"] = "Notification"
  178. ctx.Data["PageIsUserSetting"] = true
  179. ctx.Data["IsUserPageSettingNotify"] = true
  180. ctx.HTML(200, "user/notification")
  181. }
  182. func SettingSecurity(ctx *middleware.Context) {
  183. // TODO: user setting security
  184. ctx.Data["Title"] = "Security"
  185. ctx.Data["PageIsUserSetting"] = true
  186. ctx.Data["IsUserPageSettingSecurity"] = true
  187. ctx.HTML(200, "user/security")
  188. }