add param abstraction to request context
This commit is contained in:
@@ -19,7 +19,6 @@ import (
|
||||
"senan.xyz/g/gonic/assets"
|
||||
"senan.xyz/g/gonic/model"
|
||||
"senan.xyz/g/gonic/server/ctrlbase"
|
||||
"senan.xyz/g/gonic/server/key"
|
||||
"senan.xyz/g/gonic/version"
|
||||
)
|
||||
|
||||
@@ -27,6 +26,13 @@ func init() {
|
||||
gob.Register(&Flash{})
|
||||
}
|
||||
|
||||
type CtxKey int
|
||||
|
||||
const (
|
||||
CtxUser CtxKey = iota
|
||||
CtxSession
|
||||
)
|
||||
|
||||
// extendFromPaths /extends/ the given template for every asset
|
||||
// with given prefix
|
||||
func extendFromPaths(b *template.Template, p string) *template.Template {
|
||||
@@ -124,7 +130,7 @@ type Response struct {
|
||||
func (c *Controller) H(h adminHandler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
resp := h(r)
|
||||
session, ok := r.Context().Value(key.Session).(*sessions.Session)
|
||||
session, ok := r.Context().Value(CtxSession).(*sessions.Session)
|
||||
if ok {
|
||||
sessAddFlashN(session, resp.flashN)
|
||||
sessAddFlashW(session, resp.flashW)
|
||||
@@ -156,7 +162,7 @@ func (c *Controller) H(h adminHandler) http.Handler {
|
||||
return
|
||||
}
|
||||
}
|
||||
if user, ok := r.Context().Value(key.User).(*model.User); ok {
|
||||
if user, ok := r.Context().Value(CtxUser).(*model.User); ok {
|
||||
resp.data.User = user
|
||||
}
|
||||
buff := c.buffPool.Get()
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
"senan.xyz/g/gonic/model"
|
||||
"senan.xyz/g/gonic/scanner"
|
||||
"senan.xyz/g/gonic/server/key"
|
||||
"senan.xyz/g/gonic/server/lastfm"
|
||||
)
|
||||
|
||||
@@ -60,7 +59,7 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
|
||||
}
|
||||
//
|
||||
// playlists box
|
||||
user := r.Context().Value(key.User).(*model.User)
|
||||
user := r.Context().Value(CtxUser).(*model.User)
|
||||
c.DB.
|
||||
Select("*, count(items.id) as track_count").
|
||||
Joins(`
|
||||
@@ -92,7 +91,7 @@ func (c *Controller) ServeChangeOwnPasswordDo(r *http.Request) *Response {
|
||||
flashW: []string{err.Error()},
|
||||
}
|
||||
}
|
||||
user := r.Context().Value(key.User).(*model.User)
|
||||
user := r.Context().Value(CtxUser).(*model.User)
|
||||
user.Password = passwordOne
|
||||
c.DB.Save(user)
|
||||
return &Response{redirect: "/admin/home"}
|
||||
@@ -117,14 +116,14 @@ func (c *Controller) ServeLinkLastFMDo(r *http.Request) *Response {
|
||||
flashW: []string{err.Error()},
|
||||
}
|
||||
}
|
||||
user := r.Context().Value(key.User).(*model.User)
|
||||
user := r.Context().Value(CtxUser).(*model.User)
|
||||
user.LastFMSession = sessionKey
|
||||
c.DB.Save(&user)
|
||||
return &Response{redirect: "/admin/home"}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeUnlinkLastFMDo(r *http.Request) *Response {
|
||||
user := r.Context().Value(key.User).(*model.User)
|
||||
user := r.Context().Value(CtxUser).(*model.User)
|
||||
user.LastFMSession = ""
|
||||
c.DB.Save(&user)
|
||||
return &Response{redirect: "/admin/home"}
|
||||
@@ -241,7 +240,7 @@ func (c *Controller) ServeUpdateLastFMAPIKey(r *http.Request) *Response {
|
||||
data.CurrentLastFMAPIKey = c.DB.GetSetting("lastfm_api_key")
|
||||
data.CurrentLastFMAPISecret = c.DB.GetSetting("lastfm_secret")
|
||||
return &Response{
|
||||
template: "update_lastfm_api_key.tmpl",
|
||||
template: "update_lastfm_api_key",
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
@@ -285,7 +284,7 @@ func (c *Controller) ServeUploadPlaylistDo(r *http.Request) *Response {
|
||||
code: 500,
|
||||
}
|
||||
}
|
||||
user := r.Context().Value(key.User).(*model.User)
|
||||
user := r.Context().Value(CtxUser).(*model.User)
|
||||
var playlistCount int
|
||||
var errors []string
|
||||
for _, headers := range r.MultipartForm.File {
|
||||
|
||||
@@ -4,12 +4,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
|
||||
"senan.xyz/g/gonic/server/key"
|
||||
)
|
||||
|
||||
func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value(key.Session).(*sessions.Session)
|
||||
session := r.Context().Value(CtxSession).(*sessions.Session)
|
||||
username := r.FormValue("username")
|
||||
password := r.FormValue("password")
|
||||
if username == "" || password == "" {
|
||||
@@ -34,7 +32,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Controller) ServeLogout(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value(key.Session).(*sessions.Session)
|
||||
session := r.Context().Value(CtxSession).(*sessions.Session)
|
||||
session.Options.MaxAge = -1
|
||||
sessLogSave(session, w, r)
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"github.com/gorilla/sessions"
|
||||
|
||||
"senan.xyz/g/gonic/model"
|
||||
"senan.xyz/g/gonic/server/key"
|
||||
)
|
||||
|
||||
func (c *Controller) WithSession(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := c.sessDB.Get(r, "gonic")
|
||||
withSession := context.WithValue(r.Context(), key.Session, session)
|
||||
withSession := context.WithValue(r.Context(), CtxSession, session)
|
||||
next.ServeHTTP(w, r.WithContext(withSession))
|
||||
})
|
||||
}
|
||||
@@ -21,7 +20,7 @@ func (c *Controller) WithSession(next http.Handler) http.Handler {
|
||||
func (c *Controller) WithUserSession(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// session exists at this point
|
||||
session := r.Context().Value(key.Session).(*sessions.Session)
|
||||
session := r.Context().Value(CtxSession).(*sessions.Session)
|
||||
username, ok := session.Values["user"].(string)
|
||||
if !ok {
|
||||
sessAddFlashW(session, []string{"you are not authenticated"})
|
||||
@@ -39,7 +38,7 @@ func (c *Controller) WithUserSession(next http.Handler) http.Handler {
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
withUser := context.WithValue(r.Context(), key.User, user)
|
||||
withUser := context.WithValue(r.Context(), CtxUser, user)
|
||||
next.ServeHTTP(w, r.WithContext(withUser))
|
||||
})
|
||||
}
|
||||
@@ -47,8 +46,8 @@ func (c *Controller) WithUserSession(next http.Handler) http.Handler {
|
||||
func (c *Controller) WithAdminSession(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// session and user exist at this point
|
||||
session := r.Context().Value(key.Session).(*sessions.Session)
|
||||
user := r.Context().Value(key.User).(*model.User)
|
||||
session := r.Context().Value(CtxSession).(*sessions.Session)
|
||||
user := r.Context().Value(CtxUser).(*model.User)
|
||||
if !user.IsAdmin {
|
||||
sessAddFlashW(session, []string{"you are not an admin"})
|
||||
sessLogSave(session, w, r)
|
||||
|
||||
Reference in New Issue
Block a user