2020-11-29 18:35:49 +01:00
|
|
|
<?php
|
2021-01-29 18:50:35 +01:00
|
|
|
/*
|
|
|
|
* Webhook.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
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
2021-08-28 15:47:09 +02:00
|
|
|
|
2022-09-17 07:07:25 +02:00
|
|
|
use FireflyIII\Enums\WebhookDelivery;
|
|
|
|
use FireflyIII\Enums\WebhookResponse;
|
|
|
|
use FireflyIII\Enums\WebhookTrigger;
|
2023-12-10 06:51:59 +01:00
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
|
2023-11-05 19:41:37 +01:00
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait;
|
2020-11-29 18:35:49 +01:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2020-12-02 06:54:13 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2020-11-29 18:35:49 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2023-12-10 06:51:59 +01:00
|
|
|
|
2024-07-31 08:26:43 +02:00
|
|
|
/**
|
|
|
|
* @mixin IdeHelperWebhook
|
|
|
|
*/
|
2020-11-29 18:35:49 +01:00
|
|
|
class Webhook extends Model
|
|
|
|
{
|
2023-11-05 19:41:37 +01:00
|
|
|
use ReturnsIntegerIdTrait;
|
|
|
|
use ReturnsIntegerUserIdTrait;
|
2023-11-05 20:18:16 +01:00
|
|
|
use SoftDeletes;
|
2022-04-13 16:27:01 +02:00
|
|
|
|
2020-11-29 18:35:49 +01:00
|
|
|
protected $casts
|
2021-09-18 10:26:12 +02:00
|
|
|
= [
|
2024-03-18 20:25:30 +01:00
|
|
|
'active' => 'boolean',
|
|
|
|
'trigger' => 'integer',
|
|
|
|
'response' => 'integer',
|
|
|
|
'delivery' => 'integer',
|
|
|
|
];
|
2023-08-11 19:37:28 +02:00
|
|
|
protected $fillable = ['active', 'trigger', 'response', 'delivery', 'user_id', 'user_group_id', 'url', 'title', 'secret'];
|
2020-11-29 18:35:49 +01:00
|
|
|
|
2024-11-06 19:22:28 +01:00
|
|
|
protected function casts(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'delivery' => WebhookDelivery::class,
|
|
|
|
'response' => WebhookResponse::class,
|
|
|
|
'trigger' => WebhookTrigger::class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public static function getDeliveries(): array
|
2022-09-17 07:07:25 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
2022-12-29 19:42:26 +01:00
|
|
|
$set = WebhookDelivery::cases();
|
2022-09-17 07:07:25 +02:00
|
|
|
foreach ($set as $item) {
|
|
|
|
$array[$item->value] = $item->name;
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-09-17 07:07:25 +02:00
|
|
|
return $array;
|
|
|
|
}
|
2022-09-18 10:45:38 +02:00
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public static function getDeliveriesForValidation(): array
|
2022-09-18 10:45:38 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
2022-12-29 19:42:26 +01:00
|
|
|
$set = WebhookDelivery::cases();
|
2022-09-18 10:45:38 +02:00
|
|
|
foreach ($set as $item) {
|
2022-12-29 19:42:26 +01:00
|
|
|
$array[$item->name] = $item->value;
|
2022-09-18 10:45:38 +02:00
|
|
|
$array[$item->value] = $item->value;
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-09-18 10:45:38 +02:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public static function getResponses(): array
|
2022-09-18 10:45:38 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
$set = WebhookResponse::cases();
|
|
|
|
foreach ($set as $item) {
|
2022-12-29 19:42:26 +01:00
|
|
|
$array[$item->value] = $item->name;
|
2022-09-18 10:45:38 +02:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-09-18 10:45:38 +02:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public static function getResponsesForValidation(): array
|
2022-09-18 10:45:38 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
2022-12-29 19:42:26 +01:00
|
|
|
$set = WebhookResponse::cases();
|
2022-09-18 10:45:38 +02:00
|
|
|
foreach ($set as $item) {
|
2022-12-29 19:42:26 +01:00
|
|
|
$array[$item->name] = $item->value;
|
2022-09-18 10:45:38 +02:00
|
|
|
$array[$item->value] = $item->value;
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-09-18 10:45:38 +02:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public static function getTriggers(): array
|
2022-09-17 07:07:25 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
2022-12-29 19:42:26 +01:00
|
|
|
$set = WebhookTrigger::cases();
|
2022-09-17 07:07:25 +02:00
|
|
|
foreach ($set as $item) {
|
|
|
|
$array[$item->value] = $item->name;
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-09-17 07:07:25 +02:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public static function getTriggersForValidation(): array
|
2022-09-17 07:07:25 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
2022-12-29 19:42:26 +01:00
|
|
|
$set = WebhookTrigger::cases();
|
2022-09-17 07:07:25 +02:00
|
|
|
foreach ($set as $item) {
|
2022-12-29 19:42:26 +01:00
|
|
|
$array[$item->name] = $item->value;
|
|
|
|
$array[$item->value] = $item->value;
|
2022-09-17 07:07:25 +02:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-09-17 07:07:25 +02:00
|
|
|
return $array;
|
|
|
|
}
|
2022-12-29 19:42:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
|
|
|
*
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
2023-11-04 14:18:49 +01:00
|
|
|
public static function routeBinder(string $value): self
|
2022-12-29 19:42:26 +01:00
|
|
|
{
|
|
|
|
if (auth()->check()) {
|
2024-11-06 19:22:28 +01:00
|
|
|
$webhookId = (int) $value;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
/** @var User $user */
|
2024-11-06 19:32:32 +01:00
|
|
|
$user = auth()->user();
|
2023-12-20 19:35:52 +01:00
|
|
|
|
|
|
|
/** @var null|Webhook $webhook */
|
2024-11-06 19:32:32 +01:00
|
|
|
$webhook = $user->webhooks()->find($webhookId);
|
2022-12-29 19:42:26 +01:00
|
|
|
if (null !== $webhook) {
|
|
|
|
return $webhook;
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function webhookMessages(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(WebhookMessage::class);
|
|
|
|
}
|
2020-12-22 05:35:06 +01:00
|
|
|
}
|