A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

176 linhas
4.1 KiB

  1. /*
  2. * Copyright © 2019 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. "html/template"
  14. "net/http"
  15. "strconv"
  16. "time"
  17. "github.com/gorilla/mux"
  18. "github.com/writeas/impart"
  19. "github.com/writeas/nerds/store"
  20. "github.com/writeas/web-core/log"
  21. "github.com/writeas/writefreely/page"
  22. )
  23. type Invite struct {
  24. ID string
  25. MaxUses sql.NullInt64
  26. Created time.Time
  27. Expires *time.Time
  28. Inactive bool
  29. uses int64
  30. }
  31. func (i Invite) Uses() int64 {
  32. return i.uses
  33. }
  34. func (i Invite) Expired() bool {
  35. return i.Expires != nil && i.Expires.Before(time.Now())
  36. }
  37. func (i Invite) ExpiresFriendly() string {
  38. return i.Expires.Format("January 2, 2006, 3:04 PM")
  39. }
  40. func handleViewUserInvites(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  41. // Don't show page if instance doesn't allow it
  42. if !(app.cfg.App.UserInvites != "" && (u.IsAdmin() || app.cfg.App.UserInvites != "admin")) {
  43. return impart.HTTPError{http.StatusNotFound, ""}
  44. }
  45. f, _ := getSessionFlashes(app, w, r, nil)
  46. p := struct {
  47. *UserPage
  48. Invites *[]Invite
  49. }{
  50. UserPage: NewUserPage(app, r, u, "Invite People", f),
  51. }
  52. var err error
  53. p.Invites, err = app.db.GetUserInvites(u.ID)
  54. if err != nil {
  55. return err
  56. }
  57. for i := range *p.Invites {
  58. (*p.Invites)[i].uses = app.db.GetUsersInvitedCount((*p.Invites)[i].ID)
  59. }
  60. showUserPage(w, "invite", p)
  61. return nil
  62. }
  63. func handleCreateUserInvite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  64. muVal := r.FormValue("uses")
  65. expVal := r.FormValue("expires")
  66. var err error
  67. var maxUses int
  68. if muVal != "0" {
  69. maxUses, err = strconv.Atoi(muVal)
  70. if err != nil {
  71. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'max_uses'"}
  72. }
  73. }
  74. var expDate *time.Time
  75. var expires int
  76. if expVal != "0" {
  77. expires, err = strconv.Atoi(expVal)
  78. if err != nil {
  79. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'expires'"}
  80. }
  81. ed := time.Now().Add(time.Duration(expires) * time.Minute)
  82. expDate = &ed
  83. }
  84. inviteID := store.GenerateRandomString("0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz", 6)
  85. err = app.db.CreateUserInvite(inviteID, u.ID, maxUses, expDate)
  86. if err != nil {
  87. return err
  88. }
  89. return impart.HTTPError{http.StatusFound, "/me/invites"}
  90. }
  91. func handleViewInvite(app *App, w http.ResponseWriter, r *http.Request) error {
  92. inviteCode := mux.Vars(r)["code"]
  93. i, err := app.db.GetUserInvite(inviteCode)
  94. if err != nil {
  95. return err
  96. }
  97. expired := i.Expired()
  98. if !expired && i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
  99. // Invite has a max-use number, so check if we're past that limit
  100. i.uses = app.db.GetUsersInvitedCount(inviteCode)
  101. expired = i.uses >= i.MaxUses.Int64
  102. }
  103. if u := getUserSession(app, r); u != nil {
  104. // check if invite belongs to another user
  105. // error can be ignored as not important in this case
  106. if ownInvite, _ := app.db.IsUsersInvite(inviteCode, u.ID); !ownInvite {
  107. addSessionFlash(app, w, r, "You're already registered and logged in.", nil)
  108. // show homepage
  109. return impart.HTTPError{http.StatusFound, "/me/settings"}
  110. }
  111. // show invite instructions
  112. p := struct {
  113. *UserPage
  114. Invite *Invite
  115. Expired bool
  116. }{
  117. UserPage: NewUserPage(app, r, u, "Invite to "+app.cfg.App.SiteName, nil),
  118. Invite: i,
  119. Expired: expired,
  120. }
  121. showUserPage(w, "invite-help", p)
  122. return nil
  123. }
  124. p := struct {
  125. page.StaticPage
  126. Error string
  127. Flashes []template.HTML
  128. Invite string
  129. }{
  130. StaticPage: pageForReq(app, r),
  131. Invite: inviteCode,
  132. }
  133. if expired {
  134. p.Error = "This invite link has expired."
  135. }
  136. // Get error messages
  137. session, err := app.sessionStore.Get(r, cookieName)
  138. if err != nil {
  139. // Ignore this
  140. log.Error("Unable to get session in handleViewInvite; ignoring: %v", err)
  141. }
  142. flashes, _ := getSessionFlashes(app, w, r, session)
  143. for _, flash := range flashes {
  144. p.Flashes = append(p.Flashes, template.HTML(flash))
  145. }
  146. // Show landing page
  147. return renderPage(w, "signup.tmpl", p)
  148. }