Admins can change the role

merge-requests/4/head
meskio 4 years ago
parent 4f137cfd17
commit 9d85f8d0b8
Signed by: meskio
GPG Key ID: 52B8F5AC97A2DA86
  1. 27
      server/admin.go
  2. 1
      server/server.go
  3. 43
      tmpl/user.html

@ -93,6 +93,33 @@ func (s *server) groupHandler(w http.ResponseWriter, r *http.Request) {
response.execute(group)
}
func (s *server) roleHandler(w http.ResponseWriter, r *http.Request) {
response := s.newResponse("", w, r)
if !response.IsAdmin {
log.Println("Non admin attemp to change user role")
s.forbiddenHandler(w, r)
return
}
vars := mux.Vars(r)
userName := vars["name"]
roleStr := r.FormValue("role")
role := ldap.RoleFromString(roleStr)
if role == ldap.Undefined {
log.Println("Not valid role '", roleStr, "' for ", userName)
s.errorHandler(w, r)
return
}
err := s.ldap.ChangeRole(userName, role)
if err != nil {
log.Println("An error ocurred changing role of '", userName, "' to", roleStr, ": ", err)
s.errorHandler(w, r)
return
}
http.Redirect(w, r, "/users/"+userName, http.StatusFound)
}
func (s *server) passwdadmHandler(w http.ResponseWriter, r *http.Request) {
response := s.newResponse("", w, r)
if !response.IsAdmin {

@ -37,6 +37,7 @@ func Serve(addr string, l *ldap.Ldap, ldb *db.DB, usersAskRole []string) error {
r.HandleFunc("/password/", s.passwordHandler)
r.HandleFunc("/users/", s.usersHandler)
r.HandleFunc("/users/{name}", s.userHandler)
r.HandleFunc("/users/{name}/role/", s.roleHandler).Methods("POST")
r.HandleFunc("/users/{name}/password/", s.passwdadmHandler).Methods("POST")
r.HandleFunc("/users/{name}/shell/", s.shellHandler).Methods("POST")
r.HandleFunc("/adduser/", s.createInviteHandler)

@ -1,11 +1,14 @@
{{template "header.html"}}
{{template "navbar.html" .}}
{{$isAdmin := .IsAdmin}}
{{$isAdmin := .IsAdmin}}}
{{with .Data}}
<div class="row justify-content-center">
<br />
<br />
<dl>
<dt>Nombre</dt><dd>{{.User.Name}}</dd>
<dt>Rol</dt><dd>{{.User.Role}}</dd>
<dt>Shell</dt><dd>{{.User.Shell}}</dd>
<dt>Home</dt><dd>{{.User.Home}}</dd>
<dt>Mail</dt><dd>{{.User.Mail}}</dd>
@ -25,32 +28,58 @@
</div>
{{if $isAdmin}}
<br />
<div class="row justify-content-center">
<form action="/users/{{.User.Name}}/role/" method="post">
<div class="row">
<div class="col-md-5">
<label>Cambia el rol:</label>
</div>
<div class="col-md-5">
<div class="form-check from-check-inline">
<input class="form-check-input" type="radio" name="role" id="amiga" value="amiga"
{{if eq (printf "%v" .User.Role) "amiga"}}checked{{end}}>
<label class="form-check-label" for="amiga">amiga</label>
</div>
<div class="form-check from-check-inline">
<input class="form-check-input" type="radio" name="role" id="sindominante" value="sindominante"
{{if eq (printf "%v" .User.Role) "sindominante"}}checked{{end}}>
<label class="form-check-label" for="sindominante">sindominante</label>
</div>
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</div>
</form>
</div>
<br />
<div class="row justify-content-center">
<form action="/users/{{.User.Name}}/password/" method="post">
<div class="row">
<div class="col">
<div class="col-md-5">
<label for="password">Cambia la contraseña:</label>
</div>
<div class="col">
<div class="col-md-5">
<input type="password" class="form-control" id="password" name="password" placeholder="Contraseña">
</div>
<div class="col">
<div class="col-sm-2">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</div>
</form>
</div>
<br />
<div class="row justify-content-center">
<form action="/users/{{.User.Name}}/shell/" method="post">
<div class="row">
<div class="col">
<div class="col-md-5">
<label for="password">Cambia la shell:</label>
</div>
<div class="col">
<div class="col-md-5">
<input type="text" class="form-control" id="shell" name="shell" placeholder="Shell">
</div>
<div class="col">
<div class="col-sm-2">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</div>

Loading…
Cancel
Save