Force account search to validate it did not just find the source account. #10920

This commit is contained in:
James Cole
2025-09-17 07:09:40 +02:00
parent cb6b3d5f85
commit 3491fbb99d
8 changed files with 126 additions and 100 deletions

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Validation\Account;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Illuminate\Support\Facades\Log;
/**
* Trait DepositValidation
@@ -40,7 +41,7 @@ trait DepositValidation
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
app('log')->debug('Now in validateDepositDestination', $array);
Log::debug('Now in validateDepositDestination', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
@@ -48,12 +49,12 @@ trait DepositValidation
// if both values are NULL we return false,
// because the destination of a deposit can't be created.
$this->destError = (string) trans('validation.deposit_dest_need_data');
app('log')->error('Both values are NULL, cant create deposit destination.');
Log::error('Both values are NULL, cant create deposit destination.');
$result = false;
}
// if the account can be created anyway we don't need to search.
if (null === $result && true === $this->canCreateTypes($validTypes)) {
app('log')->debug('Can create some of these types, so return true.');
Log::debug('Can create some of these types, so return true.');
$result = true;
}
@@ -61,17 +62,17 @@ trait DepositValidation
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
app('log')->debug('findExistingAccount() returned NULL, so the result is false.');
Log::debug('findExistingAccount() returned NULL, so the result is false.');
$this->destError = (string) trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$result = false;
}
if (null !== $search) {
app('log')->debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
Log::debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
$this->setDestination($search);
$result = true;
}
}
app('log')->debug(sprintf('validateDepositDestination will return %s', var_export($result, true)));
Log::debug(sprintf('validateDepositDestination will return %s', var_export($result, true)));
return $result;
}
@@ -92,7 +93,7 @@ trait DepositValidation
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
app('log')->debug('Now in validateDepositSource', $array);
Log::debug('Now in validateDepositSource', $array);
// null = we found nothing at all or didn't even search
// false = invalid results
@@ -114,7 +115,7 @@ trait DepositValidation
// if there is an iban, it can only be in use by a valid source type, or we will fail.
if (null !== $accountIban && '' !== $accountIban) {
app('log')->debug('Check if there is not already another account with this IBAN');
Log::debug('Check if there is not already another account with this IBAN');
$existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true);
if (null !== $existing) {
$this->sourceError = (string) trans('validation.deposit_src_iban_exists');
@@ -128,11 +129,14 @@ trait DepositValidation
if (null !== $accountId) {
$search = $this->accountRepository->find($accountId);
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug(sprintf('User submitted an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type));
app('log')->debug(sprintf('Firefly III accepts ID #%d as valid account data.', $accountId));
Log::debug(sprintf('User submitted an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type));
Log::debug(sprintf('Firefly III does not accept ID #%d as valid account data.', $accountId));
// #10921 Set result false
$this->sourceError = (string) trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
$result = false;
}
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug('ID result is not null and seems valid, save as source account.');
Log::debug('ID result is not null and seems valid, save as source account.');
$this->setSource($search);
$result = true;
}
@@ -142,11 +146,11 @@ trait DepositValidation
if (null !== $accountIban) {
$search = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type));
Log::debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type));
$result = false;
}
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug('IBAN result is not null and seems valid, save as source account.');
Log::debug('IBAN result is not null and seems valid, save as source account.');
$this->setSource($search);
$result = true;
}
@@ -156,13 +160,13 @@ trait DepositValidation
if (null !== $accountNumber && '' !== $accountNumber) {
$search = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug(
Log::debug(
sprintf('User submitted number ("%s"), which is a "%s", so this is not a valid source.', $accountNumber, $search->accountType->type)
);
$result = false;
}
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug('Number result is not null and seems valid, save as source account.');
Log::debug('Number result is not null and seems valid, save as source account.');
$this->setSource($search);
$result = true;
}

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Validation\Account;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Illuminate\Support\Facades\Log;
/**
* Trait OBValidation
@@ -38,7 +39,7 @@ trait OBValidation
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
app('log')->debug('Now in validateOBDestination', $array);
Log::debug('Now in validateOBDestination', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source?->accountType->type] ?? [];
@@ -46,12 +47,12 @@ trait OBValidation
// if both values are NULL we return false,
// because the destination of a deposit can't be created.
$this->destError = (string) trans('validation.ob_dest_need_data');
app('log')->error('Both values are NULL, cant create OB destination.');
Log::error('Both values are NULL, cant create OB destination.');
$result = false;
}
// if the account can be created anyway we don't need to search.
if (null === $result && true === $this->canCreateTypes($validTypes)) {
app('log')->debug('Can create some of these types, so return true.');
Log::debug('Can create some of these types, so return true.');
$result = true;
}
@@ -59,17 +60,17 @@ trait OBValidation
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
app('log')->debug('findExistingAccount() returned NULL, so the result is false.', $validTypes);
Log::debug('findExistingAccount() returned NULL, so the result is false.', $validTypes);
$this->destError = (string) trans('validation.ob_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$result = false;
}
if (null !== $search) {
app('log')->debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
Log::debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
$this->setDestination($search);
$result = true;
}
}
app('log')->debug(sprintf('validateOBDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true)));
Log::debug(sprintf('validateOBDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true)));
return $result;
}
@@ -84,7 +85,7 @@ trait OBValidation
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
app('log')->debug('Now in validateOBSource', $array);
Log::debug('Now in validateOBSource', $array);
$result = null;
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
@@ -100,19 +101,19 @@ trait OBValidation
// if the user submits an ID only but that ID is not of the correct type,
// return false.
if (null !== $accountId && null === $accountName) {
app('log')->debug('Source ID is not null, but name is null.');
Log::debug('Source ID is not null, but name is null.');
$search = $this->accountRepository->find($accountId);
// the source resulted in an account, but it's not of a valid type.
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
$message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type);
app('log')->debug($message);
Log::debug($message);
$this->sourceError = $message;
$result = false;
}
// the source resulted in an account, AND it's of a valid type.
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
app('log')->debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name));
Log::debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name));
$this->setSource($search);
$result = true;
}
@@ -120,7 +121,7 @@ trait OBValidation
// if the account can be created anyway we don't need to search.
if (null === $result && true === $this->canCreateTypes($validTypes)) {
app('log')->debug('Result is still null.');
Log::debug('Result is still null.');
$result = true;
// set the source to be a (dummy) initial balance account.

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Validation\Account;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
use Illuminate\Support\Facades\Log;
/**
* Trait WithdrawalValidation
@@ -37,14 +38,14 @@ trait WithdrawalValidation
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
app('log')->debug('Now in validateGenericSource', $array);
Log::debug('Now in validateGenericSource', $array);
// source can be any of the following types.
$validTypes = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return TRUE
// because we assume the user doesn't want to submit / change anything.
$this->sourceError = (string) trans('validation.withdrawal_source_need_data');
app('log')->warning('[a] Not a valid source. Need more data.');
Log::warning('[a] Not a valid source. Need more data.');
return false;
}
@@ -53,12 +54,12 @@ trait WithdrawalValidation
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string) trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
}
$this->setSource($search);
app('log')->debug('Valid source account!');
Log::debug('Valid source account!');
return true;
}
@@ -73,10 +74,10 @@ trait WithdrawalValidation
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
app('log')->debug('Now in validateWithdrawalDestination()', $array);
Log::debug('Now in validateWithdrawalDestination()', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
app('log')->debug('Source type can be: ', $validTypes);
Log::debug('Source type can be: ', $validTypes);
if (null === $accountId && null === $accountName && null === $accountIban && null === $accountNumber && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL return false,
// because the destination of a withdrawal can never be created automatically.
@@ -86,7 +87,7 @@ trait WithdrawalValidation
}
// if there's an ID it must be of the "validTypes".
if (null !== $accountId && 0 !== $accountId) {
if (null !== $accountId && 0 !== $accountId && $accountId !== $this->source->id) {
$found = $this->accountRepository->find($accountId);
if (null !== $found) {
$type = $found->accountType->type;
@@ -104,7 +105,7 @@ trait WithdrawalValidation
// if there is an iban, it can only be in use by a valid destination type, or we will fail.
// the inverse of $validTypes is
if (null !== $accountIban && '' !== $accountIban) {
app('log')->debug('Check if there is not already an account with this IBAN');
Log::debug('Check if there is not already an account with this IBAN');
// the inverse flag reverses the search, searching for everything that is NOT a valid type.
$existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true);
if (null !== $existing) {
@@ -125,14 +126,14 @@ trait WithdrawalValidation
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
app('log')->debug('Now in validateWithdrawalSource', $array);
Log::debug('Now in validateWithdrawalSource', $array);
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
if (null === $accountId && null === $accountName && null === $accountNumber && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return false,
// because the source of a withdrawal can't be created.
$this->sourceError = (string) trans('validation.withdrawal_source_need_data');
app('log')->warning('[b] Not a valid source. Need more data.');
Log::warning('[b] Not a valid source. Need more data.');
return false;
}
@@ -141,12 +142,12 @@ trait WithdrawalValidation
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string) trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
}
$this->setSource($search);
app('log')->debug('Valid source account!');
Log::debug('Valid source account!');
return true;
}

View File

@@ -36,6 +36,7 @@ use FireflyIII\Validation\Account\OBValidation;
use FireflyIII\Validation\Account\ReconciliationValidation;
use FireflyIII\Validation\Account\TransferValidation;
use FireflyIII\Validation\Account\WithdrawalValidation;
use Illuminate\Support\Facades\Log;
/**
* Class AccountValidator
@@ -80,10 +81,10 @@ class AccountValidator
public function setSource(?Account $account): void
{
if (!$account instanceof Account) {
app('log')->debug('AccountValidator source is set to NULL');
Log::debug('AccountValidator source is set to NULL');
}
if ($account instanceof Account) {
app('log')->debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType?->type));
Log::debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType?->type));
}
$this->source = $account;
}
@@ -91,17 +92,17 @@ class AccountValidator
public function setDestination(?Account $account): void
{
if (!$account instanceof Account) {
app('log')->debug('AccountValidator destination is set to NULL');
Log::debug('AccountValidator destination is set to NULL');
}
if ($account instanceof Account) {
app('log')->debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType->type));
Log::debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType->type));
}
$this->destination = $account;
}
public function setTransactionType(string $transactionType): void
{
app('log')->debug(sprintf('Transaction type for validator is now "%s".', ucfirst($transactionType)));
Log::debug(sprintf('Transaction type for validator is now "%s".', ucfirst($transactionType)));
$this->transactionType = ucfirst($transactionType);
}
@@ -117,9 +118,9 @@ class AccountValidator
public function validateDestination(array $array): bool
{
app('log')->debug('Now in AccountValidator::validateDestination()', $array);
Log::debug('Now in AccountValidator::validateDestination()', $array);
if (!$this->source instanceof Account) {
app('log')->error('Source is NULL, always FALSE.');
Log::error('Source is NULL, always FALSE.');
$this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.';
return false;
@@ -128,7 +129,7 @@ class AccountValidator
switch ($this->transactionType) {
default:
$this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType);
app('log')->error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType));
Log::error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType));
$result = false;
@@ -170,11 +171,11 @@ class AccountValidator
public function validateSource(array $array): bool
{
app('log')->debug('Now in AccountValidator::validateSource()', $array);
Log::debug('Now in AccountValidator::validateSource()', $array);
switch ($this->transactionType) {
default:
app('log')->error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will do a generic check.', $this->transactionType));
Log::error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will do a generic check.', $this->transactionType));
$result = $this->validateGenericSource($array);
break;
@@ -205,7 +206,7 @@ class AccountValidator
break;
case TransactionTypeEnum::RECONCILIATION->value:
app('log')->debug('Calling validateReconciliationSource');
Log::debug('Calling validateReconciliationSource');
$result = $this->validateReconciliationSource($array);
break;
@@ -216,17 +217,17 @@ class AccountValidator
protected function canCreateTypes(array $accountTypes): bool
{
app('log')->debug('Can we create any of these types?', $accountTypes);
Log::debug('Can we create any of these types?', $accountTypes);
/** @var string $accountType */
foreach ($accountTypes as $accountType) {
if ($this->canCreateType($accountType)) {
app('log')->debug(sprintf('YES, we can create a %s', $accountType));
Log::debug(sprintf('YES, we can create a %s', $accountType));
return true;
}
}
app('log')->debug('NO, we cant create any of those.');
Log::debug('NO, we cant create any of those.');
return false;
}
@@ -250,8 +251,8 @@ class AccountValidator
*/
protected function findExistingAccount(array $validTypes, array $data, bool $inverse = false): ?Account
{
app('log')->debug('Now in findExistingAccount', [$validTypes, $data]);
app('log')->debug('The search will be reversed!');
Log::debug('Now in findExistingAccount', [$validTypes, $data]);
Log::debug('The search will be reversed!');
$accountId = array_key_exists('id', $data) ? $data['id'] : null;
$accountIban = array_key_exists('iban', $data) ? $data['iban'] : null;
$accountNumber = array_key_exists('number', $data) ? $data['number'] : null;
@@ -264,7 +265,7 @@ class AccountValidator
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if (($first instanceof Account) && $check) {
app('log')->debug(sprintf('ID: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
Log::debug(sprintf('ID: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}
@@ -277,7 +278,7 @@ class AccountValidator
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if (($first instanceof Account) && $check) {
app('log')->debug(sprintf('Iban: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
Log::debug(sprintf('Iban: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}
@@ -290,7 +291,7 @@ class AccountValidator
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if (($first instanceof Account) && $check) {
app('log')->debug(sprintf('Number: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
Log::debug(sprintf('Number: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}
@@ -300,12 +301,12 @@ class AccountValidator
if ('' !== (string) $accountName) {
$first = $this->accountRepository->findByName($accountName, $validTypes);
if ($first instanceof Account) {
app('log')->debug(sprintf('Name: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
Log::debug(sprintf('Name: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}
}
app('log')->debug('Found nothing in findExistingAccount()');
Log::debug('Found nothing in findExistingAccount()');
return null;
}