Browse Source

Give a meaningful error on user adds to collective

Add an error template for it and improve the existing error templates
with a back button.

* Closes: #21
gitea
meskio 2 months ago
parent
commit
43fafbcf6b
Signed by: meskio GPG Key ID: 52B8F5AC97A2DA86
  1. 33
      server/collective.go
  2. 7
      server/template.go
  3. 3
      tmpl/403.html
  4. 3
      tmpl/404.html
  5. 3
      tmpl/500.html
  6. 22
      tmpl/error.html

33
server/collective.go

@ -2,6 +2,7 @@ package server
import (
"errors"
"fmt"
"log"
"net/http"
@ -145,7 +146,7 @@ func (s *server) collectiveHandler(w http.ResponseWriter, r *http.Request) {
func (s *server) addUserCollectiveHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
collectiveName := vars["name"]
response := s.newResponse("", w, r)
response := s.newResponse("error", w, r)
if !response.IsAdmin && !s.ldap.InGroup(response.User, collectiveName) {
log.Println("Attemp to add user to collective without rights")
s.forbiddenHandler(w, r)
@ -153,12 +154,38 @@ func (s *server) addUserCollectiveHandler(w http.ResponseWriter, r *http.Request
}
user := r.FormValue("user")
err := s.ldap.AddUserGroup(user, collectiveName)
group, err := s.ldap.GetGroup(collectiveName)
if err != nil {
log.Println("An error ocurred adding user '", user, "' to ", collectiveName, ": ", err)
log.Println("An error ocurred getting colelctive ", collectiveName, "to add", user, ":", err)
s.errorHandler(w, r)
return
}
found := false
for _, member := range group.Members {
if member == user {
found = true
break
}
}
if found {
log.Println("User", user, "already colelctive ", collectiveName, "don't need to add them")
} else {
err := s.ldap.AddUserGroup(user, collectiveName)
if err != nil {
if errors.Is(err, ldap.ErrNotFound) {
response.execute(ErrorData{
Title: "Cuenta no encontrada",
Description: fmt.Sprintf("No hay ninguna cuenta con el nombre %s para añadirla al colectivo %s. Comprueba que tengas el nombre bien e intentalo de nuevo.", user, collectiveName),
BackURL: "/collective/" + collectiveName,
})
} else {
log.Println("An error ocurred adding user '", user, "' to ", collectiveName, ": ", err)
s.errorHandler(w, r)
}
return
}
}
http.Redirect(w, r, "/collective/"+collectiveName, http.StatusFound)
}

7
server/template.go

@ -20,11 +20,18 @@ type responseT struct {
tmpl *template.Template
}
type ErrorData struct {
Title string
Description string
BackURL string
}
func initTemplate() *template.Template {
return template.Must(template.ParseFiles(
"tmpl/403.html",
"tmpl/404.html",
"tmpl/500.html",
"tmpl/error.html",
"tmpl/header.html",
"tmpl/header_close.html",
"tmpl/extra_script_password_validator.html",

3
tmpl/403.html

@ -12,6 +12,9 @@
<div class="row justify-content-center">
<p>Necesitas ser administrador para acceder a esta página</p>
</div>
<div class="row float-right">
<button type="button" class="btn btn-primary" onclick="history.go(-1)">Volver</a>
</div>
</div>
{{template "footer.html"}}

3
tmpl/404.html

@ -12,6 +12,9 @@
<div class="row justify-content-center">
<p>La página que buscas no existe.</p>
</div>
<div class="row float-right">
<button type="button" class="btn btn-primary" onclick="history.go(-1)">Volver</a>
</div>
</div>
{{template "footer.html"}}

3
tmpl/500.html

@ -12,6 +12,9 @@
<div class="row justify-content-center">
<p>Ha ocurrido un error interno en el servidor, prueba de nuevo mas tarde.</p>
</div>
<div class="row float-right">
<button type="button" class="btn btn-primary" onclick="history.go(-1)">Volver</a>
</div>
</div>
{{template "footer.html"}}

22
tmpl/error.html

@ -0,0 +1,22 @@
{{template "header.html"}}
{{template "header_close.html"}}
{{if .User}}
{{template "navbar.html" .}}
{{end}}
{{with .Data}}
<div class="container">
<br />
<div class="row justify-content-center">
<h1>{{.Title}}</h1>
</div>
<div class="row justify-content-center">
<p>{{.Description}}</p>
</div>
<div class="row float-right">
<a href="{{.BackURL}}" class="btn btn-primary" {{if not .BackURL}}onclick="history.go(-1)"{{end}}>Volver</a>
</div>
</div>
{{end}}
{{template "footer.html"}}
Loading…
Cancel
Save