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.

456 lines
12 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
  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. "io/ioutil"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. SETTINGS_PROFILE base.TplName = "user/settings/profile"
  19. SETTINGS_PASSWORD base.TplName = "user/settings/password"
  20. SETTINGS_EMAILS base.TplName = "user/settings/email"
  21. SETTINGS_SSH_KEYS base.TplName = "user/settings/sshkeys"
  22. SETTINGS_SOCIAL base.TplName = "user/settings/social"
  23. SETTINGS_APPLICATIONS base.TplName = "user/settings/applications"
  24. SETTINGS_DELETE base.TplName = "user/settings/delete"
  25. NOTIFICATION base.TplName = "user/notification"
  26. SECURITY base.TplName = "user/security"
  27. )
  28. func Settings(ctx *middleware.Context) {
  29. ctx.Data["Title"] = ctx.Tr("settings")
  30. ctx.Data["PageIsSettingsProfile"] = true
  31. ctx.HTML(200, SETTINGS_PROFILE)
  32. }
  33. func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
  34. ctx.Data["Title"] = ctx.Tr("settings")
  35. ctx.Data["PageIsSettingsProfile"] = true
  36. if ctx.HasError() {
  37. ctx.HTML(200, SETTINGS_PROFILE)
  38. return
  39. }
  40. // Check if user name has been changed.
  41. if ctx.User.Name != form.UserName {
  42. if err := models.ChangeUserName(ctx.User, form.UserName); err != nil {
  43. switch {
  44. case models.IsErrUserAlreadyExist(err):
  45. ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
  46. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  47. case models.IsErrEmailAlreadyUsed(err):
  48. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  49. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  50. case models.IsErrNameReserved(err):
  51. ctx.Flash.Error(ctx.Tr("user.form.name_reserved"))
  52. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  53. case models.IsErrNamePatternNotAllowed(err):
  54. ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed"))
  55. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  56. default:
  57. ctx.Handle(500, "ChangeUserName", err)
  58. }
  59. return
  60. }
  61. log.Trace("User name changed: %s -> %s", ctx.User.Name, form.UserName)
  62. ctx.User.Name = form.UserName
  63. }
  64. ctx.User.FullName = form.FullName
  65. ctx.User.Email = form.Email
  66. ctx.User.Website = form.Website
  67. ctx.User.Location = form.Location
  68. ctx.User.Avatar = base.EncodeMd5(form.Avatar)
  69. ctx.User.AvatarEmail = form.Avatar
  70. if err := models.UpdateUser(ctx.User); err != nil {
  71. ctx.Handle(500, "UpdateUser", err)
  72. return
  73. }
  74. log.Trace("User setting updated: %s", ctx.User.Name)
  75. ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
  76. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  77. }
  78. // FIXME: limit size.
  79. func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
  80. defer ctx.Redirect(setting.AppSubUrl + "/user/settings")
  81. ctx.User.UseCustomAvatar = form.Enable
  82. if form.Avatar != nil {
  83. fr, err := form.Avatar.Open()
  84. if err != nil {
  85. ctx.Flash.Error(err.Error())
  86. return
  87. }
  88. data, err := ioutil.ReadAll(fr)
  89. if err != nil {
  90. ctx.Flash.Error(err.Error())
  91. return
  92. }
  93. if _, ok := base.IsImageFile(data); !ok {
  94. ctx.Flash.Error(ctx.Tr("settings.uploaded_avatar_not_a_image"))
  95. return
  96. }
  97. if err = ctx.User.UploadAvatar(data); err != nil {
  98. ctx.Flash.Error(err.Error())
  99. return
  100. }
  101. } else {
  102. // In case no avatar at all.
  103. if form.Enable && !com.IsFile(ctx.User.CustomAvatarPath()) {
  104. ctx.Flash.Error(ctx.Tr("settings.no_custom_avatar_available"))
  105. return
  106. }
  107. }
  108. if err := models.UpdateUser(ctx.User); err != nil {
  109. ctx.Flash.Error(err.Error())
  110. return
  111. }
  112. ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
  113. }
  114. func SettingsEmails(ctx *middleware.Context) {
  115. ctx.Data["Title"] = ctx.Tr("settings")
  116. ctx.Data["PageIsSettingsEmails"] = true
  117. emails, err := models.GetEmailAddresses(ctx.User.Id)
  118. if err != nil {
  119. ctx.Handle(500, "GetEmailAddresses", err)
  120. return
  121. }
  122. ctx.Data["Emails"] = emails
  123. ctx.HTML(200, SETTINGS_EMAILS)
  124. }
  125. func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
  126. ctx.Data["Title"] = ctx.Tr("settings")
  127. ctx.Data["PageIsSettingsEmails"] = true
  128. emails, err := models.GetEmailAddresses(ctx.User.Id)
  129. if err != nil {
  130. ctx.Handle(500, "GetEmailAddresses", err)
  131. return
  132. }
  133. ctx.Data["Emails"] = emails
  134. // Delete E-mail address.
  135. if ctx.Query("_method") == "DELETE" {
  136. id := ctx.QueryInt64("id")
  137. if id <= 0 {
  138. return
  139. }
  140. if err = models.DeleteEmailAddress(&models.EmailAddress{Id: id}); err != nil {
  141. ctx.Handle(500, "DeleteEmail", err)
  142. } else {
  143. log.Trace("Email address deleted: %s", ctx.User.Name)
  144. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  145. }
  146. return
  147. }
  148. // Make emailaddress primary.
  149. if ctx.Query("_method") == "PRIMARY" {
  150. id := ctx.QueryInt64("id")
  151. if id <= 0 {
  152. return
  153. }
  154. if err = models.MakeEmailPrimary(&models.EmailAddress{Id: id}); err != nil {
  155. ctx.Handle(500, "MakeEmailPrimary", err)
  156. } else {
  157. log.Trace("Email made primary: %s", ctx.User.Name)
  158. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  159. }
  160. return
  161. }
  162. // Add Email address.
  163. if ctx.HasError() {
  164. ctx.HTML(200, SETTINGS_EMAILS)
  165. return
  166. }
  167. cleanEmail := strings.Replace(form.Email, "\n", "", -1)
  168. e := &models.EmailAddress{
  169. Uid: ctx.User.Id,
  170. Email: cleanEmail,
  171. IsActivated: !setting.Service.RegisterEmailConfirm,
  172. }
  173. if err := models.AddEmailAddress(e); err != nil {
  174. if models.IsErrEmailAlreadyUsed(err) {
  175. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form)
  176. return
  177. }
  178. ctx.Handle(500, "AddEmailAddress", err)
  179. return
  180. } else {
  181. // Send confirmation e-mail
  182. if setting.Service.RegisterEmailConfirm {
  183. mailer.SendActivateEmail(ctx.Render, ctx.User, e)
  184. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  185. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  186. }
  187. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", cleanEmail, setting.Service.ActiveCodeLives/60))
  188. } else {
  189. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  190. }
  191. log.Trace("Email address added: %s", e.Email)
  192. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  193. return
  194. }
  195. ctx.HTML(200, SETTINGS_EMAILS)
  196. }
  197. func SettingsPassword(ctx *middleware.Context) {
  198. ctx.Data["Title"] = ctx.Tr("settings")
  199. ctx.Data["PageIsSettingsPassword"] = true
  200. ctx.HTML(200, SETTINGS_PASSWORD)
  201. }
  202. func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
  203. ctx.Data["Title"] = ctx.Tr("settings")
  204. ctx.Data["PageIsSettingsPassword"] = true
  205. if ctx.HasError() {
  206. ctx.HTML(200, SETTINGS_PASSWORD)
  207. return
  208. }
  209. tmpUser := &models.User{
  210. Passwd: form.OldPassword,
  211. Salt: ctx.User.Salt,
  212. }
  213. tmpUser.EncodePasswd()
  214. if ctx.User.Passwd != tmpUser.Passwd {
  215. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  216. } else if form.Password != form.Retype {
  217. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  218. } else {
  219. ctx.User.Passwd = form.Password
  220. ctx.User.Salt = models.GetUserSalt()
  221. ctx.User.EncodePasswd()
  222. if err := models.UpdateUser(ctx.User); err != nil {
  223. ctx.Handle(500, "UpdateUser", err)
  224. return
  225. }
  226. log.Trace("User password updated: %s", ctx.User.Name)
  227. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  228. }
  229. ctx.Redirect(setting.AppSubUrl + "/user/settings/password")
  230. }
  231. func SettingsSSHKeys(ctx *middleware.Context) {
  232. ctx.Data["Title"] = ctx.Tr("settings")
  233. ctx.Data["PageIsSettingsSSHKeys"] = true
  234. var err error
  235. ctx.Data["Keys"], err = models.ListPublicKeys(ctx.User.Id)
  236. if err != nil {
  237. ctx.Handle(500, "ssh.ListPublicKey", err)
  238. return
  239. }
  240. ctx.HTML(200, SETTINGS_SSH_KEYS)
  241. }
  242. func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  243. ctx.Data["Title"] = ctx.Tr("settings")
  244. ctx.Data["PageIsSettingsSSHKeys"] = true
  245. var err error
  246. ctx.Data["Keys"], err = models.ListPublicKeys(ctx.User.Id)
  247. if err != nil {
  248. ctx.Handle(500, "ssh.ListPublicKey", err)
  249. return
  250. }
  251. // Delete SSH key.
  252. if ctx.Query("_method") == "DELETE" {
  253. id := com.StrTo(ctx.Query("id")).MustInt64()
  254. if id <= 0 {
  255. return
  256. }
  257. if err = models.DeletePublicKey(&models.PublicKey{ID: id}); err != nil {
  258. ctx.Handle(500, "DeletePublicKey", err)
  259. } else {
  260. log.Trace("SSH key deleted: %s", ctx.User.Name)
  261. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  262. }
  263. return
  264. }
  265. // Add new SSH key.
  266. if ctx.Req.Method == "POST" {
  267. if ctx.HasError() {
  268. ctx.HTML(200, SETTINGS_SSH_KEYS)
  269. return
  270. }
  271. content, err := models.CheckPublicKeyString(form.Content)
  272. if err != nil {
  273. if err == models.ErrKeyUnableVerify {
  274. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  275. } else {
  276. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  277. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  278. return
  279. }
  280. }
  281. if err = models.AddPublicKey(ctx.User.Id, form.Title, content); err != nil {
  282. switch {
  283. case models.IsErrKeyAlreadyExist(err):
  284. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form)
  285. case models.IsErrKeyNameAlreadyUsed(err):
  286. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), SETTINGS_SSH_KEYS, &form)
  287. default:
  288. ctx.Handle(500, "AddPublicKey", err)
  289. }
  290. return
  291. } else {
  292. log.Trace("SSH key added: %s", ctx.User.Name)
  293. ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title))
  294. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  295. return
  296. }
  297. }
  298. ctx.HTML(200, SETTINGS_SSH_KEYS)
  299. }
  300. func SettingsSocial(ctx *middleware.Context) {
  301. ctx.Data["Title"] = ctx.Tr("settings")
  302. ctx.Data["PageIsSettingsSocial"] = true
  303. // Unbind social account.
  304. remove, _ := com.StrTo(ctx.Query("remove")).Int64()
  305. if remove > 0 {
  306. if err := models.DeleteOauth2ById(remove); err != nil {
  307. ctx.Handle(500, "DeleteOauth2ById", err)
  308. return
  309. }
  310. ctx.Flash.Success(ctx.Tr("settings.unbind_success"))
  311. ctx.Redirect(setting.AppSubUrl + "/user/settings/social")
  312. return
  313. }
  314. socials, err := models.GetOauthByUserId(ctx.User.Id)
  315. if err != nil {
  316. ctx.Handle(500, "GetOauthByUserId", err)
  317. return
  318. }
  319. ctx.Data["Socials"] = socials
  320. ctx.HTML(200, SETTINGS_SOCIAL)
  321. }
  322. func SettingsApplications(ctx *middleware.Context) {
  323. ctx.Data["Title"] = ctx.Tr("settings")
  324. ctx.Data["PageIsSettingsApplications"] = true
  325. tokens, err := models.ListAccessTokens(ctx.User.Id)
  326. if err != nil {
  327. ctx.Handle(500, "ListAccessTokens", err)
  328. return
  329. }
  330. ctx.Data["Tokens"] = tokens
  331. ctx.HTML(200, SETTINGS_APPLICATIONS)
  332. }
  333. func SettingsApplicationsPost(ctx *middleware.Context, form auth.NewAccessTokenForm) {
  334. ctx.Data["Title"] = ctx.Tr("settings")
  335. ctx.Data["PageIsSettingsApplications"] = true
  336. if ctx.HasError() {
  337. ctx.HTML(200, SETTINGS_APPLICATIONS)
  338. return
  339. }
  340. t := &models.AccessToken{
  341. UID: ctx.User.Id,
  342. Name: form.Name,
  343. }
  344. if err := models.NewAccessToken(t); err != nil {
  345. ctx.Handle(500, "NewAccessToken", err)
  346. return
  347. }
  348. ctx.Flash.Success(ctx.Tr("settings.generate_token_succees"))
  349. ctx.Flash.Info(t.Sha1)
  350. ctx.Redirect(setting.AppSubUrl + "/user/settings/applications")
  351. }
  352. func SettingsDeleteApplication(ctx *middleware.Context) {
  353. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id")); err != nil {
  354. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  355. } else {
  356. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  357. }
  358. ctx.JSON(200, map[string]interface{}{
  359. "redirect": setting.AppSubUrl + "/user/settings/applications",
  360. })
  361. }
  362. func SettingsDelete(ctx *middleware.Context) {
  363. ctx.Data["Title"] = ctx.Tr("settings")
  364. ctx.Data["PageIsSettingsDelete"] = true
  365. if ctx.Req.Method == "POST" {
  366. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  367. if models.IsErrUserNotExist(err) {
  368. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  369. } else {
  370. ctx.Handle(500, "UserSignIn", err)
  371. }
  372. return
  373. }
  374. if err := models.DeleteUser(ctx.User); err != nil {
  375. switch {
  376. case models.IsErrUserOwnRepos(err):
  377. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  378. ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
  379. case models.IsErrUserHasOrgs(err):
  380. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  381. ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
  382. default:
  383. ctx.Handle(500, "DeleteUser", err)
  384. }
  385. } else {
  386. log.Trace("Account deleted: %s", ctx.User.Name)
  387. ctx.Redirect(setting.AppSubUrl + "/")
  388. }
  389. return
  390. }
  391. ctx.HTML(200, SETTINGS_DELETE)
  392. }