The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

53 rader
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. }