add second flash type

This commit is contained in:
sentriz
2019-07-06 16:41:32 +01:00
parent 4d844bd704
commit 8239db3117
7 changed files with 62 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/gob"
"flag" "flag"
"log" "log"
"os" "os"
@@ -10,6 +11,7 @@ import (
"github.com/sentriz/gonic/db" "github.com/sentriz/gonic/db"
"github.com/sentriz/gonic/server" "github.com/sentriz/gonic/server"
"github.com/sentriz/gonic/server/handler"
) )
const ( const (
@@ -47,6 +49,7 @@ func main() {
log.Fatalf("error opening database: %v\n", err) log.Fatalf("error opening database: %v\n", err)
} }
defer db.Close() defer db.Close()
gob.Register(&handler.Flash{})
s := server.New( s := server.New(
db, db,
*musicPath, *musicPath,

View File

@@ -13,8 +13,9 @@
</div> </div>
<div id="content"> <div id="content">
{{ if .Flashes }} {{ if .Flashes }}
<div id="flashes" class="padded mono"> {{ $flash := index .Flashes 0 }}
<i class="mdi mdi-alert-circle"></i> {{ index .Flashes 0 }} <div class="padded mono flash-{{ $flash.Type }}">
<i class="mdi mdi-alert-circle"></i> {{ $flash.Message }}
</div> </div>
{{ end }} {{ end }}
{{ template "content" . }} {{ template "content" . }}

View File

@@ -102,12 +102,18 @@ a:hover {
height: auto; height: auto;
} }
#flashes { .flash-warning {
background-color: #fd1b1b1c; background-color: #fd1b1b1c;
border-right: 2px solid #fd1b1b1c; border-right: 2px solid #fd1b1b1c;
border-bottom: 2px solid #fd1b1b1c; border-bottom: 2px solid #fd1b1b1c;
} }
.flash-normal {
background-color: #15ff5452;
border-right: 2px solid #15ff5452;
border-bottom: 2px solid #15ff5452;
}
.text-right { .text-right {
text-align: right; text-align: right;
} }

View File

