Fix subscriber templates

This commit is contained in:
Andrea Fazzi 2019-12-04 14:10:09 +01:00
parent 98114bfab1
commit 0e9ec59c7c
5 changed files with 44 additions and 11 deletions

View file

@ -50,6 +50,7 @@ var (
"username": username,
"isAdmin": isAdmin,
"isParticipant": isParticipant,
"isSubscriber": isSubscriber,
}
)
@ -58,13 +59,17 @@ func username(claims jwt.MapClaims) string {
}
func isAdmin(claims jwt.MapClaims) bool {
return claims["admin"].(bool)
return claims["role"].(string) == "administrator"
}
func isParticipant(claims jwt.MapClaims) bool {
return claims["role"].(string) == "participant"
}
func isSubscriber(claims jwt.MapClaims) bool {
return claims["role"].(string) == "subscriber"
}
func trim(text string) string {
if len(text) > MaxTextLength {
return text[0:MaxTextLength] + "…"

View file

@ -252,36 +252,36 @@ func (rend *HTMLRenderer) Render(w http.ResponseWriter, r *http.Request, data in
if data != nil && isErrorType(data) {
session, err := store.Get(r, "flash-session")
if err != nil {
rend.writeError(w, r, &htmlTemplateData{err, nil, nil, nil})
rend.writeError(w, r, &htmlTemplateData{err, nil, claims, nil})
}
fm := session.Flashes()
err = session.Save(r, w)
if err != nil {
rend.writeError(w, r, &htmlTemplateData{err, nil, nil, nil})
rend.writeError(w, r, &htmlTemplateData{err, nil, claims, fm})
}
rend.writeError(w, r, &htmlTemplateData{data.(error), nil, claims, fm})
} else {
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, nil, nil})
rend.writeError(w, r, &htmlTemplateData{err, nil, claims, nil})
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
session, err := store.Get(r, "flash-session")
if err != nil {
rend.writeError(w, r, &htmlTemplateData{err, nil, nil, nil})
rend.writeError(w, r, &htmlTemplateData{err, nil, claims, nil})
}
fm := session.Flashes()
err = session.Save(r, w)
if err != nil {
rend.writeError(w, r, &htmlTemplateData{err, nil, nil, nil})
rend.writeError(w, r, &htmlTemplateData{err, nil, claims, fm})
}
err = t.ExecuteTemplate(w, options[0]["tpl_layout"][0], &htmlTemplateData{data, options[0], claims, fm})
if err != nil {
rend.writeError(w, r, &htmlTemplateData{err, nil, nil, nil})
rend.writeError(w, r, &htmlTemplateData{err, nil, claims, fm})
}
}
}

View file

@ -32,7 +32,11 @@
</ul>
<ul class="nav navbar-nav navbar-right">
{{if .Claims|isSubscriber}}
<li><a class="nav-link" href="/logout">Esci</a></li>
{{else}}
<li><a class="nav-link" href="/logout">Disconnetti {{.Claims|username}}</a></li>
{{end}}
</ul>
</div><!--/.nav-collapse -->
</nav>

View file

@ -1,16 +1,25 @@
{{ define "content" }}
<div class="container">
{{$isSubscriber := .Claims|isSubscriber}}
{{$update := .Options.Get "update"}}
{{if $update}}
{{if not $isSubscriber}}
{{if $update}}
{{template "breadcrumb" toSlice "Scuole" (all "School") (.Data|string) (.Data.ID|show "School") "Aggiorna" "current"}}
{{else}}
{{template "breadcrumb" toSlice "Scuole" (all "School") "Aggiungi" "current"}}
{{end}}
{{end}}
{{if $isSubscriber}}
{{template "add_update_header" dict "update" $update "addTitle" "Iscrivi scuola" "updateTitle" (printf "Aggiorna scuola %s" (.Data|string))}}
{{else}}
{{template "add_update_header" dict "update" $update "addTitle" "Crea nuova scuola" "updateTitle" (printf "Aggiorna scuola %s" (.Data|string))}}
{{end}}
{{$form := "form_add_update"}}
<form
class="needs-validation"
@ -36,7 +45,7 @@
{{$options := ` { name: "Email",id: "school_email",label: "Indirizzo email",placeholder: "Inserire l'indirizzo di posta istituzionale",type: "email",required: "true"} `}}
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "Email") "update" $update}}
{{if .Data.Role|isSubscriber}}
{{if .Claims|isSubscriber}}
{{$options := ` { saveTitle: "Invia iscrizione", model: "School" } `}}
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}
{{else}}

View file

@ -1,7 +1,22 @@
{{ define "content" }}
<div class="container">
{{if .Claims|isSubscriber}}
<p>
Grazie per aver iscritto la scuola <strong>{{.Data.Name}}</strong>
alle Olimpiadi di Economia e Finanza.
</p>
<p>
Una mail con le credenziali di accesso è stata inviata
all'indirizzo <strong>{{.Data.Email}}</strong>.
</p>
<p>
Ora è possibile <a href="/logout">uscire</a> da questa sessione e
utilizzare le credenziali ricevute per iscrivere gli studenti alla
competizione.
</p>
{{else}}
{{template "breadcrumb" toSlice "Scuole" (all "School") (.Data|string|trim) "current"}}
{{template "show_header" dict "title" (.Data|string|trim) "updatePath" (.Data.ID|update "School") "deletePath" (.Data.ID|delete "School")}}
@ -26,7 +41,7 @@
</div>
</div>
{{end}}
</div>
{{ end }}