2018-02-16 08:17:36 -06:00
|
|
|
<?php
|
2018-03-05 12:35:58 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-16 08:17:36 -06:00
|
|
|
|
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-02-16 08:17:36 -06:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation error message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
|
|
|
return trans('validation.belongs_user');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the validation rule passes.
|
2018-03-10 15:38:20 -06:00
|
|
|
*
|
2018-02-16 08:17:36 -06:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
|
|
|
$attribute = $this->parseAttribute($attribute);
|
|
|
|
if (!auth()->check()) {
|
2018-02-18 13:40:32 -06:00
|
|
|
return true; // @codeCoverageIgnore
|
2018-02-16 08:17:36 -06:00
|
|
|
}
|
2018-04-02 07:50:17 -05:00
|
|
|
$attribute = (string)$attribute;
|
2018-02-16 08:17:36 -06:00
|
|
|
switch ($attribute) {
|
|
|
|
case 'piggy_bank_id':
|
|
|
|
$count = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
|
2018-04-02 07:50:17 -05:00
|
|
|
->where('piggy_banks.id', '=', (int)$value)
|
2018-02-16 08:17:36 -06:00
|
|
|
->where('accounts.user_id', '=', auth()->user()->id)->count();
|
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
|
|
|
case 'piggy_bank_name':
|
|
|
|
$count = $this->countField(PiggyBank::class, 'name', $value);
|
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
|
|
|
case 'bill_id':
|
2018-04-02 07:50:17 -05:00
|
|
|
$count = Bill::where('id', '=', (int)$value)->where('user_id', '=', auth()->user()->id)->count();
|
2018-02-16 08:17:36 -06:00
|
|
|
|
|
|
|
return $count === 1;
|
2018-02-18 13:40:32 -06:00
|
|
|
case 'bill_name':
|
|
|
|
$count = $this->countField(Bill::class, 'name', $value);
|
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
2018-02-16 08:17:36 -06:00
|
|
|
case 'budget_id':
|
2018-04-02 07:50:17 -05:00
|
|
|
$count = Budget::where('id', '=', (int)$value)->where('user_id', '=', auth()->user()->id)->count();
|
2018-02-16 08:17:36 -06:00
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
|
|
|
case 'category_id':
|
2018-04-02 07:50:17 -05:00
|
|
|
$count = Category::where('id', '=', (int)$value)->where('user_id', '=', auth()->user()->id)->count();
|
2018-02-16 08:17:36 -06:00
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
|
|
|
case 'budget_name':
|
|
|
|
$count = $this->countField(Budget::class, 'name', $value);
|
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
2018-02-18 03:31:15 -06:00
|
|
|
case 'source_id':
|
|
|
|
case 'destination_id':
|
2018-04-02 07:50:17 -05:00
|
|
|
$count = Account::where('id', '=', (int)$value)->where('user_id', '=', auth()->user()->id)->count();
|
2018-02-16 08:17:36 -06:00
|
|
|
|
|
|
|
return $count === 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-02-18 13:40:32 -06:00
|
|
|
throw new FireflyException(sprintf('Rule BelongUser cannot handle "%s"', $attribute)); // @codeCoverageIgnore
|
2018-02-16 08:17:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param string $field
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function countField(string $class, string $field, string $value): int
|
|
|
|
{
|
|
|
|
// get all objects belonging to user:
|
|
|
|
switch ($class) {
|
|
|
|
case PiggyBank::class:
|
|
|
|
$objects = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
|
|
|
|
->where('accounts.user_id', '=', auth()->user()->id)->get(['piggy_banks.*']);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$objects = $class::where('user_id', '=', auth()->user()->id)->get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$count = 0;
|
|
|
|
foreach ($objects as $object) {
|
2018-04-02 07:50:17 -05:00
|
|
|
if (trim((string)$object->$field) === trim($value)) {
|
2018-02-16 08:17:36 -06:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $attribute
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function parseAttribute(string $attribute): string
|
|
|
|
{
|
|
|
|
$parts = explode('.', $attribute);
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\count($parts) === 1) {
|
2018-02-16 08:17:36 -06:00
|
|
|
return $attribute;
|
|
|
|
}
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\count($parts) === 3) {
|
2018-02-16 08:17:36 -06:00
|
|
|
return $parts[2];
|
|
|
|
}
|
|
|
|
|
2018-02-18 13:40:32 -06:00
|
|
|
return $attribute; // @codeCoverageIgnore
|
2018-02-16 08:17:36 -06:00
|
|
|
}
|
|
|
|
}
|