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