2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-09-25 10:20:31 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
2021-09-25 10:20:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getRetentionChecks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-11-16 18:04:50 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.JSON(w, r, common.RetentionChecks.Get(claims.Role))
|
2021-09-25 10:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startRetentionCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-11-16 18:04:50 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-09-25 10:20:31 +00:00
|
|
|
username := getURLParam(r, "username")
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(username, claims.Role)
|
2021-09-25 10:20:31 +00:00
|
|
|
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 {
|
|
|
|
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 {
|
2023-02-27 18:02:43 +00:00
|
|
|
sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %q", username),
|
2021-09-25 10:20:31 +00:00
|
|
|
http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
2022-08-04 19:50:38 +00:00
|
|
|
go c.Start() //nolint:errcheck
|
2021-09-25 10:20:31 +00:00
|
|
|
sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
|
|
|
|
}
|