A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
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.
 
 
 
 
 

676 lines
19 KiB

  1. /*
  2. * Copyright © 2018-2021 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "database/sql"
  13. "fmt"
  14. "net/http"
  15. "runtime"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/gorilla/mux"
  20. "github.com/writeas/impart"
  21. "github.com/writeas/web-core/auth"
  22. "github.com/writeas/web-core/log"
  23. "github.com/writeas/web-core/passgen"
  24. "github.com/writefreely/writefreely/appstats"
  25. "github.com/writefreely/writefreely/config"
  26. )
  27. var (
  28. appStartTime = time.Now()
  29. sysStatus systemStatus
  30. )
  31. const adminUsersPerPage = 30
  32. type systemStatus struct {
  33. Uptime string
  34. NumGoroutine int
  35. // General statistics.
  36. MemAllocated string // bytes allocated and still in use
  37. MemTotal string // bytes allocated (even if freed)
  38. MemSys string // bytes obtained from system (sum of XxxSys below)
  39. Lookups uint64 // number of pointer lookups
  40. MemMallocs uint64 // number of mallocs
  41. MemFrees uint64 // number of frees
  42. // Main allocation heap statistics.
  43. HeapAlloc string // bytes allocated and still in use
  44. HeapSys string // bytes obtained from system
  45. HeapIdle string // bytes in idle spans
  46. HeapInuse string // bytes in non-idle span
  47. HeapReleased string // bytes released to the OS
  48. HeapObjects uint64 // total number of allocated objects
  49. // Low-level fixed-size structure allocator statistics.
  50. // Inuse is bytes used now.
  51. // Sys is bytes obtained from system.
  52. StackInuse string // bootstrap stacks
  53. StackSys string
  54. MSpanInuse string // mspan structures
  55. MSpanSys string
  56. MCacheInuse string // mcache structures
  57. MCacheSys string
  58. BuckHashSys string // profiling bucket hash table
  59. GCSys string // GC metadata
  60. OtherSys string // other system allocations
  61. // Garbage collector statistics.
  62. NextGC string // next run in HeapAlloc time (bytes)
  63. LastGC string // last run in absolute time (ns)
  64. PauseTotalNs string
  65. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  66. NumGC uint32
  67. }
  68. type inspectedCollection struct {
  69. CollectionObj
  70. Followers int
  71. LastPost string
  72. }
  73. type instanceContent struct {
  74. ID string
  75. Type string
  76. Title sql.NullString
  77. Content string
  78. Updated time.Time
  79. }
  80. type AdminPage struct {
  81. UpdateAvailable bool
  82. }
  83. func NewAdminPage(app *App) *AdminPage {
  84. ap := &AdminPage{}
  85. if app.updates != nil {
  86. ap.UpdateAvailable = app.updates.AreAvailableNoCheck()
  87. }
  88. return ap
  89. }
  90. func (c instanceContent) UpdatedFriendly() string {
  91. /*
  92. // TODO: accept a locale in this method and use that for the format
  93. var loc monday.Locale = monday.LocaleEnUS
  94. return monday.Format(u.Created, monday.DateTimeFormatsByLocale[loc], loc)
  95. */
  96. return c.Updated.Format("January 2, 2006, 3:04 PM")
  97. }
  98. func handleViewAdminDash(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  99. p := struct {
  100. *UserPage
  101. *AdminPage
  102. Message string
  103. UsersCount, CollectionsCount, PostsCount int64
  104. }{
  105. UserPage: NewUserPage(app, r, u, "Admin", nil),
  106. AdminPage: NewAdminPage(app),
  107. Message: r.FormValue("m"),
  108. }
  109. // Get user stats
  110. p.UsersCount = app.db.GetAllUsersCount()
  111. var err error
  112. p.CollectionsCount, err = app.db.GetTotalCollections()
  113. if err != nil {
  114. return err
  115. }
  116. p.PostsCount, err = app.db.GetTotalPosts()
  117. if err != nil {
  118. return err
  119. }
  120. showUserPage(w, "admin", p)
  121. return nil
  122. }
  123. func handleViewAdminMonitor(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  124. updateAppStats()
  125. p := struct {
  126. *UserPage
  127. *AdminPage
  128. SysStatus systemStatus
  129. Config config.AppCfg
  130. Message, ConfigMessage string
  131. }{
  132. UserPage: NewUserPage(app, r, u, "Admin", nil),
  133. AdminPage: NewAdminPage(app),
  134. SysStatus: sysStatus,
  135. Config: app.cfg.App,
  136. Message: r.FormValue("m"),
  137. ConfigMessage: r.FormValue("cm"),
  138. }
  139. showUserPage(w, "monitor", p)
  140. return nil
  141. }
  142. func handleViewAdminSettings(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  143. p := struct {
  144. *UserPage
  145. *AdminPage
  146. Config config.AppCfg
  147. Message, ConfigMessage string
  148. }{
  149. UserPage: NewUserPage(app, r, u, "Admin", nil),
  150. AdminPage: NewAdminPage(app),
  151. Config: app.cfg.App,
  152. Message: r.FormValue("m"),
  153. ConfigMessage: r.FormValue("cm"),
  154. }
  155. showUserPage(w, "app-settings", p)
  156. return nil
  157. }
  158. func handleViewAdminUsers(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  159. p := struct {
  160. *UserPage
  161. *AdminPage
  162. Config config.AppCfg
  163. Message string
  164. Flashes []string
  165. Users *[]User
  166. CurPage int
  167. TotalUsers int64
  168. TotalPages []int
  169. }{
  170. UserPage: NewUserPage(app, r, u, "Users", nil),
  171. AdminPage: NewAdminPage(app),
  172. Config: app.cfg.App,
  173. Message: r.FormValue("m"),
  174. }
  175. p.Flashes, _ = getSessionFlashes(app, w, r, nil)
  176. p.TotalUsers = app.db.GetAllUsersCount()
  177. ttlPages := p.TotalUsers / adminUsersPerPage
  178. p.TotalPages = []int{}
  179. for i := 1; i <= int(ttlPages); i++ {
  180. p.TotalPages = append(p.TotalPages, i)
  181. }
  182. var err error
  183. p.CurPage, err = strconv.Atoi(r.FormValue("p"))
  184. if err != nil || p.CurPage < 1 {
  185. p.CurPage = 1
  186. } else if p.CurPage > int(ttlPages) {
  187. p.CurPage = int(ttlPages)
  188. }
  189. p.Users, err = app.db.GetAllUsers(uint(p.CurPage))
  190. if err != nil {
  191. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get users: %v", err)}
  192. }
  193. showUserPage(w, "users", p)
  194. return nil
  195. }
  196. func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  197. vars := mux.Vars(r)
  198. username := vars["username"]
  199. if username == "" {
  200. return impart.HTTPError{http.StatusFound, "/admin/users"}
  201. }
  202. p := struct {
  203. *UserPage
  204. *AdminPage
  205. Config config.AppCfg
  206. Message string
  207. User *User
  208. Colls []inspectedCollection
  209. LastPost string
  210. NewPassword string
  211. TotalPosts int64
  212. ClearEmail string
  213. }{
  214. AdminPage: NewAdminPage(app),
  215. Config: app.cfg.App,
  216. Message: r.FormValue("m"),
  217. Colls: []inspectedCollection{},
  218. }
  219. var err error
  220. p.User, err = app.db.GetUserForAuth(username)
  221. if err != nil {
  222. if err == ErrUserNotFound {
  223. return err
  224. }
  225. log.Error("Could not get user: %v", err)
  226. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  227. }
  228. flashes, _ := getSessionFlashes(app, w, r, nil)
  229. for _, flash := range flashes {
  230. if strings.HasPrefix(flash, "SUCCESS: ") {
  231. p.NewPassword = strings.TrimPrefix(flash, "SUCCESS: ")
  232. p.ClearEmail = p.User.EmailClear(app.keys)
  233. }
  234. }
  235. p.UserPage = NewUserPage(app, r, u, p.User.Username, nil)
  236. p.TotalPosts = app.db.GetUserPostsCount(p.User.ID)
  237. lp, err := app.db.GetUserLastPostTime(p.User.ID)
  238. if err != nil {
  239. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's last post time: %v", err)}
  240. }
  241. if lp != nil {
  242. p.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  243. }
  244. colls, err := app.db.GetCollections(p.User, app.cfg.App.Host)
  245. if err != nil {
  246. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's collections: %v", err)}
  247. }
  248. for _, c := range *colls {
  249. ic := inspectedCollection{
  250. CollectionObj: CollectionObj{Collection: c},
  251. }
  252. if app.cfg.App.Federation {
  253. folls, err := app.db.GetAPFollowers(&c)
  254. if err == nil {
  255. // TODO: handle error here (at least log it)
  256. ic.Followers = len(*folls)
  257. }
  258. }
  259. app.db.GetPostsCount(&ic.CollectionObj, true)
  260. lp, err := app.db.GetCollectionLastPostTime(c.ID)
  261. if err != nil {
  262. log.Error("Didn't get last post time for collection %d: %v", c.ID, err)
  263. }
  264. if lp != nil {
  265. ic.LastPost = lp.Format("January 2, 2006, 3:04 PM")
  266. }
  267. p.Colls = append(p.Colls, ic)
  268. }
  269. showUserPage(w, "view-user", p)
  270. return nil
  271. }
  272. func handleAdminDeleteUser(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  273. if !u.IsAdmin() {
  274. return impart.HTTPError{http.StatusForbidden, "Administrator privileges required for this action"}
  275. }
  276. vars := mux.Vars(r)
  277. username := vars["username"]
  278. confirmUsername := r.PostFormValue("confirm-username")
  279. if confirmUsername != username {
  280. return impart.HTTPError{http.StatusBadRequest, "Username was not confirmed"}
  281. }
  282. user, err := app.db.GetUserForAuth(username)
  283. if err == ErrUserNotFound {
  284. return impart.HTTPError{http.StatusNotFound, fmt.Sprintf("User '%s' was not found", username)}
  285. } else if err != nil {
  286. log.Error("get user for deletion: %v", err)
  287. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user with username '%s': %v", username, err)}
  288. }
  289. err = app.db.DeleteAccount(user.ID)
  290. if err != nil {
  291. log.Error("delete user %s: %v", user.Username, err)
  292. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not delete user account for '%s': %v", username, err)}
  293. }
  294. _ = addSessionFlash(app, w, r, fmt.Sprintf("User \"%s\" was deleted successfully.", username), nil)
  295. return impart.HTTPError{http.StatusFound, "/admin/users"}
  296. }
  297. func handleAdminToggleUserStatus(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  298. vars := mux.Vars(r)
  299. username := vars["username"]
  300. if username == "" {
  301. return impart.HTTPError{http.StatusFound, "/admin/users"}
  302. }
  303. user, err := app.db.GetUserForAuth(username)
  304. if err != nil {
  305. log.Error("failed to get user: %v", err)
  306. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user from username: %v", err)}
  307. }
  308. if user.IsSilenced() {
  309. err = app.db.SetUserStatus(user.ID, UserActive)
  310. } else {
  311. err = app.db.SetUserStatus(user.ID, UserSilenced)
  312. // reset the cache to removed silence user posts
  313. updateTimelineCache(app.timeline, true)
  314. }
  315. if err != nil {
  316. log.Error("toggle user silenced: %v", err)
  317. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not toggle user status: %v", err)}
  318. }
  319. return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s#status", username)}
  320. }
  321. func handleAdminResetUserPass(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  322. vars := mux.Vars(r)
  323. username := vars["username"]
  324. if username == "" {
  325. return impart.HTTPError{http.StatusFound, "/admin/users"}
  326. }
  327. // Generate new random password since none supplied
  328. pass := passgen.NewWordish()
  329. hashedPass, err := auth.HashPass([]byte(pass))
  330. if err != nil {
  331. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  332. }
  333. userIDVal := r.FormValue("user")
  334. log.Info("ADMIN: Changing user %s password", userIDVal)
  335. id, err := strconv.Atoi(userIDVal)
  336. if err != nil {
  337. return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Invalid user ID: %v", err)}
  338. }
  339. err = app.db.ChangePassphrase(int64(id), true, "", hashedPass)
  340. if err != nil {
  341. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  342. }
  343. log.Info("ADMIN: Successfully changed.")
  344. addSessionFlash(app, w, r, fmt.Sprintf("SUCCESS: %s", pass), nil)
  345. return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s", username)}
  346. }
  347. func handleViewAdminPages(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  348. p := struct {
  349. *UserPage
  350. *AdminPage
  351. Config config.AppCfg
  352. Message string
  353. Pages []*instanceContent
  354. }{
  355. UserPage: NewUserPage(app, r, u, "Pages", nil),
  356. AdminPage: NewAdminPage(app),
  357. Config: app.cfg.App,
  358. Message: r.FormValue("m"),
  359. }
  360. var err error
  361. p.Pages, err = app.db.GetInstancePages()
  362. if err != nil {
  363. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get pages: %v", err)}
  364. }
  365. // Add in default pages
  366. var hasAbout, hasPrivacy bool
  367. for i, c := range p.Pages {
  368. if hasAbout && hasPrivacy {
  369. break
  370. }
  371. if c.ID == "about" {
  372. hasAbout = true
  373. if !c.Title.Valid {
  374. p.Pages[i].Title = defaultAboutTitle(app.cfg)
  375. }
  376. } else if c.ID == "privacy" {
  377. hasPrivacy = true
  378. if !c.Title.Valid {
  379. p.Pages[i].Title = defaultPrivacyTitle()
  380. }
  381. }
  382. }
  383. if !hasAbout {
  384. p.Pages = append(p.Pages, &instanceContent{
  385. ID: "about",
  386. Title: defaultAboutTitle(app.cfg),
  387. Content: defaultAboutPage(app.cfg),
  388. Updated: defaultPageUpdatedTime,
  389. })
  390. }
  391. if !hasPrivacy {
  392. p.Pages = append(p.Pages, &instanceContent{
  393. ID: "privacy",
  394. Title: defaultPrivacyTitle(),
  395. Content: defaultPrivacyPolicy(app.cfg),
  396. Updated: defaultPageUpdatedTime,
  397. })
  398. }
  399. showUserPage(w, "pages", p)
  400. return nil
  401. }
  402. func handleViewAdminPage(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  403. vars := mux.Vars(r)
  404. slug := vars["slug"]
  405. if slug == "" {
  406. return impart.HTTPError{http.StatusFound, "/admin/pages"}
  407. }
  408. p := struct {
  409. *UserPage
  410. *AdminPage
  411. Config config.AppCfg
  412. Message string
  413. Banner *instanceContent
  414. Content *instanceContent
  415. }{
  416. AdminPage: NewAdminPage(app),
  417. Config: app.cfg.App,
  418. Message: r.FormValue("m"),
  419. }
  420. var err error
  421. // Get pre-defined pages, or select slug
  422. if slug == "about" {
  423. p.Content, err = getAboutPage(app)
  424. } else if slug == "privacy" {
  425. p.Content, err = getPrivacyPage(app)
  426. } else if slug == "landing" {
  427. p.Banner, err = getLandingBanner(app)
  428. if err != nil {
  429. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get banner: %v", err)}
  430. }
  431. p.Content, err = getLandingBody(app)
  432. p.Content.ID = "landing"
  433. } else if slug == "reader" {
  434. p.Content, err = getReaderSection(app)
  435. } else {
  436. p.Content, err = app.db.GetDynamicContent(slug)
  437. }
  438. if err != nil {
  439. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get page: %v", err)}
  440. }
  441. title := "New page"
  442. if p.Content != nil {
  443. title = "Edit " + p.Content.ID
  444. } else {
  445. p.Content = &instanceContent{}
  446. }
  447. p.UserPage = NewUserPage(app, r, u, title, nil)
  448. showUserPage(w, "view-page", p)
  449. return nil
  450. }
  451. func handleAdminUpdateSite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  452. vars := mux.Vars(r)
  453. id := vars["page"]
  454. // Validate
  455. if id != "about" && id != "privacy" && id != "landing" && id != "reader" {
  456. return impart.HTTPError{http.StatusNotFound, "No such page."}
  457. }
  458. var err error
  459. m := ""
  460. if id == "landing" {
  461. // Handle special landing page
  462. err = app.db.UpdateDynamicContent("landing-banner", "", r.FormValue("banner"), "section")
  463. if err != nil {
  464. m = "?m=" + err.Error()
  465. return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
  466. }
  467. err = app.db.UpdateDynamicContent("landing-body", "", r.FormValue("content"), "section")
  468. } else if id == "reader" {
  469. // Update sections with titles
  470. err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "section")
  471. } else {
  472. // Update page
  473. err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "page")
  474. }
  475. if err != nil {
  476. m = "?m=" + err.Error()
  477. }
  478. return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
  479. }
  480. func handleAdminUpdateConfig(apper Apper, u *User, w http.ResponseWriter, r *http.Request) error {
  481. apper.App().cfg.App.SiteName = r.FormValue("site_name")
  482. apper.App().cfg.App.SiteDesc = r.FormValue("site_desc")
  483. apper.App().cfg.App.Landing = r.FormValue("landing")
  484. apper.App().cfg.App.OpenRegistration = r.FormValue("open_registration") == "on"
  485. apper.App().cfg.App.OpenDeletion = r.FormValue("open_deletion") == "on"
  486. mul, err := strconv.Atoi(r.FormValue("min_username_len"))
  487. if err == nil {
  488. apper.App().cfg.App.MinUsernameLen = mul
  489. }
  490. mb, err := strconv.Atoi(r.FormValue("max_blogs"))
  491. if err == nil {
  492. apper.App().cfg.App.MaxBlogs = mb
  493. }
  494. apper.App().cfg.App.Federation = r.FormValue("federation") == "on"
  495. apper.App().cfg.App.PublicStats = r.FormValue("public_stats") == "on"
  496. apper.App().cfg.App.Monetization = r.FormValue("monetization") == "on"
  497. apper.App().cfg.App.Private = r.FormValue("private") == "on"
  498. apper.App().cfg.App.LocalTimeline = r.FormValue("local_timeline") == "on"
  499. if apper.App().cfg.App.LocalTimeline && apper.App().timeline == nil {
  500. log.Info("Initializing local timeline...")
  501. initLocalTimeline(apper.App())
  502. }
  503. apper.App().cfg.App.UserInvites = r.FormValue("user_invites")
  504. if apper.App().cfg.App.UserInvites == "none" {
  505. apper.App().cfg.App.UserInvites = ""
  506. }
  507. apper.App().cfg.App.DefaultVisibility = r.FormValue("default_visibility")
  508. m := "?cm=Configuration+saved."
  509. err = apper.SaveConfig(apper.App().cfg)
  510. if err != nil {
  511. m = "?cm=" + err.Error()
  512. }
  513. return impart.HTTPError{http.StatusFound, "/admin/settings" + m + "#config"}
  514. }
  515. func updateAppStats() {
  516. sysStatus.Uptime = appstats.TimeSincePro(appStartTime)
  517. m := new(runtime.MemStats)
  518. runtime.ReadMemStats(m)
  519. sysStatus.NumGoroutine = runtime.NumGoroutine()
  520. sysStatus.MemAllocated = appstats.FileSize(int64(m.Alloc))
  521. sysStatus.MemTotal = appstats.FileSize(int64(m.TotalAlloc))
  522. sysStatus.MemSys = appstats.FileSize(int64(m.Sys))
  523. sysStatus.Lookups = m.Lookups
  524. sysStatus.MemMallocs = m.Mallocs
  525. sysStatus.MemFrees = m.Frees
  526. sysStatus.HeapAlloc = appstats.FileSize(int64(m.HeapAlloc))
  527. sysStatus.HeapSys = appstats.FileSize(int64(m.HeapSys))
  528. sysStatus.HeapIdle = appstats.FileSize(int64(m.HeapIdle))
  529. sysStatus.HeapInuse = appstats.FileSize(int64(m.HeapInuse))
  530. sysStatus.HeapReleased = appstats.FileSize(int64(m.HeapReleased))
  531. sysStatus.HeapObjects = m.HeapObjects
  532. sysStatus.StackInuse = appstats.FileSize(int64(m.StackInuse))
  533. sysStatus.StackSys = appstats.FileSize(int64(m.StackSys))
  534. sysStatus.MSpanInuse = appstats.FileSize(int64(m.MSpanInuse))
  535. sysStatus.MSpanSys = appstats.FileSize(int64(m.MSpanSys))
  536. sysStatus.MCacheInuse = appstats.FileSize(int64(m.MCacheInuse))
  537. sysStatus.MCacheSys = appstats.FileSize(int64(m.MCacheSys))
  538. sysStatus.BuckHashSys = appstats.FileSize(int64(m.BuckHashSys))
  539. sysStatus.GCSys = appstats.FileSize(int64(m.GCSys))
  540. sysStatus.OtherSys = appstats.FileSize(int64(m.OtherSys))
  541. sysStatus.NextGC = appstats.FileSize(int64(m.NextGC))
  542. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  543. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  544. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  545. sysStatus.NumGC = m.NumGC
  546. }
  547. func adminResetPassword(app *App, u *User, newPass string) error {
  548. hashedPass, err := auth.HashPass([]byte(newPass))
  549. if err != nil {
  550. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
  551. }
  552. err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
  553. if err != nil {
  554. return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
  555. }
  556. return nil
  557. }
  558. func handleViewAdminUpdates(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  559. check := r.URL.Query().Get("check")
  560. if check == "now" && app.cfg.App.UpdateChecks {
  561. app.updates.CheckNow()
  562. }
  563. p := struct {
  564. *UserPage
  565. *AdminPage
  566. CurReleaseNotesURL string
  567. LastChecked string
  568. LastChecked8601 string
  569. LatestVersion string
  570. LatestReleaseURL string
  571. LatestReleaseNotesURL string
  572. CheckFailed bool
  573. }{
  574. UserPage: NewUserPage(app, r, u, "Updates", nil),
  575. AdminPage: NewAdminPage(app),
  576. }
  577. p.CurReleaseNotesURL = wfReleaseNotesURL(p.Version)
  578. if app.cfg.App.UpdateChecks {
  579. p.LastChecked = app.updates.lastCheck.Format("January 2, 2006, 3:04 PM")
  580. p.LastChecked8601 = app.updates.lastCheck.Format("2006-01-02T15:04:05Z")
  581. p.LatestVersion = app.updates.LatestVersion()
  582. p.LatestReleaseURL = app.updates.ReleaseURL()
  583. p.LatestReleaseNotesURL = app.updates.ReleaseNotesURL()
  584. p.UpdateAvailable = app.updates.AreAvailable()
  585. p.CheckFailed = app.updates.checkError != nil
  586. }
  587. showUserPage(w, "app-updates", p)
  588. return nil
  589. }