Nuestro burocrata preferido: Sam Lowry https://lowry.sindominio.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
lowry/server/template.go

75 lines
1.5 KiB

package server
import (
"html/template"
"log"
"net/http"
"0xacab.org/sindominio/lowry/ldap"
)
type responseT struct {
User string
IsAdmin bool
Role ldap.Role
Section string
Data interface{}
w http.ResponseWriter
r *http.Request
tmpl *template.Template
}
func initTemplate() *template.Template {
return template.Must(template.ParseFiles(
"tmpl/403.html",
"tmpl/404.html",
"tmpl/500.html",
"tmpl/header.html",
"tmpl/footer.html",
"tmpl/navbar.html",
"tmpl/login.html",
"tmpl/index.html",
"tmpl/password.html",
"tmpl/user.html",
"tmpl/users.html",
"tmpl/invite.html",
"tmpl/adduser.html",
"tmpl/adduser_success.html",
"tmpl/group.html",
"tmpl/groups.html",
))
}
func (s *server) newResponse(template string, w http.ResponseWriter, r *http.Request) responseT {
session := s.sess.get(w, r)
user := ""
role := ldap.Undefined
admin := false
if session != nil {
user = session.user
admin = s.isAdmin(user)
role = s.ldap.GetUserRole(user)
}
return responseT{
User: user,
IsAdmin: admin,
Role: role,
Section: template,
Data: nil,
w: w,
r: r,
tmpl: s.tmpl,
}
}
func (r *responseT) execute(data interface{}) {
r.Data = data
if err := r.tmpl.ExecuteTemplate(r.w, r.Section+".html", r); err != nil {
log.Println("An error ocurred loading template '", r.Section, "': ", err)
r.tmpl.ExecuteTemplate(r.w, "500.html", r)
}
}
func (s *server) isAdmin(user string) bool {
return s.ldap.InGroup(user, "adm")
}