2018-09-03 10:08:03 +00:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
2022-01-27 00:38:29 +00:00
|
|
|
"errors"
|
2018-09-03 10:08:03 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-10-17 18:29:12 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2018-09-03 10:08:03 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @summary Delete a webhook
|
2021-10-11 23:12:08 +00:00
|
|
|
// @description **Access policy**: authenticated
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
|
|
|
// @tags webhooks
|
|
|
|
// @param id path int true "Webhook id"
|
|
|
|
// @success 202 "Webhook deleted"
|
|
|
|
// @failure 400
|
|
|
|
// @failure 500
|
|
|
|
// @router /webhooks/{id} [delete]
|
2018-09-03 10:08:03 +00:00
|
|
|
func (handler *Handler) webhookDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
id, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid webhook id", err)
|
2018-09-03 10:08:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:38:29 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve user info from request context", err)
|
2022-01-27 00:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !securityContext.IsAdmin {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.Forbidden("Not authorized to delete a webhook", errors.New("not authorized to delete a webhook"))
|
2022-01-27 00:38:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 21:28:07 +00:00
|
|
|
err = handler.DataStore.Webhook().Delete(portainer.WebhookID(id))
|
2018-09-03 10:08:03 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to remove the webhook from the database", err)
|
2018-09-03 10:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|