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.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 SettingPassword(ctx *middleware.Context) {
  61. ctx.Data["Title"] = "Password"
  62. ctx.Data["PageIsUserSetting"] = true
  63. ctx.Data["IsUserPageSettingPasswd"] = true
  64. ctx.HTML(200, "user/password")
  65. }
  66. func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
  67. ctx.Data["Title"] = "Password"
  68. ctx.Data["PageIsUserSetting"] = true
  69. ctx.Data["IsUserPageSettingPasswd"] = true
  70. if ctx.HasError() {
  71. ctx.HTML(200, "user/password")
  72. return
  73. }
  74. user := ctx.User
  75. tmpUser := &models.User{
  76. Passwd: form.OldPasswd,
  77. Salt: user.Salt,
  78. }
  79. tmpUser.EncodePasswd()
  80. if user.Passwd != tmpUser.Passwd {
  81. ctx.Flash.Error("Old password is not correct")
  82. } else if form.NewPasswd != form.RetypePasswd {
  83. ctx.Flash.Error("New password and re-type password are not same")
  84. } else {
  85. user.Passwd = form.NewPasswd
  86. user.Salt = models.GetUserSalt()
  87. user.EncodePasswd()
  88. if err := models.UpdateUser(user); err != nil {
  89. ctx.Handle(200, "setting.SettingPassword", err)
  90. return
  91. }
  92. log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  93. ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.")
  94. }
  95. ctx.Redirect("/user/setting/password")
  96. }
  97. func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  98. ctx.Data["Title"] = "SSH Keys"
  99. // Delete SSH key.
  100. if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
  101. id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
  102. if err != nil {
  103. log.Error("ssh.DelPublicKey: %v", err)
  104. ctx.JSON(200, map[string]interface{}{
  105. "ok": false,
  106. "err": err.Error(),
  107. })
  108. return
  109. }
  110. k := &models.PublicKey{
  111. Id: id,
  112. OwnerId: ctx.User.Id,
  113. }
  114. if err = models.DeletePublicKey(k); 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. } else {
  121. log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  122. ctx.JSON(200, map[string]interface{}{
  123. "ok": true,
  124. })
  125. }
  126. return
  127. }
  128. // Add new SSH key.
  129. if ctx.Req.Method == "POST" {
  130. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  131. ctx.HTML(200, "user/publickey")
  132. return
  133. }
  134. k := &models.PublicKey{OwnerId: ctx.User.Id,
  135. Name: form.KeyName,
  136. Content: form.KeyContent,
  137. }
  138. if err := models.AddPublicKey(k); err != nil {
  139. if err.Error() == models.ErrKeyAlreadyExist.Error() {
  140. ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
  141. return
  142. }
  143. ctx.Handle(200, "ssh.AddPublicKey", err)
  144. log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  145. return
  146. } else {
  147. ctx.Data["AddSSHKeySuccess"] = true
  148. }
  149. }
  150. // List existed SSH keys.
  151. keys, err := models.ListPublicKey(ctx.User.Id)
  152. if err != nil {
  153. ctx.Handle(200, "ssh.ListPublicKey", err)
  154. return
  155. }
  156. ctx.Data["PageIsUserSetting"] = true
  157. ctx.Data["IsUserPageSettingSSH"] = true
  158. ctx.Data["Keys"] = keys
  159. ctx.HTML(200, "user/publickey")
  160. }
  161. func SettingNotification(ctx *middleware.Context) {
  162. // TODO: user setting notification
  163. ctx.Data["Title"] = "Notification"
  164. ctx.Data["PageIsUserSetting"] = true
  165. ctx.Data["IsUserPageSettingNotify"] = true
  166. ctx.HTML(200, "user/notification")
  167. }
  168. func SettingSecurity(ctx *middleware.Context) {
  169. // TODO: user setting security
  170. ctx.Data["Title"] = "Security"
  171. ctx.Data["PageIsUserSetting"] = true
  172. ctx.Data["IsUserPageSettingSecurity"] = true
  173. ctx.HTML(200, "user/security")
  174. }