firefly-iii/app/Rules/BelongsUser.php

251 lines
6.5 KiB
PHP
Raw Normal View History

<?php
2018-02-23 09:59:21 -06:00
/**
* BelongsUser.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
namespace FireflyIII\Rules;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\PiggyBank;
use Illuminate\Contracts\Validation\Rule;
/**
* Class BelongsUser
*/
class BelongsUser implements Rule
{
/**
* Create a new rule instance.
*
* @return void
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*/
public function __construct()
{
//
}
/**
* Get the validation error message.
*
* @return string
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*/
public function message(): string
{
2018-07-15 02:38:49 -05:00
return (string)trans('validation.belongs_user');
}
/**
* Determine if the validation rule passes.
2018-03-10 15:38:20 -06:00
*
* @param string $attribute
* @param mixed $value
*
* @return bool
* @throws FireflyException
2018-07-25 23:10:17 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
2018-07-25 23:10:17 -05:00
public function passes($attribute, $value): bool
{
$attribute = $this->parseAttribute($attribute);
if (!auth()->check()) {
2018-02-18 13:40:32 -06:00
return true; // @codeCoverageIgnore
}
2018-04-02 07:50:17 -05:00
$attribute = (string)$attribute;
switch ($attribute) {
case 'piggy_bank_id':
2018-07-25 23:10:17 -05:00
return $this->validatePiggyBankId((int)$value);
case 'piggy_bank_name':
2018-07-25 23:10:17 -05:00
return $this->validatePiggyBankName($value);
case 'bill_id':
2018-07-25 23:10:17 -05:00
return $this->validateBillId((int)$value);
2018-02-18 13:40:32 -06:00
case 'bill_name':
2018-07-25 23:10:17 -05:00
return $this->validateBillName($value);
case 'budget_id':
2018-07-25 23:10:17 -05:00
return $this->validateBudgetId((int)$value);
case 'category_id':
2018-07-25 23:10:17 -05:00
return $this->validateCategoryId((int)$value);
case 'budget_name':
2018-07-25 23:10:17 -05:00
return $this->validateBudgetName($value);
2018-02-18 03:31:15 -06:00
case 'source_id':
case 'destination_id':
2018-07-25 23:10:17 -05:00
return $this->validateAccountId((int)$value);
default:
2018-02-18 13:40:32 -06:00
throw new FireflyException(sprintf('Rule BelongUser cannot handle "%s"', $attribute)); // @codeCoverageIgnore
}
}
/**
* @param string $class
* @param string $field
* @param string $value
*
* @return int
2018-07-25 23:10:17 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function countField(string $class, string $field, string $value): int
{
2018-07-25 23:10:17 -05:00
$objects = [];
// get all objects belonging to user:
2018-07-25 23:10:17 -05:00
if (PiggyBank::class === $class) {
$objects = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('accounts.user_id', '=', auth()->user()->id)->get(['piggy_banks.*']);
}
if (PiggyBank::class !== $class) {
$objects = $class::where('user_id', '=', auth()->user()->id)->get();
}
$count = 0;
foreach ($objects as $object) {
2018-04-02 07:50:17 -05:00
if (trim((string)$object->$field) === trim($value)) {
$count++;
}
}
return $count;
}
/**
* @param string $attribute
*
* @return string
*/
private function parseAttribute(string $attribute): string
{
$parts = explode('.', $attribute);
if (1 === count($parts)) {
return $attribute;
}
if (3 === count($parts)) {
return $parts[2];
}
2018-02-18 13:40:32 -06:00
return $attribute; // @codeCoverageIgnore
}
2018-07-25 23:10:17 -05:00
/**
* @param int $value
*
* @return bool
*/
private function validateAccountId(int $value): bool
{
2018-07-26 22:03:37 -05:00
$count = Account::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count();
2018-07-25 23:10:17 -05:00
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateBillId(int $value): bool
{
$count = Bill::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count();
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validateBillName(string $value): bool
{
$count = $this->countField(Bill::class, 'name', $value);
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateBudgetId(int $value): bool
{
$count = Budget::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count();
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validateBudgetName(string $value): bool
{
$count = $this->countField(Budget::class, 'name', $value);
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateCategoryId(int $value): bool
{
$count = Category::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count();
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validatePiggyBankId(int $value): bool
{
$count = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('piggy_banks.id', '=', $value)
->where('accounts.user_id', '=', auth()->user()->id)->count();
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validatePiggyBankName(string $value): bool
{
$count = $this->countField(PiggyBank::class, 'name', $value);
return 1 === $count;
}
}