setting.go 13 KB

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