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.

275 lines
7.5 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
  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. "github.com/Unknwon/com"
  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. const (
  14. SETTINGS_PROFILE base.TplName = "user/settings/profile"
  15. SETTINGS_PASSWORD base.TplName = "user/settings/password"
  16. SETTINGS_SSH_KEYS base.TplName = "user/settings/sshkeys"
  17. SETTINGS_SOCIAL base.TplName = "user/settings/social"
  18. SETTINGS_ORGS base.TplName = "user/settings/orgs"
  19. SETTINGS_DELETE base.TplName = "user/settings/delete"
  20. NOTIFICATION base.TplName = "user/notification"
  21. SECURITY base.TplName = "user/security"
  22. )
  23. func Settings(ctx *middleware.Context) {
  24. ctx.Data["Title"] = ctx.Tr("settings")
  25. ctx.Data["PageIsUserSettings"] = true
  26. ctx.Data["PageIsSettingsProfile"] = true
  27. ctx.HTML(200, SETTINGS_PROFILE)
  28. }
  29. func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
  30. ctx.Data["Title"] = ctx.Tr("settings")
  31. ctx.Data["PageIsUserSettings"] = true
  32. ctx.Data["PageIsSettingsProfile"] = true
  33. if ctx.HasError() {
  34. ctx.HTML(200, SETTINGS_PROFILE)
  35. return
  36. }
  37. // Check if user name has been changed.
  38. if ctx.User.Name != form.UserName {
  39. isExist, err := models.IsUserExist(form.UserName)
  40. if err != nil {
  41. ctx.Handle(500, "IsUserExist", err)
  42. return
  43. } else if isExist {
  44. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form)
  45. return
  46. } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
  47. if err == models.ErrUserNameIllegal {
  48. ctx.Flash.Error(ctx.Tr("form.illegal_username"))
  49. ctx.Redirect("/user/settings")
  50. return
  51. } else {
  52. ctx.Handle(500, "ChangeUserName", err)
  53. }
  54. return
  55. }
  56. log.Trace("User name changed: %s -> %s", ctx.User.Name, form.UserName)
  57. ctx.User.Name = form.UserName
  58. }
  59. ctx.User.FullName = form.FullName
  60. ctx.User.Email = form.Email
  61. ctx.User.Website = form.Website
  62. ctx.User.Location = form.Location
  63. ctx.User.Avatar = base.EncodeMd5(form.Avatar)
  64. ctx.User.AvatarEmail = form.Avatar
  65. if err := models.UpdateUser(ctx.User); err != nil {
  66. ctx.Handle(500, "UpdateUser", err)
  67. return
  68. }
  69. log.Trace("User setting updated: %s", ctx.User.Name)
  70. ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
  71. ctx.Redirect("/user/settings")
  72. }
  73. func SettingsPassword(ctx *middleware.Context) {
  74. ctx.Data["Title"] = ctx.Tr("settings")
  75. ctx.Data["PageIsUserSettings"] = true
  76. ctx.Data["PageIsSettingsPassword"] = true
  77. ctx.HTML(200, SETTINGS_PASSWORD)
  78. }
  79. func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
  80. ctx.Data["Title"] = ctx.Tr("settings")
  81. ctx.Data["PageIsUserSettings"] = true
  82. ctx.Data["PageIsSettingsPassword"] = true
  83. if ctx.HasError() {
  84. ctx.HTML(200, SETTINGS_PASSWORD)
  85. return
  86. }
  87. tmpUser := &models.User{
  88. Passwd: form.OldPassword,
  89. Salt: ctx.User.Salt,
  90. }
  91. tmpUser.EncodePasswd()
  92. if ctx.User.Passwd != tmpUser.Passwd {
  93. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  94. } else if form.Password != form.Retype {
  95. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  96. } else {
  97. ctx.User.Passwd = form.Password
  98. ctx.User.Salt = models.GetUserSalt()
  99. ctx.User.EncodePasswd()
  100. if err := models.UpdateUser(ctx.User); err != nil {
  101. ctx.Handle(500, "UpdateUser", err)
  102. return
  103. }
  104. log.Trace("User password updated: %s", ctx.User.Name)
  105. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  106. }
  107. ctx.Redirect("/user/settings/password")
  108. }
  109. func SettingsSSHKeys(ctx *middleware.Context) {
  110. ctx.Data["Title"] = ctx.Tr("settings")
  111. ctx.Data["PageIsUserSettings"] = true
  112. ctx.Data["PageIsSettingsSSHKeys"] = true
  113. var err error
  114. ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
  115. if err != nil {
  116. ctx.Handle(500, "ssh.ListPublicKey", err)
  117. return
  118. }
  119. ctx.HTML(200, SETTINGS_SSH_KEYS)
  120. }
  121. func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  122. ctx.Data["Title"] = ctx.Tr("settings")
  123. ctx.Data["PageIsUserSettings"] = true
  124. ctx.Data["PageIsSettingsSSHKeys"] = true
  125. var err error
  126. ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
  127. if err != nil {
  128. ctx.Handle(500, "ssh.ListPublicKey", err)
  129. return
  130. }
  131. // Delete SSH key.
  132. if ctx.Query("_method") == "DELETE" {
  133. id := com.StrTo(ctx.Query("id")).MustInt64()
  134. if id <= 0 {
  135. return
  136. }
  137. if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil {
  138. ctx.Handle(500, "DeletePublicKey", err)
  139. } else {
  140. log.Trace("SSH key deleted: %s", ctx.User.Name)
  141. ctx.Redirect("/user/settings/ssh")
  142. }
  143. return
  144. }
  145. // Add new SSH key.
  146. if ctx.Req.Method == "POST" {
  147. if ctx.HasError() {
  148. ctx.HTML(200, SETTINGS_SSH_KEYS)
  149. return
  150. }
  151. if ok, err := models.CheckPublicKeyString(form.Content); !ok {
  152. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  153. ctx.Redirect("/user/settings/ssh")
  154. return
  155. }
  156. k := &models.PublicKey{
  157. OwnerId: ctx.User.Id,
  158. Name: form.SSHTitle,
  159. Content: form.Content,
  160. }
  161. if err := models.AddPublicKey(k); err != nil {
  162. if err == models.ErrKeyAlreadyExist {
  163. ctx.RenderWithErr(ctx.Tr("form.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form)
  164. return
  165. }
  166. ctx.Handle(500, "ssh.AddPublicKey", err)
  167. return
  168. } else {
  169. log.Trace("SSH key added: %s", ctx.User.Name)
  170. ctx.Flash.Success(ctx.Tr("settings.add_key_success"))
  171. ctx.Redirect("/user/settings/ssh")
  172. return
  173. }
  174. }
  175. ctx.HTML(200, SETTINGS_SSH_KEYS)
  176. }
  177. // func SettingSocial(ctx *middleware.Context) {
  178. // ctx.Data["Title"] = "Social Account"
  179. // ctx.Data["PageIsUserSetting"] = true
  180. // ctx.Data["IsUserPageSettingSocial"] = true
  181. // // Unbind social account.
  182. // remove, _ := base.StrTo(ctx.Query("remove")).Int64()
  183. // if remove > 0 {
  184. // if err := models.DeleteOauth2ById(remove); err != nil {
  185. // ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err)
  186. // return
  187. // }
  188. // ctx.Flash.Success("OAuth2 has been unbinded.")
  189. // ctx.Redirect("/user/settings/social")
  190. // return
  191. // }
  192. // var err error
  193. // ctx.Data["Socials"], err = models.GetOauthByUserId(ctx.User.Id)
  194. // if err != nil {
  195. // ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err)
  196. // return
  197. // }
  198. // ctx.HTML(200, SOCIAL)
  199. // }
  200. func SettingsSocial(ctx *middleware.Context) {
  201. ctx.Data["Title"] = ctx.Tr("settings")
  202. ctx.Data["PageIsUserSettings"] = true
  203. ctx.Data["PageIsSettingsSocial"] = true
  204. ctx.HTML(200, SETTINGS_SOCIAL)
  205. }
  206. func SettingsOrgs(ctx *middleware.Context) {
  207. ctx.Data["Title"] = ctx.Tr("settings")
  208. ctx.Data["PageIsUserSettings"] = true
  209. ctx.Data["PageIsSettingsOrgs"] = true
  210. ctx.HTML(200, SETTINGS_ORGS)
  211. }
  212. func SettingsDelete(ctx *middleware.Context) {
  213. ctx.Data["Title"] = ctx.Tr("settings")
  214. ctx.Data["PageIsUserSettings"] = true
  215. ctx.Data["PageIsSettingsDelete"] = true
  216. if ctx.Req.Method == "POST" {
  217. // tmpUser := models.User{
  218. // Passwd: ctx.Query("password"),
  219. // Salt: ctx.User.Salt,
  220. // }
  221. // tmpUser.EncodePasswd()
  222. // if tmpUser.Passwd != ctx.User.Passwd {
  223. // ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
  224. // } else {
  225. if err := models.DeleteUser(ctx.User); err != nil {
  226. switch err {
  227. case models.ErrUserOwnRepos:
  228. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  229. ctx.Redirect("/user/settings/delete")
  230. return
  231. default:
  232. ctx.Handle(500, "DeleteUser", err)
  233. return
  234. }
  235. } else {
  236. log.Trace("Account deleted: %s", ctx.User.Name)
  237. ctx.Redirect("/")
  238. return
  239. }
  240. }
  241. ctx.HTML(200, SETTINGS_DELETE)
  242. }