Skip to content

Commit

Permalink
add multiple flash support
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Nov 27, 2019
1 parent 8d983d3 commit 605c587
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
45 changes: 22 additions & 23 deletions server/ctrladmin/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ type Response struct {
data *templateData
// code is 303
redirect string
flashN string // normal
flashW string // warning
flashN []string // normal
flashW []string // warning
// code is >= 400
code int
err string
Expand Down Expand Up @@ -177,6 +177,13 @@ func (c *Controller) H(h adminHandler) http.Handler {
// ## begin utilities
// ## begin utilities

type FlashType string

const (
FlashNormal = FlashType("normal")
FlashWarning = FlashType("warning")
)

func firstExisting(or string, strings ...string) string {
for _, s := range strings {
if s != "" {
Expand All @@ -194,35 +201,27 @@ func sessLogSave(s *sessions.Session, w http.ResponseWriter, r *http.Request) {

type Flash struct {
Message string
Type string
Type FlashType
}

func sessAddFlashW(s *sessions.Session, message string) {
if message == "" {
return
}
s.AddFlash(Flash{
Message: message,
Type: "warning",
})
func sessAddFlashN(s *sessions.Session, messages []string) {
sessAddFlash(s, messages, FlashNormal)
}

func sessAddFlashWf(s *sessions.Session, message string, a ...interface{}) {
sessAddFlashW(s, fmt.Sprintf(message, a...))
func sessAddFlashW(s *sessions.Session, messages []string) {
sessAddFlash(s, messages, FlashWarning)
}

func sessAddFlashN(s *sessions.Session, message string) {
if message == "" {
func sessAddFlash(s *sessions.Session, messages []string, flashT FlashType) {
if len(messages) == 0 {
return
}
s.AddFlash(Flash{
Message: message,
Type: "normal",
})
}

func sessAddFlashNf(s *sessions.Session, message string, a ...interface{}) {
sessAddFlashN(s, fmt.Sprintf(message, a...))
for _, message := range messages {
s.AddFlash(Flash{
Message: message,
Type: flashT,
})
}
}

// ## begin validation
Expand Down
14 changes: 7 additions & 7 deletions server/ctrladmin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Controller) ServeChangeOwnPasswordDo(r *http.Request) *Response {
if err != nil {
return &Response{
redirect: r.Referer(),
flashW: err.Error(),
flashW: []string{err.Error()},
}
}
user := r.Context().Value(key.User).(*model.User)
Expand All @@ -101,7 +101,7 @@ func (c *Controller) ServeLinkLastFMDo(r *http.Request) *Response {
if err != nil {
return &Response{
redirect: "/admin/home",
flashW: err.Error(),
flashW: []string{err.Error()},
}
}
user := r.Context().Value(key.User).(*model.User)
Expand Down Expand Up @@ -148,7 +148,7 @@ func (c *Controller) ServeChangePasswordDo(r *http.Request) *Response {
if err != nil {
return &Response{
redirect: r.Referer(),
flashW: err.Error(),
flashW: []string{err.Error()},
}
}
user := c.DB.GetUserFromName(username)
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c *Controller) ServeCreateUserDo(r *http.Request) *Response {
if err != nil {
return &Response{
redirect: r.Referer(),
flashW: err.Error(),
flashW: []string{err.Error()},
}
}
passwordOne := r.FormValue("password_one")
Expand All @@ -206,7 +206,7 @@ func (c *Controller) ServeCreateUserDo(r *http.Request) *Response {
if err != nil {
return &Response{
redirect: r.Referer(),
flashW: err.Error(),
flashW: []string{err.Error()},
}
}
user := model.User{
Expand All @@ -217,7 +217,7 @@ func (c *Controller) ServeCreateUserDo(r *http.Request) *Response {
if err != nil {
return &Response{
redirect: r.Referer(),
flashW: fmt.Sprintf("could not create user `%s`: %v", username, err),
flashW: []string{fmt.Sprintf("could not create user `%s`: %v", username, err)},
}
}
return &Response{redirect: "/admin/home"}
Expand All @@ -239,7 +239,7 @@ func (c *Controller) ServeUpdateLastFMAPIKeyDo(r *http.Request) *Response {
if err := validateAPIKey(apiKey, secret); err != nil {
return &Response{
redirect: r.Referer(),
flashW: err.Error(),
flashW: []string{err.Error()},
}
}
c.DB.SetSetting("lastfm_api_key", apiKey)
Expand Down
4 changes: 2 additions & 2 deletions server/ctrladmin/handlers_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username")
password := r.FormValue("password")
if username == "" || password == "" {
sessAddFlashW(session, "please provide both a username and password")
sessAddFlashW(session, []string{"please provide username and password"})
sessLogSave(session, w, r)
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
return
}
user := c.DB.GetUserFromName(username)
if user == nil || password != user.Password {
sessAddFlashW(session, "invalid username / password")
sessAddFlashW(session, []string{"invalid username / password"})
sessLogSave(session, w, r)
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
return
Expand Down
4 changes: 2 additions & 2 deletions server/ctrladmin/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *Controller) WithUserSession(next http.Handler) http.Handler {
session := r.Context().Value(key.Session).(*sessions.Session)
username, ok := session.Values["user"].(string)
if !ok {
sessAddFlashW(session, "you are not authenticated")
sessAddFlashW(session, []string{"you are not authenticated"})
sessLogSave(session, w, r)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return
Expand All @@ -50,7 +50,7 @@ func (c *Controller) WithAdminSession(next http.Handler) http.Handler {
session := r.Context().Value(key.Session).(*sessions.Session)
user := r.Context().Value(key.User).(*model.User)
if !user.IsAdmin {
sessAddFlashW(session, "you are not an admin")
sessAddFlashW(session, []string{"you are not an admin"})
sessLogSave(session, w, r)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return
Expand Down

0 comments on commit 605c587

Please sign in to comment.