Merge remote-tracking branch 'upstream/main' into feat/expression-engine

This commit is contained in:
Michael Thomas
2024-03-06 21:38:40 -05:00
2225 changed files with 111169 additions and 148980 deletions

View File

@@ -28,27 +28,24 @@ use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Support\Request\GetRuleConfiguration;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
use function is_array;
/**
* Class StoreRequest
*/
class StoreRequest extends FormRequest
{
use ChecksLogin;
use ConvertsDataTypes;
use GetRuleConfiguration;
use ChecksLogin;
/**
* Get all data from the request.
*
* @return array
*/
public function getAll(): array
{
$fields = [
$fields = [
'title' => ['title', 'convertString'],
'description' => ['description', 'convertString'],
'rule_group_id' => ['rule_group_id', 'convertInteger'],
@@ -59,7 +56,7 @@ class StoreRequest extends FormRequest
'stop_processing' => ['stop_processing', 'boolean'],
'active' => ['active', 'boolean'],
];
$data = $this->getAllData($fields);
$data = $this->getAllData($fields);
$data['triggers'] = $this->getRuleTriggers();
$data['actions'] = $this->getRuleActions();
@@ -67,9 +64,6 @@ class StoreRequest extends FormRequest
return $data;
}
/**
* @return array
*/
private function getRuleTriggers(): array
{
$triggers = $this->get('triggers');
@@ -88,9 +82,6 @@ class StoreRequest extends FormRequest
return $return;
}
/**
* @return array
*/
private function getRuleActions(): array
{
$actions = $this->get('actions');
@@ -111,30 +102,28 @@ class StoreRequest extends FormRequest
/**
* The rules that the incoming request must be matched against.
*
* @return array
*/
public function rules(): array
{
$validTriggers = $this->getTriggers();
$validActions = array_keys(config('firefly.rule-actions'));
$validTriggers = $this->getTriggers();
$validActions = array_keys(config('firefly.rule-actions'));
// some triggers and actions require text:
$contextTriggers = implode(',', $this->getTriggersWithContext());
$contextActions = implode(',', config('firefly.context-rule-actions'));
return [
'title' => 'required|between:1,100|uniqueObjectForUser:rules,title',
'description' => 'between:1,5000|nullable',
'title' => 'required|min:1|max:100|uniqueObjectForUser:rules,title',
'description' => 'min:1|max:32768|nullable',
'rule_group_id' => 'belongsToUser:rule_groups|required_without:rule_group_title',
'rule_group_title' => 'nullable|between:1,255|required_without:rule_group_id|belongsToUser:rule_groups,title',
'rule_group_title' => 'nullable|min:1|max:255|required_without:rule_group_id|belongsToUser:rule_groups,title',
'trigger' => 'required|in:store-journal,update-journal',
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers),
'triggers.*.value' => 'required_if:actions.*.type,' . $contextTriggers . '|min:1|ruleTriggerValue|max:1024',
'triggers.*.type' => 'required|in:'.implode(',', $validTriggers),
'triggers.*.value' => 'required_if:actions.*.type,'.$contextTriggers.'|min:1|ruleTriggerValue|max:1024',
'triggers.*.stop_processing' => [new IsBoolean()],
'triggers.*.active' => [new IsBoolean()],
'actions.*.type' => 'required|in:' . implode(',', $validActions),
'actions.*.value' => 'required_if:actions.*.type,' . $contextActions . '|ruleActionValue',
'actions.*.type' => 'required|in:'.implode(',', $validActions),
'actions.*.value' => 'required_if:actions.*.type,'.$contextActions.'|ruleActionValue',
'actions.*.stop_processing' => [new IsBoolean()],
'actions.*.active' => [new IsBoolean()],
'strict' => [new IsBoolean()],
@@ -145,27 +134,24 @@ class StoreRequest extends FormRequest
/**
* Configure the validator instance.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
function (Validator $validator): void {
$this->atLeastOneTrigger($validator);
$this->atLeastOneAction($validator);
$this->atLeastOneActiveTrigger($validator);
$this->atLeastOneActiveAction($validator);
}
);
if ($validator->fails()) {
Log::channel('audit')->error(sprintf('Validation errors in %s', __CLASS__), $validator->errors()->toArray());
}
}
/**
* Adds an error to the validator when there are no triggers in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneTrigger(Validator $validator): void
{
@@ -179,8 +165,6 @@ class StoreRequest extends FormRequest
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneAction(Validator $validator): void
{
@@ -194,13 +178,13 @@ class StoreRequest extends FormRequest
/**
* Adds an error to the validator when there are no ACTIVE triggers in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneActiveTrigger(Validator $validator): void
{
$data = $validator->getData();
$triggers = $data['triggers'] ?? [];
$data = $validator->getData();
/** @var null|array|int|string $triggers */
$triggers = $data['triggers'] ?? [];
// need at least one trigger
if (!is_countable($triggers) || 0 === count($triggers)) {
return;
@@ -223,13 +207,13 @@ class StoreRequest extends FormRequest
/**
* Adds an error to the validator when there are no ACTIVE actions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneActiveAction(Validator $validator): void
{
$data = $validator->getData();
$actions = $data['actions'] ?? [];
$data = $validator->getData();
/** @var null|array|int|string $actions */
$actions = $data['actions'] ?? [];
// need at least one trigger
if (!is_countable($actions) || 0 === count($actions)) {
return;

View File

@@ -34,12 +34,9 @@ use Illuminate\Foundation\Http\FormRequest;
*/
class TestRequest extends FormRequest
{
use ConvertsDataTypes;
use ChecksLogin;
use ConvertsDataTypes;
/**
* @return array
*/
public function getTestParameters(): array
{
return [
@@ -47,39 +44,34 @@ class TestRequest extends FormRequest
'start' => $this->getDate('start'),
'end' => $this->getDate('end'),
'accounts' => $this->getAccounts(),
];
}
/**
* @return int
*/
private function getPage(): int
{
return 0 === (int)$this->query('page') ? 1 : (int)$this->query('page');
}
/**
* @param string $field
*
* @return Carbon|null
*/
private function getDate(string $field): ?Carbon
{
return null === $this->query($field) ? null : Carbon::createFromFormat('Y-m-d', $this->query($field));
$value = $this->query($field);
if (is_array($value)) {
return null;
}
$value = (string)$value;
$result = null === $this->query($field) ? null : Carbon::createFromFormat('Y-m-d', substr($value, 0, 10));
if (false === $result) {
return null;
}
return $result;
}
/**
* @return array
*/
private function getAccounts(): array
{
return $this->get('accounts');
}
/**
* @return array
*/
public function rules(): array
{
return [

View File

@@ -34,12 +34,9 @@ use Illuminate\Foundation\Http\FormRequest;
*/
class TriggerRequest extends FormRequest
{
use ConvertsDataTypes;
use ChecksLogin;
use ConvertsDataTypes;
/**
* @return array
*/
public function getTriggerParameters(): array
{
return [
@@ -49,27 +46,26 @@ class TriggerRequest extends FormRequest
];
}
/**
* @param string $field
*
* @return Carbon|null
*/
private function getDate(string $field): ?Carbon
{
return null === $this->query($field) ? null : Carbon::createFromFormat('Y-m-d', substr($this->query($field), 0, 10));
$value = $this->query($field);
if (is_array($value)) {
return null;
}
$value = (string)$value;
$result = null === $this->query($field) ? null : Carbon::createFromFormat('Y-m-d', substr($value, 0, 10));
if (false === $result) {
return null;
}
return $result;
}
/**
* @return array
*/
private function getAccounts(): array
{
return $this->get('accounts') ?? [];
}
/**
* @return array
*/
public function rules(): array
{
return [

View File

@@ -29,27 +29,24 @@ use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Support\Request\GetRuleConfiguration;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
use function is_array;
/**
* Class UpdateRequest
*/
class UpdateRequest extends FormRequest
{
use ChecksLogin;
use ConvertsDataTypes;
use GetRuleConfiguration;
use ChecksLogin;
/**
* Get all data from the request.
*
* @return array
*/
public function getAll(): array
{
$fields = [
$fields = [
'title' => ['title', 'convertString'],
'description' => ['description', 'stringWithNewlines'],
'rule_group_id' => ['rule_group_id', 'convertInteger'],
@@ -73,9 +70,6 @@ class UpdateRequest extends FormRequest
return $return;
}
/**
* @return array|null
*/
private function getRuleTriggers(): ?array
{
if (!$this->has('triggers')) {
@@ -99,9 +93,6 @@ class UpdateRequest extends FormRequest
return $return;
}
/**
* @return array|null
*/
private function getRuleActions(): ?array
{
if (!$this->has('actions')) {
@@ -125,65 +116,60 @@ class UpdateRequest extends FormRequest
/**
* The rules that the incoming request must be matched against.
*
* @return array
*/
public function rules(): array
{
$validTriggers = $this->getTriggers();
$validActions = array_keys(config('firefly.rule-actions'));
$validTriggers = $this->getTriggers();
$validActions = array_keys(config('firefly.rule-actions'));
/** @var Rule $rule */
$rule = $this->route()->parameter('rule');
$rule = $this->route()->parameter('rule');
// some triggers and actions require text:
$contextTriggers = implode(',', $this->getTriggersWithContext());
$contextActions = implode(',', config('firefly.context-rule-actions'));
return [
'title' => sprintf('between:1,100|uniqueObjectForUser:rules,title,%d', $rule->id),
'description' => 'between:1,5000|nullable',
'title' => sprintf('min:1|max:100|uniqueObjectForUser:rules,title,%d', $rule->id),
'description' => 'min:1|max:32768|nullable',
'rule_group_id' => 'belongsToUser:rule_groups',
'rule_group_title' => 'nullable|between:1,255|belongsToUser:rule_groups,title',
'rule_group_title' => 'nullable|min:1|max:255|belongsToUser:rule_groups,title',
'trigger' => 'in:store-journal,update-journal',
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers),
'triggers.*.value' => 'required_if:actions.*.type,' . $contextTriggers . '|min:1|ruleTriggerValue|max:1024',
'triggers.*.type' => 'required|in:'.implode(',', $validTriggers),
'triggers.*.value' => 'required_if:actions.*.type,'.$contextTriggers.'|min:1|ruleTriggerValue|max:1024',
'triggers.*.stop_processing' => [new IsBoolean()],
'triggers.*.active' => [new IsBoolean()],
'actions.*.type' => 'required|in:' . implode(',', $validActions),
'actions.*.value' => 'required_if:actions.*.type,' . $contextActions . '|ruleActionValue',
'actions.*.type' => 'required|in:'.implode(',', $validActions),
'actions.*.value' => 'required_if:actions.*.type,'.$contextActions.'|ruleActionValue',
'actions.*.stop_processing' => [new IsBoolean()],
'actions.*.active' => [new IsBoolean()],
'strict' => [new IsBoolean()],
'stop_processing' => [new IsBoolean()],
'active' => [new IsBoolean()],
'order' => 'numeric|between:1,1337',
'order' => 'numeric|min:1|max:2048',
];
}
/**
* Configure the validator instance.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
function (Validator $validator): void {
$this->atLeastOneTrigger($validator);
$this->atLeastOneValidTrigger($validator);
$this->atLeastOneAction($validator);
$this->atLeastOneValidAction($validator);
}
);
if ($validator->fails()) {
Log::channel('audit')->error(sprintf('Validation errors in %s', __CLASS__), $validator->errors()->toArray());
}
}
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneTrigger(Validator $validator): void
{
@@ -197,8 +183,6 @@ class UpdateRequest extends FormRequest
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneValidTrigger(Validator $validator): void
{
@@ -226,8 +210,6 @@ class UpdateRequest extends FormRequest
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneAction(Validator $validator): void
{
@@ -241,8 +223,6 @@ class UpdateRequest extends FormRequest
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneValidAction(Validator $validator): void
{