setting.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. "bytes"
  7. "encoding/base64"
  8. "fmt"
  9. "html/template"
  10. "image/png"
  11. "io/ioutil"
  12. "strings"
  13. "github.com/pquerna/otp"
  14. "github.com/pquerna/otp/totp"
  15. "github.com/unknwon/com"
  16. log "unknwon.dev/clog/v2"
  17. "gogs.io/gogs/internal/conf"
  18. "gogs.io/gogs/internal/context"
  19. "gogs.io/gogs/internal/db"
  20. "gogs.io/gogs/internal/db/errors"
  21. "gogs.io/gogs/internal/email"
  22. "gogs.io/gogs/internal/form"
  23. "gogs.io/gogs/internal/tool"
  24. )
  25. const (
  26. SETTINGS_PROFILE = "user/settings/profile"
  27. SETTINGS_AVATAR = "user/settings/avatar"
  28. SETTINGS_PASSWORD = "user/settings/password"
  29. SETTINGS_EMAILS = "user/settings/email"
  30. SETTINGS_SSH_KEYS = "user/settings/sshkeys"
  31. SETTINGS_SECURITY = "user/settings/security"
  32. SETTINGS_TWO_FACTOR_ENABLE = "user/settings/two_factor_enable"
  33. SETTINGS_TWO_FACTOR_RECOVERY_CODES = "user/settings/two_factor_recovery_codes"
  34. SETTINGS_REPOSITORIES = "user/settings/repositories"
  35. SETTINGS_ORGANIZATIONS = "user/settings/organizations"
  36. SETTINGS_APPLICATIONS = "user/settings/applications"
  37. SETTINGS_DELETE = "user/settings/delete"
  38. NOTIFICATION = "user/notification"
  39. )
  40. func Settings(c *context.Context) {
  41. c.Title("settings.profile")
  42. c.PageIs("SettingsProfile")
  43. c.Data["origin_name"] = c.User.Name
  44. c.Data["name"] = c.User.Name
  45. c.Data["full_name"] = c.User.FullName
  46. c.Data["email"] = c.User.Email
  47. c.Data["website"] = c.User.Website
  48. c.Data["location"] = c.User.Location
  49. c.Success(SETTINGS_PROFILE)
  50. }
  51. func SettingsPost(c *context.Context, f form.UpdateProfile) {
  52. c.Title("settings.profile")
  53. c.PageIs("SettingsProfile")
  54. c.Data["origin_name"] = c.User.Name
  55. if c.HasError() {
  56. c.Success(SETTINGS_PROFILE)
  57. return
  58. }
  59. // Non-local users are not allowed to change their username
  60. if c.User.IsLocal() {
  61. // Check if username characters have been changed
  62. if c.User.LowerName != strings.ToLower(f.Name) {
  63. if err := db.ChangeUserName(c.User, f.Name); err != nil {
  64. c.FormErr("Name")
  65. var msg string
  66. switch {
  67. case db.IsErrUserAlreadyExist(err):
  68. msg = c.Tr("form.username_been_taken")
  69. case db.IsErrNameReserved(err):
  70. msg = c.Tr("form.name_reserved")
  71. case db.IsErrNamePatternNotAllowed(err):
  72. msg = c.Tr("form.name_pattern_not_allowed")
  73. default:
  74. c.Error(err, "change user name")
  75. return
  76. }
  77. c.RenderWithErr(msg, SETTINGS_PROFILE, &f)
  78. return
  79. }
  80. log.Trace("Username changed: %s -> %s", c.User.Name, f.Name)
  81. }
  82. // In case it's just a case change
  83. c.User.Name = f.Name
  84. c.User.LowerName = strings.ToLower(f.Name)
  85. }
  86. c.User.FullName = f.FullName
  87. c.User.Email = f.Email
  88. c.User.Website = f.Website
  89. c.User.Location = f.Location
  90. if err := db.UpdateUser(c.User); err != nil {
  91. if db.IsErrEmailAlreadyUsed(err) {
  92. msg := c.Tr("form.email_been_used")
  93. c.RenderWithErr(msg, SETTINGS_PROFILE, &f)
  94. return
  95. }
  96. c.Errorf(err, "update user")
  97. return
  98. }
  99. c.Flash.Success(c.Tr("settings.update_profile_success"))
  100. c.RedirectSubpath("/user/settings")
  101. }
  102. // FIXME: limit upload size
  103. func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *db.User) error {
  104. ctxUser.UseCustomAvatar = f.Source == form.AVATAR_LOCAL
  105. if len(f.Gravatar) > 0 {
  106. ctxUser.Avatar = tool.MD5(f.Gravatar)
  107. ctxUser.AvatarEmail = f.Gravatar
  108. }
  109. if f.Avatar != nil && f.Avatar.Filename != "" {
  110. r, err := f.Avatar.Open()
  111. if err != nil {
  112. return fmt.Errorf("open avatar reader: %v", err)
  113. }
  114. defer func() {
  115. _ = r.Close()
  116. }()
  117. data, err := ioutil.ReadAll(r)
  118. if err != nil {
  119. return fmt.Errorf("read avatar content: %v", err)
  120. }
  121. if !tool.IsImageFile(data) {
  122. return errors.New(c.Tr("settings.uploaded_avatar_not_a_image"))
  123. }
  124. if err = ctxUser.UploadAvatar(data); err != nil {
  125. return fmt.Errorf("upload avatar: %v", err)
  126. }
  127. } else {
  128. // No avatar is uploaded but setting has been changed to enable,
  129. // generate a random one when needed.
  130. if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) {
  131. if err := ctxUser.GenerateRandomAvatar(); err != nil {
  132. log.Error("generate random avatar [%d]: %v", ctxUser.ID, err)
  133. }
  134. }
  135. }
  136. if err := db.UpdateUser(ctxUser); err != nil {
  137. return fmt.Errorf("update user: %v", err)
  138. }
  139. return nil
  140. }
  141. func SettingsAvatar(c *context.Context) {
  142. c.Title("settings.avatar")
  143. c.PageIs("SettingsAvatar")
  144. c.Success(SETTINGS_AVATAR)
  145. }
  146. func SettingsAvatarPost(c *context.Context, f form.Avatar) {
  147. if err := UpdateAvatarSetting(c, f, c.User); err != nil {
  148. c.Flash.Error(err.Error())
  149. } else {
  150. c.Flash.Success(c.Tr("settings.update_avatar_success"))
  151. }
  152. c.RedirectSubpath("/user/settings/avatar")
  153. }
  154. func SettingsDeleteAvatar(c *context.Context) {
  155. if err := c.User.DeleteAvatar(); err != nil {
  156. c.Flash.Error(fmt.Sprintf("Failed to delete avatar: %v", err))
  157. }
  158. c.RedirectSubpath("/user/settings/avatar")
  159. }
  160. func SettingsPassword(c *context.Context) {
  161. c.Title("settings.password")
  162. c.PageIs("SettingsPassword")
  163. c.Success(SETTINGS_PASSWORD)
  164. }
  165. func SettingsPasswordPost(c *context.Context, f form.ChangePassword) {
  166. c.Title("settings.password")
  167. c.PageIs("SettingsPassword")
  168. if c.HasError() {
  169. c.Success(SETTINGS_PASSWORD)
  170. return
  171. }
  172. if !c.User.ValidatePassword(f.OldPassword) {
  173. c.Flash.Error(c.Tr("settings.password_incorrect"))
  174. } else if f.Password != f.Retype {
  175. c.Flash.Error(c.Tr("form.password_not_match"))
  176. } else {
  177. c.User.Passwd = f.Password
  178. var err error
  179. if c.User.Salt, err = db.GetUserSalt(); err != nil {
  180. c.Errorf(err, "get user salt")
  181. return
  182. }
  183. c.User.EncodePasswd()
  184. if err := db.UpdateUser(c.User); err != nil {
  185. c.Errorf(err, "update user")
  186. return
  187. }
  188. c.Flash.Success(c.Tr("settings.change_password_success"))
  189. }
  190. c.RedirectSubpath("/user/settings/password")
  191. }
  192. func SettingsEmails(c *context.Context) {
  193. c.Title("settings.emails")
  194. c.PageIs("SettingsEmails")
  195. emails, err := db.GetEmailAddresses(c.User.ID)
  196. if err != nil {
  197. c.Errorf(err, "get email addresses")
  198. return
  199. }
  200. c.Data["Emails"] = emails
  201. c.Success(SETTINGS_EMAILS)
  202. }
  203. func SettingsEmailPost(c *context.Context, f form.AddEmail) {
  204. c.Title("settings.emails")
  205. c.PageIs("SettingsEmails")
  206. // Make emailaddress primary.
  207. if c.Query("_method") == "PRIMARY" {
  208. if err := db.MakeEmailPrimary(c.UserID(), &db.EmailAddress{ID: c.QueryInt64("id")}); err != nil {
  209. c.Errorf(err, "make email primary")
  210. return
  211. }
  212. c.RedirectSubpath("/user/settings/email")
  213. return
  214. }
  215. // Add Email address.
  216. emails, err := db.GetEmailAddresses(c.User.ID)
  217. if err != nil {
  218. c.Errorf(err, "get email addresses")
  219. return
  220. }
  221. c.Data["Emails"] = emails
  222. if c.HasError() {
  223. c.Success(SETTINGS_EMAILS)
  224. return
  225. }
  226. emailAddr := &db.EmailAddress{
  227. UID: c.User.ID,
  228. Email: f.Email,
  229. IsActivated: !conf.Auth.RequireEmailConfirmation,
  230. }
  231. if err := db.AddEmailAddress(emailAddr); err != nil {
  232. if db.IsErrEmailAlreadyUsed(err) {
  233. c.RenderWithErr(c.Tr("form.email_been_used"), SETTINGS_EMAILS, &f)
  234. } else {
  235. c.Errorf(err, "add email address")
  236. }
  237. return
  238. }
  239. // Send confirmation email
  240. if conf.Auth.RequireEmailConfirmation {
  241. email.SendActivateEmailMail(c.Context, db.NewMailerUser(c.User), emailAddr.Email)
  242. if err := c.Cache.Put("MailResendLimit_"+c.User.LowerName, c.User.LowerName, 180); err != nil {
  243. log.Error("Set cache 'MailResendLimit' failed: %v", err)
  244. }
  245. c.Flash.Info(c.Tr("settings.add_email_confirmation_sent", emailAddr.Email, conf.Auth.ActivateCodeLives/60))
  246. } else {
  247. c.Flash.Success(c.Tr("settings.add_email_success"))
  248. }
  249. c.RedirectSubpath("/user/settings/email")
  250. }
  251. func DeleteEmail(c *context.Context) {
  252. if err := db.DeleteEmailAddress(&db.EmailAddress{
  253. ID: c.QueryInt64("id"),
  254. UID: c.User.ID,
  255. }); err != nil {
  256. c.Errorf(err, "delete email address")
  257. return
  258. }
  259. c.Flash.Success(c.Tr("settings.email_deletion_success"))
  260. c.JSONSuccess(map[string]interface{}{
  261. "redirect": conf.Server.Subpath + "/user/settings/email",
  262. })
  263. }
  264. func SettingsSSHKeys(c *context.Context) {
  265. c.Title("settings.ssh_keys")
  266. c.PageIs("SettingsSSHKeys")
  267. keys, err := db.ListPublicKeys(c.User.ID)
  268. if err != nil {
  269. c.Errorf(err, "list public keys")
  270. return
  271. }
  272. c.Data["Keys"] = keys
  273. c.Success(SETTINGS_SSH_KEYS)
  274. }
  275. func SettingsSSHKeysPost(c *context.Context, f form.AddSSHKey) {
  276. c.Title("settings.ssh_keys")
  277. c.PageIs("SettingsSSHKeys")
  278. keys, err := db.ListPublicKeys(c.User.ID)
  279. if err != nil {
  280. c.Errorf(err, "list public keys")
  281. return
  282. }
  283. c.Data["Keys"] = keys
  284. if c.HasError() {
  285. c.Success(SETTINGS_SSH_KEYS)
  286. return
  287. }
  288. content, err := db.CheckPublicKeyString(f.Content)
  289. if err != nil {
  290. if db.IsErrKeyUnableVerify(err) {
  291. c.Flash.Info(c.Tr("form.unable_verify_ssh_key"))
  292. } else {
  293. c.Flash.Error(c.Tr("form.invalid_ssh_key", err.Error()))
  294. c.RedirectSubpath("/user/settings/ssh")
  295. return
  296. }
  297. }
  298. if _, err = db.AddPublicKey(c.User.ID, f.Title, content); err != nil {
  299. c.Data["HasError"] = true
  300. switch {
  301. case db.IsErrKeyAlreadyExist(err):
  302. c.FormErr("Content")
  303. c.RenderWithErr(c.Tr("settings.ssh_key_been_used"), SETTINGS_SSH_KEYS, &f)
  304. case db.IsErrKeyNameAlreadyUsed(err):
  305. c.FormErr("Title")
  306. c.RenderWithErr(c.Tr("settings.ssh_key_name_used"), SETTINGS_SSH_KEYS, &f)
  307. default:
  308. c.Errorf(err, "add public key")
  309. }
  310. return
  311. }
  312. c.Flash.Success(c.Tr("settings.add_key_success", f.Title))
  313. c.RedirectSubpath("/user/settings/ssh")
  314. }
  315. func DeleteSSHKey(c *context.Context) {
  316. if err := db.DeletePublicKey(c.User, c.QueryInt64("id")); err != nil {
  317. c.Flash.Error("DeletePublicKey: " + err.Error())
  318. } else {
  319. c.Flash.Success(c.Tr("settings.ssh_key_deletion_success"))
  320. }
  321. c.JSONSuccess(map[string]interface{}{
  322. "redirect": conf.Server.Subpath + "/user/settings/ssh",
  323. })
  324. }
  325. func SettingsSecurity(c *context.Context) {
  326. c.Title("settings.security")
  327. c.PageIs("SettingsSecurity")
  328. t, err := db.GetTwoFactorByUserID(c.UserID())
  329. if err != nil && !errors.IsTwoFactorNotFound(err) {
  330. c.Errorf(err, "get two factor by user ID")
  331. return
  332. }
  333. c.Data["TwoFactor"] = t
  334. c.Success(SETTINGS_SECURITY)
  335. }
  336. func SettingsTwoFactorEnable(c *context.Context) {
  337. if c.User.IsEnabledTwoFactor() {
  338. c.NotFound()
  339. return
  340. }
  341. c.Title("settings.two_factor_enable_title")
  342. c.PageIs("SettingsSecurity")
  343. var key *otp.Key
  344. var err error
  345. keyURL := c.Session.Get("twoFactorURL")
  346. if keyURL != nil {
  347. key, _ = otp.NewKeyFromURL(keyURL.(string))
  348. }
  349. if key == nil {
  350. key, err = totp.Generate(totp.GenerateOpts{
  351. Issuer: conf.App.BrandName,
  352. AccountName: c.User.Email,
  353. })
  354. if err != nil {
  355. c.Errorf(err, "generate TOTP")
  356. return
  357. }
  358. }
  359. c.Data["TwoFactorSecret"] = key.Secret()
  360. img, err := key.Image(240, 240)
  361. if err != nil {
  362. c.Errorf(err, "generate image")
  363. return
  364. }
  365. var buf bytes.Buffer
  366. if err = png.Encode(&buf, img); err != nil {
  367. c.Errorf(err, "encode image")
  368. return
  369. }
  370. c.Data["QRCode"] = template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(buf.Bytes()))
  371. _ = c.Session.Set("twoFactorSecret", c.Data["TwoFactorSecret"])
  372. _ = c.Session.Set("twoFactorURL", key.String())
  373. c.Success(SETTINGS_TWO_FACTOR_ENABLE)
  374. }
  375. func SettingsTwoFactorEnablePost(c *context.Context) {
  376. secret, ok := c.Session.Get("twoFactorSecret").(string)
  377. if !ok {
  378. c.NotFound()
  379. return
  380. }
  381. if !totp.Validate(c.Query("passcode"), secret) {
  382. c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode"))
  383. c.RedirectSubpath("/user/settings/security/two_factor_enable")
  384. return
  385. }
  386. if err := db.NewTwoFactor(c.UserID(), secret); err != nil {
  387. c.Flash.Error(c.Tr("settings.two_factor_enable_error", err))
  388. c.RedirectSubpath("/user/settings/security/two_factor_enable")
  389. return
  390. }
  391. _ = c.Session.Delete("twoFactorSecret")
  392. _ = c.Session.Delete("twoFactorURL")
  393. c.Flash.Success(c.Tr("settings.two_factor_enable_success"))
  394. c.RedirectSubpath("/user/settings/security/two_factor_recovery_codes")
  395. }
  396. func SettingsTwoFactorRecoveryCodes(c *context.Context) {
  397. if !c.User.IsEnabledTwoFactor() {
  398. c.NotFound()
  399. return
  400. }
  401. c.Title("settings.two_factor_recovery_codes_title")
  402. c.PageIs("SettingsSecurity")
  403. recoveryCodes, err := db.GetRecoveryCodesByUserID(c.UserID())
  404. if err != nil {
  405. c.Errorf(err, "get recovery codes by user ID")
  406. return
  407. }
  408. c.Data["RecoveryCodes"] = recoveryCodes
  409. c.Success(SETTINGS_TWO_FACTOR_RECOVERY_CODES)
  410. }
  411. func SettingsTwoFactorRecoveryCodesPost(c *context.Context) {
  412. if !c.User.IsEnabledTwoFactor() {
  413. c.NotFound()
  414. return
  415. }
  416. if err := db.RegenerateRecoveryCodes(c.UserID()); err != nil {
  417. c.Flash.Error(c.Tr("settings.two_factor_regenerate_recovery_codes_error", err))
  418. } else {
  419. c.Flash.Success(c.Tr("settings.two_factor_regenerate_recovery_codes_success"))
  420. }
  421. c.RedirectSubpath("/user/settings/security/two_factor_recovery_codes")
  422. }
  423. func SettingsTwoFactorDisable(c *context.Context) {
  424. if !c.User.IsEnabledTwoFactor() {
  425. c.NotFound()
  426. return
  427. }
  428. if err := db.DeleteTwoFactor(c.UserID()); err != nil {
  429. c.Errorf(err, "delete two factor")
  430. return
  431. }
  432. c.Flash.Success(c.Tr("settings.two_factor_disable_success"))
  433. c.JSONSuccess(map[string]interface{}{
  434. "redirect": conf.Server.Subpath + "/user/settings/security",
  435. })
  436. }
  437. func SettingsRepos(c *context.Context) {
  438. c.Title("settings.repos")
  439. c.PageIs("SettingsRepositories")
  440. repos, err := db.GetUserAndCollaborativeRepositories(c.User.ID)
  441. if err != nil {
  442. c.Errorf(err, "get user and collaborative repositories")
  443. return
  444. }
  445. if err = db.RepositoryList(repos).LoadAttributes(); err != nil {
  446. c.Errorf(err, "load attributes")
  447. return
  448. }
  449. c.Data["Repos"] = repos
  450. c.Success(SETTINGS_REPOSITORIES)
  451. }
  452. func SettingsLeaveRepo(c *context.Context) {
  453. repo, err := db.GetRepositoryByID(c.QueryInt64("id"))
  454. if err != nil {
  455. c.NotFoundOrError(err, "get repository by ID")
  456. return
  457. }
  458. if err = repo.DeleteCollaboration(c.User.ID); err != nil {
  459. c.Errorf(err, "delete collaboration")
  460. return
  461. }
  462. c.Flash.Success(c.Tr("settings.repos.leave_success", repo.FullName()))
  463. c.JSONSuccess(map[string]interface{}{
  464. "redirect": conf.Server.Subpath + "/user/settings/repositories",
  465. })
  466. }
  467. func SettingsOrganizations(c *context.Context) {
  468. c.Title("settings.orgs")
  469. c.PageIs("SettingsOrganizations")
  470. orgs, err := db.GetOrgsByUserID(c.User.ID, true)
  471. if err != nil {
  472. c.Errorf(err, "get organizations by user ID")
  473. return
  474. }
  475. c.Data["Orgs"] = orgs
  476. c.Success(SETTINGS_ORGANIZATIONS)
  477. }
  478. func SettingsLeaveOrganization(c *context.Context) {
  479. if err := db.RemoveOrgUser(c.QueryInt64("id"), c.User.ID); err != nil {
  480. if db.IsErrLastOrgOwner(err) {
  481. c.Flash.Error(c.Tr("form.last_org_owner"))
  482. } else {
  483. c.Errorf(err, "remove organization user")
  484. return
  485. }
  486. }
  487. c.JSONSuccess(map[string]interface{}{
  488. "redirect": conf.Server.Subpath + "/user/settings/organizations",
  489. })
  490. }
  491. func SettingsApplications(c *context.Context) {
  492. c.Title("settings.applications")
  493. c.PageIs("SettingsApplications")
  494. tokens, err := db.ListAccessTokens(c.User.ID)
  495. if err != nil {
  496. c.Errorf(err, "list access tokens")
  497. return
  498. }
  499. c.Data["Tokens"] = tokens
  500. c.Success(SETTINGS_APPLICATIONS)
  501. }
  502. func SettingsApplicationsPost(c *context.Context, f form.NewAccessToken) {
  503. c.Title("settings.applications")
  504. c.PageIs("SettingsApplications")
  505. if c.HasError() {
  506. tokens, err := db.ListAccessTokens(c.User.ID)
  507. if err != nil {
  508. c.Errorf(err, "list access tokens")
  509. return
  510. }
  511. c.Data["Tokens"] = tokens
  512. c.Success(SETTINGS_APPLICATIONS)
  513. return
  514. }
  515. t := &db.AccessToken{
  516. UID: c.User.ID,
  517. Name: f.Name,
  518. }
  519. if err := db.NewAccessToken(t); err != nil {
  520. if errors.IsAccessTokenNameAlreadyExist(err) {
  521. c.Flash.Error(c.Tr("settings.token_name_exists"))
  522. c.RedirectSubpath("/user/settings/applications")
  523. } else {
  524. c.Errorf(err, "new access token")
  525. }
  526. return
  527. }
  528. c.Flash.Success(c.Tr("settings.generate_token_succees"))
  529. c.Flash.Info(t.Sha1)
  530. c.RedirectSubpath("/user/settings/applications")
  531. }
  532. func SettingsDeleteApplication(c *context.Context) {
  533. if err := db.DeleteAccessTokenOfUserByID(c.User.ID, c.QueryInt64("id")); err != nil {
  534. c.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  535. } else {
  536. c.Flash.Success(c.Tr("settings.delete_token_success"))
  537. }
  538. c.JSONSuccess(map[string]interface{}{
  539. "redirect": conf.Server.Subpath + "/user/settings/applications",
  540. })
  541. }
  542. func SettingsDelete(c *context.Context) {
  543. c.Title("settings.delete")
  544. c.PageIs("SettingsDelete")
  545. if c.Req.Method == "POST" {
  546. if _, err := db.UserLogin(c.User.Name, c.Query("password"), c.User.LoginSource); err != nil {
  547. if db.IsErrUserNotExist(err) {
  548. c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  549. } else {
  550. c.Errorf(err, "authenticate user")
  551. }
  552. return
  553. }
  554. if err := db.DeleteUser(c.User); err != nil {
  555. switch {
  556. case db.IsErrUserOwnRepos(err):
  557. c.Flash.Error(c.Tr("form.still_own_repo"))
  558. c.Redirect(conf.Server.Subpath + "/user/settings/delete")
  559. case db.IsErrUserHasOrgs(err):
  560. c.Flash.Error(c.Tr("form.still_has_org"))
  561. c.Redirect(conf.Server.Subpath + "/user/settings/delete")
  562. default:
  563. c.Errorf(err, "delete user")
  564. }
  565. } else {
  566. log.Trace("Account deleted: %s", c.User.Name)
  567. c.Redirect(conf.Server.Subpath + "/")
  568. }
  569. return
  570. }
  571. c.Success(SETTINGS_DELETE)
  572. }