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.

setting.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.HTML(200, "user/setting")
  22. return
  23. }
  24. // below is for POST requests
  25. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  26. ctx.HTML(200, "user/setting")
  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.HTML(200, "user/setting")
  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.HTML(200, "user/password")
  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.HTML(200, "user/password")
  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. log.Error("ssh.DelPublicKey: %v", err)
  81. ctx.JSON(200, map[string]interface{}{
  82. "ok": false,
  83. "err": err.Error(),
  84. })
  85. return
  86. }
  87. k := &models.PublicKey{
  88. Id: id,
  89. OwnerId: ctx.User.Id,
  90. }
  91. if err = models.DeletePublicKey(k); err != nil {
  92. log.Error("ssh.DelPublicKey: %v", err)
  93. ctx.JSON(200, map[string]interface{}{
  94. "ok": false,
  95. "err": err.Error(),
  96. })
  97. } else {
  98. log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  99. ctx.JSON(200, map[string]interface{}{
  100. "ok": true,
  101. })
  102. }
  103. return
  104. }
  105. // Add new SSH key.
  106. if ctx.Req.Method == "POST" {
  107. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  108. ctx.HTML(200, "user/publickey")
  109. return
  110. }
  111. k := &models.PublicKey{OwnerId: ctx.User.Id,
  112. Name: form.KeyName,
  113. Content: form.KeyContent,
  114. }
  115. if err := models.AddPublicKey(k); err != nil {
  116. if err.Error() == models.ErrKeyAlreadyExist.Error() {
  117. ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
  118. return
  119. }
  120. ctx.Handle(200, "ssh.AddPublicKey", err)
  121. log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
  122. return
  123. } else {
  124. ctx.Data["AddSSHKeySuccess"] = true
  125. }
  126. }
  127. // List existed SSH keys.
  128. keys, err := models.ListPublicKey(ctx.User.Id)
  129. if err != nil {
  130. ctx.Handle(200, "ssh.ListPublicKey", err)
  131. return
  132. }
  133. ctx.Data["PageIsUserSetting"] = true
  134. ctx.Data["IsUserPageSettingSSH"] = true
  135. ctx.Data["Keys"] = keys
  136. ctx.HTML(200, "user/publickey")
  137. }
  138. func SettingNotification(ctx *middleware.Context) {
  139. // TODO: user setting notification
  140. ctx.Data["Title"] = "Notification"
  141. ctx.Data["PageIsUserSetting"] = true
  142. ctx.Data["IsUserPageSettingNotify"] = true
  143. ctx.HTML(200, "user/notification")
  144. }
  145. func SettingSecurity(ctx *middleware.Context) {
  146. // TODO: user setting security
  147. ctx.Data["Title"] = "Security"
  148. ctx.Data["PageIsUserSetting"] = true
  149. ctx.Data["IsUserPageSettingSecurity"] = true
  150. ctx.HTML(200, "user/security")
  151. }