setting.go 13 KB

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