From 1695ce0168b51a8d9e7ae2efcce74da984fd2319 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 26 May 2022 16:47:57 +0200 Subject: [PATCH] Implement small improvements - fix golangci-lint warnings - start setup for user management - nicer formatting of user login information --- cmd/boardvoting/handlers.go | 13 ++++++++----- cmd/boardvoting/main.go | 4 ++-- cmd/boardvoting/middleware.go | 3 ++- cmd/boardvoting/routes.go | 2 +- internal/models/motions.go | 8 ++++---- ui/html/base.html | 10 ++++++---- ui/html/pages/create_motion.html | 2 +- ui/html/pages/motion.html | 2 +- ui/html/pages/motions.html | 2 +- ui/html/pages/users.html | 15 +++++++++++++++ 10 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 ui/html/pages/users.html diff --git a/cmd/boardvoting/handlers.go b/cmd/boardvoting/handlers.go index 374239e..5ba1e40 100644 --- a/cmd/boardvoting/handlers.go +++ b/cmd/boardvoting/handlers.go @@ -105,9 +105,10 @@ const ( type templateData struct { PrevPage string NextPage string - User *models.User Motion *models.MotionForDisplay Motions []*models.MotionForDisplay + User *models.User + Users []*models.User Request *http.Request Flash string Form any @@ -272,7 +273,7 @@ func (app *application) calculateMotionListOptions(r *http.Request) (*models.Mot } if voter != nil { - listOptions.VoterId = voter.ID + listOptions.VoterID = voter.ID } } @@ -472,8 +473,10 @@ func (app *application) proxyVoteSubmit(_ http.ResponseWriter, _ *http.Request) panic("not implemented") } -func (app *application) userList(_ http.ResponseWriter, _ *http.Request) { - panic("not implemented") +func (app *application) userList(w http.ResponseWriter, r *http.Request) { + data := app.newTemplateData(r, topLevelNavUsers, subLevelNavUsers) + + app.render(w, http.StatusOK, "users.html", data) } func (app *application) submitUserRoles(_ http.ResponseWriter, _ *http.Request) { @@ -499,6 +502,6 @@ func (app *application) deleteUserForm(_ http.ResponseWriter, _ *http.Request) { panic("not implemented") } -func (app *application) deletetUserSubmit(_ http.ResponseWriter, _ *http.Request) { +func (app *application) deleteUserSubmit(_ http.ResponseWriter, _ *http.Request) { panic("not implemented") } diff --git a/cmd/boardvoting/main.go b/cmd/boardvoting/main.go index 0728bd0..1cd4bc6 100644 --- a/cmd/boardvoting/main.go +++ b/cmd/boardvoting/main.go @@ -190,7 +190,7 @@ func setupHTTPRedirect(config *Config, errChan chan error) { redirect := &http.Server{ Addr: config.HTTPAddress, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - redirectUrl := url.URL{ + redirectURL := url.URL{ Scheme: "https://", Host: strings.Join( []string{ @@ -202,7 +202,7 @@ func setupHTTPRedirect(config *Config, errChan chan error) { Path: r.URL.Path, } - http.Redirect(w, r, redirectUrl.String(), http.StatusMovedPermanently) + http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently) }), IdleTimeout: config.Timeouts.Idle, ReadHeaderTimeout: config.Timeouts.ReadHeader, diff --git a/cmd/boardvoting/middleware.go b/cmd/boardvoting/middleware.go index df1de5e..030883e 100644 --- a/cmd/boardvoting/middleware.go +++ b/cmd/boardvoting/middleware.go @@ -64,9 +64,10 @@ func (app *application) authenticateRequest(r *http.Request) (*models.User, erro } emails := r.TLS.PeerCertificates[0].EmailAddresses + user, err := app.users.GetUser(r.Context(), emails) if err != nil { - return nil, err + return nil, fmt.Errorf("could not get user information from database: %w", err) } return user, nil diff --git a/cmd/boardvoting/routes.go b/cmd/boardvoting/routes.go index 013b8d5..aeed07a 100644 --- a/cmd/boardvoting/routes.go +++ b/cmd/boardvoting/routes.go @@ -92,7 +92,7 @@ func (app *application) routes() http.Handler { router.Handler(http.MethodGet, "/users/:id/add-mail", canManageUsers.ThenFunc(app.userAddEmailForm)) router.Handler(http.MethodPost, "/users/:id/add-mail", canManageUsers.ThenFunc(app.userAddEmailSubmit)) router.Handler(http.MethodGet, "/users/:id/delete", canManageUsers.ThenFunc(app.deleteUserForm)) - router.Handler(http.MethodPost, "/users/:id/delete", canManageUsers.ThenFunc(app.deletetUserSubmit)) + router.Handler(http.MethodPost, "/users/:id/delete", canManageUsers.ThenFunc(app.deleteUserSubmit)) standard := alice.New(app.logRequest, secureHeaders) diff --git a/internal/models/motions.go b/internal/models/motions.go index 04ce9e1..c807591 100644 --- a/internal/models/motions.go +++ b/internal/models/motions.go @@ -441,7 +441,7 @@ type MotionListOptions struct { Limit int UnvotedOnly bool Before, After *time.Time - VoterId int64 + VoterID int64 } func (m *MotionModel) TimestampRange(ctx context.Context, options *MotionListOptions) (*time.Time, *time.Time, error) { @@ -455,7 +455,7 @@ FROM decisions WHERE due >= ? AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)`, time.Now().UTC(), - options.VoterId, + options.VoterID, ) } else { row = m.DB.QueryRowxContext( @@ -684,7 +684,7 @@ WHERE NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ? AND due >= ? ORDER BY decisions.proposed DESC LIMIT ?`, - options.VoterId, + options.VoterID, time.Now().UTC(), options.Limit, ) @@ -709,6 +709,7 @@ LIMIT ?`, options.Limit, ) } + if err != nil { return nil, fmt.Errorf("could not query motions: %w", err) } @@ -809,5 +810,4 @@ func (m *MotionModel) GetByID(ctx context.Context, id int64) (*Motion, error) { } return &motion, nil - } diff --git a/ui/html/base.html b/ui/html/base.html index be12bbf..15e40f5 100644 --- a/ui/html/base.html +++ b/ui/html/base.html @@ -17,11 +17,13 @@

{{ template "title" . }} - {{ if .User }} - Authenticated as {{ .User.Name }} <{{ .User.Reminder }}> - - {{ end }}

+ {{ with .User }} +
+ + Authenticated as {{ .Name }} <{{ .Reminder }}> +
+ {{ end }}
diff --git a/ui/html/pages/create_motion.html b/ui/html/pages/create_motion.html index 845cd0a..9093e51 100644 --- a/ui/html/pages/create_motion.html +++ b/ui/html/pages/create_motion.html @@ -1,4 +1,4 @@ -{{ define "title" }}CAcert Board Decisions: New motion{{ end }} +{{ define "title" }}New motion{{ end }} {{ define "main" }}
diff --git a/ui/html/pages/motion.html b/ui/html/pages/motion.html index 5559a29..8724bb4 100644 --- a/ui/html/pages/motion.html +++ b/ui/html/pages/motion.html @@ -1,4 +1,4 @@ -{{ define "title" }}CAcert Board Decisions: {{ .Motion.Tag }}{{ end }} +{{ define "title" }}Motion {{ .Motion.Tag }}{{ end }} {{ define "main" }} {{ $voter := .User }} diff --git a/ui/html/pages/motions.html b/ui/html/pages/motions.html index f38f430..0e3c092 100644 --- a/ui/html/pages/motions.html +++ b/ui/html/pages/motions.html @@ -1,4 +1,4 @@ -{{ define "title" }}CAcert Board Decisions{{ end }} +{{ define "title" }}Board Decisions{{ end }} {{ define "main" }} {{ $page := . }} diff --git a/ui/html/pages/users.html b/ui/html/pages/users.html new file mode 100644 index 0000000..3d7dc46 --- /dev/null +++ b/ui/html/pages/users.html @@ -0,0 +1,15 @@ +{{ define "title"}}User Management{{ end }} + +{{ define "main"}} + {{ if .Users }} + {{ else }} +
+
+ +
+
No users found.
+
+
+
+ {{ end }} +{{ end }} \ No newline at end of file