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.

484 lines
13 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
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["PageIsUserSettings"] = true
  31. ctx.Data["PageIsSettingsProfile"] = true
  32. ctx.HTML(200, SETTINGS_PROFILE)
  33. }
  34. func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
  35. ctx.Data["Title"] = ctx.Tr("settings")
  36. ctx.Data["PageIsUserSettings"] = true
  37. ctx.Data["PageIsSettingsProfile"] = true
  38. if ctx.HasError() {
  39. ctx.HTML(200, SETTINGS_PROFILE)
  40. return
  41. }
  42. // Check if user name has been changed.
  43. if ctx.User.Name != form.UserName {
  44. isExist, err := models.IsUserExist(form.UserName)
  45. if err != nil {
  46. ctx.Handle(500, "IsUserExist", err)
  47. return
  48. } else if isExist {
  49. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form)
  50. return
  51. } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
  52. if err == models.ErrUserNameIllegal {
  53. ctx.Flash.Error(ctx.Tr("form.illegal_username"))
  54. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  55. return
  56. } else {
  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["PageIsUserSettings"] = true
  117. ctx.Data["PageIsSettingsEmails"] = true
  118. var err error
  119. ctx.Data["Emails"], err = models.GetEmailAddresses(ctx.User.Id)
  120. if err != nil {
  121. ctx.Handle(500, "email.GetEmailAddresses", err)
  122. return
  123. }
  124. ctx.HTML(200, SETTINGS_EMAILS)
  125. }
  126. func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
  127. ctx.Data["Title"] = ctx.Tr("settings")
  128. ctx.Data["PageIsUserSettings"] = true
  129. ctx.Data["PageIsSettingsEmails"] = true
  130. var err error
  131. ctx.Data["Emails"], err = models.GetEmailAddresses(ctx.User.Id)
  132. if err != nil {
  133. ctx.Handle(500, "email.GetEmailAddresses", err)
  134. return
  135. }
  136. // Delete Email address.
  137. if ctx.Query("_method") == "DELETE" {
  138. id := com.StrTo(ctx.Query("id")).MustInt64()
  139. if id <= 0 {
  140. return
  141. }
  142. if err = models.DeleteEmailAddress(&models.EmailAddress{Id: id}); err != nil {
  143. ctx.Handle(500, "DeleteEmail", err)
  144. } else {
  145. log.Trace("Email address deleted: %s", ctx.User.Name)
  146. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  147. }
  148. return
  149. }
  150. // Make emailaddress primary.
  151. if ctx.Query("_method") == "PRIMARY" {
  152. id := com.StrTo(ctx.Query("id")).MustInt64()
  153. if id <= 0 {
  154. return
  155. }
  156. if err = models.MakeEmailPrimary(&models.EmailAddress{Id: id}); err != nil {
  157. ctx.Handle(500, "MakeEmailPrimary", err)
  158. } else {
  159. log.Trace("Email made primary: %s", ctx.User.Name)
  160. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  161. }
  162. return
  163. }
  164. // Add Email address.
  165. if ctx.Req.Method == "POST" {
  166. if ctx.HasError() {
  167. ctx.HTML(200, SETTINGS_EMAILS)
  168. return
  169. }
  170. cleanEmail := strings.Replace(form.Email, "\n", "", -1)
  171. e := &models.EmailAddress{
  172. Uid: ctx.User.Id,
  173. Email: cleanEmail,
  174. IsActivated: !setting.Service.RegisterEmailConfirm,
  175. }
  176. if err := models.AddEmailAddress(e); err != nil {
  177. if err == models.ErrEmailAlreadyUsed {
  178. ctx.RenderWithErr(ctx.Tr("form.email_has_been_used"), SETTINGS_EMAILS, &form)
  179. return
  180. }
  181. ctx.Handle(500, "email.AddEmailAddress", err)
  182. return
  183. } else {
  184. // Send confirmation e-mail
  185. if setting.Service.RegisterEmailConfirm {
  186. mailer.SendActivateEmail(ctx.Render, ctx.User, e)
  187. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  188. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  189. }
  190. ctx.Flash.Success(ctx.Tr("settings.add_email_success_confirmation_email_sent"))
  191. } else {
  192. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  193. }
  194. log.Trace("Email address added: %s", e.Email)
  195. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  196. return
  197. }
  198. }
  199. ctx.HTML(200, SETTINGS_EMAILS)
  200. }
  201. func SettingsPassword(ctx *middleware.Context) {
  202. ctx.Data["Title"] = ctx.Tr("settings")
  203. ctx.Data["PageIsUserSettings"] = true
  204. ctx.Data["PageIsSettingsPassword"] = true
  205. ctx.HTML(200, SETTINGS_PASSWORD)
  206. }
  207. func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
  208. ctx.Data["Title"] = ctx.Tr("settings")
  209. ctx.Data["PageIsUserSettings"] = true
  210. ctx.Data["PageIsSettingsPassword"] = true
  211. if ctx.HasError() {
  212. ctx.HTML(200, SETTINGS_PASSWORD)
  213. return
  214. }
  215. tmpUser := &models.User{
  216. Passwd: form.OldPassword,
  217. Salt: ctx.User.Salt,
  218. }
  219. tmpUser.EncodePasswd()
  220. if ctx.User.Passwd != tmpUser.Passwd {
  221. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  222. } else if form.Password != form.Retype {
  223. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  224. } else {
  225. ctx.User.Passwd = form.Password
  226. ctx.User.Salt = models.GetUserSalt()
  227. ctx.User.EncodePasswd()
  228. if err := models.UpdateUser(ctx.User); err != nil {
  229. ctx.Handle(500, "UpdateUser", err)
  230. return
  231. }
  232. log.Trace("User password updated: %s", ctx.User.Name)
  233. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  234. }
  235. ctx.Redirect(setting.AppSubUrl + "/user/settings/password")
  236. }
  237. func SettingsSSHKeys(ctx *middleware.Context) {
  238. ctx.Data["Title"] = ctx.Tr("settings")
  239. ctx.Data["PageIsUserSettings"] = true
  240. ctx.Data["PageIsSettingsSSHKeys"] = true
  241. var err error
  242. ctx.Data["Keys"], err = models.ListPublicKeys(ctx.User.Id)
  243. if err != nil {
  244. ctx.Handle(500, "ssh.ListPublicKey", err)
  245. return
  246. }
  247. ctx.HTML(200, SETTINGS_SSH_KEYS)
  248. }
  249. func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  250. ctx.Data["Title"] = ctx.Tr("settings")
  251. ctx.Data["PageIsUserSettings"] = true
  252. ctx.Data["PageIsSettingsSSHKeys"] = true
  253. var err error
  254. ctx.Data["Keys"], err = models.ListPublicKeys(ctx.User.Id)
  255. if err != nil {
  256. ctx.Handle(500, "ssh.ListPublicKey", err)
  257. return
  258. }
  259. // Delete SSH key.
  260. if ctx.Query("_method") == "DELETE" {
  261. id := com.StrTo(ctx.Query("id")).MustInt64()
  262. if id <= 0 {
  263. return
  264. }
  265. if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil {
  266. ctx.Handle(500, "DeletePublicKey", err)
  267. } else {
  268. log.Trace("SSH key deleted: %s", ctx.User.Name)
  269. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  270. }
  271. return
  272. }
  273. // Add new SSH key.
  274. if ctx.Req.Method == "POST" {
  275. if ctx.HasError() {
  276. ctx.HTML(200, SETTINGS_SSH_KEYS)
  277. return
  278. }
  279. // Parse openssh style string from form content
  280. content, err := models.ParseKeyString(form.Content)
  281. if err != nil {
  282. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  283. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  284. return
  285. }
  286. if ok, err := models.CheckPublicKeyString(content); !ok {
  287. if err == models.ErrKeyUnableVerify {
  288. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  289. } else {
  290. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  291. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  292. return
  293. }
  294. }
  295. k := &models.PublicKey{
  296. OwnerId: ctx.User.Id,
  297. Name: form.SSHTitle,
  298. Content: content,
  299. }
  300. if err := models.AddPublicKey(k); err != nil {
  301. if err == models.ErrKeyAlreadyExist {
  302. ctx.RenderWithErr(ctx.Tr("form.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form)
  303. return
  304. }
  305. ctx.Handle(500, "ssh.AddPublicKey", err)
  306. return
  307. } else {
  308. log.Trace("SSH key added: %s", ctx.User.Name)
  309. ctx.Flash.Success(ctx.Tr("settings.add_key_success"))
  310. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  311. return
  312. }
  313. }
  314. ctx.HTML(200, SETTINGS_SSH_KEYS)
  315. }
  316. func SettingsSocial(ctx *middleware.Context) {
  317. ctx.Data["Title"] = ctx.Tr("settings")
  318. ctx.Data["PageIsUserSettings"] = true
  319. ctx.Data["PageIsSettingsSocial"] = true
  320. // Unbind social account.
  321. remove, _ := com.StrTo(ctx.Query("remove")).Int64()
  322. if remove > 0 {
  323. if err := models.DeleteOauth2ById(remove); err != nil {
  324. ctx.Handle(500, "DeleteOauth2ById", err)
  325. return
  326. }
  327. ctx.Flash.Success(ctx.Tr("settings.unbind_success"))
  328. ctx.Redirect(setting.AppSubUrl + "/user/settings/social")
  329. return
  330. }
  331. socials, err := models.GetOauthByUserId(ctx.User.Id)
  332. if err != nil {
  333. ctx.Handle(500, "GetOauthByUserId", err)
  334. return
  335. }
  336. ctx.Data["Socials"] = socials
  337. ctx.HTML(200, SETTINGS_SOCIAL)
  338. }
  339. func SettingsApplications(ctx *middleware.Context) {
  340. ctx.Data["Title"] = ctx.Tr("settings")
  341. ctx.Data["PageIsUserSettings"] = true
  342. ctx.Data["PageIsSettingsApplications"] = true
  343. // Delete access token.
  344. remove, _ := com.StrTo(ctx.Query("remove")).Int64()
  345. if remove > 0 {
  346. if err := models.DeleteAccessTokenById(remove); err != nil {
  347. ctx.Handle(500, "DeleteAccessTokenById", err)
  348. return
  349. }
  350. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  351. ctx.Redirect(setting.AppSubUrl + "/user/settings/applications")
  352. return
  353. }
  354. tokens, err := models.ListAccessTokens(ctx.User.Id)
  355. if err != nil {
  356. ctx.Handle(500, "ListAccessTokens", err)
  357. return
  358. }
  359. ctx.Data["Tokens"] = tokens
  360. ctx.HTML(200, SETTINGS_APPLICATIONS)
  361. }
  362. // FIXME: split to two different functions and pages to handle access token and oauth2
  363. func SettingsApplicationsPost(ctx *middleware.Context, form auth.NewAccessTokenForm) {
  364. ctx.Data["Title"] = ctx.Tr("settings")
  365. ctx.Data["PageIsUserSettings"] = true
  366. ctx.Data["PageIsSettingsApplications"] = true
  367. switch ctx.Query("type") {
  368. case "token":
  369. if ctx.HasError() {
  370. ctx.HTML(200, SETTINGS_APPLICATIONS)
  371. return
  372. }
  373. t := &models.AccessToken{
  374. Uid: ctx.User.Id,
  375. Name: form.Name,
  376. }
  377. if err := models.NewAccessToken(t); err != nil {
  378. ctx.Handle(500, "NewAccessToken", err)
  379. return
  380. }
  381. ctx.Flash.Success(ctx.Tr("settings.generate_token_succees"))
  382. ctx.Flash.Info(t.Sha1)
  383. }
  384. ctx.Redirect(setting.AppSubUrl + "/user/settings/applications")
  385. }
  386. func SettingsDelete(ctx *middleware.Context) {
  387. ctx.Data["Title"] = ctx.Tr("settings")
  388. ctx.Data["PageIsUserSettings"] = true
  389. ctx.Data["PageIsSettingsDelete"] = true
  390. if ctx.Req.Method == "POST" {
  391. // tmpUser := models.User{
  392. // Passwd: ctx.Query("password"),
  393. // Salt: ctx.User.Salt,
  394. // }
  395. // tmpUser.EncodePasswd()
  396. // if tmpUser.Passwd != ctx.User.Passwd {
  397. // ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
  398. // } else {
  399. if err := models.DeleteUser(ctx.User); err != nil {
  400. switch err {
  401. case models.ErrUserOwnRepos:
  402. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  403. ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
  404. case models.ErrUserHasOrgs:
  405. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  406. ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
  407. default:
  408. ctx.Handle(500, "DeleteUser", err)
  409. }
  410. } else {
  411. log.Trace("Account deleted: %s", ctx.User.Name)
  412. ctx.Redirect(setting.AppSubUrl + "/")
  413. }
  414. return
  415. }
  416. ctx.HTML(200, SETTINGS_DELETE)
  417. }