| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-08-25 07:48:46 +02:00
										 |  |  |  * RuleStoreRequest.php | 
					
						
							| 
									
										
										
										
											2020-01-23 19:44:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02: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-06-30 16:46:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02: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-06-30 16:46:51 +02: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-06-30 16:46:51 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 17:43:06 +01:00
										 |  |  | namespace FireflyIII\Api\V1\Requests\Models\Rule; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-07 16:03:05 +01:00
										 |  |  | use FireflyIII\Rules\IsBoolean; | 
					
						
							| 
									
										
										
										
											2024-03-09 12:57:34 -05:00
										 |  |  | use FireflyIII\Rules\IsValidActionExpression; | 
					
						
							| 
									
										
										
										
											2020-11-08 13:36:13 +01:00
										 |  |  | use FireflyIII\Support\Request\ChecksLogin; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  | use FireflyIII\Support\Request\ConvertsDataTypes; | 
					
						
							| 
									
										
										
										
											2020-08-21 08:23:44 +02:00
										 |  |  | use FireflyIII\Support\Request\GetRuleConfiguration; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:25:25 +02:00
										 |  |  | use Illuminate\Foundation\Http\FormRequest; | 
					
						
							| 
									
										
										
										
											2024-01-09 20:58:18 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-03-06 17:43:06 +01:00
										 |  |  |  * Class StoreRequest | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-06 17:43:06 +01:00
										 |  |  | class StoreRequest extends FormRequest | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |     use ChecksLogin; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |     use ConvertsDataTypes; | 
					
						
							|  |  |  |     use GetRuleConfiguration; | 
					
						
							| 
									
										
										
										
											2020-11-08 13:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Get all data from the request. | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $fields           = [ | 
					
						
							| 
									
										
										
										
											2022-05-04 05:53:47 +02:00
										 |  |  |             'title'            => ['title', 'convertString'], | 
					
						
							|  |  |  |             'description'      => ['description', 'convertString'], | 
					
						
							| 
									
										
										
										
											2022-10-01 05:29:42 +02:00
										 |  |  |             'rule_group_id'    => ['rule_group_id', 'convertInteger'], | 
					
						
							|  |  |  |             'order'            => ['order', 'convertInteger'], | 
					
						
							| 
									
										
										
										
											2022-05-04 05:53:47 +02:00
										 |  |  |             'rule_group_title' => ['rule_group_title', 'convertString'], | 
					
						
							|  |  |  |             'trigger'          => ['trigger', 'convertString'], | 
					
						
							| 
									
										
										
										
											2021-03-14 16:08:49 +01:00
										 |  |  |             'strict'           => ['strict', 'boolean'], | 
					
						
							|  |  |  |             'stop_processing'  => ['stop_processing', 'boolean'], | 
					
						
							|  |  |  |             'active'           => ['active', 'boolean'], | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $data             = $this->getAllData($fields); | 
					
						
							| 
									
										
										
										
											2021-03-14 16:08:49 +01:00
										 |  |  |         $data['triggers'] = $this->getRuleTriggers(); | 
					
						
							|  |  |  |         $data['actions']  = $this->getRuleActions(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private function getRuleTriggers(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $triggers = $this->get('triggers'); | 
					
						
							|  |  |  |         $return   = []; | 
					
						
							|  |  |  |         if (is_array($triggers)) { | 
					
						
							|  |  |  |             foreach ($triggers as $trigger) { | 
					
						
							|  |  |  |                 $return[] = [ | 
					
						
							|  |  |  |                     'type'            => $trigger['type'], | 
					
						
							|  |  |  |                     'value'           => $trigger['value'], | 
					
						
							|  |  |  |                     'active'          => $this->convertBoolean((string)($trigger['active'] ?? 'true')), | 
					
						
							|  |  |  |                     'stop_processing' => $this->convertBoolean((string)($trigger['stop_processing'] ?? 'false')), | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getRuleActions(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $actions = $this->get('actions'); | 
					
						
							|  |  |  |         $return  = []; | 
					
						
							|  |  |  |         if (is_array($actions)) { | 
					
						
							|  |  |  |             foreach ($actions as $action) { | 
					
						
							|  |  |  |                 $return[] = [ | 
					
						
							|  |  |  |                     'type'            => $action['type'], | 
					
						
							|  |  |  |                     'value'           => $action['value'], | 
					
						
							|  |  |  |                     'active'          => $this->convertBoolean((string)($action['active'] ?? 'true')), | 
					
						
							|  |  |  |                     'stop_processing' => $this->convertBoolean((string)($action['stop_processing'] ?? 'false')), | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |         $validTriggers   = $this->getTriggers(); | 
					
						
							|  |  |  |         $validActions    = array_keys(config('firefly.rule-actions')); | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 21:42:08 +02:00
										 |  |  |         // some triggers and actions require text:
 | 
					
						
							| 
									
										
										
										
											2020-08-21 08:23:44 +02:00
										 |  |  |         $contextTriggers = implode(',', $this->getTriggersWithContext()); | 
					
						
							| 
									
										
										
										
											2018-10-16 21:42:08 +02:00
										 |  |  |         $contextActions  = implode(',', config('firefly.context-rule-actions')); | 
					
						
							| 
									
										
										
										
											2020-03-15 08:16:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2024-01-05 09:48:59 +01:00
										 |  |  |             'title'                      => 'required|min:1|max:100|uniqueObjectForUser:rules,title', | 
					
						
							|  |  |  |             'description'                => 'min:1|max:32768|nullable', | 
					
						
							| 
									
										
										
										
											2021-03-14 16:08:49 +01:00
										 |  |  |             'rule_group_id'              => 'belongsToUser:rule_groups|required_without:rule_group_title', | 
					
						
							| 
									
										
										
										
											2024-01-05 09:48:59 +01:00
										 |  |  |             'rule_group_title'           => 'nullable|min:1|max:255|required_without:rule_group_id|belongsToUser:rule_groups,title', | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  |             'trigger'                    => 'required|in:store-journal,update-journal', | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |             'triggers.*.type'            => 'required|in:'.implode(',', $validTriggers), | 
					
						
							|  |  |  |             'triggers.*.value'           => 'required_if:actions.*.type,'.$contextTriggers.'|min:1|ruleTriggerValue|max:1024', | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |             'triggers.*.stop_processing' => [new IsBoolean()], | 
					
						
							|  |  |  |             'triggers.*.active'          => [new IsBoolean()], | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |             'actions.*.type'             => 'required|in:'.implode(',', $validActions), | 
					
						
							| 
									
										
										
										
											2024-03-10 06:24:32 +01:00
										 |  |  |             'actions.*.value'            => [sprintf('required_if:actions.*.type,%s', $contextActions), new IsValidActionExpression(), 'ruleActionValue'], | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |             'actions.*.stop_processing'  => [new IsBoolean()], | 
					
						
							|  |  |  |             'actions.*.active'           => [new IsBoolean()], | 
					
						
							|  |  |  |             'strict'                     => [new IsBoolean()], | 
					
						
							|  |  |  |             'stop_processing'            => [new IsBoolean()], | 
					
						
							|  |  |  |             'active'                     => [new IsBoolean()], | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Configure the validator instance. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function withValidator(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $validator->after( | 
					
						
							| 
									
										
										
										
											2023-12-21 05:07:26 +01:00
										 |  |  |             function (Validator $validator): void { | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |                 $this->atLeastOneTrigger($validator); | 
					
						
							|  |  |  |                 $this->atLeastOneAction($validator); | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |                 $this->atLeastOneActiveTrigger($validator); | 
					
						
							|  |  |  |                 $this->atLeastOneActiveAction($validator); | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2024-02-22 01:29:01 +01:00
										 |  |  |         if ($validator->fails()) { | 
					
						
							| 
									
										
										
										
											2024-01-09 20:58:18 +01:00
										 |  |  |             Log::channel('audit')->error(sprintf('Validation errors in %s', __CLASS__), $validator->errors()->toArray()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:41:13 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Adds an error to the validator when there are no triggers in the array of data. | 
					
						
							| 
									
										
										
										
											2018-08-05 15:41:13 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function atLeastOneTrigger(Validator $validator): void | 
					
						
							| 
									
										
										
										
											2018-08-05 15:41:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $data     = $validator->getData(); | 
					
						
							|  |  |  |         $triggers = $data['triggers'] ?? []; | 
					
						
							| 
									
										
										
										
											2019-05-30 12:39:06 +02:00
										 |  |  |         // need at least one trigger
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (!is_countable($triggers) || 0 === count($triggers)) { | 
					
						
							|  |  |  |             $validator->errors()->add('title', (string)trans('validation.at_least_one_trigger')); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:56:27 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Adds an error to the validator when there are no repetitions in the array of data. | 
					
						
							| 
									
										
										
										
											2022-03-29 14:56:27 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function atLeastOneAction(Validator $validator): void | 
					
						
							| 
									
										
										
										
											2022-03-29 14:56:27 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $data    = $validator->getData(); | 
					
						
							|  |  |  |         $actions = $data['actions'] ?? []; | 
					
						
							|  |  |  |         // need at least one trigger
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |         if (!is_countable($actions) || 0 === count($actions)) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $validator->errors()->add('title', (string)trans('validation.at_least_one_action')); | 
					
						
							| 
									
										
										
										
											2018-08-05 15:41:13 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-09 08:26:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Adds an error to the validator when there are no ACTIVE triggers in the array of data. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function atLeastOneActiveTrigger(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |         $data          = $validator->getData(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|array|int|string $triggers */ | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |         $triggers      = $data['triggers'] ?? []; | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |         // need at least one trigger
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |         if (!is_countable($triggers) || 0 === count($triggers)) { | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $allInactive   = true; | 
					
						
							|  |  |  |         $inactiveIndex = 0; | 
					
						
							|  |  |  |         foreach ($triggers as $index => $trigger) { | 
					
						
							|  |  |  |             $active = array_key_exists('active', $trigger) ? $trigger['active'] : true; // assume true
 | 
					
						
							|  |  |  |             if (true === $active) { | 
					
						
							|  |  |  |                 $allInactive = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (false === $active) { | 
					
						
							|  |  |  |                 $inactiveIndex = $index; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (true === $allInactive) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             $validator->errors()->add(sprintf('triggers.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_trigger')); | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Adds an error to the validator when there are no ACTIVE actions in the array of data. | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function atLeastOneActiveAction(Validator $validator): void | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |         $data          = $validator->getData(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|array|int|string $actions */ | 
					
						
							| 
									
										
										
										
											2024-03-10 08:11:58 +01:00
										 |  |  |         $actions       = $data['actions'] ?? []; | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |         // need at least one trigger
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (!is_countable($actions) || 0 === count($actions)) { | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $allInactive   = true; | 
					
						
							|  |  |  |         $inactiveIndex = 0; | 
					
						
							|  |  |  |         foreach ($actions as $index => $action) { | 
					
						
							|  |  |  |             $active = array_key_exists('active', $action) ? $action['active'] : true; // assume true
 | 
					
						
							|  |  |  |             if (true === $active) { | 
					
						
							|  |  |  |                 $allInactive = false; | 
					
						
							| 
									
										
										
										
											2022-02-07 09:42:23 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             if (false === $active) { | 
					
						
							|  |  |  |                 $inactiveIndex = $index; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-06-09 08:26:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (true === $allInactive) { | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('actions.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_action')); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-09 08:26:23 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |