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