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

218 lines
5.8 KiB

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