The backend for the project formerly known as signet, now known as beignet.
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.

53 regels
1.2 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/imosed/signet/auth"
  6. . "github.com/imosed/signet/data"
  7. "github.com/rs/zerolog/log"
  8. )
  9. type EscalatePrivilegesRequest struct {
  10. Username string
  11. }
  12. func EscalatePrivileges(w http.ResponseWriter, r *http.Request) {
  13. var req EscalatePrivilegesRequest
  14. err := json.NewDecoder(r.Body).Decode(&req)
  15. if err != nil {
  16. log.Error().Err(err).Msg("Could not decode body in EscalatePrivileges call")
  17. return
  18. }
  19. var resp SuccessResponse
  20. var user User
  21. var claims *auth.Claims
  22. claims, err = auth.GetUserClaims(r)
  23. if claims.Privileges == SuperUser {
  24. Db.Table("users").Where("username = ?", req.Username).Find(&user)
  25. if user.Privileges == SuperUser || user.Privileges == AdminPlus {
  26. resp.Success = false
  27. err = json.NewEncoder(w).Encode(resp)
  28. if err != nil {
  29. log.Error().Err(err).Msg("Could not deliver failed escalate privileges response")
  30. }
  31. return
  32. }
  33. user.Privileges = AdminPlus
  34. resp.Success = true
  35. } else {
  36. resp.Success = false
  37. }
  38. err = json.NewEncoder(w).Encode(resp)
  39. if err != nil {
  40. log.Error().Err(err).Msg("Could not deliver successful escalate privileges response")
  41. }
  42. }