2020-11-29 18:35:49 +01:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2021-01-29 18:50:35 +01:00
|
|
|
/*
|
|
|
|
* UpdateRequest.php
|
|
|
|
* Copyright (c) 2021 james@firefly-iii.org
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* 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, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-12-22 05:35:06 +01:00
|
|
|
declare(strict_types=1);
|
2020-11-29 18:35:49 +01:00
|
|
|
|
2021-03-07 12:13:22 +01:00
|
|
|
namespace FireflyIII\Api\V1\Requests\Models\Webhook;
|
2020-11-29 18:35:49 +01:00
|
|
|
|
2025-08-17 07:40:19 +02:00
|
|
|
use FireflyIII\Enums\WebhookResponse;
|
|
|
|
use FireflyIII\Enums\WebhookTrigger;
|
2025-08-20 06:22:55 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2022-09-17 07:07:25 +02:00
|
|
|
use FireflyIII\Models\Webhook;
|
2020-11-29 18:35:49 +01:00
|
|
|
use FireflyIII\Rules\IsBoolean;
|
|
|
|
use FireflyIII\Support\Request\ChecksLogin;
|
|
|
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
2025-08-21 20:07:12 +02:00
|
|
|
use FireflyIII\Support\Request\ValidatesWebhooks;
|
2025-08-17 07:40:19 +02:00
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2020-11-29 18:35:49 +01:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2025-08-17 07:40:19 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class UpdateRequest
|
|
|
|
*/
|
|
|
|
class UpdateRequest extends FormRequest
|
|
|
|
{
|
2022-10-30 14:23:00 +01:00
|
|
|
use ChecksLogin;
|
|
|
|
use ConvertsDataTypes;
|
2025-08-21 20:07:12 +02:00
|
|
|
use ValidatesWebhooks;
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
public function getData(): array
|
|
|
|
{
|
2024-01-01 14:41:31 +01:00
|
|
|
$fields = [
|
2022-05-04 05:53:47 +02:00
|
|
|
'title' => ['title', 'convertString'],
|
2020-12-02 17:12:58 +01:00
|
|
|
'active' => ['active', 'boolean'],
|
2022-05-04 05:53:47 +02:00
|
|
|
'url' => ['url', 'convertString'],
|
2020-12-02 17:12:58 +01:00
|
|
|
];
|
|
|
|
|
2025-08-20 06:22:55 +02:00
|
|
|
$triggers = $this->get('triggers', []);
|
|
|
|
$responses = $this->get('responses', []);
|
|
|
|
$deliveries = $this->get('deliveries', []);
|
|
|
|
|
|
|
|
if (0 === count($triggers) || 0 === count($responses) || 0 === count($deliveries)) {
|
|
|
|
throw new FireflyException('Unexpectedly got no responses, triggers or deliveries.');
|
2021-03-08 09:56:40 +01:00
|
|
|
}
|
2020-12-02 17:12:58 +01:00
|
|
|
|
2025-08-20 06:22:55 +02:00
|
|
|
$return = $this->getAllData($fields);
|
|
|
|
$return['triggers'] = $triggers;
|
|
|
|
$return['responses'] = $responses;
|
|
|
|
$return['deliveries'] = $deliveries;
|
|
|
|
|
2020-12-03 06:54:42 +01:00
|
|
|
return $return;
|
2020-11-29 18:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rules for this request.
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2025-08-20 06:22:55 +02:00
|
|
|
$triggers = implode(',', array_values(Webhook::getTriggers()));
|
|
|
|
$responses = implode(',', array_values(Webhook::getResponses()));
|
|
|
|
$deliveries = implode(',', array_values(Webhook::getDeliveries()));
|
2023-10-22 19:01:18 +02:00
|
|
|
$validProtocols = config('firefly.valid_url_protocols');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-10-31 05:39:22 +01:00
|
|
|
/** @var Webhook $webhook */
|
2024-01-01 14:41:31 +01:00
|
|
|
$webhook = $this->route()->parameter('webhook');
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
return [
|
2024-01-05 09:48:59 +01:00
|
|
|
'title' => sprintf('min:1|max:255|uniqueObjectForUser:webhooks,title,%d', $webhook->id),
|
2022-10-30 14:23:00 +01:00
|
|
|
'active' => [new IsBoolean()],
|
2025-08-20 06:22:55 +02:00
|
|
|
|
|
|
|
'trigger' => 'prohibited',
|
|
|
|
'triggers' => 'required|array|min:1|max:10',
|
|
|
|
'triggers.*' => sprintf('required|in:%s', $triggers),
|
|
|
|
'response' => 'prohibited',
|
|
|
|
'responses' => 'required|array|min:1|max:1',
|
|
|
|
'responses.*' => sprintf('required|in:%s', $responses),
|
|
|
|
'delivery' => 'prohibited',
|
|
|
|
'deliveries' => 'required|array|min:1|max:1',
|
|
|
|
'deliveries.*' => sprintf('required|in:%s', $deliveries),
|
|
|
|
|
2023-10-22 19:01:18 +02:00
|
|
|
'url' => [sprintf('url:%s', $validProtocols), sprintf('uniqueExistingWebhook:%d', $webhook->id)],
|
2020-11-29 18:35:49 +01:00
|
|
|
];
|
|
|
|
}
|
2020-12-22 05:35:06 +01:00
|
|
|
}
|