mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-10 12:24:50 +00:00
Expand API and refactor for user groups.
This commit is contained in:
@@ -139,7 +139,7 @@ trait DepositValidation
|
||||
// if the user submits an ID, but that ID is not of the correct type,
|
||||
// return false.
|
||||
if (null !== $accountId) {
|
||||
$search = $this->accountRepository->find($accountId);
|
||||
$search = $this->getRepository()->find($accountId);
|
||||
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
|
||||
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 accepts ID #%d as valid account data.', $accountId));
|
||||
@@ -153,7 +153,7 @@ trait DepositValidation
|
||||
|
||||
// if user submits an IBAN:
|
||||
if (null !== $accountIban) {
|
||||
$search = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
|
||||
$search = $this->getRepository()->findByIbanNull($accountIban, $validTypes);
|
||||
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
|
||||
Log::debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type));
|
||||
$result = false;
|
||||
@@ -167,7 +167,7 @@ trait DepositValidation
|
||||
|
||||
// if user submits a number:
|
||||
if (null !== $accountNumber && '' !== $accountNumber) {
|
||||
$search = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
|
||||
$search = $this->getRepository()->findByAccountNumber($accountNumber, $validTypes);
|
||||
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
|
||||
Log::debug(
|
||||
sprintf('User submitted number ("%s"), which is a "%s", so this is not a valid source.', $accountNumber, $search->accountType->type)
|
||||
|
||||
@@ -115,7 +115,7 @@ trait OBValidation
|
||||
// return false.
|
||||
if (null !== $accountId && null === $accountName) {
|
||||
Log::debug('Source ID is not null, but name is null.');
|
||||
$search = $this->accountRepository->find($accountId);
|
||||
$search = $this->getRepository()->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)) {
|
||||
|
||||
@@ -108,7 +108,7 @@ trait WithdrawalValidation
|
||||
|
||||
// if there's an ID it must be of the "validTypes".
|
||||
if (null !== $accountId && 0 !== $accountId) {
|
||||
$found = $this->accountRepository->find($accountId);
|
||||
$found = $this->getRepository()->find($accountId);
|
||||
if (null !== $found) {
|
||||
$type = $found->accountType->type;
|
||||
if (in_array($type, $validTypes, true)) {
|
||||
|
||||
@@ -26,9 +26,10 @@ namespace FireflyIII\Validation;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Models\UserGroup;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as UserGroupAccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use FireflyIII\Validation\Account\AccountValidatorProperties;
|
||||
use FireflyIII\Validation\Account\DepositValidation;
|
||||
use FireflyIII\Validation\Account\LiabilityValidation;
|
||||
use FireflyIII\Validation\Account\OBValidation;
|
||||
@@ -42,7 +43,6 @@ use Illuminate\Support\Facades\Log;
|
||||
*/
|
||||
class AccountValidator
|
||||
{
|
||||
use AccountValidatorProperties;
|
||||
use WithdrawalValidation;
|
||||
use DepositValidation;
|
||||
use TransferValidation;
|
||||
@@ -50,28 +50,32 @@ class AccountValidator
|
||||
use OBValidation;
|
||||
use LiabilityValidation;
|
||||
|
||||
public bool $createMode;
|
||||
public string $destError;
|
||||
public ?Account $destination;
|
||||
public ?Account $source;
|
||||
public string $sourceError;
|
||||
private AccountRepositoryInterface $accountRepository;
|
||||
private array $combinations;
|
||||
private string $transactionType;
|
||||
private User $user;
|
||||
public bool $createMode;
|
||||
public string $destError;
|
||||
public ?Account $destination;
|
||||
public ?Account $source;
|
||||
public string $sourceError;
|
||||
private AccountRepositoryInterface $accountRepository;
|
||||
private array $combinations;
|
||||
private string $transactionType;
|
||||
private bool $useUserGroupRepository = false;
|
||||
private User $user;
|
||||
private UserGroup $userGroup;
|
||||
private UserGroupAccountRepositoryInterface $userGroupAccountRepository;
|
||||
|
||||
/**
|
||||
* AccountValidator constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->createMode = false;
|
||||
$this->destError = 'No error yet.';
|
||||
$this->sourceError = 'No error yet.';
|
||||
$this->combinations = config('firefly.source_dests');
|
||||
$this->source = null;
|
||||
$this->destination = null;
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->createMode = false;
|
||||
$this->destError = 'No error yet.';
|
||||
$this->sourceError = 'No error yet.';
|
||||
$this->combinations = config('firefly.source_dests');
|
||||
$this->source = null;
|
||||
$this->destination = null;
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->userGroupAccountRepository = app(UserGroupAccountRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,6 +130,19 @@ class AccountValidator
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->accountRepository->setUser($user);
|
||||
$this->useUserGroupRepository = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserGroup $userGroup
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUserGroup(UserGroup $userGroup): void
|
||||
{
|
||||
$this->userGroup = $userGroup;
|
||||
$this->userGroupAccountRepository->setUserGroup($userGroup);
|
||||
$this->useUserGroupRepository = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,7 +282,7 @@ class AccountValidator
|
||||
|
||||
// find by ID
|
||||
if (null !== $accountId && $accountId > 0) {
|
||||
$first = $this->accountRepository->find($accountId);
|
||||
$first = $this->getRepository()->find($accountId);
|
||||
$accountType = null === $first ? 'invalid' : $first->accountType->type;
|
||||
$check = in_array($accountType, $validTypes, true);
|
||||
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
|
||||
@@ -277,7 +294,7 @@ class AccountValidator
|
||||
|
||||
// find by iban
|
||||
if (null !== $accountIban && '' !== (string)$accountIban) {
|
||||
$first = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
|
||||
$first = $this->getRepository()->findByIbanNull($accountIban, $validTypes);
|
||||
$accountType = null === $first ? 'invalid' : $first->accountType->type;
|
||||
$check = in_array($accountType, $validTypes, true);
|
||||
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
|
||||
@@ -289,7 +306,7 @@ class AccountValidator
|
||||
|
||||
// find by number
|
||||
if (null !== $accountNumber && '' !== (string)$accountNumber) {
|
||||
$first = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
|
||||
$first = $this->getRepository()->findByAccountNumber($accountNumber, $validTypes);
|
||||
$accountType = null === $first ? 'invalid' : $first->accountType->type;
|
||||
$check = in_array($accountType, $validTypes, true);
|
||||
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
|
||||
@@ -301,7 +318,7 @@ class AccountValidator
|
||||
|
||||
// find by name:
|
||||
if ('' !== (string)$accountName) {
|
||||
$first = $this->accountRepository->findByName($accountName, $validTypes);
|
||||
$first = $this->getRepository()->findByName($accountName, $validTypes);
|
||||
if (null !== $first) {
|
||||
app('log')->debug(sprintf('Name: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
|
||||
return $first;
|
||||
@@ -311,4 +328,16 @@ class AccountValidator
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AccountRepositoryInterface|UserGroupAccountRepositoryInterface
|
||||
*/
|
||||
private function getRepository(): AccountRepositoryInterface | UserGroupAccountRepositoryInterface
|
||||
{
|
||||
if ($this->useUserGroupRepository) {
|
||||
return $this->userGroupAccountRepository;
|
||||
}
|
||||
|
||||
return $this->accountRepository;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ namespace FireflyIII\Validation\Administration;
|
||||
|
||||
use FireflyIII\Enums\UserRoleEnum;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\UserRole;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
|
||||
@@ -47,7 +47,6 @@ use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException;
|
||||
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
|
||||
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
|
||||
use ValueError;
|
||||
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,7 +30,9 @@ use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Models\UserGroup;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
@@ -42,17 +44,20 @@ trait TransactionValidation
|
||||
/**
|
||||
* Validates the given account information. Switches on given transaction type.
|
||||
*
|
||||
* @param Validator $validator
|
||||
* Inclusion of user and/or group is optional.
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @param User|null $user
|
||||
* @param UserGroup|null $userGroup
|
||||
*/
|
||||
public function validateAccountInformation(Validator $validator): void
|
||||
public function validateAccountInformation(Validator $validator, User $user = null, UserGroup $userGroup = null): void
|
||||
{
|
||||
if ($validator->errors()->count() > 0) {
|
||||
return;
|
||||
}
|
||||
Log::debug('Now in validateAccountInformation (TransactionValidation) ()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
$data = $validator->getData();
|
||||
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
$data = $validator->getData();
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
|
||||
Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
|
||||
@@ -61,6 +66,8 @@ trait TransactionValidation
|
||||
* @var array $transaction
|
||||
*/
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$transaction['user'] = $user;
|
||||
$transaction['user_group'] = $userGroup;
|
||||
if (!is_int($index)) {
|
||||
continue;
|
||||
}
|
||||
@@ -107,6 +114,13 @@ trait TransactionValidation
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
if (array_key_exists('user', $transaction) && null !== $transaction['user']) {
|
||||
$accountValidator->setUser($transaction['user']);
|
||||
}
|
||||
if (array_key_exists('user_group', $transaction) && null !== $transaction['user_group']) {
|
||||
$accountValidator->setUserGroup($transaction['user_group']);
|
||||
}
|
||||
|
||||
$transactionType = $transaction['type'] ?? $transactionType;
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
@@ -204,7 +218,8 @@ trait TransactionValidation
|
||||
array $transaction,
|
||||
string $transactionType,
|
||||
int $index
|
||||
): void {
|
||||
): void
|
||||
{
|
||||
Log::debug('Now in sanityCheckForeignCurrency()');
|
||||
if (0 !== $validator->errors()->count()) {
|
||||
Log::debug('Already have errors, return');
|
||||
|
||||
Reference in New Issue
Block a user