api_retention.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "fmt"
  17. "net/http"
  18. "github.com/go-chi/render"
  19. "github.com/drakkan/sftpgo/v2/internal/common"
  20. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  21. )
  22. func getRetentionChecks(w http.ResponseWriter, r *http.Request) {
  23. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  24. claims, err := getTokenClaims(r)
  25. if err != nil || claims.Username == "" {
  26. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  27. return
  28. }
  29. render.JSON(w, r, common.RetentionChecks.Get(claims.Role))
  30. }
  31. func startRetentionCheck(w http.ResponseWriter, r *http.Request) {
  32. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  33. claims, err := getTokenClaims(r)
  34. if err != nil || claims.Username == "" {
  35. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  36. return
  37. }
  38. username := getURLParam(r, "username")
  39. user, err := dataprovider.GetUserWithGroupSettings(username, claims.Role)
  40. if err != nil {
  41. sendAPIResponse(w, r, err, "", getRespStatus(err))
  42. return
  43. }
  44. var check common.RetentionCheck
  45. err = render.DecodeJSON(r.Body, &check.Folders)
  46. if err != nil {
  47. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  48. return
  49. }
  50. check.Notifications = getCommaSeparatedQueryParam(r, "notifications")
  51. for _, notification := range check.Notifications {
  52. if notification == common.RetentionCheckNotificationEmail {
  53. admin, err := dataprovider.AdminExists(claims.Username)
  54. if err != nil {
  55. sendAPIResponse(w, r, err, "", getRespStatus(err))
  56. return
  57. }
  58. check.Email = admin.Email
  59. }
  60. }
  61. if err := check.Validate(); err != nil {
  62. sendAPIResponse(w, r, err, "Invalid retention check", http.StatusBadRequest)
  63. return
  64. }
  65. c := common.RetentionChecks.Add(check, &user)
  66. if c == nil {
  67. sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %q", username),
  68. http.StatusConflict)
  69. return
  70. }
  71. go c.Start() //nolint:errcheck
  72. sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
  73. }