Implement small improvements

- fix golangci-lint warnings
- start setup for user management
- nicer formatting of user login information
main
Jan Dittberner 2 years ago
parent 47092bfa9b
commit 1695ce0168

@ -105,9 +105,10 @@ const (
type templateData struct { type templateData struct {
PrevPage string PrevPage string
NextPage string NextPage string
User *models.User
Motion *models.MotionForDisplay Motion *models.MotionForDisplay
Motions []*models.MotionForDisplay Motions []*models.MotionForDisplay
User *models.User
Users []*models.User
Request *http.Request Request *http.Request
Flash string Flash string
Form any Form any
@ -272,7 +273,7 @@ func (app *application) calculateMotionListOptions(r *http.Request) (*models.Mot
} }
if voter != nil { 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") panic("not implemented")
} }
func (app *application) userList(_ http.ResponseWriter, _ *http.Request) { func (app *application) userList(w http.ResponseWriter, r *http.Request) {
panic("not implemented") data := app.newTemplateData(r, topLevelNavUsers, subLevelNavUsers)
app.render(w, http.StatusOK, "users.html", data)
} }
func (app *application) submitUserRoles(_ http.ResponseWriter, _ *http.Request) { func (app *application) submitUserRoles(_ http.ResponseWriter, _ *http.Request) {
@ -499,6 +502,6 @@ func (app *application) deleteUserForm(_ http.ResponseWriter, _ *http.Request) {
panic("not implemented") panic("not implemented")
} }
func (app *application) deletetUserSubmit(_ http.ResponseWriter, _ *http.Request) { func (app *application) deleteUserSubmit(_ http.ResponseWriter, _ *http.Request) {
panic("not implemented") panic("not implemented")
} }

@ -190,7 +190,7 @@ func setupHTTPRedirect(config *Config, errChan chan error) {
redirect := &http.Server{ redirect := &http.Server{
Addr: config.HTTPAddress, Addr: config.HTTPAddress,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
redirectUrl := url.URL{ redirectURL := url.URL{
Scheme: "https://", Scheme: "https://",
Host: strings.Join( Host: strings.Join(
[]string{ []string{
@ -202,7 +202,7 @@ func setupHTTPRedirect(config *Config, errChan chan error) {
Path: r.URL.Path, Path: r.URL.Path,
} }
http.Redirect(w, r, redirectUrl.String(), http.StatusMovedPermanently) http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
}), }),
IdleTimeout: config.Timeouts.Idle, IdleTimeout: config.Timeouts.Idle,
ReadHeaderTimeout: config.Timeouts.ReadHeader, ReadHeaderTimeout: config.Timeouts.ReadHeader,

@ -64,9 +64,10 @@ func (app *application) authenticateRequest(r *http.Request) (*models.User, erro
} }
emails := r.TLS.PeerCertificates[0].EmailAddresses emails := r.TLS.PeerCertificates[0].EmailAddresses
user, err := app.users.GetUser(r.Context(), emails) user, err := app.users.GetUser(r.Context(), emails)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("could not get user information from database: %w", err)
} }
return user, nil return user, nil

@ -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.MethodGet, "/users/:id/add-mail", canManageUsers.ThenFunc(app.userAddEmailForm))
router.Handler(http.MethodPost, "/users/:id/add-mail", canManageUsers.ThenFunc(app.userAddEmailSubmit)) 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.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) standard := alice.New(app.logRequest, secureHeaders)

@ -441,7 +441,7 @@ type MotionListOptions struct {
Limit int Limit int
UnvotedOnly bool UnvotedOnly bool
Before, After *time.Time Before, After *time.Time
VoterId int64 VoterID int64
} }
func (m *MotionModel) TimestampRange(ctx context.Context, options *MotionListOptions) (*time.Time, *time.Time, error) { func (m *MotionModel) TimestampRange(ctx context.Context, options *MotionListOptions) (*time.Time, *time.Time, error) {
@ -455,7 +455,7 @@ FROM decisions
WHERE due >= ? WHERE due >= ?
AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)`, AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)`,
time.Now().UTC(), time.Now().UTC(),
options.VoterId, options.VoterID,
) )
} else { } else {
row = m.DB.QueryRowxContext( row = m.DB.QueryRowxContext(
@ -684,7 +684,7 @@ WHERE NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?
AND due >= ? AND due >= ?
ORDER BY decisions.proposed DESC ORDER BY decisions.proposed DESC
LIMIT ?`, LIMIT ?`,
options.VoterId, options.VoterID,
time.Now().UTC(), time.Now().UTC(),
options.Limit, options.Limit,
) )
@ -709,6 +709,7 @@ LIMIT ?`,
options.Limit, options.Limit,
) )
} }
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query motions: %w", err) 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 return &motion, nil
} }

@ -17,11 +17,13 @@
<div class="ui text container"> <div class="ui text container">
<h1 class="ui header"> <h1 class="ui header">
{{ template "title" . }} {{ template "title" . }}
{{ if .User }}
<span class="ui left pointing label">Authenticated as {{ .User.Name }} &lt;{{ .User.Reminder }}&gt;
</span>
{{ end }}
</h1> </h1>
{{ with .User }}
<div class="ui label">
<i class="user icon"></i>
Authenticated as {{ .Name }} &lt;{{ .Reminder }}&gt;
</div>
{{ end }}
</div> </div>
</div> </div>
</header> </header>

@ -1,4 +1,4 @@
{{ define "title" }}CAcert Board Decisions: New motion{{ end }} {{ define "title" }}New motion{{ end }}
{{ define "main" }} {{ define "main" }}
<div class="ui raised segment"> <div class="ui raised segment">

@ -1,4 +1,4 @@
{{ define "title" }}CAcert Board Decisions: {{ .Motion.Tag }}{{ end }} {{ define "title" }}Motion {{ .Motion.Tag }}{{ end }}
{{ define "main" }} {{ define "main" }}
{{ $voter := .User }} {{ $voter := .User }}

@ -1,4 +1,4 @@
{{ define "title" }}CAcert Board Decisions{{ end }} {{ define "title" }}Board Decisions{{ end }}
{{ define "main" }} {{ define "main" }}
{{ $page := . }} {{ $page := . }}

@ -0,0 +1,15 @@
{{ define "title"}}User Management{{ end }}
{{ define "main"}}
{{ if .Users }}
{{ else }}
<div class="ui basic segment">
<div class="ui icon message">
<i class="users icon"></i>
<div class="content">
<div class="header">No users found.</div>
</div>
</div>
</div>
{{ end }}
{{ end }}
Loading…
Cancel
Save