setting.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // Render user setting page (email, website modify)
  14. func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
  15. ctx.Data["Title"] = "Setting"
  16. ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
  17. ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
  18. user := ctx.User
  19. ctx.Data["Owner"] = user
  20. if ctx.Req.Method == "GET" {
  21. ctx.Render.HTML(200, "user/setting", ctx.Data)
  22. return
  23. }
  24. // below is for POST requests
  25. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  26. ctx.Render.HTML(200, "user/setting", ctx.Data)
  27. return
  28. }
  29. user.Email = form.Email
  30. user.Website = form.Website
  31. user.Location = form.Location
  32. user.Avatar = base.EncodeMd5(form.Avatar)
  33. user.AvatarEmail = form.Avatar
  34. if err := models.UpdateUser(user); err != nil {
  35. ctx.Handle(200, "setting.Setting", err)
  36. return
  37. }
  38. ctx.Data["IsSuccess"] = true
  39. ctx.Render.HTML(200, "user/setting", ctx.Data)
  40. log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  41. }
  42. func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) {
  43. ctx.Data["Title"] = "Password"
  44. ctx.Data["PageIsUserSetting"] = true
  45. ctx.Data["IsUserPageSettingPasswd"] = true
  46. if ctx.Req.Method == "GET" {
  47. ctx.Render.HTML(200, "user/password", ctx.Data)
  48. return
  49. }
  50. user := ctx.User
  51. newUser := &models.User{Passwd: form.NewPasswd}
  52. if err := newUser.EncodePasswd(); err != nil {
  53. ctx.Handle(200, "setting.SettingPassword", err)
  54. return
  55. }
  56. if user.Passwd != newUser.Passwd {
  57. ctx.Data["HasError"] = true
  58. ctx.Data["ErrorMsg"] = "Old password is not correct"
  59. } else if form.NewPasswd != form.RetypePasswd {
  60. ctx.Data["HasError"] = true
  61. ctx.Data["ErrorMsg"] = "New password and re-type password are not same"
  62. } else {
  63. user.Passwd = newUser.Passwd
  64. if err := models.UpdateUser(user); err != nil {
  65. ctx.Handle(200, "setting.SettingPassword", err)
  66. return
  67. }
  68. ctx.Data["IsSuccess"] = true
  69. }
  70. ctx.Data["Owner"] = user
  71. ctx.Render.HTML(200, "user/password", ctx.Data)
  72. log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  73. }
  74. func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  75. ctx.Data["Title"] = "SSH Keys"
  76. // Delete SSH key.
  77. if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
  78. id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
  79. if err != nil {
  80. ctx.Data["ErrorMsg"] = err
  81. log.Error("ssh.DelPublicKey: %v", err)
  82. ctx.Render.JSON(200, map[string]interface{}{
  83. "ok": false,
  84. "err": err.Error(),
  85. })
  86. return
  87. }
  88. k := &models.PublicKey{
  89. Id: id,
  90. OwnerId: ctx.User.Id,
  91. }
  92. if err = models.DeletePublicKey(k); err != nil {
  93. ctx.Data["ErrorMsg"] = err
  94. log.Error("ssh.DelPublicKey: %v", err)
  95. ctx.Render.JSON(200, map[string]interface{}{
  96. "ok": false,
  97. "err": err.Error(),
  98. })
  99. } else {
  100. log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  101. ctx.Render.JSON(200, map[string]interface{}{
  102. "ok": true,
  103. })
  104. }
  105. return
  106. }
  107. // Add new SSH key.
  108. if ctx.Req.Method == "POST" {
  109. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  110. ctx.Render.HTML(200, "user/publickey", ctx.Data)
  111. return
  112. }
  113. k := &models.PublicKey{OwnerId: ctx.User.Id,
  114. Name: form.KeyName,
  115. Content: form.KeyContent,
  116. }
  117. if err := models.AddPublicKey(k); err != nil {
  118. if err.Error() == models.ErrKeyAlreadyExist.Error() {
  119. ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
  120. return
  121. }
  122. ctx.Handle(200, "ssh.AddPublicKey", err)
  123. log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  124. return
  125. } else {
  126. ctx.Data["AddSSHKeySuccess"] = true
  127. }
  128. }
  129. // List existed SSH keys.
  130. keys, err := models.ListPublicKey(ctx.User.Id)
  131. if err != nil {
  132. ctx.Handle(200, "ssh.ListPublicKey", err)
  133. return
  134. }
  135. ctx.Data["PageIsUserSetting"] = true
  136. ctx.Data["IsUserPageSettingSSH"] = true
  137. ctx.Data["Keys"] = keys
  138. ctx.Render.HTML(200, "user/publickey", ctx.Data)
  139. }
  140. func SettingNotification(ctx *middleware.Context) {
  141. // TODO: user setting notification
  142. ctx.Data["Title"] = "Notification"
  143. ctx.Data["PageIsUserSetting"] = true
  144. ctx.Data["IsUserPageSettingNotify"] = true
  145. ctx.Render.HTML(200, "user/notification", ctx.Data)
  146. }
  147. func SettingSecurity(ctx *middleware.Context) {
  148. // TODO: user setting security
  149. ctx.Data["Title"] = "Security"
  150. ctx.Data["PageIsUserSetting"] = true
  151. ctx.Data["IsUserPageSettingSecurity"] = true
  152. ctx.Render.HTML(200, "user/security", ctx.Data)
  153. }