mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
I know it's bad form to submit a large PR like this but this fixes almost everything in https://github.com/firefly-iii/firefly-iii/issues/9183 and I was too lazy to create a branch for it.
This commit is contained in:
@@ -62,7 +62,7 @@ class FireflyValidator extends Validator
|
||||
if (!is_string($value) || 6 !== strlen($value)) {
|
||||
return false;
|
||||
}
|
||||
$user = auth()->user();
|
||||
$user = auth()->user();
|
||||
if (null === $user) {
|
||||
app('log')->error('No user during validate2faCode');
|
||||
|
||||
@@ -73,9 +73,25 @@ class FireflyValidator extends Validator
|
||||
if (is_array($secret)) {
|
||||
$secret = '';
|
||||
}
|
||||
$secret = (string) $secret;
|
||||
|
||||
return (bool)\Google2FA::verifyKey((string)$secret, $value);
|
||||
return (bool) \Google2FA::verifyKey((string) $secret, $value);
|
||||
}
|
||||
public function validateExistingMfaCode($attribute, $value): bool
|
||||
{
|
||||
if (!is_string($value) || 6 !== strlen($value)) {
|
||||
return false;
|
||||
}
|
||||
$user = auth()->user();
|
||||
if (null === $user) {
|
||||
app('log')->error('No user during validate2faCode');
|
||||
|
||||
return false;
|
||||
}
|
||||
$secret = (string)$user->mfa_secret;
|
||||
|
||||
return (bool) \Google2FA::verifyKey($secret, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
@@ -88,7 +104,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();
|
||||
@@ -179,15 +195,15 @@ class FireflyValidator extends Validator
|
||||
$value = strtoupper($value);
|
||||
|
||||
// replace characters outside of ASCI range.
|
||||
$value = (string)iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
|
||||
$value = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
|
||||
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
|
||||
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'];
|
||||
|
||||
// take
|
||||
$first = substr($value, 0, 4);
|
||||
$last = substr($value, 4);
|
||||
$iban = $last.$first;
|
||||
$iban = trim(str_replace($search, $replace, $iban));
|
||||
$first = substr($value, 0, 4);
|
||||
$last = substr($value, 4);
|
||||
$iban = $last . $first;
|
||||
$iban = trim(str_replace($search, $replace, $iban));
|
||||
if ('' === $iban) {
|
||||
return false;
|
||||
}
|
||||
@@ -202,7 +218,7 @@ class FireflyValidator extends Validator
|
||||
return false;
|
||||
}
|
||||
|
||||
return 1 === (int)$checksum;
|
||||
return 1 === (int) $checksum;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +233,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,7 +248,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,7 +262,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();
|
||||
@@ -258,8 +274,8 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
// first, get the index from this string:
|
||||
$value ??= '';
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int)($parts[1] ?? '0');
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
|
||||
// get the name of the trigger from the data array:
|
||||
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
|
||||
@@ -328,8 +344,8 @@ class FireflyValidator extends Validator
|
||||
public function validateRuleTriggerValue(string $attribute, ?string $value = null): bool
|
||||
{
|
||||
// first, get the index from this string:
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int)($parts[1] ?? '0');
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
|
||||
// get the name of the trigger from the data array:
|
||||
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
|
||||
@@ -340,14 +356,14 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
// these trigger types need a numerical check:
|
||||
$numerical = ['amount_less', 'amount_more', 'amount_exactly'];
|
||||
$numerical = ['amount_less', 'amount_more', 'amount_exactly'];
|
||||
if (in_array($triggerType, $numerical, true)) {
|
||||
return is_numeric($value);
|
||||
}
|
||||
|
||||
// these triggers need just the word "true":
|
||||
// TODO create a helper to automatically return these.
|
||||
$needTrue = [
|
||||
$needTrue = [
|
||||
'reconciled', 'has_attachments', 'has_any_category', 'has_any_budget', 'has_any_bill', 'has_any_tag', 'any_notes', 'any_external_url', 'has_no_attachments', 'has_no_category', 'has_no_budget', 'has_no_bill', 'has_no_tag', 'no_notes', 'no_external_url',
|
||||
'source_is_cash',
|
||||
'destination_is_cash',
|
||||
@@ -362,7 +378,7 @@ class FireflyValidator extends Validator
|
||||
|
||||
// these trigger types need a simple strlen check:
|
||||
// TODO create a helper to automatically return these.
|
||||
$length = [
|
||||
$length = [
|
||||
'source_account_starts',
|
||||
'source_account_ends',
|
||||
'source_account_is',
|
||||
@@ -391,7 +407,7 @@ class FireflyValidator extends Validator
|
||||
// check if it's an existing account.
|
||||
// TODO create a helper to automatically return these.
|
||||
if (in_array($triggerType, ['destination_account_id', 'source_account_id'], true)) {
|
||||
return is_numeric($value) && (int)$value > 0;
|
||||
return is_numeric($value) && (int) $value > 0;
|
||||
}
|
||||
|
||||
// check transaction type.
|
||||
@@ -430,7 +446,7 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
$verify = false;
|
||||
if (array_key_exists('verify_password', $this->data)) {
|
||||
$verify = 1 === (int)$this->data['verify_password'];
|
||||
$verify = 1 === (int) $this->data['verify_password'];
|
||||
}
|
||||
if ($verify) {
|
||||
/** @var Verifier $service */
|
||||
@@ -465,7 +481,7 @@ class FireflyValidator extends Validator
|
||||
if (array_key_exists('type', $this->data)) {
|
||||
app('log')->debug('validateUniqueAccountForUser::typeString');
|
||||
|
||||
return $this->validateByAccountTypeString($value, $parameters, (string)$this->data['type']);
|
||||
return $this->validateByAccountTypeString($value, $parameters, (string) $this->data['type']);
|
||||
}
|
||||
if (array_key_exists('account_type_id', $this->data)) {
|
||||
app('log')->debug('validateUniqueAccountForUser::typeId');
|
||||
@@ -476,7 +492,7 @@ class FireflyValidator extends Validator
|
||||
if (null !== $parameterId) {
|
||||
app('log')->debug('validateUniqueAccountForUser::paramId');
|
||||
|
||||
return $this->validateByParameterId((int)$parameterId, $value);
|
||||
return $this->validateByParameterId((int) $parameterId, $value);
|
||||
}
|
||||
if (array_key_exists('id', $this->data)) {
|
||||
app('log')->debug('validateUniqueAccountForUser::accountId');
|
||||
@@ -497,9 +513,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($this->data['user_id']);
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$value = $this->data['name'];
|
||||
$user = User::find($this->data['user_id']);
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$value = $this->data['name'];
|
||||
|
||||
/** @var null|Account $result */
|
||||
$result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first();
|
||||
@@ -510,21 +526,20 @@ class FireflyValidator extends Validator
|
||||
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
||||
{
|
||||
/** @var null|array $search */
|
||||
$search = \Config::get('firefly.accountTypeByIdentifier.'.$type);
|
||||
$search = \Config::get('firefly.accountTypeByIdentifier.' . $type);
|
||||
|
||||
if (null === $search) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$accountTypes = AccountType::whereIn('type', $search)->get();
|
||||
$ignore = (int)($parameters[0] ?? 0.0);
|
||||
$ignore = (int) ($parameters[0] ?? 0.0);
|
||||
$accountTypeIds = $accountTypes->pluck('id')->toArray();
|
||||
|
||||
/** @var null|Account $result */
|
||||
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first();
|
||||
|
||||
return null === $result;
|
||||
}
|
||||
@@ -536,13 +551,12 @@ 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 null|Account $result */
|
||||
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
->where('name', $value)
|
||||
->first();
|
||||
|
||||
return null === $result;
|
||||
}
|
||||
@@ -555,13 +569,12 @@ class FireflyValidator extends Validator
|
||||
/** @var Account $existingAccount */
|
||||
$existingAccount = Account::find($accountId);
|
||||
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first();
|
||||
|
||||
return null === $entry;
|
||||
}
|
||||
@@ -574,13 +587,12 @@ class FireflyValidator extends Validator
|
||||
/** @var Account $existingAccount */
|
||||
$existingAccount = Account::find($this->data['id']);
|
||||
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first();
|
||||
|
||||
return null === $entry;
|
||||
}
|
||||
@@ -599,24 +611,23 @@ 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')
|
||||
->whereNull('accounts.deleted_at')
|
||||
->where('accounts.user_id', auth()->user()->id)
|
||||
->where('account_meta.name', 'account_number')
|
||||
->where('account_meta.data', json_encode($value))
|
||||
;
|
||||
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->whereNull('accounts.deleted_at')
|
||||
->where('accounts.user_id', auth()->user()->id)
|
||||
->where('account_meta.name', 'account_number')
|
||||
->where('account_meta.data', json_encode($value));
|
||||
|
||||
if ($accountId > 0) {
|
||||
// exclude current account from check.
|
||||
$query->where('account_meta.account_id', '!=', $accountId);
|
||||
}
|
||||
$set = $query->get(['account_meta.*']);
|
||||
$count = $set->count();
|
||||
$set = $query->get(['account_meta.*']);
|
||||
$count = $set->count();
|
||||
if (0 === $count) {
|
||||
return true;
|
||||
}
|
||||
@@ -624,7 +635,7 @@ class FireflyValidator extends Validator
|
||||
// pretty much impossible but still.
|
||||
return false;
|
||||
}
|
||||
$type = $this->data['objectType'] ?? 'unknown';
|
||||
$type = $this->data['objectType'] ?? 'unknown';
|
||||
if ('expense' !== $type && 'revenue' !== $type) {
|
||||
app('log')->warning(sprintf('Account number "%s" is not unique and account type "%s" cannot share its account number.', $value, $type));
|
||||
|
||||
@@ -636,7 +647,7 @@ class FireflyValidator extends Validator
|
||||
/** @var AccountMeta $entry */
|
||||
foreach ($set as $entry) {
|
||||
$otherAccount = $entry->account;
|
||||
$otherType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type));
|
||||
$otherType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type));
|
||||
if (('expense' === $otherType || 'revenue' === $otherType) && $otherType !== $type) {
|
||||
app('log')->debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType));
|
||||
|
||||
@@ -653,7 +664,7 @@ class FireflyValidator extends Validator
|
||||
*/
|
||||
public function validateUniqueCurrencyCode(?string $attribute, ?string $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('code', (string)$attribute, (string)$value);
|
||||
return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -666,12 +677,12 @@ class FireflyValidator extends Validator
|
||||
|
||||
public function validateUniqueCurrencyName(?string $attribute, ?string $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('name', (string)$attribute, (string)$value);
|
||||
return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value);
|
||||
}
|
||||
|
||||
public function validateUniqueCurrencySymbol(?string $attribute, ?string $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('symbol', (string)$attribute, (string)$value);
|
||||
return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -683,7 +694,7 @@ class FireflyValidator extends Validator
|
||||
*/
|
||||
public function validateUniqueExistingWebhook($value, $parameters, $something): bool
|
||||
{
|
||||
$existingId = (int)($something[0] ?? 0);
|
||||
$existingId = (int) ($something[0] ?? 0);
|
||||
$trigger = 0;
|
||||
$response = 0;
|
||||
$delivery = 0;
|
||||
@@ -694,7 +705,7 @@ class FireflyValidator extends Validator
|
||||
// get existing webhook value:
|
||||
if (0 !== $existingId) {
|
||||
/** @var null|Webhook $webhook */
|
||||
$webhook = auth()->user()->webhooks()->find($existingId);
|
||||
$webhook = auth()->user()->webhooks()->find($existingId);
|
||||
if (null === $webhook) {
|
||||
return false;
|
||||
}
|
||||
@@ -712,12 +723,11 @@ class FireflyValidator extends Validator
|
||||
$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()
|
||||
;
|
||||
->where('trigger', $trigger)
|
||||
->where('response', $response)
|
||||
->where('delivery', $delivery)
|
||||
->where('id', '!=', $existingId)
|
||||
->where('url', $url)->count();
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -739,22 +749,21 @@ 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 (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int)$data['id'] > 0) {
|
||||
$exclude = (int)$data['id'];
|
||||
$data = $this->getData();
|
||||
if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int) $data['id'] > 0) {
|
||||
$exclude = (int) $data['id'];
|
||||
}
|
||||
// get entries from table
|
||||
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
||||
->where('id', '!=', $exclude)
|
||||
->where($field, $value)
|
||||
->first([$field])
|
||||
;
|
||||
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
||||
->where('id', '!=', $exclude)
|
||||
->where($field, $value)
|
||||
->first([$field]);
|
||||
if (null === $result) {
|
||||
return true; // not found, so true.
|
||||
}
|
||||
@@ -774,12 +783,11 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
$exclude = $parameters[0] ?? null;
|
||||
$query = \DB::table('object_groups')
|
||||
->whereNull('object_groups.deleted_at')
|
||||
->where('object_groups.user_id', auth()->user()->id)
|
||||
->where('object_groups.title', $value)
|
||||
;
|
||||
->whereNull('object_groups.deleted_at')
|
||||
->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();
|
||||
@@ -796,10 +804,9 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
$exclude = $parameters[0] ?? null;
|
||||
$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)
|
||||
;
|
||||
->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);
|
||||
}
|
||||
$query->where('piggy_banks.name', $value);
|
||||
|
||||
@@ -820,18 +827,17 @@ class FireflyValidator extends Validator
|
||||
$deliveries = Webhook::getDeliveriesForValidation();
|
||||
|
||||
// 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;
|
||||
$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()
|
||||
;
|
||||
->where('trigger', $trigger)
|
||||
->where('response', $response)
|
||||
->where('delivery', $delivery)
|
||||
->where('url', $url)->count();
|
||||
}
|
||||
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user