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 6.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "strings"
  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. func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
  21. ctx.Data["Title"] = "Setting"
  22. ctx.Data["PageIsUserSetting"] = true
  23. ctx.Data["IsUserPageSetting"] = true
  24. user := ctx.User
  25. ctx.Data["Owner"] = user
  26. if ctx.HasError() {
  27. ctx.HTML(200, "user/setting")
  28. return
  29. }
  30. // Check if user name has been changed.
  31. if user.Name != form.UserName {
  32. isExist, err := models.IsUserExist(form.UserName)
  33. if err != nil {
  34. ctx.Handle(500, "user.Setting(update: check existence)", err)
  35. return
  36. } else if isExist {
  37. ctx.RenderWithErr("User name has been taken.", "user/setting", &form)
  38. return
  39. } else if err = models.ChangeUserName(user, form.UserName); err != nil {
  40. ctx.Handle(500, "user.Setting(change user name)", err)
  41. return
  42. }
  43. log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName)
  44. user.Name = form.UserName
  45. }
  46. user.FullName = form.FullName
  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. // Unbind social account.
  65. remove, _ := base.StrTo(ctx.Query("remove")).Int64()
  66. if remove > 0 {
  67. if err := models.DeleteOauth2ById(remove); err != nil {
  68. ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err)
  69. return
  70. }
  71. ctx.Flash.Success("OAuth2 has been unbinded.")
  72. ctx.Redirect("/user/settings/social")
  73. return
  74. }
  75. socials, err := models.GetOauthByUserId(ctx.User.Id)
  76. if err != nil {
  77. ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err)
  78. return
  79. }
  80. ctx.Data["Socials"] = socials
  81. ctx.HTML(200, "user/social")
  82. }
  83. func SettingPassword(ctx *middleware.Context) {
  84. ctx.Data["Title"] = "Password"
  85. ctx.Data["PageIsUserSetting"] = true
  86. ctx.Data["IsUserPageSettingPasswd"] = true
  87. ctx.HTML(200, "user/password")
  88. }
  89. func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
  90. ctx.Data["Title"] = "Password"
  91. ctx.Data["PageIsUserSetting"] = true
  92. ctx.Data["IsUserPageSettingPasswd"] = true
  93. if ctx.HasError() {
  94. ctx.HTML(200, "user/password")
  95. return
  96. }
  97. user := ctx.User
  98. tmpUser := &models.User{
  99. Passwd: form.OldPasswd,
  100. Salt: user.Salt,
  101. }
  102. tmpUser.EncodePasswd()
  103. if user.Passwd != tmpUser.Passwd {
  104. ctx.Flash.Error("Old password is not correct.")
  105. } else if form.NewPasswd != form.RetypePasswd {
  106. ctx.Flash.Error("New password and re-type password are not same.")
  107. } else {
  108. user.Passwd = form.NewPasswd
  109. user.Salt = models.GetUserSalt()
  110. user.EncodePasswd()
  111. if err := models.UpdateUser(user); err != nil {
  112. ctx.Handle(200, "setting.SettingPassword", err)
  113. return
  114. }
  115. log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  116. ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.")
  117. }
  118. ctx.Redirect("/user/settings/password")
  119. }
  120. func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  121. ctx.Data["Title"] = "SSH Keys"
  122. ctx.Data["PageIsUserSetting"] = true
  123. ctx.Data["IsUserPageSettingSSH"] = true
  124. // Delete SSH key.
  125. if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
  126. id, err := base.StrTo(ctx.Query("id")).Int64()
  127. if err != nil {
  128. log.Error("ssh.DelPublicKey: %v", err)
  129. ctx.JSON(200, map[string]interface{}{
  130. "ok": false,
  131. "err": err.Error(),
  132. })
  133. return
  134. }
  135. if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil {
  136. log.Error("ssh.DelPublicKey: %v", err)
  137. ctx.JSON(200, map[string]interface{}{
  138. "ok": false,
  139. "err": err.Error(),
  140. })
  141. } else {
  142. log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  143. ctx.JSON(200, map[string]interface{}{
  144. "ok": true,
  145. })
  146. }
  147. return
  148. }
  149. // List existed SSH keys.
  150. keys, err := models.ListPublicKey(ctx.User.Id)
  151. if err != nil {
  152. ctx.Handle(500, "ssh.ListPublicKey", err)
  153. return
  154. }
  155. ctx.Data["Keys"] = keys
  156. // Add new SSH key.
  157. if ctx.Req.Method == "POST" {
  158. if ctx.HasError() {
  159. ctx.HTML(200, "user/publickey")
  160. return
  161. }
  162. if len(form.KeyContent) < 100 || !strings.HasPrefix(form.KeyContent, "ssh-rsa") {
  163. ctx.Flash.Error("SSH key content is not valid.")
  164. ctx.Redirect("/user/settings/ssh")
  165. return
  166. }
  167. k := &models.PublicKey{
  168. OwnerId: ctx.User.Id,
  169. Name: form.KeyName,
  170. Content: form.KeyContent,
  171. }
  172. if err := models.AddPublicKey(k); err != nil {
  173. if err.Error() == models.ErrKeyAlreadyExist.Error() {
  174. ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
  175. return
  176. }
  177. ctx.Handle(500, "ssh.AddPublicKey", err)
  178. return
  179. } else {
  180. log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  181. ctx.Flash.Success("New SSH Key has been added!")
  182. ctx.Redirect("/user/settings/ssh")
  183. return
  184. }
  185. }
  186. ctx.HTML(200, "user/publickey")
  187. }
  188. func SettingNotification(ctx *middleware.Context) {
  189. // TODO: user setting notification
  190. ctx.Data["Title"] = "Notification"
  191. ctx.Data["PageIsUserSetting"] = true
  192. ctx.Data["IsUserPageSettingNotify"] = true
  193. ctx.HTML(200, "user/notification")
  194. }
  195. func SettingSecurity(ctx *middleware.Context) {
  196. // TODO: user setting security
  197. ctx.Data["Title"] = "Security"
  198. ctx.Data["PageIsUserSetting"] = true
  199. ctx.Data["IsUserPageSettingSecurity"] = true
  200. ctx.HTML(200, "user/security")
  201. }