mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
API ability to manage webhooks.
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user