2016-05-20 05:42:21 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* FireflyValidator.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-03-24 09:15:12 -05:00
|
|
|
declare(strict_types=1);
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Validation;
|
|
|
|
|
2015-03-26 12:05:23 -05:00
|
|
|
use Config;
|
2015-02-07 18:15:15 -06:00
|
|
|
use DB;
|
2015-03-30 13:08:27 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2016-02-10 23:40:16 -06:00
|
|
|
use FireflyIII\Models\AccountMeta;
|
2015-03-26 12:05:23 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2016-01-14 14:34:17 -06:00
|
|
|
use FireflyIII\Models\Budget;
|
2015-06-27 01:06:24 -05:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
2016-01-15 02:25:32 -06:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-09-15 06:43:57 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2018-04-06 23:20:45 -05:00
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
2016-01-14 14:34:17 -06:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2017-08-04 08:48:26 -05:00
|
|
|
use FireflyIII\Services\Password\Verifier;
|
2017-09-14 10:40:02 -05:00
|
|
|
use FireflyIII\TransactionRules\Triggers\TriggerInterface;
|
2015-06-05 05:18:20 -05:00
|
|
|
use FireflyIII\User;
|
2016-03-20 10:49:49 -05:00
|
|
|
use Google2FA;
|
2018-08-04 10:30:47 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-11 00:35:10 -06:00
|
|
|
use Illuminate\Validation\Validator;
|
2015-02-07 18:15:15 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class FireflyValidator.
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2015-02-07 18:15:15 -06:00
|
|
|
class FireflyValidator extends Validator
|
|
|
|
{
|
2016-03-03 13:45:27 -06:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-03-03 13:45:27 -06:00
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validate2faCode($attribute, $value): bool
|
|
|
|
{
|
2019-06-07 10:58:11 -05:00
|
|
|
if (!\is_string($value) || null === $value || 6 !== strlen($value)) {
|
2016-03-03 13:45:27 -06:00
|
|
|
return false;
|
2016-03-14 14:53:56 -05:00
|
|
|
}
|
2016-03-03 13:45:27 -06:00
|
|
|
|
2017-03-25 07:41:17 -05:00
|
|
|
$secret = session('two-factor-secret');
|
2016-03-14 14:53:56 -05:00
|
|
|
|
2016-06-24 14:58:57 -05:00
|
|
|
return Google2FA::verifyKey($secret, $value);
|
2016-03-03 13:45:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2015-02-24 15:53:38 -06:00
|
|
|
* @param $attribute
|
2016-01-20 08:23:36 -06:00
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
2015-02-24 15:53:38 -06:00
|
|
|
*
|
2016-01-14 14:34:17 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateBelongsToUser($attribute, $value, $parameters): bool
|
|
|
|
{
|
2016-02-05 01:03:26 -06:00
|
|
|
$field = $parameters[1] ?? 'id';
|
2016-01-15 02:25:32 -06:00
|
|
|
|
2018-04-02 07:50:17 -05:00
|
|
|
if (0 === (int)$value) {
|
2016-05-05 11:59:46 -05:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-16 05:15:58 -05:00
|
|
|
$count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count();
|
2016-01-15 02:25:32 -06:00
|
|
|
|
2018-03-29 12:01:47 -05:00
|
|
|
return 1 === $count;
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
|
2016-11-28 11:55:56 -06:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-11-28 11:55:56 -06:00
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateBic($attribute, $value): bool
|
|
|
|
{
|
2016-11-28 11:55:56 -06:00
|
|
|
$regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i';
|
|
|
|
$result = preg_match($regex, $value);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (false === $result) {
|
2016-11-28 11:55:56 -06:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $result) {
|
2016-11-28 11:55:56 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-01-20 08:23:36 -06:00
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateIban($attribute, $value): bool
|
|
|
|
{
|
2019-06-07 10:58:11 -05:00
|
|
|
if (!\is_string($value) || null === $value || strlen($value) < 6) {
|
2016-01-20 08:23:36 -06:00
|
|
|
return false;
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
2018-01-12 13:32:09 -06:00
|
|
|
// strip spaces
|
|
|
|
$search = [
|
|
|
|
"\x20", // normal space
|
|
|
|
"\u{0001}", // start of heading
|
|
|
|
"\u{0002}", // start of text
|
|
|
|
"\u{0003}", // end of text
|
|
|
|
"\u{0004}", // end of transmission
|
|
|
|
"\u{0005}", // enquiry
|
|
|
|
"\u{0006}", // ACK
|
|
|
|
"\u{0007}", // BEL
|
|
|
|
"\u{0008}", // backspace
|
|
|
|
"\u{000E}", // shift out
|
|
|
|
"\u{000F}", // shift in
|
|
|
|
"\u{0010}", // data link escape
|
|
|
|
"\u{0011}", // DC1
|
|
|
|
"\u{0012}", // DC2
|
|
|
|
"\u{0013}", // DC3
|
|
|
|
"\u{0014}", // DC4
|
|
|
|
"\u{0015}", // NAK
|
|
|
|
"\u{0016}", // SYN
|
|
|
|
"\u{0017}", // ETB
|
|
|
|
"\u{0018}", // CAN
|
|
|
|
"\u{0019}", // EM
|
|
|
|
"\u{001A}", // SUB
|
|
|
|
"\u{001B}", // escape
|
|
|
|
"\u{001C}", // file separator
|
|
|
|
"\u{001D}", // group separator
|
|
|
|
"\u{001E}", // record separator
|
|
|
|
"\u{001F}", // unit separator
|
|
|
|
"\u{007F}", // DEL
|
|
|
|
"\u{00A0}", // non-breaking space
|
|
|
|
"\u{1680}", // ogham space mark
|
|
|
|
"\u{180E}", // mongolian vowel separator
|
|
|
|
"\u{2000}", // en quad
|
|
|
|
"\u{2001}", // em quad
|
|
|
|
"\u{2002}", // en space
|
|
|
|
"\u{2003}", // em space
|
|
|
|
"\u{2004}", // three-per-em space
|
|
|
|
"\u{2005}", // four-per-em space
|
|
|
|
"\u{2006}", // six-per-em space
|
|
|
|
"\u{2007}", // figure space
|
|
|
|
"\u{2008}", // punctuation space
|
|
|
|
"\u{2009}", // thin space
|
|
|
|
"\u{200A}", // hair space
|
|
|
|
"\u{200B}", // zero width space
|
|
|
|
"\u{202F}", // narrow no-break space
|
|
|
|
"\u{3000}", // ideographic space
|
|
|
|
"\u{FEFF}", // zero width no -break space
|
|
|
|
];
|
|
|
|
$replace = '';
|
|
|
|
$value = str_replace($search, $replace, $value);
|
|
|
|
$value = strtoupper($value);
|
2016-01-20 08:23:36 -06:00
|
|
|
|
|
|
|
$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',
|
2017-11-15 05:25:49 -06:00
|
|
|
'32', '33', '34', '35',];
|
2016-01-20 08:23:36 -06:00
|
|
|
|
|
|
|
// take
|
|
|
|
$first = substr($value, 0, 4);
|
|
|
|
$last = substr($value, 4);
|
|
|
|
$iban = $last . $first;
|
|
|
|
$iban = str_replace($search, $replace, $iban);
|
|
|
|
$checksum = bcmod($iban, '97');
|
|
|
|
|
2018-04-02 07:50:17 -05:00
|
|
|
return 1 === (int)$checksum;
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
|
|
|
|
2017-01-04 10:25:28 -06:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-08-04 10:30:47 -05:00
|
|
|
public function validateLess($attribute, $value, $parameters): bool
|
2018-02-11 08:27:28 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
/** @var mixed $compare */
|
|
|
|
$compare = $parameters[0] ?? '0';
|
2017-01-30 09:35:41 -06:00
|
|
|
|
2018-08-04 10:30:47 -05:00
|
|
|
return bccomp((string)$value, (string)$compare) < 0;
|
2017-01-04 10:25:28 -06:00
|
|
|
}
|
|
|
|
|
2018-07-22 01:36:30 -05:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-08-04 10:30:47 -05:00
|
|
|
public function validateMore($attribute, $value, $parameters): bool
|
2018-07-22 01:36:30 -05:00
|
|
|
{
|
|
|
|
/** @var mixed $compare */
|
|
|
|
$compare = $parameters[0] ?? '0';
|
|
|
|
|
2018-08-04 10:30:47 -05:00
|
|
|
return bccomp((string)$value, (string)$compare) > 0;
|
2018-07-22 01:36:30 -05:00
|
|
|
}
|
|
|
|
|
2016-10-21 12:06:22 -05:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-10-21 12:06:22 -05:00
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateMustExist($attribute, $value, $parameters): bool
|
|
|
|
{
|
2016-10-21 12:06:22 -05:00
|
|
|
$field = $parameters[1] ?? 'id';
|
|
|
|
|
2018-04-02 07:50:17 -05:00
|
|
|
if (0 === (int)$value) {
|
2016-10-21 12:06:22 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$count = DB::table($parameters[0])->where($field, $value)->count();
|
|
|
|
|
2018-03-29 12:01:47 -05:00
|
|
|
return 1 === $count;
|
2016-10-21 12:06:22 -05:00
|
|
|
}
|
|
|
|
|
2016-01-14 14:34:17 -06:00
|
|
|
/**
|
2018-08-06 12:14:30 -05:00
|
|
|
* @param string $attribute
|
|
|
|
*
|
|
|
|
* @param string $value
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-09-15 06:43:57 -05:00
|
|
|
public function validateRuleActionValue(string $attribute, string $value = null): bool
|
2016-01-14 14:34:17 -06:00
|
|
|
{
|
2018-08-05 08:34:20 -05:00
|
|
|
// first, get the index from this string:
|
2018-09-15 06:43:57 -05:00
|
|
|
$value = $value ?? '';
|
2016-01-15 02:25:32 -06:00
|
|
|
$parts = explode('.', $attribute);
|
2018-08-05 08:34:20 -05:00
|
|
|
$index = (int)($parts[1] ?? '0');
|
|
|
|
|
|
|
|
// get the name of the trigger from the data array:
|
2018-12-16 06:55:19 -06:00
|
|
|
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
|
2018-06-30 09:46:51 -05:00
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
// if it's "invalid" return false.
|
|
|
|
if ('invalid' === $actionType) {
|
|
|
|
return false;
|
2018-06-30 09:46:51 -05:00
|
|
|
}
|
2018-08-05 08:34:20 -05:00
|
|
|
|
|
|
|
// if it's set_budget, verify the budget name:
|
|
|
|
if ('set_budget' === $actionType) {
|
|
|
|
/** @var BudgetRepositoryInterface $repository */
|
|
|
|
$repository = app(BudgetRepositoryInterface::class);
|
|
|
|
$budgets = $repository->getBudgets();
|
|
|
|
// count budgets, should have at least one
|
|
|
|
$count = $budgets->filter(
|
|
|
|
function (Budget $budget) use ($value) {
|
|
|
|
return $budget->name === $value;
|
|
|
|
}
|
|
|
|
)->count();
|
|
|
|
|
|
|
|
return 1 === $count;
|
2018-06-30 09:46:51 -05:00
|
|
|
}
|
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
// if it's link to bill, verify the name of the bill.
|
|
|
|
if ('link_to_bill' === $actionType) {
|
|
|
|
/** @var BillRepositoryInterface $repository */
|
|
|
|
$repository = app(BillRepositoryInterface::class);
|
|
|
|
$bill = $repository->findByName($value);
|
2018-06-30 09:46:51 -05:00
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
return null !== $bill;
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
|
|
|
|
2018-09-15 06:43:57 -05:00
|
|
|
// if it's convert_transfer, it must be a valid asset account name.
|
|
|
|
if ('convert_transfer' === $actionType) {
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
2018-09-26 13:35:01 -05:00
|
|
|
$account = $repository->findByName(
|
|
|
|
$value,
|
|
|
|
[AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE,
|
|
|
|
AccountType::CREDITCARD]
|
2018-09-15 06:43:57 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
return null !== $account;
|
|
|
|
}
|
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
// return true for the rest.
|
|
|
|
return true;
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-09 13:54:11 -06:00
|
|
|
* $attribute has the format triggers.%d.value.
|
2018-08-05 08:34:20 -05:00
|
|
|
*
|
|
|
|
* @param string $attribute
|
|
|
|
* @param string $value
|
2016-01-02 09:31:14 -06:00
|
|
|
*
|
2015-02-24 15:53:38 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-10-14 10:38:26 -05:00
|
|
|
public function validateRuleTriggerValue(string $attribute, string $value = null): bool
|
2016-01-02 09:57:31 -06:00
|
|
|
{
|
2018-08-05 08:34:20 -05:00
|
|
|
// first, get the index from this string:
|
2016-01-20 08:23:36 -06:00
|
|
|
$parts = explode('.', $attribute);
|
2018-08-05 08:34:20 -05:00
|
|
|
$index = (int)($parts[1] ?? '0');
|
|
|
|
|
|
|
|
// get the name of the trigger from the data array:
|
2018-12-16 06:55:19 -06:00
|
|
|
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
|
2018-08-05 08:34:20 -05:00
|
|
|
|
|
|
|
// invalid always returns false:
|
|
|
|
if ('invalid' === $triggerType) {
|
|
|
|
return false;
|
2018-06-30 09:46:51 -05:00
|
|
|
}
|
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
// these trigger types need a numerical check:
|
|
|
|
$numerical = ['amount_less', 'amount_more', 'amount_exactly'];
|
2019-06-21 12:10:02 -05:00
|
|
|
if (in_array($triggerType, $numerical, true)) {
|
2018-08-05 08:34:20 -05:00
|
|
|
return is_numeric($value);
|
2018-06-30 09:46:51 -05:00
|
|
|
}
|
2018-08-05 08:34:20 -05:00
|
|
|
|
|
|
|
// these trigger types need a simple strlen check:
|
|
|
|
$length = ['from_account_starts', 'from_account_ends', 'from_account_is', 'from_account_contains', 'to_account_starts', 'to_account_ends',
|
|
|
|
'to_account_is', 'to_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',];
|
2019-06-21 12:10:02 -05:00
|
|
|
if (in_array($triggerType, $length, true)) {
|
2018-08-05 08:34:20 -05:00
|
|
|
return '' !== $value;
|
2018-06-30 09:46:51 -05:00
|
|
|
}
|
2015-03-30 13:08:27 -05:00
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
// check transaction type.
|
|
|
|
if ('transaction_type' === $triggerType) {
|
2019-04-08 13:40:12 -05:00
|
|
|
$count = TransactionType::where('type', ucfirst($value))->count();
|
2018-08-05 08:34:20 -05:00
|
|
|
|
2018-09-26 13:35:01 -05:00
|
|
|
return 1 === $count;
|
2015-07-06 18:07:19 -05:00
|
|
|
}
|
2016-01-29 00:35:14 -06:00
|
|
|
|
2018-08-05 08:34:20 -05:00
|
|
|
// and finally a "will match everything check":
|
|
|
|
$classes = app('config')->get('firefly.rule-triggers');
|
|
|
|
/** @var TriggerInterface $class */
|
2018-08-05 08:41:13 -05:00
|
|
|
$class = $classes[$triggerType] ?? false;
|
2018-08-06 12:14:30 -05:00
|
|
|
if (false === $class) {
|
2018-08-05 08:41:13 -05:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-05 08:34:20 -05:00
|
|
|
|
|
|
|
return !$class::willMatchEverything($value);
|
2015-07-03 05:51:14 -05:00
|
|
|
}
|
2017-08-04 08:46:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateSecurePassword($attribute, $value): bool
|
|
|
|
{
|
2017-08-08 09:30:21 -05:00
|
|
|
$verify = false;
|
|
|
|
if (isset($this->data['verify_password'])) {
|
2018-04-02 07:50:17 -05:00
|
|
|
$verify = 1 === (int)$this->data['verify_password'];
|
2017-08-08 09:30:21 -05:00
|
|
|
}
|
|
|
|
if ($verify) {
|
|
|
|
/** @var Verifier $service */
|
|
|
|
$service = app(Verifier::class);
|
|
|
|
|
|
|
|
return $service->validPassword($value);
|
2017-08-04 08:46:52 -05:00
|
|
|
}
|
2017-08-04 08:48:26 -05:00
|
|
|
|
2017-08-08 09:30:21 -05:00
|
|
|
return true;
|
2017-08-04 08:46:52 -05:00
|
|
|
}
|
2015-07-03 05:51:14 -05:00
|
|
|
|
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2015-07-03 05:51:14 -05:00
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
2015-03-26 12:05:23 -05:00
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateUniqueAccountForUser($attribute, $value, $parameters): bool
|
|
|
|
{
|
2015-06-05 05:18:20 -05:00
|
|
|
// because a user does not have to be logged in (tests and what-not).
|
2019-06-10 13:14:00 -05:00
|
|
|
|
2016-09-16 05:07:45 -05:00
|
|
|
if (!auth()->check()) {
|
2015-06-05 05:18:20 -05:00
|
|
|
return $this->validateAccountAnonymously();
|
|
|
|
}
|
|
|
|
if (isset($this->data['what'])) {
|
2018-02-13 11:24:06 -06:00
|
|
|
return $this->validateByAccountTypeString($value, $parameters, $this->data['what']);
|
|
|
|
}
|
|
|
|
if (isset($this->data['type'])) {
|
|
|
|
return $this->validateByAccountTypeString($value, $parameters, $this->data['type']);
|
2015-06-05 05:18:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->data['account_type_id'])) {
|
|
|
|
return $this->validateByAccountTypeId($value, $parameters);
|
|
|
|
}
|
2015-06-29 00:22:51 -05:00
|
|
|
if (isset($this->data['id'])) {
|
|
|
|
return $this->validateByAccountId($value);
|
2015-06-28 14:13:08 -05:00
|
|
|
}
|
|
|
|
|
2019-06-10 13:14:00 -05:00
|
|
|
// without type, just try to validate the name.
|
|
|
|
return $this->validateByAccountName($value);
|
2015-06-05 05:18:20 -05:00
|
|
|
}
|
2015-03-30 13:16:33 -05:00
|
|
|
|
2016-02-12 10:34:42 -06:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-02-12 10:34:42 -06:00
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-13 11:24:06 -06:00
|
|
|
public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool
|
2018-02-11 08:27:28 -06:00
|
|
|
{
|
2018-04-02 07:50:17 -05:00
|
|
|
$accountId = (int)($this->data['id'] ?? 0.0);
|
2018-08-06 12:14:30 -05:00
|
|
|
if (0 === $accountId) {
|
2018-04-02 07:50:17 -05:00
|
|
|
$accountId = (int)($parameters[0] ?? 0.0);
|
2018-02-13 11:24:06 -06:00
|
|
|
}
|
2016-02-10 23:40:16 -06:00
|
|
|
|
2016-12-15 14:35:33 -06:00
|
|
|
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
2018-02-11 08:27:28 -06:00
|
|
|
->whereNull('accounts.deleted_at')
|
2016-09-16 05:15:58 -05:00
|
|
|
->where('accounts.user_id', auth()->user()->id)
|
2016-02-10 23:40:16 -06:00
|
|
|
->where('account_meta.name', 'accountNumber');
|
|
|
|
|
2018-08-06 12:14:30 -05:00
|
|
|
if ($accountId > 0) {
|
2016-02-10 23:40:16 -06:00
|
|
|
// exclude current account from check.
|
2018-08-04 10:30:47 -05:00
|
|
|
$query->where('account_meta.account_id', '!=', $accountId);
|
2016-02-10 23:40:16 -06:00
|
|
|
}
|
|
|
|
$set = $query->get(['account_meta.*']);
|
|
|
|
|
|
|
|
/** @var AccountMeta $entry */
|
|
|
|
foreach ($set as $entry) {
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($entry->data === $value) {
|
2016-02-10 23:40:16 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-05 05:48:58 -05:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2016-01-20 08:23:36 -06:00
|
|
|
* Validate an object and its unicity. Checks for encryption / encrypted values as well.
|
|
|
|
*
|
|
|
|
* parameter 0: the table
|
|
|
|
* parameter 1: the field
|
|
|
|
* parameter 2: an id to ignore (when editing)
|
|
|
|
*
|
|
|
|
* @param $attribute
|
2015-06-05 05:48:58 -05:00
|
|
|
* @param $value
|
2016-01-20 08:23:36 -06:00
|
|
|
* @param $parameters
|
2015-06-05 05:48:58 -05:00
|
|
|
*
|
2016-01-20 08:23:36 -06:00
|
|
|
* @return bool
|
2015-06-05 05:48:58 -05:00
|
|
|
*/
|
2018-02-10 02:22:13 -06:00
|
|
|
public function validateUniqueObjectForUser($attribute, $value, $parameters): bool
|
2015-06-05 05:48:58 -05:00
|
|
|
{
|
2018-08-04 10:30:47 -05:00
|
|
|
[$table, $field] = $parameters;
|
2018-04-02 07:50:17 -05:00
|
|
|
$exclude = (int)($parameters[2] ?? 0.0);
|
2016-01-20 08:23:36 -06:00
|
|
|
|
2018-02-10 02:22:13 -06:00
|
|
|
/*
|
|
|
|
* If other data (in $this->getData()) contains
|
|
|
|
* ID field, set that field to be the $exclude.
|
|
|
|
*/
|
|
|
|
$data = $this->getData();
|
2018-04-02 07:50:17 -05:00
|
|
|
if (!isset($parameters[2]) && isset($data['id']) && (int)$data['id'] > 0) {
|
|
|
|
$exclude = (int)$data['id'];
|
2018-02-10 02:22:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
// get entries from table
|
2017-09-01 04:20:51 -05:00
|
|
|
$set = DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
2016-01-20 08:23:36 -06:00
|
|
|
->where('id', '!=', $exclude)->get([$field]);
|
|
|
|
|
|
|
|
foreach ($set as $entry) {
|
2019-01-31 12:56:09 -06:00
|
|
|
$fieldValue = $entry->$field;
|
2016-01-20 08:23:36 -06:00
|
|
|
|
|
|
|
if ($fieldValue === $value) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 05:48:58 -05:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
return true;
|
2015-06-05 05:48:58 -05:00
|
|
|
}
|
|
|
|
|
2015-06-05 05:18:20 -05:00
|
|
|
/**
|
2017-01-02 08:22:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-01-20 08:23:36 -06:00
|
|
|
* @param $attribute
|
2015-06-05 05:18:20 -05:00
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-11 08:27:28 -06:00
|
|
|
public function validateUniquePiggyBankForUser($attribute, $value, $parameters): bool
|
|
|
|
{
|
2016-02-06 03:11:06 -06:00
|
|
|
$exclude = $parameters[0] ?? null;
|
2016-01-20 08:23:36 -06:00
|
|
|
$query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
|
2016-09-16 05:15:58 -05:00
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $exclude) {
|
2018-05-26 00:48:49 -05:00
|
|
|
$query->where('piggy_banks.id', '!=', (int)$exclude);
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
$set = $query->get(['piggy_banks.*']);
|
2015-06-05 05:18:20 -05:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/** @var PiggyBank $entry */
|
2015-06-05 05:18:20 -05:00
|
|
|
foreach ($set as $entry) {
|
2018-05-26 00:48:49 -05:00
|
|
|
|
2019-01-31 12:56:09 -06:00
|
|
|
$fieldValue = $entry->name;
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($fieldValue === $value) {
|
2015-06-05 05:18:20 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
2015-06-05 05:18:20 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
private function validateAccountAnonymously(): bool
|
2015-06-05 05:18:20 -05:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
if (!isset($this->data['user_id'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 05:18:20 -05:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
$user = User::find($this->data['user_id']);
|
|
|
|
$type = AccountType::find($this->data['account_type_id'])->first();
|
2019-01-31 12:56:09 -06:00
|
|
|
$value = $this->data['name'];
|
2016-01-20 08:23:36 -06:00
|
|
|
|
|
|
|
$set = $user->accounts()->where('account_type_id', $type->id)->get();
|
2015-03-30 13:08:27 -05:00
|
|
|
/** @var Account $entry */
|
|
|
|
foreach ($set as $entry) {
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($entry->name === $value) {
|
2015-03-30 13:08:27 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-26 12:05:23 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 13:08:27 -05:00
|
|
|
return true;
|
2015-03-26 12:05:23 -05:00
|
|
|
}
|
|
|
|
|
2015-06-28 14:13:08 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
private function validateByAccountId($value): bool
|
2015-06-28 14:13:08 -05:00
|
|
|
{
|
|
|
|
/** @var Account $existingAccount */
|
|
|
|
$existingAccount = Account::find($this->data['id']);
|
|
|
|
|
|
|
|
$type = $existingAccount->accountType;
|
|
|
|
$ignore = $existingAccount->id;
|
|
|
|
|
2018-08-06 12:14:30 -05:00
|
|
|
/** @var Collection $set */
|
2016-09-16 05:15:58 -05:00
|
|
|
$set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
|
2015-06-28 14:13:08 -05:00
|
|
|
/** @var Account $entry */
|
|
|
|
foreach ($set as $entry) {
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($entry->name === $value) {
|
2015-06-28 14:13:08 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2015-03-31 07:16:25 -05:00
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 03:11:06 -06:00
|
|
|
private function validateByAccountTypeId($value, $parameters): bool
|
2015-03-31 07:16:25 -05:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
$type = AccountType::find($this->data['account_type_id'])->first();
|
2018-04-02 07:50:17 -05:00
|
|
|
$ignore = (int)($parameters[0] ?? 0.0);
|
2015-03-31 07:16:25 -05:00
|
|
|
|
2018-08-06 12:14:30 -05:00
|
|
|
/** @var Collection $set */
|
2016-09-16 05:15:58 -05:00
|
|
|
$set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
|
2016-01-20 08:23:36 -06:00
|
|
|
/** @var Account $entry */
|
2015-03-31 07:16:25 -05:00
|
|
|
foreach ($set as $entry) {
|
2019-06-10 13:14:00 -05:00
|
|
|
// TODO no longer need to loop like this.
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($entry->name === $value) {
|
2015-03-31 07:16:25 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-27 14:20:52 -05:00
|
|
|
/**
|
2018-02-13 11:24:06 -06:00
|
|
|
* @param string $value
|
2019-06-10 13:14:00 -05:00
|
|
|
* @param array $parameters
|
2018-02-13 11:24:06 -06:00
|
|
|
* @param string $type
|
2015-03-27 14:20:52 -05:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-13 11:24:06 -06:00
|
|
|
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
2015-03-27 14:20:52 -05:00
|
|
|
{
|
2018-08-04 10:30:47 -05:00
|
|
|
/** @var array $search */
|
|
|
|
$search = Config::get('firefly.accountTypeByIdentifier.' . $type);
|
2018-12-03 00:18:05 -06:00
|
|
|
|
|
|
|
if (null === $search) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-04 10:30:47 -05:00
|
|
|
/** @var Collection $accountTypes */
|
|
|
|
$accountTypes = AccountType::whereIn('type', $search)->get();
|
|
|
|
$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();
|
2016-01-20 08:23:36 -06:00
|
|
|
/** @var Account $entry */
|
2015-04-07 11:26:14 -05:00
|
|
|
foreach ($set as $entry) {
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($entry->name === $value) {
|
2015-03-31 07:16:25 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-27 14:20:52 -05:00
|
|
|
}
|
|
|
|
|
2015-03-31 07:16:25 -05:00
|
|
|
return true;
|
2015-03-27 14:20:52 -05:00
|
|
|
}
|
2019-06-10 13:14:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function validateByAccountName(string $value): bool
|
|
|
|
{
|
|
|
|
return auth()->user()->accounts()->where('name', $value)->count() === 0;
|
|
|
|
}
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|