A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

694 satır
20 KiB

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