2021-09-25 10:20:31 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getRetentionChecks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
render.JSON(w, r, common.RetentionChecks.Get())
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRetentionCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
username := getURLParam(r, "username")
|
|
|
|
user, err := dataprovider.UserExists(username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var check common.RetentionCheck
|
2021-10-23 13:47:21 +00:00
|
|
|
|
2021-09-25 10:20:31 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &check.Folders)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-10-23 13:47:21 +00:00
|
|
|
|
|
|
|
check.Notifications = getCommaSeparatedQueryParam(r, "notifications")
|
2021-10-03 13:17:49 +00:00
|
|
|
for _, notification := range check.Notifications {
|
|
|
|
if notification == common.RetentionCheckNotificationEmail {
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
check.Email = admin.Email
|
2021-10-02 20:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 10:20:31 +00:00
|
|
|
if err := check.Validate(); err != nil {
|
2021-10-02 20:25:41 +00:00
|
|
|
sendAPIResponse(w, r, err, "Invalid retention check", http.StatusBadRequest)
|
2021-09-25 10:20:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c := common.RetentionChecks.Add(check, &user)
|
|
|
|
if c == nil {
|
|
|
|
sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %#v", username),
|
|
|
|
http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go c.Start()
|
|
|
|
sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
|
|
|
|
}
|