From a827a51f6ce6dc8b1c6e1b3192113c1ac7551624 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 11:57:26 -0800 Subject: [PATCH 01/14] Enforce 65k limit when attempting to store setting values. --- src/Api/Controller/SetSettingsController.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Api/Controller/SetSettingsController.php b/src/Api/Controller/SetSettingsController.php index 6d0615c986..b34d11006c 100644 --- a/src/Api/Controller/SetSettingsController.php +++ b/src/Api/Controller/SetSettingsController.php @@ -13,6 +13,8 @@ use Flarum\Settings\Event; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Validation\Factory; +use Illuminate\Validation\ValidationException; use Laminas\Diactoros\Response\EmptyResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -33,10 +35,11 @@ class SetSettingsController implements RequestHandlerInterface /** * @param SettingsRepositoryInterface $settings */ - public function __construct(SettingsRepositoryInterface $settings, Dispatcher $dispatcher) + public function __construct(SettingsRepositoryInterface $settings, Dispatcher $dispatcher, Factory $validatorFactory) { $this->settings = $settings; $this->dispatcher = $dispatcher; + $this->validatorFactory = $validatorFactory; } /** @@ -48,6 +51,17 @@ public function handle(ServerRequestInterface $request): ResponseInterface $settings = $request->getParsedBody(); + $validation = $this->validatorFactory->make( + $settings, + array_map(function($value) { + return 'max:65000'; + }, $settings), + ); + + if ($validation->fails()) { + throw new ValidationException($validation); + } + $this->dispatcher->dispatch(new Event\Saving($settings)); foreach ($settings as $k => $v) { From 1730ad5ea1dadea78217eb520f444ce7933cf975 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 12:05:37 -0800 Subject: [PATCH 02/14] Add space for style. --- src/Api/Controller/SetSettingsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/Controller/SetSettingsController.php b/src/Api/Controller/SetSettingsController.php index b34d11006c..b0cfd51857 100644 --- a/src/Api/Controller/SetSettingsController.php +++ b/src/Api/Controller/SetSettingsController.php @@ -53,7 +53,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface $validation = $this->validatorFactory->make( $settings, - array_map(function($value) { + array_map(function ($value) { return 'max:65000'; }, $settings), ); From 0994552b9e5928f53a22cf038701c7474b63be25 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 13:17:36 -0800 Subject: [PATCH 03/14] Move setting validation into Saving event listener. --- src/Api/Controller/SetSettingsController.php | 16 +--------------- src/Forum/ForumServiceProvider.php | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Api/Controller/SetSettingsController.php b/src/Api/Controller/SetSettingsController.php index b0cfd51857..6d0615c986 100644 --- a/src/Api/Controller/SetSettingsController.php +++ b/src/Api/Controller/SetSettingsController.php @@ -13,8 +13,6 @@ use Flarum\Settings\Event; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Contracts\Validation\Factory; -use Illuminate\Validation\ValidationException; use Laminas\Diactoros\Response\EmptyResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -35,11 +33,10 @@ class SetSettingsController implements RequestHandlerInterface /** * @param SettingsRepositoryInterface $settings */ - public function __construct(SettingsRepositoryInterface $settings, Dispatcher $dispatcher, Factory $validatorFactory) + public function __construct(SettingsRepositoryInterface $settings, Dispatcher $dispatcher) { $this->settings = $settings; $this->dispatcher = $dispatcher; - $this->validatorFactory = $validatorFactory; } /** @@ -51,17 +48,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface $settings = $request->getParsedBody(); - $validation = $this->validatorFactory->make( - $settings, - array_map(function ($value) { - return 'max:65000'; - }, $settings), - ); - - if ($validation->fails()) { - throw new ValidationException($validation); - } - $this->dispatcher->dispatch(new Event\Saving($settings)); foreach ($settings as $k => $v) { diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 3b0a952fee..77a4be7bff 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -33,7 +33,9 @@ use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Validation\Factory as ValidatorFactory; use Illuminate\Contracts\View\Factory; +use Illuminate\Validation\ValidationException; use Laminas\Stratigility\MiddlewarePipe; use Symfony\Contracts\Translation\TranslatorInterface; @@ -131,7 +133,7 @@ public function register() }); } - public function boot(Container $container, Dispatcher $events, Factory $view) + public function boot(Container $container, Dispatcher $events, Factory $view, ValidatorFactory $validatorFactory) { $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum'); @@ -172,7 +174,18 @@ function (Saved $event) use ($container) { $events->listen( Saving::class, - function (Saving $event) use ($container) { + function (Saving $event) use ($container, $validatorFactory) { + $validation = $validatorFactory->make( + $event->settings, + array_map(function ($value) { + return 'max:65000'; + }, $event->settings), + ); + + if ($validation->fails()) { + throw new ValidationException($validation); + } + $validator = new ValidateCustomLess( $container->make('flarum.assets.forum'), $container->make('flarum.locales'), From 0418533fee42802230a7b00571ea58d5e928e1db Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 13:19:23 -0800 Subject: [PATCH 04/14] Use consistent var names --- src/Forum/ForumServiceProvider.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 77a4be7bff..5ef618a1f4 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -175,15 +175,15 @@ function (Saved $event) use ($container) { $events->listen( Saving::class, function (Saving $event) use ($container, $validatorFactory) { - $validation = $validatorFactory->make( + $validator = $validatorFactory->make( $event->settings, array_map(function ($value) { return 'max:65000'; }, $event->settings), ); - if ($validation->fails()) { - throw new ValidationException($validation); + if ($validator->fails()) { + throw new ValidationException($validator); } $validator = new ValidateCustomLess( From 2b2ebe7dd381b7f572712a95b9e0326e8eb1475c Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 13:23:18 -0800 Subject: [PATCH 05/14] remove extra space --- src/Forum/ForumServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 5ef618a1f4..69d0a2194d 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -181,7 +181,7 @@ function (Saving $event) use ($container, $validatorFactory) { return 'max:65000'; }, $event->settings), ); - + if ($validator->fails()) { throw new ValidationException($validator); } From 485313d0ce02823271d8e669db3471fdc569f0c7 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 14:41:05 -0800 Subject: [PATCH 06/14] Move settings validation into separate class. --- src/Forum/ForumServiceProvider.php | 14 ++------- src/Settings/SettingsValidator.php | 48 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 src/Settings/SettingsValidator.php diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 69d0a2194d..75d437214f 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -31,11 +31,11 @@ use Flarum\Settings\Event\Saved; use Flarum\Settings\Event\Saving; use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Validation\Factory as ValidatorFactory; use Illuminate\Contracts\View\Factory; -use Illuminate\Validation\ValidationException; use Laminas\Stratigility\MiddlewarePipe; use Symfony\Contracts\Translation\TranslatorInterface; @@ -175,16 +175,8 @@ function (Saved $event) use ($container) { $events->listen( Saving::class, function (Saving $event) use ($container, $validatorFactory) { - $validator = $validatorFactory->make( - $event->settings, - array_map(function ($value) { - return 'max:65000'; - }, $event->settings), - ); - - if ($validator->fails()) { - throw new ValidationException($validator); - } + $settingsValidator = new SettingsValidator($validatorFactory, $event->settings); + $settingsValidator->validate(); $validator = new ValidateCustomLess( $container->make('flarum.assets.forum'), diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php new file mode 100644 index 0000000000..ed557e0a86 --- /dev/null +++ b/src/Settings/SettingsValidator.php @@ -0,0 +1,48 @@ +validatorFactory = $validatorFactory; + $this->settings = $settings; + } + + public function validate() + { + $validator = $this->validatorFactory->make( + $this->settings, + array_map(function ($value) { + return 'max:65000'; + }, $this->settings), + ); + + if ($validator->fails()) { + throw new ValidationException($validator); + } + } +} From 2f732faffb6e9c8eec577ade03bd970c6bbf9728 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 14:41:37 -0800 Subject: [PATCH 07/14] Remove unused class. --- src/Settings/SettingsValidator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index ed557e0a86..b57504639b 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -9,7 +9,6 @@ namespace Flarum\Settings; -use Flarum\Forum\ValidateCustomLess; use Illuminate\Contracts\Validation\Factory; use Illuminate\Validation\ValidationException; From 0f24e0fe2c3cf9effba3fff7490b5d03652b9269 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 14:42:00 -0800 Subject: [PATCH 08/14] Remove extra line. --- src/Settings/SettingsValidator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index b57504639b..453011bb47 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -19,7 +19,6 @@ class SettingsValidator */ protected $validatorFactory; - /** * @var array */ From 55c165c9ec3d3a80b01c32e2ae806fa20ccc00d4 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 16:59:30 -0800 Subject: [PATCH 09/14] Move ValidateCustomLess to SettingsServiceProvider. Use existing convention for validator. --- src/Forum/ForumServiceProvider.php | 41 +-------------------- src/Settings/SettingsServiceProvider.php | 45 ++++++++++++++++++++++++ src/Settings/SettingsValidator.php | 41 +++++++++------------ 3 files changed, 62 insertions(+), 65 deletions(-) diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 75d437214f..8ea1d2a94e 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -28,13 +28,9 @@ use Flarum\Http\RouteHandlerFactory; use Flarum\Http\UrlGenerator; use Flarum\Locale\LocaleManager; -use Flarum\Settings\Event\Saved; -use Flarum\Settings\Event\Saving; use Flarum\Settings\SettingsRepositoryInterface; -use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Contracts\Validation\Factory as ValidatorFactory; use Illuminate\Contracts\View\Factory; use Laminas\Stratigility\MiddlewarePipe; use Symfony\Contracts\Translation\TranslatorInterface; @@ -133,7 +129,7 @@ public function register() }); } - public function boot(Container $container, Dispatcher $events, Factory $view, ValidatorFactory $validatorFactory) + public function boot(Container $container, Dispatcher $events, Factory $view) { $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum'); @@ -152,41 +148,6 @@ function () use ($container) { $recompile->flush(); } ); - - $events->listen( - Saved::class, - function (Saved $event) use ($container) { - $recompile = new RecompileFrontendAssets( - $container->make('flarum.assets.forum'), - $container->make(LocaleManager::class) - ); - $recompile->whenSettingsSaved($event); - - $validator = new ValidateCustomLess( - $container->make('flarum.assets.forum'), - $container->make('flarum.locales'), - $container, - $container->make('flarum.less.config') - ); - $validator->whenSettingsSaved($event); - } - ); - - $events->listen( - Saving::class, - function (Saving $event) use ($container, $validatorFactory) { - $settingsValidator = new SettingsValidator($validatorFactory, $event->settings); - $settingsValidator->validate(); - - $validator = new ValidateCustomLess( - $container->make('flarum.assets.forum'), - $container->make('flarum.locales'), - $container, - $container->make('flarum.less.config') - ); - $validator->whenSettingsSaving($event); - } - ); } /** diff --git a/src/Settings/SettingsServiceProvider.php b/src/Settings/SettingsServiceProvider.php index 4f12a8b158..fb1eb74fc2 100644 --- a/src/Settings/SettingsServiceProvider.php +++ b/src/Settings/SettingsServiceProvider.php @@ -10,7 +10,14 @@ namespace Flarum\Settings; use Flarum\Foundation\AbstractServiceProvider; +use Flarum\Frontend\RecompileFrontendAssets; +use Flarum\Forum\ValidateCustomLess; +use Flarum\Locale\LocaleManager; +use Flarum\Settings\Event\Saved; +use Flarum\Settings\Event\Saving; +use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\ConnectionInterface; use Illuminate\Support\Collection; @@ -41,4 +48,42 @@ public function register() $this->container->alias(SettingsRepositoryInterface::class, 'flarum.settings'); } + + + public function boot(Container $container, Dispatcher $events, SettingsValidator $settingsValidator) + { + $events->listen( + Saved::class, + function (Saved $event) use ($container) { + $recompile = new RecompileFrontendAssets( + $container->make('flarum.assets.forum'), + $container->make(LocaleManager::class) + ); + $recompile->whenSettingsSaved($event); + + $validator = new ValidateCustomLess( + $container->make('flarum.assets.forum'), + $container->make('flarum.locales'), + $container, + $container->make('flarum.less.config') + ); + $validator->whenSettingsSaved($event); + } + ); + + $events->listen( + Saving::class, + function (Saving $event) use ($container, $settingsValidator) { + $settingsValidator->assertValid($event->settings); + + $validator = new ValidateCustomLess( + $container->make('flarum.assets.forum'), + $container->make('flarum.locales'), + $container, + $container->make('flarum.less.config') + ); + $validator->whenSettingsSaving($event); + } + ); + } } diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index 453011bb47..2f962b141d 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -9,38 +9,29 @@ namespace Flarum\Settings; -use Illuminate\Contracts\Validation\Factory; -use Illuminate\Validation\ValidationException; +use Flarum\Foundation\AbstractValidator; -class SettingsValidator +class SettingsValidator extends AbstractValidator { /** - * @var Factory + * Make a new validator instance for this model. + * + * @param array $attributes + * @return \Illuminate\Validation\Validator */ - protected $validatorFactory; - - /** - * @var array - */ - protected $settings; - - public function __construct(Factory $validatorFactory, array $settings) + protected function makeValidator(array $attributes) { - $this->validatorFactory = $validatorFactory; - $this->settings = $settings; - } + // These rules apply to all settings. + $rules = array_map(function ($value) { + return 'max:65000'; + }, $attributes); - public function validate() - { - $validator = $this->validatorFactory->make( - $this->settings, - array_map(function ($value) { - return 'max:65000'; - }, $this->settings), - ); + $validator = $this->validator->make($attributes, $rules, $this->getMessages()); - if ($validator->fails()) { - throw new ValidationException($validator); + foreach ($this->configuration as $callable) { + $callable($this, $validator); } + + return $validator; } } From b38f98055e34d28da9545971b17a90b743a6af2f Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 18:50:45 -0800 Subject: [PATCH 10/14] Update src/Settings/SettingsValidator.php Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> --- src/Settings/SettingsValidator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index 2f962b141d..b94c49cc4c 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -21,7 +21,8 @@ class SettingsValidator extends AbstractValidator */ protected function makeValidator(array $attributes) { - // These rules apply to all settings. + // Entries in the default DB settings table are limited to 65,000 + // characters. We validate against this to avoid confusing errors. $rules = array_map(function ($value) { return 'max:65000'; }, $attributes); From 31ee602b7acfc4571d40d4de6d155e708879870f Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 19:44:08 -0800 Subject: [PATCH 11/14] Revert moving of ValidateCustomLess logic. Allow for attribute specific setting validation rules. --- src/Forum/ForumServiceProvider.php | 38 +++++++++++++++++++++++- src/Settings/SettingsServiceProvider.php | 35 ++-------------------- src/Settings/SettingsValidator.php | 33 +++++++++++++++++--- 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 8ea1d2a94e..d4305260ec 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -28,9 +28,13 @@ use Flarum\Http\RouteHandlerFactory; use Flarum\Http\UrlGenerator; use Flarum\Locale\LocaleManager; +use Flarum\Settings\Event\Saved; +use Flarum\Settings\Event\Saving; use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Validation\Factory as ValidatorFactory; use Illuminate\Contracts\View\Factory; use Laminas\Stratigility\MiddlewarePipe; use Symfony\Contracts\Translation\TranslatorInterface; @@ -129,7 +133,7 @@ public function register() }); } - public function boot(Container $container, Dispatcher $events, Factory $view) + public function boot(Container $container, Dispatcher $events, Factory $view, ValidatorFactory $validatorFactory) { $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum'); @@ -148,6 +152,38 @@ function () use ($container) { $recompile->flush(); } ); + + $events->listen( + Saved::class, + function (Saved $event) use ($container) { + $recompile = new RecompileFrontendAssets( + $container->make('flarum.assets.forum'), + $container->make(LocaleManager::class) + ); + $recompile->whenSettingsSaved($event); + + $validator = new ValidateCustomLess( + $container->make('flarum.assets.forum'), + $container->make('flarum.locales'), + $container, + $container->make('flarum.less.config') + ); + $validator->whenSettingsSaved($event); + } + ); + + $events->listen( + Saving::class, + function (Saving $event) use ($container) { + $validator = new ValidateCustomLess( + $container->make('flarum.assets.forum'), + $container->make('flarum.locales'), + $container, + $container->make('flarum.less.config') + ); + $validator->whenSettingsSaving($event); + } + ); } /** diff --git a/src/Settings/SettingsServiceProvider.php b/src/Settings/SettingsServiceProvider.php index fb1eb74fc2..5e8d307250 100644 --- a/src/Settings/SettingsServiceProvider.php +++ b/src/Settings/SettingsServiceProvider.php @@ -10,10 +10,6 @@ namespace Flarum\Settings; use Flarum\Foundation\AbstractServiceProvider; -use Flarum\Frontend\RecompileFrontendAssets; -use Flarum\Forum\ValidateCustomLess; -use Flarum\Locale\LocaleManager; -use Flarum\Settings\Event\Saved; use Flarum\Settings\Event\Saving; use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; @@ -50,39 +46,12 @@ public function register() } - public function boot(Container $container, Dispatcher $events, SettingsValidator $settingsValidator) + public function boot(Dispatcher $events, SettingsValidator $settingsValidator) { - $events->listen( - Saved::class, - function (Saved $event) use ($container) { - $recompile = new RecompileFrontendAssets( - $container->make('flarum.assets.forum'), - $container->make(LocaleManager::class) - ); - $recompile->whenSettingsSaved($event); - - $validator = new ValidateCustomLess( - $container->make('flarum.assets.forum'), - $container->make('flarum.locales'), - $container, - $container->make('flarum.less.config') - ); - $validator->whenSettingsSaved($event); - } - ); - $events->listen( Saving::class, - function (Saving $event) use ($container, $settingsValidator) { + function (Saving $event) use ($settingsValidator) { $settingsValidator->assertValid($event->settings); - - $validator = new ValidateCustomLess( - $container->make('flarum.assets.forum'), - $container->make('flarum.locales'), - $container, - $container->make('flarum.less.config') - ); - $validator->whenSettingsSaving($event); } ); } diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index b94c49cc4c..6ddbf74b5d 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -10,9 +10,28 @@ namespace Flarum\Settings; use Flarum\Foundation\AbstractValidator; +use Illuminate\Support\Arr; class SettingsValidator extends AbstractValidator { + /** + * @var array + */ + protected $rules = []; + + + /** + * These rules apply to all attributes. + * + * Entries in the default DB settings table are limited to 65,000 + * characters. We validate against this to avoid confusing errors. + * + * @var array + */ + protected $globalRules = [ + 'max:65000', + ]; + /** * Make a new validator instance for this model. * @@ -21,12 +40,18 @@ class SettingsValidator extends AbstractValidator */ protected function makeValidator(array $attributes) { - // Entries in the default DB settings table are limited to 65,000 - // characters. We validate against this to avoid confusing errors. - $rules = array_map(function ($value) { - return 'max:65000'; + // Apply global rules first. + $rules = array_map(function () { + return $this->globalRules; }, $attributes); + // Apply attribute specific rules. + foreach ($rules as $key => $value) { + if (array_key_exists($key, $this->rules)) { + $rules[$key] = array_merge($rules[$key], $this->rules[$key]); + } + } + $validator = $this->validator->make($attributes, $rules, $this->getMessages()); foreach ($this->configuration as $callable) { From 102e70d069b6a3ea7ce58455eaa2dcdecfff7675 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 19:47:49 -0800 Subject: [PATCH 12/14] Style fixes. --- src/Forum/ForumServiceProvider.php | 4 +--- src/Settings/SettingsServiceProvider.php | 1 - src/Settings/SettingsValidator.php | 5 ++--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index d4305260ec..3b0a952fee 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -31,10 +31,8 @@ use Flarum\Settings\Event\Saved; use Flarum\Settings\Event\Saving; use Flarum\Settings\SettingsRepositoryInterface; -use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Contracts\Validation\Factory as ValidatorFactory; use Illuminate\Contracts\View\Factory; use Laminas\Stratigility\MiddlewarePipe; use Symfony\Contracts\Translation\TranslatorInterface; @@ -133,7 +131,7 @@ public function register() }); } - public function boot(Container $container, Dispatcher $events, Factory $view, ValidatorFactory $validatorFactory) + public function boot(Container $container, Dispatcher $events, Factory $view) { $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum'); diff --git a/src/Settings/SettingsServiceProvider.php b/src/Settings/SettingsServiceProvider.php index 5e8d307250..8c6e95d0a7 100644 --- a/src/Settings/SettingsServiceProvider.php +++ b/src/Settings/SettingsServiceProvider.php @@ -45,7 +45,6 @@ public function register() $this->container->alias(SettingsRepositoryInterface::class, 'flarum.settings'); } - public function boot(Dispatcher $events, SettingsValidator $settingsValidator) { $events->listen( diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index 6ddbf74b5d..619841e23c 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -19,13 +19,12 @@ class SettingsValidator extends AbstractValidator */ protected $rules = []; - /** * These rules apply to all attributes. - * + * * Entries in the default DB settings table are limited to 65,000 * characters. We validate against this to avoid confusing errors. - * + * * @var array */ protected $globalRules = [ From c7cfbab4441c5c884568662f40290c64ca891839 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 19:48:33 -0800 Subject: [PATCH 13/14] Style fixes. --- src/Settings/SettingsValidator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Settings/SettingsValidator.php b/src/Settings/SettingsValidator.php index 619841e23c..cf73aa5be6 100644 --- a/src/Settings/SettingsValidator.php +++ b/src/Settings/SettingsValidator.php @@ -10,7 +10,6 @@ namespace Flarum\Settings; use Flarum\Foundation\AbstractValidator; -use Illuminate\Support\Arr; class SettingsValidator extends AbstractValidator { From f43b3a669416dd4c1e8818a1e2e3fac868366d51 Mon Sep 17 00:00:00 2001 From: Garrett Grimm Date: Thu, 11 Nov 2021 19:51:32 -0800 Subject: [PATCH 14/14] Style fixes. --- src/Settings/SettingsServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Settings/SettingsServiceProvider.php b/src/Settings/SettingsServiceProvider.php index 8c6e95d0a7..942d842c90 100644 --- a/src/Settings/SettingsServiceProvider.php +++ b/src/Settings/SettingsServiceProvider.php @@ -11,7 +11,6 @@ use Flarum\Foundation\AbstractServiceProvider; use Flarum\Settings\Event\Saving; -use Flarum\Settings\SettingsValidator; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\ConnectionInterface;