setting.go 17 KB

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