Skip to content

Firefly III: a personal finances manager

API ability to manage webhooks.

James Cole59962005dbb2
diff --git a/app/Api/V1/Controllers/Webhook/CreateController.php b/app/Api/V1/Controllers/Webhook/CreateController.php
new file mode 100644
index 0000000000..293fc9f8b8
--- /dev/null
+++ b/app/Api/V1/Controllers/Webhook/CreateController.php
@@ -0,0 +1,81 @@
+<?php
+/*
+ * CreateController.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Api\V1\Controllers\Webhook;
+
+
+use FireflyIII\Api\V1\Controllers\Controller;
+use FireflyIII\Api\V1\Requests\Webhook\CreateRequest;
+use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
+use FireflyIII\Transformers\WebhookTransformer;
+use FireflyIII\User;
+use Illuminate\Http\JsonResponse;
+use League\Fractal\Resource\Item;
+
+/**
+ * Class CreateController
+ */
+class CreateController extends Controller
+{
+ private WebhookRepositoryInterface $repository;
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ $this->middleware(
+ function ($request, $next) {
+ /** @var User $admin */
+ $admin = auth()->user();
+
+ /** @var WebhookRepositoryInterface repository */
+ $this->repository = app(WebhookRepositoryInterface::class);
+ $this->repository->setUser($admin);
+
+ return $next($request);
+ }
+ );
+ }
+
+ /**
+ * @param CreateRequest $request
+ *
+ * @return JsonResponse
+ */
+ public function store(CreateRequest $request): JsonResponse
+ {
+ $data = $request->getData();
+ $webhook = $this->repository->store($data);
+
+
+ $manager = $this->getManager();
+ /** @var WebhookTransformer $transformer */
+ $transformer = app(WebhookTransformer::class);
+ $transformer->setParameters($this->parameters);
+
+ $resource = new Item($webhook, $transformer, 'webhooks');
+
+ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
+ }
+
+}
\ No newline at end of file
diff --git a/app/Api/V1/Controllers/Webhook/DeleteController.php b/app/Api/V1/Controllers/Webhook/DeleteController.php
new file mode 100644
index 0000000000..2726b8b365
--- /dev/null
+++ b/app/Api/V1/Controllers/Webhook/DeleteController.php
@@ -0,0 +1,74 @@
+<?php
+/*
+ * DeleteController.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Api\V1\Controllers\Webhook;
+
+
+use FireflyIII\Api\V1\Controllers\Controller;
+use FireflyIII\Models\Webhook;
+use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
+use FireflyIII\User;
+use Illuminate\Http\JsonResponse;
+
+/**
+ * Class DeleteController
+ */
+class DeleteController extends Controller
+{
+ private WebhookRepositoryInterface $repository;
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ $this->middleware(
+ function ($request, $next) {
+ /** @var User $admin */
+ $admin = auth()->user();
+
+ /** @var WebhookRepositoryInterface repository */
+ $this->repository = app(WebhookRepositoryInterface::class);
+ $this->repository->setUser($admin);
+
+ return $next($request);
+ }
+ );
+ }
+
+ /**
+ * Remove the specified resource from storage.
+ *
+ * @param Webhook $webhook
+ *
+ * @return JsonResponse
+ * @codeCoverageIgnore
+ */
+ public function destroy(Webhook $webhook): JsonResponse
+ {
+ $this->repository->destroy($webhook);
+
+ return response()->json([], 204);
+ }
+
+
+}
\ No newline at end of file
diff --git a/app/Api/V1/Controllers/Webhook/EditController.php b/app/Api/V1/Controllers/Webhook/EditController.php
new file mode 100644
index 0000000000..611bd8b3ac
--- /dev/null
+++ b/app/Api/V1/Controllers/Webhook/EditController.php
@@ -0,0 +1,82 @@
+<?php
+/*
+ * EditController.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Api\V1\Controllers\Webhook;
+
+
+use FireflyIII\Api\V1\Controllers\Controller;
+use FireflyIII\Api\V1\Requests\Webhook\UpdateRequest;
+use FireflyIII\Models\Webhook;
+use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
+use FireflyIII\Transformers\WebhookTransformer;
+use FireflyIII\User;
+use Illuminate\Http\JsonResponse;
+use League\Fractal\Resource\Item;
+
+/**
+ * Class EditController
+ */
+class EditController extends Controller
+{
+ private WebhookRepositoryInterface $repository;
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ $this->middleware(
+ function ($request, $next) {
+ /** @var User $admin */
+ $admin = auth()->user();
+
+ /** @var WebhookRepositoryInterface repository */
+ $this->repository = app(WebhookRepositoryInterface::class);
+ $this->repository->setUser($admin);
+
+ return $next($request);
+ }
+ );
+ }
+
+ /**
+ * @param Webhook $webhook
+ * @param UpdateRequest $request
+ *
+ * @return JsonResponse
+ */
+ public function update(Webhook $webhook, UpdateRequest $request): JsonResponse
+ {
+ $data = $request->getData();
+ $webhook = $this->repository->update($webhook, $data);
+
+
+ $manager = $this->getManager();
+ /** @var WebhookTransformer $transformer */
+ $transformer = app(WebhookTransformer::class);
+ $transformer->setParameters($this->parameters);
+
+ $resource = new Item($webhook, $transformer, 'webhooks');
+
+ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
+ }
+}
\ No newline at end of file
diff --git a/app/Api/V1/Controllers/Webhook/IndexController.php b/app/Api/V1/Controllers/Webhook/IndexController.php
new file mode 100644
index 0000000000..30b4a9af0e
--- /dev/null
+++ b/app/Api/V1/Controllers/Webhook/IndexController.php
@@ -0,0 +1,87 @@
+<?php
+/*
+ * IndexController.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Api\V1\Controllers\Webhook;
+
+
+use FireflyIII\Api\V1\Controllers\Controller;
+use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
+use FireflyIII\Transformers\WebhookTransformer;
+use FireflyIII\User;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Pagination\LengthAwarePaginator;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection as FractalCollection;
+
+/**
+ * Class IndexController
+ */
+class IndexController extends Controller
+{
+ private WebhookRepositoryInterface $repository;
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ $this->middleware(
+ function ($request, $next) {
+ /** @var User $admin */
+ $admin = auth()->user();
+
+ /** @var WebhookRepositoryInterface repository */
+ $this->repository = app(WebhookRepositoryInterface::class);
+ $this->repository->setUser($admin);
+
+ return $next($request);
+ }
+ );
+ }
+
+
+ /**
+ * Display a listing of the resource.
+ *
+ * @return JsonResponse
+ * @codeCoverageIgnore
+ */
+ public function index(): JsonResponse
+ {
+ $webhooks = $this->repository->all();
+ $manager = $this->getManager();
+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
+ $count = $webhooks->count();
+ $bills = $webhooks->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
+ $paginator = new LengthAwarePaginator($webhooks, $count, $pageSize, $this->parameters->get('page'));
+
+ /** @var WebhookTransformer $transformer */
+ $transformer = app(WebhookTransformer::class);
+ $transformer->setParameters($this->parameters);
+
+ $resource = new FractalCollection($bills, $transformer, 'webhooks');
+ $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
+
+ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
+ }
+
+}
\ No newline at end of file
diff --git a/app/Api/V1/Requests/Webhook/CreateRequest.php b/app/Api/V1/Requests/Webhook/CreateRequest.php
new file mode 100644
index 0000000000..393ca1739a
--- /dev/null
+++ b/app/Api/V1/Requests/Webhook/CreateRequest.php
@@ -0,0 +1,73 @@
+<?php
+/*
+ * CreateRequest.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Api\V1\Requests\Webhook;
+
+use FireflyIII\Rules\IsBoolean;
+use FireflyIII\Support\Request\ChecksLogin;
+use FireflyIII\Support\Request\ConvertsDataTypes;
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * Class CreateRequest
+ */
+class CreateRequest extends FormRequest
+{
+ use ChecksLogin, ConvertsDataTypes;
+
+ /**
+ * @return array
+ */
+ public function getData(): array
+ {
+ $triggers = array_flip(config('firefly.webhooks.triggers'));
+ $responses = array_flip(config('firefly.webhooks.responses'));
+ $deliveries = array_flip(config('firefly.webhooks.deliveries'));
+
+ return [
+ 'active' => $this->boolean('active'),
+ 'trigger' => $triggers[$this->string('trigger')] ?? 0,
+ 'response' => $responses[$this->string('response')] ?? 0,
+ 'delivery' => $deliveries[$this->string('delivery')] ?? 0,
+ 'url' => $this->string('url'),
+ ];
+ }
+
+ /**
+ * Rules for this request.
+ *
+ * @return array
+ */
+ public function rules(): array
+ {
+ $triggers = implode(',', array_values(config('firefly.webhooks.triggers')));
+ $responses = implode(',', array_values(config('firefly.webhooks.responses')));
+ $deliveries = implode(',', array_values(config('firefly.webhooks.deliveries')));
+
+ return [
+ 'active' => [new IsBoolean],
+ 'trigger' => sprintf('required|in:%s', $triggers),
+ 'response' => sprintf('required|in:%s', $responses),
+ 'delivery' => sprintf('required|in:%s', $deliveries),
+ 'url' => ['required', 'url', 'starts_with:https://', 'uniqueWebhook'],
+ ];
+ }
+}
\ No newline at end of file
diff --git a/app/Api/V1/Requests/Webhook/UpdateRequest.php b/app/Api/V1/Requests/Webhook/UpdateRequest.php
new file mode 100644
index 0000000000..b9749f9ea5
--- /dev/null
+++ b/app/Api/V1/Requests/Webhook/UpdateRequest.php
@@ -0,0 +1,74 @@
+<?php
+/*
+ * UpdateRequest.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Api\V1\Requests\Webhook;
+
+use FireflyIII\Rules\IsBoolean;
+use FireflyIII\Support\Request\ChecksLogin;
+use FireflyIII\Support\Request\ConvertsDataTypes;
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * Class UpdateRequest
+ */
+class UpdateRequest extends FormRequest
+{
+ use ChecksLogin, ConvertsDataTypes;
+
+ /**
+ * @return array
+ */
+ public function getData(): array
+ {
+ $triggers = array_flip(config('firefly.webhooks.triggers'));
+ $responses = array_flip(config('firefly.webhooks.responses'));
+ $deliveries = array_flip(config('firefly.webhooks.deliveries'));
+
+ return [
+ 'active' => $this->boolean('active'),
+ 'trigger' => $triggers[$this->string('trigger')] ?? 0,
+ 'response' => $responses[$this->string('response')] ?? 0,
+ 'delivery' => $deliveries[$this->string('delivery')] ?? 0,
+ 'url' => $this->string('url'),
+ ];
+ }
+
+ /**
+ * Rules for this request.
+ *
+ * @return array
+ */
+ public function rules(): array
+ {
+ $triggers = implode(',', array_values(config('firefly.webhooks.triggers')));
+ $responses = implode(',', array_values(config('firefly.webhooks.responses')));
+ $deliveries = implode(',', array_values(config('firefly.webhooks.deliveries')));
+ $webhook = $this->route()->parameter('webhook');
+
+ return [
+ 'active' => [new IsBoolean],
+ 'trigger' => sprintf('required|in:%s', $triggers),
+ 'response' => sprintf('required|in:%s', $responses),
+ 'delivery' => sprintf('required|in:%s', $deliveries),
+ 'url' => ['required', 'url', 'starts_with:https://', sprintf('uniqueExistingWebhook:%d', $webhook->id)],
+ ];
+ }
+}
\ No newline at end of file
diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php
new file mode 100644
index 0000000000..f1cdc919e0
--- /dev/null
+++ b/app/Models/Webhook.php
@@ -0,0 +1,118 @@
+<?php
+/*
+ * Webhook.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Models;
+
+
+use FireflyIII\User;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\SoftDeletes;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+
+/**
+ * Class Webhook
+ *
+ * @property int $id
+ * @property \Illuminate\Support\Carbon|null $created_at
+ * @property \Illuminate\Support\Carbon|null $updated_at
+ * @property string|null $deleted_at
+ * @property int $user_id
+ * @property bool $active
+ * @property int $trigger
+ * @property int $response
+ * @property int $delivery
+ * @property string $url
+ * @property-read User $user
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook newModelQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook newQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook query()
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereActive($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereCreatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereDeletedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereDelivery($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereResponse($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereTrigger($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereUpdatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereUrl($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Webhook whereUserId($value)
+ * @mixin \Eloquent
+ */
+class Webhook extends Model
+{
+ use SoftDeletes;
+ // dont forget to update the config in firefly.php
+ // triggers
+ public const TRIGGER_CREATE_TRANSACTION = 100;
+ public const TRIGGER_UPDATE_TRANSACTION = 110;
+ public const TRIGGER_DELETE_TRANSACTION = 120;
+
+ // actions
+ public const MESSAGE_TRANSACTIONS = 200;
+ public const MESSAGE_ACCOUNTS = 210;
+
+ // delivery
+ public const DELIVERY_JSON = 300;
+
+ protected $fillable = ['active', 'trigger', 'response', 'delivery', 'user_id', 'url'];
+
+ protected $casts
+ = [
+ 'active' => 'boolean',
+ 'trigger' => 'integer',
+ 'response' => 'integer',
+ 'delivery' => 'integer',
+ ];
+
+ /**
+ * Route binder. Converts the key in the URL to the specified object (or throw 404).
+ *
+ * @param string $value
+ *
+ * @throws NotFoundHttpException
+ * @return Webhook
+ */
+ public static function routeBinder(string $value): Webhook
+ {
+ if (auth()->check()) {
+ $budgetId = (int) $value;
+ /** @var User $user */
+ $user = auth()->user();
+ /** @var Webhook $webhook */
+ $webhook = $user->webhooks()->find($budgetId);
+ if (null !== $webhook) {
+ return $webhook;
+ }
+ }
+ throw new NotFoundHttpException;
+ }
+
+ /**
+ * @codeCoverageIgnore
+ * @return BelongsTo
+ */
+ public function user(): BelongsTo
+ {
+ return $this->belongsTo(User::class);
+ }
+
+}
\ No newline at end of file
diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php
index a14ca6f7ed..e1bcfde49e 100644
--- a/app/Providers/FireflyServiceProvider.php
+++ b/app/Providers/FireflyServiceProvider.php
@@ -44,6 +44,8 @@ use FireflyIII\Repositories\TransactionType\TransactionTypeRepository;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
use FireflyIII\Repositories\User\UserRepository;
use FireflyIII\Repositories\User\UserRepositoryInterface;
+use FireflyIII\Repositories\Webhook\WebhookRepository;
+use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
use FireflyIII\Services\FireflyIIIOrg\Update\UpdateRequest;
use FireflyIII\Services\FireflyIIIOrg\Update\UpdateRequestInterface;
use FireflyIII\Services\Password\PwndVerifierV2;
@@ -190,6 +192,19 @@ class FireflyServiceProvider extends ServiceProvider
}
);
+ $this->app->bind(
+ WebhookRepositoryInterface::class,
+ static function (Application $app) {
+ /** @var WebhookRepository $repository */
+ $repository = app(WebhookRepository::class);
+ if ($app->auth->check()) {
+ $repository->setUser(auth()->user());
+ }
+
+ return $repository;
+ }
+ );
+
$this->app->bind(
RuleEngineInterface::class,
static function (Application $app) {
diff --git a/app/Repositories/Webhook/WebhookRepository.php b/app/Repositories/Webhook/WebhookRepository.php
new file mode 100644
index 0000000000..aa2272796b
--- /dev/null
+++ b/app/Repositories/Webhook/WebhookRepository.php
@@ -0,0 +1,90 @@
+<?php
+/*
+ * WebhookRepository.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Repositories\Webhook;
+
+use FireflyIII\Models\Webhook;
+use FireflyIII\User;
+use Illuminate\Support\Collection;
+
+/**
+ * Class WebhookRepository
+ */
+class WebhookRepository implements WebhookRepositoryInterface
+{
+ private User $user;
+
+ /**
+ * @inheritDoc
+ */
+ public function all(): Collection
+ {
+ return $this->user->webhooks()->get();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function setUser(User $user): void
+ {
+ $this->user = $user;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function store(array $data): Webhook
+ {
+ $fullData = [
+ 'user_id' => $this->user->id,
+ 'active' => $data['active'],
+ 'trigger' => $data['trigger'],
+ 'response' => $data['response'],
+ 'delivery' => $data['delivery'],
+ 'url' => $data['url'],
+ ];
+
+ return Webhook::create($fullData);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function update(Webhook $webhook, array $data): Webhook
+ {
+ $webhook->active = $data['active'] ?? $webhook->active;
+ $webhook->trigger = $data['trigger'] ?? $webhook->trigger;
+ $webhook->response = $data['response'] ?? $webhook->response;
+ $webhook->delivery = $data['delivery'] ?? $webhook->delivery;
+ $webhook->url = $data['url'] ?? $webhook->url;
+ $webhook->save();
+
+ return $webhook;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function destroy(Webhook $webhook): void
+ {
+ $webhook->delete();
+ }
+}
\ No newline at end of file
diff --git a/app/Repositories/Webhook/WebhookRepositoryInterface.php b/app/Repositories/Webhook/WebhookRepositoryInterface.php
new file mode 100644
index 0000000000..70486b7efc
--- /dev/null
+++ b/app/Repositories/Webhook/WebhookRepositoryInterface.php
@@ -0,0 +1,67 @@
+<?php
+/*
+ * WebhookRepositoryInterface.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Repositories\Webhook;
+
+use FireflyIII\Models\Webhook;
+use FireflyIII\User;
+use Illuminate\Support\Collection;
+
+/**
+ * Interface WebhookRepositoryInterface
+ */
+interface WebhookRepositoryInterface
+{
+ /**
+ * Return all webhooks.
+ *
+ * @return Collection
+ */
+ public function all(): Collection;
+
+ /**
+ * Set user.
+ *
+ * @param User $user
+ */
+ public function setUser(User $user): void;
+
+ /**
+ * @param array $data
+ *
+ * @return Webhook
+ */
+ public function store(array $data): Webhook;
+
+ /**
+ * @param Webhook $webhook
+ * @param array $data
+ *
+ * @return Webhook
+ */
+ public function update(Webhook $webhook, array $data): Webhook;
+
+ /**
+ * @param Webhook $webhook
+ */
+ public function destroy(Webhook $webhook): void;
+
+}
\ No newline at end of file
diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php
index 1786a5e451..3bd96346f4 100644
--- a/app/Transformers/UserTransformer.php
+++ b/app/Transformers/UserTransformer.php
@@ -36,19 +36,6 @@ class UserTransformer extends AbstractTransformer
/** @var UserRepositoryInterface */
private $repository;
- /**
- * UserTransformer constructor.
- *
- * @codeCoverageIgnore
- */
- public function __construct()
- {
- $this->repository = app(UserRepositoryInterface::class);
- if ('testing' === config('app.env')) {
- Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
- }
- }
-
/**
* Transform user.
*
diff --git a/app/Transformers/WebhookTransformer.php b/app/Transformers/WebhookTransformer.php
new file mode 100644
index 0000000000..6a9ebfe07b
--- /dev/null
+++ b/app/Transformers/WebhookTransformer.php
@@ -0,0 +1,73 @@
+<?php
+/*
+ * WebhookTransformer.php
+ * Copyright (c) 2020 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Transformers;
+
+use FireflyIII\Models\Webhook;
+
+/**
+ * Class WebhookTransformer
+ */
+class WebhookTransformer extends AbstractTransformer
+{
+ private array $enums;
+
+ /**
+ * WebhookTransformer constructor.
+ */
+ public function __construct()
+ {
+ // array merge kills the keys
+ $this->enums = config('firefly.webhooks.triggers') + config('firefly.webhooks.responses') + config('firefly.webhooks.deliveries');
+ }
+
+ /**
+ * Transform webhook.
+ *
+ * @param Webhook $webhook
+ *
+ * @return array
+ */
+ public function transform(Webhook $webhook): array
+ {
+ return [
+ 'id' => (int)$webhook->id,
+ 'created_at' => $webhook->created_at->toAtomString(),
+ 'updated_at' => $webhook->updated_at->toAtomString(),
+ 'active' => $webhook->active,
+ 'trigger' => $this->getEnum($webhook->trigger),
+ 'response' => $this->getEnum($webhook->response),
+ 'delivery' => $this->getEnum($webhook->delivery),
+ 'url' => $webhook->url,
+ 'links' => [
+ [
+ 'rel' => 'self',
+ 'uri' => sprintf('/webhooks/%d', $webhook->id),
+ ],
+ ],
+ ];
+ }
+
+ private function getEnum(int $value)
+ {
+ return $this->enums[$value] ?? 'UNKNOWN_VALUE';
+ }
+}
\ No newline at end of file
diff --git a/app/User.php b/app/User.php
index b46196932b..91b37f202d 100644
--- a/app/User.php
+++ b/app/User.php
@@ -45,6 +45,7 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\Webhook;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -64,42 +65,42 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class User.
*
- * @property int $id
- * @property string $email
- * @property bool $isAdmin used in admin user
+ * @property int $id
+ * @property string $email
+ * @property bool $isAdmin used in admin user
* controller.
- * @property bool $has2FA used in admin user
+ * @property bool $has2FA used in admin user
* controller.
- * @property array $prefs used in admin user
+ * @property array $prefs used in admin user
* controller.
- * @property string password
- * @property string $mfa_secret
- * @property Collection roles
- * @property string blocked_code
- * @property bool blocked
- * @property Carbon|null $created_at
- * @property Carbon|null $updated_at
- * @property string|null $remember_token
- * @property string|null $reset
- * @property-read \Illuminate\Database\Eloquent\Collection|Account[] $accounts
- * @property-read \Illuminate\Database\Eloquent\Collection|Attachment[] $attachments
- * @property-read \Illuminate\Database\Eloquent\Collection|AvailableBudget[] $availableBudgets
- * @property-read \Illuminate\Database\Eloquent\Collection|Bill[] $bills
- * @property-read \Illuminate\Database\Eloquent\Collection|Budget[] $budgets
- * @property-read \Illuminate\Database\Eloquent\Collection|Category[] $categories
- * @property-read \Illuminate\Database\Eloquent\Collection|Client[] $clients
- * @property-read \Illuminate\Database\Eloquent\Collection|CurrencyExchangeRate[] $currencyExchangeRates
- * @property-read DatabaseNotificationCollection|DatabaseNotification[] $notifications
- * @property-read \Illuminate\Database\Eloquent\Collection|PiggyBank[] $piggyBanks
- * @property-read \Illuminate\Database\Eloquent\Collection|Preference[] $preferences
- * @property-read \Illuminate\Database\Eloquent\Collection|Recurrence[] $recurrences
- * @property-read \Illuminate\Database\Eloquent\Collection|RuleGroup[] $ruleGroups
- * @property-read \Illuminate\Database\Eloquent\Collection|Rule[] $rules
- * @property-read \Illuminate\Database\Eloquent\Collection|Tag[] $tags
- * @property-read \Illuminate\Database\Eloquent\Collection|Token[] $tokens
- * @property-read \Illuminate\Database\Eloquent\Collection|TransactionGroup[] $transactionGroups
- * @property-read \Illuminate\Database\Eloquent\Collection|TransactionJournal[] $transactionJournals
- * @property-read \Illuminate\Database\Eloquent\Collection|Transaction[] $transactions
+ * @property string password
+ * @property string $mfa_secret
+ * @property Collection roles
+ * @property string blocked_code
+ * @property bool blocked
+ * @property Carbon|null $created_at
+ * @property Carbon|null $updated_at
+ * @property string|null $remember_token
+ * @property string|null $reset
+ * @property-read \Illuminate\Database\Eloquent\Collection|Account[] $accounts
+ * @property-read \Illuminate\Database\Eloquent\Collection|Attachment[] $attachments
+ * @property-read \Illuminate\Database\Eloquent\Collection|AvailableBudget[] $availableBudgets
+ * @property-read \Illuminate\Database\Eloquent\Collection|Bill[] $bills
+ * @property-read \Illuminate\Database\Eloquent\Collection|Budget[] $budgets
+ * @property-read \Illuminate\Database\Eloquent\Collection|Category[] $categories
+ * @property-read \Illuminate\Database\Eloquent\Collection|Client[] $clients
+ * @property-read \Illuminate\Database\Eloquent\Collection|CurrencyExchangeRate[] $currencyExchangeRates
+ * @property-read DatabaseNotificationCollection|DatabaseNotification[] $notifications
+ * @property-read \Illuminate\Database\Eloquent\Collection|PiggyBank[] $piggyBanks
+ * @property-read \Illuminate\Database\Eloquent\Collection|Preference[] $preferences
+ * @property-read \Illuminate\Database\Eloquent\Collection|Recurrence[] $recurrences
+ * @property-read \Illuminate\Database\Eloquent\Collection|RuleGroup[] $ruleGroups
+ * @property-read \Illuminate\Database\Eloquent\Collection|Rule[] $rules
+ * @property-read \Illuminate\Database\Eloquent\Collection|Tag[] $tags
+ * @property-read \Illuminate\Database\Eloquent\Collection|Token[] $tokens
+ * @property-read \Illuminate\Database\Eloquent\Collection|TransactionGroup[] $transactionGroups
+ * @property-read \Illuminate\Database\Eloquent\Collection|TransactionJournal[] $transactionJournals
+ * @property-read \Illuminate\Database\Eloquent\Collection|Transaction[] $transactions
* @method static Builder|User newModelQuery()
* @method static Builder|User newQuery()
* @method static Builder|User query()
@@ -113,31 +114,35 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static Builder|User whereReset($value)
* @method static Builder|User whereUpdatedAt($value)
* @mixin Eloquent
- * @property string|null $objectguid
- * @property-read int|null $accounts_count
- * @property-read int|null $attachments_count
- * @property-read int|null $available_budgets_count
- * @property-read int|null $bills_count
- * @property-read int|null $budgets_count
- * @property-read int|null $categories_count
- * @property-read int|null $clients_count
- * @property-read int|null $currency_exchange_rates_count
- * @property-read int|null $notifications_count
- * @property-read int|null $piggy_banks_count
- * @property-read int|null $preferences_count
- * @property-read int|null $recurrences_count
- * @property-read int|null $roles_count
- * @property-read int|null $rule_groups_count
- * @property-read int|null $rules_count
- * @property-read int|null $tags_count
- * @property-read int|null $tokens_count
- * @property-read int|null $transaction_groups_count
- * @property-read int|null $transaction_journals_count
- * @property-read int|null $transactions_count
+ * @property string|null $objectguid
+ * @property-read int|null $accounts_count
+ * @property-read int|null $attachments_count
+ * @property-read int|null $available_budgets_count
+ * @property-read int|null $bills_count
+ * @property-read int|null $budgets_count
+ * @property-read int|null $categories_count
+ * @property-read int|null $clients_count
+ * @property-read int|null $currency_exchange_rates_count
+ * @property-read int|null $notifications_count
+ * @property-read int|null $piggy_banks_count
+ * @property-read int|null $preferences_count
+ * @property-read int|null $recurrences_count
+ * @property-read int|null $roles_count
+ * @property-read int|null $rule_groups_count
+ * @property-read int|null $rules_count
+ * @property-read int|null $tags_count
+ * @property-read int|null $tokens_count
+ * @property-read int|null $transaction_groups_count
+ * @property-read int|null $transaction_journals_count
+ * @property-read int|null $transactions_count
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\User whereMfaSecret($value)
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\User whereObjectguid($value)
- * @property string|null $provider
+ * @property string|null $provider
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\User whereProvider($value)
+ * @property-read \Illuminate\Database\Eloquent\Collection|ObjectGroup[] $objectGroups
+ * @property-read int|null $object_groups_count
+ * @property-read \Illuminate\Database\Eloquent\Collection|Webhook[] $webhooks
+ * @property-read int|null $webhooks_count
*/
class User extends Authenticatable
{
@@ -213,6 +218,18 @@ class User extends Authenticatable
return $this->hasMany(Attachment::class);
}
+ /**
+ * @codeCoverageIgnore
+ *
+ * Link to webhooks
+ *
+ * @return HasMany
+ */
+ public function webhooks(): HasMany
+ {
+ return $this->hasMany(Webhook::class);
+ }
+
/**
* @param string $role
*
diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php
index 7a86b7787a..89b0b59cc5 100644
--- a/app/Validation/FireflyValidator.php
+++ b/app/Validation/FireflyValidator.php
@@ -31,6 +31,7 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\TransactionType;
+use FireflyIII\Models\Webhook;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
@@ -77,7 +78,7 @@ class FireflyValidator extends Validator
{
$field = $parameters[1] ?? 'id';
- if (0 === (int) $value) {
+ if (0 === (int)$value) {
return true;
}
$count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count();
@@ -180,13 +181,14 @@ class FireflyValidator extends Validator
$iban = str_replace($search, $replace, $iban);
$checksum = bcmod($iban, '97');
- return 1 === (int) $checksum;
+ return 1 === (int)$checksum;
}
/**
* @param $attribute
* @param $value
* @param $parameters
+ *
* @return bool
*/
public function validateLess($attribute, $value, $parameters): bool
@@ -194,13 +196,14 @@ class FireflyValidator extends Validator
/** @var mixed $compare */
$compare = $parameters[0] ?? '0';
- return bccomp((string) $value, (string) $compare) < 0;
+ return bccomp((string)$value, (string)$compare) < 0;
}
/**
* @param $attribute
* @param $value
* @param $parameters
+ *
* @return bool
*/
public function validateMore($attribute, $value, $parameters): bool
@@ -208,7 +211,7 @@ class FireflyValidator extends Validator
/** @var mixed $compare */
$compare = $parameters[0] ?? '0';
- return bccomp((string) $value, (string) $compare) > 0;
+ return bccomp((string)$value, (string)$compare) > 0;
}
/**
@@ -222,7 +225,7 @@ class FireflyValidator extends Validator
{
$field = $parameters[1] ?? 'id';
- if (0 === (int) $value) {
+ if (0 === (int)$value) {
return true;
}
$count = DB::table($parameters[0])->where($field, $value)->count();
@@ -242,7 +245,7 @@ class FireflyValidator extends Validator
// first, get the index from this string:
$value = $value ?? '';
$parts = explode('.', $attribute);
- $index = (int) ($parts[1] ?? '0');
+ $index = (int)($parts[1] ?? '0');
// get the name of the trigger from the data array:
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
@@ -293,7 +296,8 @@ class FireflyValidator extends Validator
if ('update_piggy' === $actionType) {
/** @var PiggyBankRepositoryInterface $repository */
$repository = app(PiggyBankRepositoryInterface::class);
- $piggy = $repository->findByName($value);
+ $piggy = $repository->findByName($value);
+
return null !== $piggy;
}
@@ -313,7 +317,7 @@ class FireflyValidator extends Validator
{
// first, get the index from this string:
$parts = explode('.', $attribute);
- $index = (int) ($parts[1] ?? '0');
+ $index = (int)($parts[1] ?? '0');
// get the name of the trigger from the data array:
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
@@ -330,8 +334,10 @@ class FireflyValidator extends Validator
}
// these trigger types need a simple strlen check:
- $length = ['source_account_starts', 'source_account_ends', 'source_account_is', 'source_account_contains', 'destination_account_starts', 'destination_account_ends',
- 'destination_account_is', 'destination_account_contains', 'description_starts', 'description_ends', 'description_contains', 'description_is', 'category_is',
+ $length = ['source_account_starts', 'source_account_ends', 'source_account_is', 'source_account_contains', 'destination_account_starts',
+ 'destination_account_ends',
+ 'destination_account_is', 'destination_account_contains', 'description_starts', 'description_ends', 'description_contains', 'description_is',
+ 'category_is',
'budget_is', 'tag_is', 'currency_is', 'notes_contain', 'notes_start', 'notes_end', 'notes_are',];
if (in_array($triggerType, $length, true)) {
return '' !== $value;
@@ -339,7 +345,7 @@ class FireflyValidator extends Validator
// check if it's an existing account.
if (in_array($triggerType, ['destination_account_id', 'source_account_id'])) {
- return is_numeric($value) && (int) $value > 0;
+ return is_numeric($value) && (int)$value > 0;
}
// check transaction type.
@@ -362,6 +368,7 @@ class FireflyValidator extends Validator
return false;
}
}
+
return true;
}
@@ -376,7 +383,7 @@ class FireflyValidator extends Validator
{
$verify = false;
if (isset($this->data['verify_password'])) {
- $verify = 1 === (int) $this->data['verify_password'];
+ $verify = 1 === (int)$this->data['verify_password'];
}
if ($verify) {
/** @var Verifier $service */
@@ -414,7 +421,7 @@ class FireflyValidator extends Validator
}
$parameterId = $parameters[0] ?? null;
if (null !== $parameterId) {
- return $this->validateByParameterId((int) $parameterId, $value);
+ return $this->validateByParameterId((int)$parameterId, $value);
}
if (isset($this->data['id'])) {
return $this->validateByAccountId($value);
@@ -433,9 +440,9 @@ class FireflyValidator extends Validator
*/
public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool
{
- $accountId = (int) ($this->data['id'] ?? 0.0);
+ $accountId = (int)($this->data['id'] ?? 0.0);
if (0 === $accountId) {
- $accountId = (int) ($parameters[0] ?? 0.0);
+ $accountId = (int)($parameters[0] ?? 0.0);
}
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
@@ -476,15 +483,15 @@ class FireflyValidator extends Validator
public function validateUniqueObjectForUser($attribute, $value, $parameters): bool
{
[$table, $field] = $parameters;
- $exclude = (int) ($parameters[2] ?? 0.0);
+ $exclude = (int)($parameters[2] ?? 0.0);
/*
* If other data (in $this->getData()) contains
* ID field, set that field to be the $exclude.
*/
$data = $this->getData();
- if (!isset($parameters[2]) && isset($data['id']) && (int) $data['id'] > 0) {
- $exclude = (int) $data['id'];
+ if (!isset($parameters[2]) && isset($data['id']) && (int)$data['id'] > 0) {
+ $exclude = (int)$data['id'];
}
@@ -518,7 +525,7 @@ class FireflyValidator extends Validator
->where('object_groups.user_id', auth()->user()->id)
->where('object_groups.title', $value);
if (null !== $exclude) {
- $query->where('object_groups.id', '!=', (int) $exclude);
+ $query->where('object_groups.id', '!=', (int)$exclude);
}
return 0 === $query->count();
@@ -540,7 +547,7 @@ class FireflyValidator extends Validator
$query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
if (null !== $exclude) {
- $query->where('piggy_banks.id', '!=', (int) $exclude);
+ $query->where('piggy_banks.id', '!=', (int)$exclude);
}
$set = $query->get(['piggy_banks.*']);
@@ -624,6 +631,67 @@ class FireflyValidator extends Validator
return null === $entry;
}
+ public function validateUniqueExistingWebhook($value, $parameters, $something): bool
+ {
+ $existingId = (int)($something[0] ?? 0);
+
+ if (auth()->check()) {
+ // possible values
+ $triggers = array_flip(config('firefly.webhooks.triggers'));
+ $responses = array_flip(config('firefly.webhooks.responses'));
+ $deliveries = array_flip(config('firefly.webhooks.deliveries'));
+
+ // integers
+ $trigger = $triggers[$this->data['trigger']] ?? 0;
+ $response = $responses[$this->data['response']] ?? 0;
+ $delivery = $deliveries[$this->data['delivery']] ?? 0;
+ $url = $this->data['url'];
+ $userId = auth()->user()->id;
+
+ return 0 === Webhook::whereUserId($userId)
+ ->where('trigger', $trigger)
+ ->where('response', $response)
+ ->where('delivery', $delivery)
+ ->where('id', '!=', $existingId)
+ ->where('url', $url)->count();
+ }
+ return false;
+ }
+
+ /**
+ * @param $value
+ * @param $parameters
+ *
+ * @return bool
+ */
+ public function validateUniqueWebhook($value, $parameters): bool
+ {
+ if (auth()->check()) {
+ // possible values
+ $triggers = array_flip(config('firefly.webhooks.triggers'));
+ $responses = array_flip(config('firefly.webhooks.responses'));
+ $deliveries = array_flip(config('firefly.webhooks.deliveries'));
+
+ // integers
+ $trigger = $triggers[$this->data['trigger']] ?? 0;
+ $response = $responses[$this->data['response']] ?? 0;
+ $delivery = $deliveries[$this->data['delivery']] ?? 0;
+ $url = $this->data['url'];
+ $userId = auth()->user()->id;
+
+ return 0 === Webhook::whereUserId($userId)
+ ->where('trigger', $trigger)
+ ->where('response', $response)
+ ->where('delivery', $delivery)
+ ->where('url', $url)->count();
+ // find similar webhook for user:
+ //= var_dump($this->data);
+ //exit;
+ }
+
+ return false;
+ }
+
/**
* @param $value
* @param $parameters
@@ -633,7 +701,7 @@ class FireflyValidator extends Validator
private function validateByAccountTypeId($value, $parameters): bool
{
$type = AccountType::find($this->data['account_type_id'])->first();
- $ignore = (int) ($parameters[0] ?? 0.0);
+ $ignore = (int)($parameters[0] ?? 0.0);
/** @var Collection $set */
$set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
@@ -667,7 +735,7 @@ class FireflyValidator extends Validator
/** @var Collection $accountTypes */
$accountTypes = AccountType::whereIn('type', $search)->get();
- $ignore = (int) ($parameters[0] ?? 0.0);
+ $ignore = (int)($parameters[0] ?? 0.0);
$accountTypeIds = $accountTypes->pluck('id')->toArray();
/** @var Collection $set */
$set = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)->get();
@@ -684,6 +752,7 @@ class FireflyValidator extends Validator
/**
* @param string $value
+ *
* @return bool
*/
private function validateByAccountName(string $value): bool
diff --git a/config/firefly.php b/config/firefly.php
index 7791da39a1..540fe6e837 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -44,6 +44,7 @@ use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Models\TransactionType as TransactionTypeModel;
+use FireflyIII\Models\Webhook;
use FireflyIII\Support\Binder\AccountList;
use FireflyIII\Support\Binder\BudgetList;
use FireflyIII\Support\Binder\CategoryList;
@@ -377,6 +378,7 @@ return [
'ruleGroup' => RuleGroup::class,
'transactionGroup' => TransactionGroup::class,
'user' => User::class,
+ 'webhook' => Webhook::class,
// strings
'currency_code' => CurrencyCode::class,
@@ -820,4 +822,18 @@ return [
// recurring transactions
'recurrence_total', 'recurrence_count',
],
+ 'webhooks' => [
+ 'triggers' => [
+ 100 => 'TRIGGER_CREATE_TRANSACTION',
+ 110 => 'TRIGGER_UPDATE_TRANSACTION',
+ 120 => 'TRIGGER_DELETE_TRANSACTION',
+ ],
+ 'responses' => [
+ 200 => 'RESPONSE_TRANSACTIONS',
+ 210 => 'RESPONSE_ACCOUNTS',
+ ],
+ 'deliveries' => [
+ 300 => 'DELIVERY_JSON',
+ ],
+ ],
];
diff --git a/database/migrations/2020_11_12_070604_changes_for_v550.php b/database/migrations/2020_11_12_070604_changes_for_v550.php
index 1f47cf2abb..e0bcdf6f68 100644
--- a/database/migrations/2020_11_12_070604_changes_for_v550.php
+++ b/database/migrations/2020_11_12_070604_changes_for_v550.php
@@ -97,8 +97,6 @@ class ChangesForV550 extends Migration
'budget_transaction_journal', function (Blueprint $table) {
$table->integer('budget_limit_id', false, true)->nullable()->default(null)->after('budget_id');
$table->foreign('budget_limit_id','budget_id_foreign')->references('id')->on('budget_limits')->onDelete('set null');
-
-
}
);
@@ -117,15 +115,16 @@ class ChangesForV550 extends Migration
'webhooks',
static function (Blueprint $table) {
$table->increments('id');
- $table->integer('user_id', false, true);
+ $table->timestamps();
$table->softDeletes();
+ $table->integer('user_id', false, true);
$table->boolean('active')->default(true);
- $table->string('trigger',32);
- $table->string('response', 32);
- $table->string('delivery',32);
- $table->string('url',512);
-
+ $table->unsignedSmallInteger('trigger',false);
+ $table->unsignedSmallInteger('response', false);
+ $table->unsignedSmallInteger('delivery',false);
+ $table->string('url',512)->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
+ $table->unique(['user_id','trigger','response','delivery','url']);
}
);
@@ -134,8 +133,9 @@ class ChangesForV550 extends Migration
'webhook_messages',
static function (Blueprint $table) {
$table->increments('id');
- $table->integer('webhook_id', false, true);
+ $table->timestamps();
$table->softDeletes();
+ $table->integer('webhook_id', false, true);
$table->boolean('sent')->default(false);
$table->boolean('errored')->default(false);
$table->string('uuid',64);
diff --git a/routes/api.php b/routes/api.php
index d0389c3e21..0e22242e48 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -385,6 +385,23 @@ Route::group(
}
);
+Route::group(
+ ['namespace' => 'FireflyIII\Api\V1\Controllers\Webhook', 'prefix' => 'webhooks',
+ 'as' => 'api.v1.webhooks.',],
+ static function () {
+
+ // Webhook API routes:
+ Route::get('', ['uses' => 'IndexController@index', 'as' => 'index']);
+
+ // create new one.
+ Route::post('', ['uses' => 'CreateController@store', 'as' => 'store']);
+
+ // update
+ Route::put('{webhook}', ['uses' => 'EditController@update', 'as' => 'update']);
+ Route::delete('{webhook}', ['uses' => 'DeleteController@destroy', 'as' => 'destroy']);
+ }
+);
+
Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'summary',
'as' => 'api.v1.summary.',],