Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
* fix accept and consumed view
* Apply suggestions from Alex' code review
* define admin.Databases options struct
* structify database parameters of web/handlers
  • Loading branch information
cryptix committed Mar 10, 2021
1 parent 672647c commit fd21dfc
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 60 deletions.
9 changes: 9 additions & 0 deletions admindb/sqlite/invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ func (i Invites) GetByToken(ctx context.Context, token string) (admindb.Invite,
qm.Load("CreatedByAuthFallback"),
).One(ctx, i.db)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return inv, admindb.ErrNotFound
}
return inv, err
}

Expand All @@ -174,6 +177,9 @@ func (i Invites) GetByID(ctx context.Context, id int64) (admindb.Invite, error)
qm.Load("CreatedByAuthFallback"),
).One(ctx, i.db)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return inv, admindb.ErrNotFound
}
return inv, err
}

Expand Down Expand Up @@ -225,6 +231,9 @@ func (i Invites) Revoke(ctx context.Context, id int64) error {
qm.Where("active = true AND id = ?", id),
).One(ctx, tx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return admindb.ErrNotFound
}
return err
}

Expand Down
14 changes: 8 additions & 6 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ func runroomsrv() error {
repo.New(repoDir),
httpsDomain,
roomsrv.StateManager,
db.AuthWithSSB,
db.AuthFallback,
db.AllowList,
db.Invites,
db.Notices,
db.PinnedNotices,
handlers.Databases{
AuthWithSSB: db.AuthWithSSB,
AuthFallback: db.AuthFallback,
AllowList: db.AllowList,
Invites: db.Invites,
Notices: db.Notices,
PinnedNotices: db.PinnedNotices,
},
)
if err != nil {
return fmt.Errorf("failed to create HTTPdashboard handler: %w", err)
Expand Down
10 changes: 6 additions & 4 deletions web/handlers/admin/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ func newSession(t *testing.T) *testSession {
ts.Domain,
r,
ts.RoomState,
ts.AllowListDB,
ts.InvitesDB,
ts.NoticeDB,
ts.PinnedDB,
Databases{
AllowList: ts.AllowListDB,
Invites: ts.InvitesDB,
Notices: ts.NoticeDB,
PinnedNotices: ts.PinnedDB,
},
)

handler = user.MiddlewareForTests(ts.User)(handler)
Expand Down
21 changes: 13 additions & 8 deletions web/handlers/admin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@ var HTMLTemplates = []string{
"admin/notice-edit.tmpl",
}

// Databases is an option struct that encapsualtes the required database services
type Databases struct {
AllowList admindb.AllowListService
Invites admindb.InviteService
Notices admindb.NoticesService
PinnedNotices admindb.PinnedNoticesService
}

// Handler supplies the elevated access pages to known users.
// It is not registering on the mux router like other pages to clean up the authorize flow.
func Handler(
domainName string,
r *render.Renderer,
roomState *roomstate.Manager,
al admindb.AllowListService,
is admindb.InviteService,
ndb admindb.NoticesService,
pdb admindb.PinnedNoticesService,
dbs Databases,
) http.Handler {
mux := &http.ServeMux{}
// TODO: configure 404 handler
Expand All @@ -59,7 +64,7 @@ func Handler(

var ah = allowListHandler{
r: r,
al: al,
al: dbs.AllowList,
}
mux.HandleFunc("/members", r.HTML("admin/allow-list.tmpl", ah.overview))
mux.HandleFunc("/members/add", ah.add)
Expand All @@ -68,7 +73,7 @@ func Handler(

var ih = invitesHandler{
r: r,
db: is,
db: dbs.Invites,

domainName: domainName,
}
Expand All @@ -79,8 +84,8 @@ func Handler(

var nh = noticeHandler{
r: r,
noticeDB: ndb,
pinnedDB: pdb,
noticeDB: dbs.Notices,
pinnedDB: dbs.PinnedNotices,
}
mux.HandleFunc("/notice/edit", r.HTML("admin/notice-edit.tmpl", nh.edit))
mux.HandleFunc("/notice/translation/draft", r.HTML("admin/notice-edit.tmpl", nh.draftTranslation))
Expand Down
51 changes: 30 additions & 21 deletions web/handlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ var HTMLTemplates = []string{
"error.tmpl",
}

// Databases is an options stuct for the required databases of the web handlers
type Databases struct {
AuthWithSSB admindb.AuthWithSSBService
AuthFallback admindb.AuthFallbackService
AllowList admindb.AllowListService
Invites admindb.InviteService
Notices admindb.NoticesService
PinnedNotices admindb.PinnedNoticesService
}

// New initializes the whole web stack for rooms, with all the sub-modules and routing.
func New(
logger logging.Interface,
repo repo.Interface,
domainName string,
roomState *roomstate.Manager,
as admindb.AuthWithSSBService,
fs admindb.AuthFallbackService,
al admindb.AllowListService,
is admindb.InviteService,
ns admindb.NoticesService,
ps admindb.PinnedNoticesService,
dbs Databases,

) (http.Handler, error) {
m := router.CompleteApp()

Expand Down Expand Up @@ -93,7 +99,7 @@ func New(
if !noticeName.Valid() {
return nil
}
notice, err := ps.Get(r.Context(), noticeName, "en-GB")
notice, err := dbs.PinnedNotices.Get(r.Context(), noticeName, "en-GB")
if err != nil {
return nil
}
Expand Down Expand Up @@ -173,7 +179,7 @@ func New(
}, nil
})

a, err := auth.NewHandler(fs,
a, err := auth.NewHandler(dbs.AuthFallback,
auth.SetStore(store),
auth.SetErrorHandler(authErrH),
auth.SetNotAuthorizedHandler(notAuthorizedH),
Expand Down Expand Up @@ -204,18 +210,21 @@ func New(
// hookup handlers to the router
roomsAuth.Handler(m, r, a)

adminHandler := a.Authenticate(admin.Handler(
adminHandler := admin.Handler(
domainName,
r,
roomState,
al,
is,
ns,
ps))
mainMux.Handle("/admin/", adminHandler)
admin.Databases{
AllowList: dbs.AllowList,
Invites: dbs.Invites,
Notices: dbs.Notices,
PinnedNotices: dbs.PinnedNotices,
},
)
mainMux.Handle("/admin/", a.Authenticate(adminHandler))

m.Get(router.CompleteIndex).Handler(r.HTML("landing/index.tmpl", func(w http.ResponseWriter, req *http.Request) (interface{}, error) {
notice, err := ps.Get(req.Context(), admindb.NoticeDescription, "en-GB")
notice, err := dbs.PinnedNotices.Get(req.Context(), admindb.NoticeDescription, "en-GB")
if err != nil {
return nil, fmt.Errorf("failed to find description: %w", err)
}
Expand All @@ -230,14 +239,14 @@ func New(
m.Get(router.CompleteAbout).Handler(r.StaticHTML("landing/about.tmpl"))

var nh = noticeHandler{
notices: ns,
pinned: ps,
notices: dbs.Notices,
pinned: dbs.PinnedNotices,
}
m.Get(router.CompleteNoticeList).Handler(r.HTML("notice/list.tmpl", nh.list))
m.Get(router.CompleteNoticeShow).Handler(r.HTML("notice/show.tmpl", nh.show))

var ih = inviteHandler{
invites: is,
invites: dbs.Invites,
}
m.Get(router.CompleteInviteAccept).Handler(r.HTML("invite/accept.tmpl", ih.acceptForm))
m.Get(router.CompleteInviteConsume).Handler(r.HTML("invite/consumed.tmpl", ih.consume))
Expand All @@ -255,7 +264,7 @@ func New(
// apply HTTP middleware
middlewares := []func(http.Handler) http.Handler{
logging.InjectHandler(logger),
user.ContextInjecter(fs, a),
user.ContextInjecter(dbs.AuthFallback, a),
CSRF,
}

Expand All @@ -264,8 +273,8 @@ func New(
}

var finalHandler http.Handler = mainMux
for _, mw := range middlewares {
finalHandler = mw(finalHandler)
for _, applyMiddleware := range middlewares {
finalHandler = applyMiddleware(finalHandler)
}

return finalHandler, nil
Expand Down
21 changes: 19 additions & 2 deletions web/handlers/invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net/http"

"go.mindeco.de/http/render"
"go.mindeco.de/logging"

"github.com/go-kit/kit/log/level"
"github.com/gorilla/csrf"
"github.com/ssb-ngi-pointer/go-ssb-room/admindb"
weberrors "github.com/ssb-ngi-pointer/go-ssb-room/web/errors"
Expand All @@ -20,7 +22,9 @@ type inviteHandler struct {
}

func (h inviteHandler) acceptForm(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
inv, err := h.invites.GetByToken(req.Context(), req.URL.Query().Get("token"))
token := req.URL.Query().Get("token")

inv, err := h.invites.GetByToken(req.Context(), token)
if err != nil {
if errors.Is(err, admindb.ErrNotFound) {
return nil, weberrors.ErrNotFound{What: "invite"}
Expand All @@ -29,7 +33,9 @@ func (h inviteHandler) acceptForm(rw http.ResponseWriter, req *http.Request) (in
}

return map[string]interface{}{
"Invite": inv,
"Token": token,
"Invite": inv,

csrf.TemplateTag: csrf.TemplateField(req),
}, nil
}
Expand All @@ -39,6 +45,8 @@ func (h inviteHandler) consume(rw http.ResponseWriter, req *http.Request) (inter
return nil, weberrors.ErrBadRequest{Where: "form data", Details: err}
}

alias := req.FormValue("alias")

token := req.FormValue("token")

newMember, err := refs.ParseFeedRef(req.FormValue("new_member"))
Expand All @@ -53,6 +61,15 @@ func (h inviteHandler) consume(rw http.ResponseWriter, req *http.Request) (inter
}
return nil, err
}
log := logging.FromContext(req.Context())
level.Info(log).Log("event", "invite consumed", "id", inv.ID, "ref", newMember.ShortRef())

if alias != "" {
level.Warn(log).Log(
"TODO", "invite registration",
"alias", alias,
)
}

return map[string]interface{}{
"TunnelAddress": "pew pew",
Expand Down
14 changes: 8 additions & 6 deletions web/handlers/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ func setup(t *testing.T) *testSession {
testRepo,
"localhost",
ts.RoomState,
ts.AuthDB,
ts.AuthFallbackDB,
ts.AllowListDB,
ts.InvitesDB,
ts.NoticeDB,
ts.PinnedDB,
Databases{
AuthWithSSB: ts.AuthDB,
AuthFallback: ts.AuthFallbackDB,
AllowList: ts.AllowListDB,
Invites: ts.InvitesDB,
Notices: ts.NoticeDB,
PinnedNotices: ts.PinnedDB,
},
)
if err != nil {
t.Fatal("setup: handler init failed:", err)
Expand Down
7 changes: 6 additions & 1 deletion web/i18n/defaults/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ GenericConfirm = "Yes"
GenericGoBack = "Back"
GenericSave = "Save"
GenericPreview = "Preview"
GenericLanguage = "Language"

PageNotFound = "The requested page was not found."

Expand Down Expand Up @@ -45,9 +46,13 @@ NavAdminInvites = "Invites"
NavAdminNotices = "Notices"

InviteAccept = "Accept invite"
InviteAcceptTitle = "Accept Invite
InviteAcceptTitle = "Accept Invite"
InviteAcceptWelcome = "elaborate welcome message for a new member with good words and stuff."
InviteAcceptAliasSuggestion = "The persone who created thought you might like this alias:"
InviteAcceptPublicKey = "Public Key"

InviteConsumedTitle = "Invite accepted!"
InviteConsumedWelcome = "Even more elaborate message that the person is now a member of the room!"

NoticeEditTitle = "Edit Notice"
NoticeList = "Notices"
Expand Down
2 changes: 1 addition & 1 deletion web/templates/admin/notice-edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
>{{.Notice.Content}}</textarea>

<div class="my-4 flex flex-row items-center justify-start">
<label class="mr-2">Language</label>
<label class="mr-2">{{i18n "GenericLanguage"}}</label>
<input
type="text"
name="language"
Expand Down
Loading

0 comments on commit fd21dfc

Please sign in to comment.