@@ -24,15 +24,15 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username") username := r.FormValue("username")
password := r.FormValue("password") password := r.FormValue("password")
if username == "" || password == "" { if username == "" || password == "" {
session.AddFlash("please provide both a username and password") sessAddFlashW("please provide both a username and password", session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
user := c.DB.GetUserFromName(username) user := c.DB.GetUserFromName(username)
if user == nil || password != user.Password { if user == nil || password != user.Password {
session.AddFlash("invalid username / password") sessAddFlashW("invalid username / password", session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -40,14 +40,14 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
// are wrapped with WithUserSession() which will get the name from the // are wrapped with WithUserSession() which will get the name from the
// session and put the row into the request context. // session and put the row into the request context.
session.Values["user"] = user.Name session.Values["user"] = user.Name
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/home", http.StatusSeeOther) http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
} }
func (c *Controller) ServeLogout(w http.ResponseWriter, r *http.Request) { func (c *Controller) ServeLogout(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(contextSessionKey).(*sessions.Session) session := r.Context().Value(contextSessionKey).(*sessions.Session)
session.Options.MaxAge = -1 session.Options.MaxAge = -1
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther) http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
} }
@@ -102,8 +102,8 @@ func (c *Controller) ServeChangeOwnPasswordDo(w http.ResponseWriter, r *http.Req
passwordTwo := r.FormValue("password_two") passwordTwo := r.FormValue("password_two")
err := validatePasswords(passwordOne, passwordTwo) err := validatePasswords(passwordOne, passwordTwo)
if err != nil { if err != nil {
session.AddFlash(err.Error()) sessAddFlashW(err.Error(), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -126,8 +126,8 @@ func (c *Controller) ServeLinkLastFMDo(w http.ResponseWriter, r *http.Request) {
) )
session := r.Context().Value(contextSessionKey).(*sessions.Session) session := r.Context().Value(contextSessionKey).(*sessions.Session)
if err != nil { if err != nil {
session.AddFlash(err.Error()) sessAddFlashW(err.Error(), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/home", http.StatusSeeOther) http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
return return
} }
@@ -175,8 +175,8 @@ func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Reques
passwordTwo := r.FormValue("password_two") passwordTwo := r.FormValue("password_two")
err := validatePasswords(passwordOne, passwordTwo) err := validatePasswords(passwordOne, passwordTwo)
if err != nil { if err != nil {
session.AddFlash(err.Error()) sessAddFlashW(err.Error(), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -224,8 +224,8 @@ func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username") username := r.FormValue("username")
err := validateUsername(username) err := validateUsername(username)
if err != nil { if err != nil {
session.AddFlash(err.Error()) sessAddFlashW(err.Error(), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -233,8 +233,8 @@ func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
passwordTwo := r.FormValue("password_two") passwordTwo := r.FormValue("password_two")
err = validatePasswords(passwordOne, passwordTwo) err = validatePasswords(passwordOne, passwordTwo)
if err != nil { if err != nil {
session.AddFlash(err.Error()) sessAddFlashW(err.Error(), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -244,10 +244,10 @@ func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
} }
err = c.DB.Create(&user).Error err = c.DB.Create(&user).Error
if err != nil { if err != nil {
session.AddFlash(fmt.Sprintf( sessAddFlashW(fmt.Sprintf(
"could not create user `%s`: %v", username, err, "could not create user `%s`: %v", username, err,
)) ), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -267,8 +267,8 @@ func (c *Controller) ServeUpdateLastFMAPIKeyDo(w http.ResponseWriter, r *http.Re
secret := r.FormValue("secret") secret := r.FormValue("secret")
err := validateAPIKey(apiKey, secret) err := validateAPIKey(apiKey, secret)
if err != nil { if err != nil {
session.AddFlash(err.Error()) sessAddFlashW(err.Error(), session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
return return
} }
@@ -279,8 +279,8 @@ func (c *Controller) ServeUpdateLastFMAPIKeyDo(w http.ResponseWriter, r *http.Re
func (c *Controller) ServeStartScanDo(w http.ResponseWriter, r *http.Request) { func (c *Controller) ServeStartScanDo(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(contextSessionKey).(*sessions.Session) session := r.Context().Value(contextSessionKey).(*sessions.Session)
session.AddFlash("scan started") sessAddFlashN("scan started", session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/home", http.StatusSeeOther) http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
go func() { go func() {
err := scanner. err := scanner.

View File

@@ -16,8 +16,27 @@ func firstExisting(or string, strings ...string) string {
return or return or
} }
func sessionLogSave(w http.ResponseWriter, r *http.Request, s *sessions.Session) { func sessLogSave(w http.ResponseWriter, r *http.Request, s *sessions.Session) {
if err := s.Save(r, w); err != nil { if err := s.Save(r, w); err != nil {
log.Printf("error saving session: %v\n", err) log.Printf("error saving session: %v\n", err)
} }
} }
type Flash struct {
Message string
Type string
}
func sessAddFlashW(message string, s *sessions.Session) {
s.AddFlash(Flash{
Message: message,
Type: "warning",
})
}
func sessAddFlashN(message string, s *sessions.Session) {
s.AddFlash(Flash{
Message: message,
Type: "normal",
})
}

View File

@@ -25,8 +25,8 @@ func (c *Controller) WithUserSession(next http.HandlerFunc) http.HandlerFunc {
session := r.Context().Value(contextSessionKey).(*sessions.Session) session := r.Context().Value(contextSessionKey).(*sessions.Session)
username, ok := session.Values["user"].(string) username, ok := session.Values["user"].(string)
if !ok { if !ok {
session.AddFlash("you are not authenticated") sessAddFlashW("you are not authenticated", session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther) http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return return
} }
@@ -36,7 +36,7 @@ func (c *Controller) WithUserSession(next http.HandlerFunc) http.HandlerFunc {
// the username in the client's session no longer relates to a // the username in the client's session no longer relates to a
// user in the database (maybe the user was deleted) // user in the database (maybe the user was deleted)
session.Options.MaxAge = -1 session.Options.MaxAge = -1
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther) http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return return
} }
@@ -52,8 +52,8 @@ func (c *Controller) WithAdminSession(next http.HandlerFunc) http.HandlerFunc {
session := r.Context().Value(contextSessionKey).(*sessions.Session) session := r.Context().Value(contextSessionKey).(*sessions.Session)
user := r.Context().Value(contextUserKey).(*model.User) user := r.Context().Value(contextUserKey).(*model.User)
if !user.IsAdmin { if !user.IsAdmin {
session.AddFlash("you are not an admin") sessAddFlashW("you are not an admin", session)
sessionLogSave(w, r, session) sessLogSave(w, r, session)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther) http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return return
} }

View File

@@ -41,7 +41,7 @@ func renderTemplate(
} }
session := r.Context().Value(contextSessionKey).(*sessions.Session) session := r.Context().Value(contextSessionKey).(*sessions.Session)
data.Flashes = session.Flashes() data.Flashes = session.Flashes()
sessionLogSave(w, r, session) sessLogSave(w, r, session)
data.User, _ = r.Context().Value(contextUserKey).(*model.User) data.User, _ = r.Context().Value(contextUserKey).(*model.User)
err := tmpl.Execute(w, data) err := tmpl.Execute(w, data)
if err != nil { if err != nil {