| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BelongsUser.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:25 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2018-02-23 16:59:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01: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; | 
					
						
							| 
									
										
										
										
											2019-08-09 05:58:52 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BelongsUser | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BelongsUser implements Rule | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new rule instance. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the validation error message. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-22 21:32:58 +02:00
										 |  |  |     public function message(): string | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-15 09:38:49 +02:00
										 |  |  |         return (string)trans('validation.belongs_user'); | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Determine if the validation rule passes. | 
					
						
							| 
									
										
										
										
											2018-03-10 22:38:20 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-09-01 14:49:26 +02:00
										 |  |  |      * @param string $attribute | 
					
						
							|  |  |  |      * @param mixed  $value | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |     public function passes($attribute, $value): bool | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $attribute = $this->parseAttribute($attribute); | 
					
						
							|  |  |  |         if (!auth()->check()) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         $attribute = (string)$attribute; | 
					
						
							| 
									
										
										
										
											2019-08-09 05:58:52 +02:00
										 |  |  |         Log::debug(sprintf('Going to validate %s', $attribute)); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return match ($attribute) { | 
					
						
							|  |  |  |             'piggy_bank_id' => $this->validatePiggyBankId((int)$value), | 
					
						
							|  |  |  |             'piggy_bank_name' => $this->validatePiggyBankName($value), | 
					
						
							|  |  |  |             'bill_id' => $this->validateBillId((int)$value), | 
					
						
							|  |  |  |             'bill_name' => $this->validateBillName($value), | 
					
						
							|  |  |  |             'budget_id' => $this->validateBudgetId((int)$value), | 
					
						
							|  |  |  |             'category_id' => $this->validateCategoryId((int)$value), | 
					
						
							|  |  |  |             'budget_name' => $this->validateBudgetName($value), | 
					
						
							|  |  |  |             'source_id', 'destination_id' => $this->validateAccountId((int)$value), | 
					
						
							|  |  |  |             default => throw new FireflyException(sprintf('Rule BelongUser cannot handle "%s"', $attribute)), | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @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]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |         return $attribute; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $class | 
					
						
							|  |  |  |      * @param string $field | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return int | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function countField(string $class, string $field, string $value): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-09 05:58:52 +02:00
										 |  |  |         $value   = trim($value); | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |         $objects = []; | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |         // get all objects belonging to user:
 | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02: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(); | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $count = 0; | 
					
						
							|  |  |  |         foreach ($objects as $object) { | 
					
						
							| 
									
										
										
										
											2019-08-09 05:58:52 +02:00
										 |  |  |             $objectValue = trim((string)$object->$field); | 
					
						
							|  |  |  |             Log::debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value)); | 
					
						
							|  |  |  |             if ($objectValue === $value) { | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |                 $count++; | 
					
						
							| 
									
										
										
										
											2019-08-09 05:58:52 +02:00
										 |  |  |                 Log::debug(sprintf('Hit! Count is now %d', $count)); | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $count; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param int $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validateBillId(int $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-04 08:31:15 +02:00
										 |  |  |         if (0 === $value) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |         $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); | 
					
						
							| 
									
										
										
										
											2019-08-09 05:58:52 +02:00
										 |  |  |         Log::debug(sprintf('Result of countField for bill name "%s" is %d', $value, $count)); | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 1 === $count; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param int $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validateBudgetId(int $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-01 14:49:26 +02:00
										 |  |  |         if (0 === $value) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |         $count = Budget::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @param string $value | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private function validateBudgetName(string $value): bool | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $count = $this->countField(Budget::class, 'name', $value); | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 1 === $count; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @param int $value | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private function validateAccountId(int $value): bool | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (0 === $value) { | 
					
						
							|  |  |  |             // its ok to submit 0. other checks will fail.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $count = Account::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count(); | 
					
						
							| 
									
										
										
										
											2018-07-26 06:10:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 1 === $count; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-16 15:17:36 +01:00
										 |  |  | } |