Reorganize template code into its own module

merge-requests/6/head
meskio 5 years ago
parent 8c6a1bcc13
commit e55e711ac3
Signed by: meskio
GPG Key ID: 52B8F5AC97A2DA86
  1. 32
      server/admin.go
  2. 19
      server/server.go
  3. 63
      server/template.go
  4. 31
      server/user.go
  5. 4
      tmpl/group.html
  6. 4
      tmpl/groups.html
  7. 4
      tmpl/index.html
  8. 4
      tmpl/login.html
  9. 15
      tmpl/navbar.html
  10. 10
      tmpl/password.html
  11. 4
      tmpl/user.html
  12. 4
      tmpl/users.html

@ -9,8 +9,8 @@ import (
)
func (s *server) usersHandler(w http.ResponseWriter, r *http.Request) {
session := s.sess.get(w, r)
if session == nil || !s.isAdmin(session.user) {
response := s.newResponse("users", w, r)
if !response.IsAdmin {
log.Println("Non admin attemp to list users")
http.NotFound(w, r)
return
@ -22,16 +22,14 @@ func (s *server) usersHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
if err = tmpl.ExecuteTemplate(w, "users.html", users); err != nil {
log.Println("An error ocurred loading user list: ", err)
}
response.execute(users)
}
func (s *server) userHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userName := vars["name"]
session := s.sess.get(w, r)
if session == nil || (!s.isAdmin(session.user) && userName != session.user) {
response := s.newResponse("user", w, r)
if !response.IsAdmin && userName != response.User {
log.Println("Non admin attemp to list users")
http.NotFound(w, r)
return
@ -51,14 +49,12 @@ func (s *server) userHandler(w http.ResponseWriter, r *http.Request) {
User ldap.User
Groups []ldap.Group
}{user, groups}
if err = tmpl.ExecuteTemplate(w, "user.html", data); err != nil {
log.Println("An error ocurred loading user: ", err)
}
response.execute(data)
}
func (s *server) groupsHandler(w http.ResponseWriter, r *http.Request) {
session := s.sess.get(w, r)
if session == nil || !s.isAdmin(session.user) {
response := s.newResponse("groups", w, r)
if !response.IsAdmin {
log.Println("Non admin attemp to list users")
http.NotFound(w, r)
return
@ -70,16 +66,14 @@ func (s *server) groupsHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
if err = tmpl.ExecuteTemplate(w, "groups.html", groups); err != nil {
log.Println("An error ocurred loading group list: ", err)
}
response.execute(groups)
}
func (s *server) groupHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
groupName := vars["name"]
session := s.sess.get(w, r)
if session == nil || (!s.isAdmin(session.user) && !s.ldap.InGroup(session.user, groupName)) {
response := s.newResponse("group", w, r)
if !response.IsAdmin && !s.ldap.InGroup(response.User, groupName) {
log.Println("Non admin attemp to list users")
http.NotFound(w, r)
return
@ -91,7 +85,5 @@ func (s *server) groupHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
if err = tmpl.ExecuteTemplate(w, "group.html", group); err != nil {
log.Println("An error ocurred loading group: ", err)
}
response.execute(group)
}

@ -8,22 +8,10 @@ import (
"github.com/gorilla/mux"
)
var tmpl = template.Must(template.ParseFiles(
"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/group.html",
"tmpl/groups.html",
))
type server struct {
ldap *ldap.Ldap
sess *sessionStore
tmpl *template.Template
}
// Serve lowry web site
@ -31,6 +19,7 @@ func Serve(addr string, l *ldap.Ldap) error {
var s server
s.ldap = l
s.sess = initSessionStore()
s.tmpl = initTemplate()
r := mux.NewRouter()
r.HandleFunc("/", s.homeHandler)
@ -48,7 +37,3 @@ func Serve(addr string, l *ldap.Ldap) error {
return http.ListenAndServe(addr, r)
}
func (s *server) isAdmin(user string) bool {
return s.ldap.InGroup(user, "adm")
}

@ -0,0 +1,63 @@
package server
import (
"html/template"
"log"
"net/http"
)
type responseT struct {
User string
IsAdmin bool
Section string
Data interface{}
w http.ResponseWriter
r *http.Request
tmpl *template.Template
}
func initTemplate() *template.Template {
return template.Must(template.ParseFiles(
"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/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 := ""
admin := false
if session != nil {
user = session.user
admin = s.isAdmin(user)
}
return responseT{
User: user,
IsAdmin: admin,
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)
// TODO: render something
}
}
func (s *server) isAdmin(user string) bool {
return s.ldap.InGroup(user, "adm")
}

@ -5,16 +5,11 @@ import (
)
func (s *server) homeHandler(w http.ResponseWriter, r *http.Request) {
session := s.sess.get(w, r)
if session == nil {
tmpl.ExecuteTemplate(w, "login.html", false)
} else {
data := struct {
User string
Admin bool
}{session.user, s.isAdmin(session.user)}
tmpl.ExecuteTemplate(w, "index.html", data)
response := s.newResponse("index", w, r)
if response.User == "" {
response = s.newResponse("login", w, r)
}
response.execute(false)
}
func (s *server) loginHandler(w http.ResponseWriter, r *http.Request) {
@ -23,7 +18,8 @@ func (s *server) loginHandler(w http.ResponseWriter, r *http.Request) {
err := s.ldap.ValidateUser(user, pass)
if err != nil {
tmpl.ExecuteTemplate(w, "login.html", true)
response := s.newResponse("login", w, r)
response.execute(true)
return
}
@ -37,14 +33,14 @@ func (s *server) logoutHandler(w http.ResponseWriter, r *http.Request) {
}
func (s *server) passwordHandler(w http.ResponseWriter, r *http.Request) {
session := s.sess.get(w, r)
if session == nil {
response := s.newResponse("password", w, r)
if response.User == "" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
if r.Method != "POST" {
tmpl.ExecuteTemplate(w, "password.html", "")
response.execute("")
return
}
@ -52,14 +48,15 @@ func (s *server) passwordHandler(w http.ResponseWriter, r *http.Request) {
pass := r.FormValue("password")
pass2 := r.FormValue("password2")
if pass != pass2 {
tmpl.ExecuteTemplate(w, "password.html", "WrongPass")
response.execute("")
return
}
err := s.ldap.ChangePass(session.user, oldpass, pass)
err := s.ldap.ChangePass(response.User, oldpass, pass)
if err != nil {
tmpl.ExecuteTemplate(w, "password.html", "WrongOldpass")
response.execute("WrongOldpass")
} else {
tmpl.ExecuteTemplate(w, "password.html", "PassChanged")
response.execute("PassChanged")
}
}

@ -1,6 +1,7 @@
{{template "header.html"}}
{{template "navbar.html" "groups"}}
{{template "navbar.html" .}}
{{with .Data}}
<div class="row justify-content-center">
<dl>
<dt>Nombre</dt><dd>{{.Name}}</dd>
@ -11,5 +12,6 @@
</dd>
</dl>
</div>
{{end}}
{{template "footer.html"}}

@ -1,5 +1,5 @@
{{template "header.html"}}
{{template "navbar.html" "groups"}}
{{template "navbar.html" .}}
<table class="table">
<thead>
@ -9,7 +9,7 @@
</tr>
</thead>
<tbody>
{{range .}}
{{range .Data}}
<tr>
<th scope="row"><a href="/groups/{{.Name}}">{{.Name}}</a></th>
<td>

@ -1,12 +1,12 @@
{{template "header.html"}}
{{template "navbar.html" "index"}}
{{template "navbar.html" .}}
<div class="container">
<br />
<div class="row justify-content-center">
<h1 class="col-4">Hola {{.User}}!!</h1>
</div>
{{if .Admin}}
{{if .IsAdmin}}
<br />
<div class="row justify-content-center">
<p>Parece que eres una admin.<br />

@ -10,12 +10,12 @@
<form class="col-4" action="/login/" method="post">
<div class="form-group">
<label for="user">Usuaria:</label>
<input type="text" class="form-control {{if .}}is-invalid{{end}}" id="user" name="user" placeholder="Nombre de usuaria">
<input type="text" class="form-control {{if .Data}}is-invalid{{end}}" id="user" name="user" placeholder="Nombre de usuaria">
</div>
<div class="form-group">
<label for="password">Contraseña:</label>
<input type="password" class="form-control {{if .}}is-invalid{{end}}" id="password" name="password" placeholder="Contraseña">
<input type="password" class="form-control {{if .Data}}is-invalid{{end}}" id="password" name="password" placeholder="Contraseña">
<div class="invalid-feedback">Contraseña o nombre de usuaria incorrecto.</div>
</div>

@ -7,10 +7,23 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item {{if eq . "password"}}active{{end}}">
<li class="nav-item">
<a class="nav-link" href="/users/{{.User}}">{{.User}}</a>
</li>
<li class="nav-item {{if eq .Section "password"}}active{{end}}">
<a class="nav-link" href="/password/">Contraseña</a>
</li>
</ul>
{{if .IsAdmin}}
<ul class="navbar-nav ml-mr-auto">
<li class="nav-item {{if eq .Section "user" "users"}}active{{end}}">
<a class="nav-link" href="/users/">Usuarios</a>
</li>
<li class="nav-item {{if eq .Section "group" "groups"}}active{{end}}">
<a class="nav-link" href="/groups/">Grupos</a>
</li>
</ul>
{{end}}
<form class="form-inline my-2 my-lg-0" action="/logout/" method="post">
<button type="submit" class="btn btn-outline-success my-2 my-sm-0">Salir</button>

@ -1,9 +1,9 @@
{{template "header.html"}}
{{template "navbar.html" "password"}}
{{template "navbar.html" .}}
<div class="container">
<br />
{{if eq . "PassChanged"}}
{{if eq .Data "PassChanged"}}
<h1 class="row justify-content-center">La contraseña se ha actualizado.</h1>
{{else}}
<h1 class="row justify-content-center">Cambio de contraseña</h1>
@ -14,8 +14,8 @@
<form class="col-4" id="needs-validation" action="/password/" method="post" novalidate>
<div class="form-group">
<label for="oldpass">Contraseña actual:</label>
<input type="password" class="form-control {{if eq . "WrongOldpass"}}is-invalid{{end}}" id="oldpass" name="oldpass" placeholder="Contraseña">
{{if eq . "WrongOldpass"}}<div class="invalid-feedback">Contraseña incorrecta.</div>{{end}}
<input type="password" class="form-control {{if eq .Data "WrongOldpass"}}is-invalid{{end}}" id="oldpass" name="oldpass" placeholder="Contraseña">
{{if eq .Data "WrongOldpass"}}<div class="invalid-feedback">Contraseña incorrecta.</div>{{end}}
</div>
<div class="form-group">
<label for="password">Nueva contraseña:</label>
@ -23,7 +23,7 @@
</div>
<div class="form-group">
<label for="password2">Repita la contraseña nueva:</label>
<input type="password" class="form-control {{if eq . "WrongPass"}}is-invalid{{end}}" id="password2" name="password2" placeholder="Contraseña">
<input type="password" class="form-control {{if eq .Data "WrongPass"}}is-invalid{{end}}" id="password2" name="password2" placeholder="Contraseña">
<div class="invalid-feedback">No ha introducido la misma contraseña.</div>
</div>
<button type="submit" class="btn btn-primary">Cambiar</button>

@ -1,6 +1,7 @@
{{template "header.html"}}
{{template "navbar.html" "users"}}
{{template "navbar.html" .}}
{{with .Data}}
<div class="row justify-content-center">
<dl>
<dt>Nombre</dt><dd>{{.User.Name}}</dd>
@ -17,5 +18,6 @@
<dt>GID</dt><dd>{{.User.GID}}</dd>
</dl>
</div>
{{end}}
{{template "footer.html"}}

@ -1,5 +1,5 @@
{{template "header.html"}}
{{template "navbar.html" "users"}}
{{template "navbar.html" .}}
<table class="table">
<thead>
@ -12,7 +12,7 @@
</tr>
</thead>
<tbody>
{{range .}}
{{range .Data}}
<tr {{if ne .Shell "/bin/false"}}class="table-success"{{end}}>
<th scope="row"><a href="/users/{{.Name}}">{{.Name}}</a></th>
<td>{{.Shell}}</td>

Loading…
Cancel
Save