diff --git a/handlers/handlers.go b/handlers/handlers.go index 9def5003..cb325191 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -270,7 +270,6 @@ func (h *Handlers) cookieExtractor(r *http.Request) (string, error) { if err != nil { return "", nil } - if session.Values["token"] == nil { return "", nil } @@ -344,7 +343,10 @@ func (h *Handlers) get(w http.ResponseWriter, r *http.Request, model string, pat return err } format := r.URL.Query().Get("format") - h.Renderer[format].Render(w, r, h.CookieStore, data, r.URL.Query()) + err = h.Renderer[format].Render(w, r, h.CookieStore, data, r.URL.Query()) + if err != nil { + return err + } return nil } @@ -363,7 +365,10 @@ func (h *Handlers) post(w http.ResponseWriter, r *http.Request, model string, pa } } else { format := renderer.GetContentFormat(r) - h.Renderer[format].Render(w, r, h.CookieStore, data.(orm.IDer).GetID()) + err := h.Renderer[format].Render(w, r, h.CookieStore, data.(orm.IDer).GetID()) + if err != nil { + return err + } } return nil @@ -385,7 +390,10 @@ func (h *Handlers) delete(w http.ResponseWriter, r *http.Request, model string, json.NewEncoder(w).Encode(data) } else { format := renderer.GetContentFormat(r) - h.Renderer[format].Render(w, r, h.CookieStore, data.(orm.IDer).GetID()) + err := h.Renderer[format].Render(w, r, h.CookieStore, data.(orm.IDer).GetID()) + if err != nil { + return err + } } return nil @@ -399,10 +407,12 @@ func respondWithError(h *Handlers, w http.ResponseWriter, r *http.Request, err e format = renderer.GetContentFormat(r) } if h.Config.LogLevel > config.LOG_LEVEL_OFF { - log.Println(err) + log.Println("Error:", err) } + // FIXME: this call could be superflous when an error occurs + // in a template execution w.WriteHeader(http.StatusInternalServerError) - h.Renderer[format].Render(w, r, h.CookieStore, err) + h.Renderer[format].WriteError(w, r, err) } func (h *Handlers) Create(model interface{}) http.Handler { @@ -550,10 +560,6 @@ func (m *rootMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } // This is where our error handling logic starts. - if m.h.Config.LogLevel > config.LOG_LEVEL_OFF { - log.Printf("An error accured: %v", err) // Log the error. - } - if err, ok := err.(*errors.Error); ok { err.Referer = r.Header.Get("Referer") } diff --git a/handlers/login.go b/handlers/login.go index bc378dff..e569c472 100644 --- a/handlers/login.go +++ b/handlers/login.go @@ -7,8 +7,8 @@ import ( "strconv" "time" - "git.andreafazzi.eu/andrea/oef/orm" oef_errors "git.andreafazzi.eu/andrea/oef/errors" + "git.andreafazzi.eu/andrea/oef/orm" jwt "github.com/dgrijalva/jwt-go" "github.com/gorilla/sessions" ) @@ -25,17 +25,28 @@ type UserToken struct { UserID string } +func clearSession(response http.ResponseWriter) { + cookie := &http.Cookie{ + Name: "login-session", + Value: "", + Path: "/", + MaxAge: -1, + } + http.SetCookie(response, cookie) +} + func DefaultLogoutHandler(store *sessions.CookieStore) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { - session, err := store.Get(r, "login-session") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - session.Values["token"] = []uint8{} - session.Save(r, w) + // session, err := store.Get(r, "login-session") + // if err != nil { + // // http.Error(w, err.Error(), http.StatusInternalServerError) + // store.Set(w) + // return + // } + // session.Values["token"] = []uint8{} + // session.Save(r, w) + clearSession(w) http.Redirect(w, r, "/", http.StatusSeeOther) } diff --git a/renderer/renderer.go b/renderer/renderer.go index 8a345706..141970d4 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -18,8 +18,7 @@ import ( "strings" "time" - "git.andreafazzi.eu/andrea/oef/errors" - + oef_errors "git.andreafazzi.eu/andrea/oef/errors" "github.com/gocarina/gocsv" "github.com/gorilla/schema" "github.com/gorilla/sessions" @@ -29,6 +28,7 @@ import ( type Renderer interface { Render(http.ResponseWriter, *http.Request, *sessions.CookieStore, interface{}, ...url.Values) error + WriteError(http.ResponseWriter, *http.Request, interface{}) } type JSONRenderer struct{} @@ -69,6 +69,10 @@ func NewJSONRenderer() (*JSONRenderer, error) { return &JSONRenderer{}, nil } +func (rend *JSONRenderer) WriteError(w http.ResponseWriter, r *http.Request, data interface{}) { + +} + func (rend *JSONRenderer) Render(w http.ResponseWriter, r *http.Request, store *sessions.CookieStore, data interface{}, options ...url.Values) error { w.Header().Set("Content-Type", "application/json; charset=utf-8") if isErrorType(data) { @@ -180,9 +184,16 @@ func NewHTMLRenderer(templatePath string) (*HTMLRenderer, error) { } func (rend *HTMLRenderer) writeError(w http.ResponseWriter, r *http.Request, data interface{}) { - var t *template.Template + var ( + t *template.Template + claims jwt.MapClaims + ) - err, ok := data.(*htmlTemplateData).Data.(*errors.Error) + if r.Context().Value("user") != nil { + claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims) + } + + err, ok := data.(*oef_errors.Error) if ok { t, ok = rend.templates[err.TemplateName] if !ok { @@ -197,12 +208,25 @@ func (rend *HTMLRenderer) writeError(w http.ResponseWriter, r *http.Request, dat } w.Header().Set("Content-Type", "text/html; charset=utf-8") - e := t.ExecuteTemplate(w, "error_not_authorized", err) - if e != nil { - panic(e) + + if claims != nil { + e := t.ExecuteTemplate(w, "base", &htmlTemplateData{data, nil, claims}) + if e != nil { + panic(e) + } + } else { + e := t.ExecuteTemplate(w, "error", &htmlTemplateData{data, nil, nil}) + if e != nil { + panic(e) + } + } } +func (rend *HTMLRenderer) WriteError(w http.ResponseWriter, r *http.Request, data interface{}) { + rend.writeError(w, r, data) +} + func (rend *HTMLRenderer) Render(w http.ResponseWriter, r *http.Request, store *sessions.CookieStore, data interface{}, options ...url.Values) error { var claims jwt.MapClaims @@ -210,26 +234,22 @@ func (rend *HTMLRenderer) Render(w http.ResponseWriter, r *http.Request, store * claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims) } - if data != nil { - err, ok := data.(*errors.Error) - if ok { - rend.writeError(w, r, &htmlTemplateData{data, nil, claims}) - return err - } + if err, ok := data.(*oef_errors.Error); ok { + // rend.writeError(w, r, &htmlTemplateData{data, nil, claims}) + return err + } - t, ok := rend.templates[options[0]["tpl_content"][0]] - if !ok { - err := fmt.Errorf("Template %s not found", options[0]["tpl_content"][0]) - rend.writeError(w, r, &htmlTemplateData{err, nil, claims}) - return err - } + t, ok := rend.templates[options[0]["tpl_content"][0]] + if !ok { + err := fmt.Errorf("Template %s not found", options[0]["tpl_content"][0]) + // rend.writeError(w, r, &htmlTemplateData{err, nil, claims}) + return err + } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - - if err := t.ExecuteTemplate(w, options[0]["tpl_layout"][0], &htmlTemplateData{data, options[0], claims}); err != nil { - rend.writeError(w, r, &htmlTemplateData{err, nil, claims}) - return err - } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + if err := t.ExecuteTemplate(w, options[0]["tpl_layout"][0], &htmlTemplateData{data, options[0], claims}); err != nil { + // rend.writeError(w, r, &htmlTemplateData{err, nil, claims}) + return err } return nil diff --git a/templates/error_category_exists.html.tpl b/templates/error_category_exists.html.tpl index d0b05ce7..591068c0 100644 --- a/templates/error_category_exists.html.tpl +++ b/templates/error_category_exists.html.tpl @@ -1,4 +1,4 @@ {{ define "content" }} -{{$options := `title: "Errore durante l'iscrizione della scuola"`}} -{{template "error" dict "options" ($options|yaml) "data" .Data}} +{{$options := `title: "Errore durante l'iscrizione di un partecipante"`}} +{{template "show_error" dict "options" ($options|yaml) "data" .Data}} {{end}} diff --git a/templates/error_contest_has_zero_questions.html.tpl b/templates/error_contest_has_zero_questions.html.tpl index cc8d724c..4a775ba5 100644 --- a/templates/error_contest_has_zero_questions.html.tpl +++ b/templates/error_contest_has_zero_questions.html.tpl @@ -1,4 +1,4 @@ {{ define "content" }} {{$options := `title: "Errore nella creazione/aggiornamento di un partecipante"`}} -{{template "error" dict "options" ($options|yaml) "data" .Data}} +{{template "show_error" dict "options" ($options|yaml) "data" .Data}} {{end}} diff --git a/templates/error_not_authorized.html.tpl b/templates/error_not_authorized.html.tpl index 439c71cb..5faf2255 100644 --- a/templates/error_not_authorized.html.tpl +++ b/templates/error_not_authorized.html.tpl @@ -1,4 +1,13 @@ {{ define "content" }} -{{$options := `title: "Errore di autorizzazione"`}} -{{template "error" dict "options" ($options|yaml) "data" .Data}} +
+
+-
-+
+E' possibile iscrivere fino a due partecipanti alla gara (di diversa categoria). @@ -141,7 +142,7 @@ Iscrizione completa. Non è possibile aggiungere ulteriori partecipanti.
Si è verificato un errore. Clicca qui per uscire da questa sessione.
- {{else}}Grazie per aver iscritto la scuola {{.Data.Name}} alle Olimpiadi di Economia e Finanza. @@ -27,7 +24,7 @@ utilizzare le credenziali ricevute per iscrivere gli studenti alla competizione.
- {{end}} + {{else}} {{$deletePath := ""}}