setting.go 5.0 KB

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