Refactoring handlers

This commit is contained in:
Andrea Fazzi 2020-01-24 07:15:23 +01:00
parent 2ece2dcba4
commit ca17d60709

View file

@ -253,6 +253,10 @@ func (h *Handlers) cookieExtractor(r *http.Request) (string, error) {
return string(token), nil return string(token), nil
} }
func getClaims(r *http.Request) jwt.MapClaims {
return r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
}
func DefaultRecoverHandler(next http.Handler) http.Handler { func DefaultRecoverHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
defer func() { defer func() {
@ -280,7 +284,10 @@ func (h *Handlers) setFlashMessage(w http.ResponseWriter, r *http.Request, key s
return nil return nil
} }
func (h *Handlers) hasPermission(role, path string) bool { func (h *Handlers) hasPermission(r *http.Request, path string) bool {
claims := getClaims(r)
role := claims["role"].(string)
if h.permissions[role] == nil { if h.permissions[role] == nil {
return false return false
} }
@ -288,61 +295,48 @@ func (h *Handlers) hasPermission(role, path string) bool {
return h.permissions[role][path] return h.permissions[role][path]
} }
func (h *Handlers) callModelFunc(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) (interface{}, error) {
fn, err := h.Database.GetFunc(pattern.Path(model))
if err != nil {
return nil, err
}
if !h.hasPermission(r, pattern.Path(model)) {
return nil, errors.NotAuthorized
}
data, err := fn(h.Database, mux.Vars(r), w, r)
if err != nil {
return nil, err
}
return data, nil
}
func (h *Handlers) get(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) error { func (h *Handlers) get(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) error {
format := r.URL.Query().Get("format") data, err := h.callModelFunc(w, r, model, pattern)
getFn, err := h.Database.GetFunc(pattern.Path(model))
if err != nil { if err != nil {
return err return err
} else {
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
role := claims["role"].(string)
if !h.hasPermission(role, pattern.Path(model)) {
return errors.NotAuthorized
} else {
data, err := getFn(h.Database, mux.Vars(r), w, r)
if err != nil {
h.Renderer[format].Render(w, r, h.CookieStore, err)
} else {
h.Renderer[format].Render(w, r, h.CookieStore, data, r.URL.Query())
}
}
} }
format := r.URL.Query().Get("format")
h.Renderer[format].Render(w, r, h.CookieStore, data, r.URL.Query())
return nil return nil
} }
func (h *Handlers) post(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) error { func (h *Handlers) post(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) error {
var ( data, err := h.callModelFunc(w, r, model, pattern)
data interface{}
err error
)
respFormat := renderer.GetContentFormat(r)
postFn, err := h.Database.GetFunc(pattern.Path(model))
if err != nil { if err != nil {
return err return err
} else { }
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims) if pattern.RedirectPattern != "" {
if id := mux.Vars(r)["id"]; id != "" {
role := claims["role"].(string) modelId, _ := strconv.Atoi(id)
if !h.hasPermission(role, pattern.Path(model)) { http.Redirect(w, r, pattern.RedirectPath(model, uint(modelId)), http.StatusSeeOther)
return errors.NotAuthorized
} else { } else {
data, err = postFn(h.Database, mux.Vars(r), w, r) http.Redirect(w, r, pattern.RedirectPath(model, data.(orm.IDer).GetID()), http.StatusSeeOther)
if err != nil {
return err
} else if pattern.RedirectPattern != "" {
if id := mux.Vars(r)["id"]; id != "" {
modelId, _ := strconv.Atoi(id)
http.Redirect(w, r, pattern.RedirectPath(model, uint(modelId)), http.StatusSeeOther)
} else {
http.Redirect(w, r, pattern.RedirectPath(model, data.(orm.IDer).GetID()), http.StatusSeeOther)
}
} else {
h.Renderer[respFormat].Render(w, r, h.CookieStore, data.(orm.IDer).GetID())
}
} }
} else {
format := renderer.GetContentFormat(r)
h.Renderer[format].Render(w, r, h.CookieStore, data.(orm.IDer).GetID())
} }
return nil return nil
@ -350,34 +344,21 @@ func (h *Handlers) post(w http.ResponseWriter, r *http.Request, model string, pa
} }
func (h *Handlers) delete(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) error { func (h *Handlers) delete(w http.ResponseWriter, r *http.Request, model string, pattern config.PathPattern) error {
var data interface{} data, err := h.callModelFunc(w, r, model, pattern)
if err != nil {
return err
}
if pattern.RedirectPattern != "" {
var data struct {
RedirectUrl string `json:"redirect_url"`
}
data.RedirectUrl = pattern.RedirectPath(model)
respFormat := renderer.GetContentFormat(r) w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
role := claims["role"].(string)
if !h.hasPermission(role, pattern.Path(model)) {
return errors.NotAuthorized
} else { } else {
postFn, err := h.Database.GetFunc(pattern.Path(model)) format := renderer.GetContentFormat(r)
if err != nil { h.Renderer[format].Render(w, r, h.CookieStore, data.(orm.IDer).GetID())
return err
// h.Renderer[r.URL.Query().Get("format")].Render(w, r, h.CookieStore, err)
}
data, err = postFn(h.Database, mux.Vars(r), w, r)
if err != nil {
return err
} else if pattern.RedirectPattern != "" {
var data struct {
RedirectUrl string `json:"redirect_url"`
}
data.RedirectUrl = pattern.RedirectPath(model)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
} else {
h.Renderer[respFormat].Render(w, r, h.CookieStore, data.(orm.IDer).GetID())
}
} }
return nil return nil
@ -453,7 +434,8 @@ func (h *Handlers) modelHandler(model string, pattern config.PathPattern) handle
func DefaultHomeHandler() http.Handler { func DefaultHomeHandler() http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
claims := getClaims(r)
switch claims["role"] { switch claims["role"] {