| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Validation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  | use Auth; | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  | use Config; | 
					
						
							| 
									
										
										
										
											2015-03-30 20:16:33 +02:00
										 |  |  | use Crypt; | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | use DB; | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2015-06-27 08:06:24 +02:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:34:45 +02:00
										 |  |  | use Illuminate\Contracts\Encryption\DecryptException; | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2015-05-01 18:44:49 +02:00
										 |  |  | use Symfony\Component\Translation\TranslatorInterface; | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class FireflyValidator | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Validation | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | class FireflyValidator extends Validator | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-01 18:44:49 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TranslatorInterface $translator | 
					
						
							|  |  |  |      * @param array               $data | 
					
						
							|  |  |  |      * @param array               $rules | 
					
						
							|  |  |  |      * @param array               $messages | 
					
						
							|  |  |  |      * @param array               $customAttributes | 
					
						
							| 
									
										
										
										
											2015-05-17 10:10:58 +02:00
										 |  |  |      * @SuppressWarnings(PHPMD.ExcessiveParameterList) | 
					
						
							| 
									
										
										
										
											2015-05-01 18:44:49 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = []) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-17 10:10:58 +02:00
										 |  |  |         parent::__construct($translator, $data, $rules, $messages, $customAttributes); | 
					
						
							| 
									
										
										
										
											2015-05-01 18:44:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $attribute | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateBelongsToUser($attribute, $value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |         $count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count(); | 
					
						
							|  |  |  |         if ($count == 1) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $attribute | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateUniqueAccountForUser($attribute, $value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |         // because a user does not have to be logged in (tests and what-not).
 | 
					
						
							|  |  |  |         if (!Auth::check()) { | 
					
						
							|  |  |  |             return $this->validateAccountAnonymously(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->data['what'])) { | 
					
						
							|  |  |  |             return $this->validateByAccountTypeString($value, $parameters); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->data['account_type_id'])) { | 
					
						
							|  |  |  |             return $this->validateByAccountTypeId($value, $parameters); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-28 21:13:08 +02:00
										 |  |  |         if(isset($this->data['id'])) { | 
					
						
							|  |  |  |             return $this->validateByAccountId($value, $parameters); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-27 09:24:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:34:45 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-30 20:16:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function validateAccountAnonymously() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!isset($this->data['user_id'])) { | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-03-27 07:20:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |         $user  = User::find($this->data['user_id']); | 
					
						
							|  |  |  |         $type  = AccountType::find($this->data['account_type_id'])->first(); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:48:58 +02:00
										 |  |  |         $value = $this->tryDecrypt($this->data['name']); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $set = $user->accounts()->where('account_type_id', $type->id)->get(); | 
					
						
							|  |  |  |         /** @var Account $entry */ | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             if ($entry->name == $value) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:48:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function tryDecrypt($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $value = Crypt::decrypt($value); | 
					
						
							|  |  |  |         } catch (DecryptException $e) { | 
					
						
							|  |  |  |             // do not care.
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function validateByAccountTypeString($value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']); | 
					
						
							|  |  |  |         $type   = AccountType::whereType($search)->first(); | 
					
						
							|  |  |  |         $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); | 
					
						
							|  |  |  |         /** @var Account $entry */ | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             if ($entry->name == $value) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function validateByAccountTypeId($value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type   = AccountType::find($this->data['account_type_id'])->first(); | 
					
						
							|  |  |  |         $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:48:58 +02:00
										 |  |  |         $value  = $this->tryDecrypt($value); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  |         /** @var Account $entry */ | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             if ($entry->name == $value) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-28 21:13:08 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function validateByAccountId($value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Account $existingAccount */ | 
					
						
							|  |  |  |         $existingAccount = Account::find($this->data['id']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $type   = $existingAccount->accountType; | 
					
						
							|  |  |  |         $ignore = $existingAccount->id; | 
					
						
							|  |  |  |         $value  = $this->tryDecrypt($value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); | 
					
						
							|  |  |  |         /** @var Account $entry */ | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             if ($entry->name == $value) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $attribute | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  |     public function validateUniqueForUser($attribute, $value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         $query = DB::table($parameters[0])->where($parameters[1], $value); | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  |         $query->where('user_id', Auth::user()->id); | 
					
						
							| 
									
										
										
										
											2015-05-26 19:38:52 +02:00
										 |  |  |         if (isset($parameters[2])) { | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |             $query->where('id', '!=', $parameters[2]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $count = $query->count(); | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |         if ($count == 0) { | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Validate an object and its unicity. Checks for encryption / encrypted values as well. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * parameter 0: the table | 
					
						
							|  |  |  |      * parameter 1: the field | 
					
						
							| 
									
										
										
										
											2015-06-05 12:34:45 +02:00
										 |  |  |      * parameter 2: an id to ignore (when editing) | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param $attribute | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateUniqueObjectForUser($attribute, $value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-05 12:48:58 +02:00
										 |  |  |         $value = $this->tryDecrypt($value); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:34:45 +02:00
										 |  |  |         // exclude?
 | 
					
						
							|  |  |  |         $table   = $parameters[0]; | 
					
						
							|  |  |  |         $field   = $parameters[1]; | 
					
						
							| 
									
										
										
										
											2015-06-07 09:09:27 +02:00
										 |  |  |         $exclude = isset($parameters[2]) ? intval($parameters[2]) : 0; | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:34:45 +02:00
										 |  |  |         // get entries from table
 | 
					
						
							|  |  |  |         $set = DB::table($table)->where('user_id', Auth::user()->id)->where('id', '!=', $exclude)->get([$field]); | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2015-06-05 12:48:58 +02:00
										 |  |  |             $fieldValue = $this->tryDecrypt($entry->$field); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:34:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ($fieldValue === $value) { | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $attribute | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * @param $parameters | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateUniquePiggyBankForUser($attribute, $value, $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |         $exclude = isset($parameters[0]) ? $parameters[0] : null; | 
					
						
							|  |  |  |         $query   = DB::table('piggy_banks'); | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |         $query->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id'); | 
					
						
							|  |  |  |         $query->where('accounts.user_id', Auth::user()->id); | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |         if (!is_null($exclude)) { | 
					
						
							|  |  |  |             $query->where('piggy_banks.id', '!=', $exclude); | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |         $set = $query->get(['piggy_banks.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-27 08:06:24 +02:00
										 |  |  |         /** @var PiggyBank $entry */ | 
					
						
							| 
									
										
										
										
											2015-04-07 18:26:14 +02:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2015-06-06 23:09:12 +02:00
										 |  |  |             $fieldValue = $this->tryDecrypt($entry->name); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:48:58 +02:00
										 |  |  |             if ($fieldValue == $value) { | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |