diff --git a/extensions/package-manager/src/AllValidatorRules.php b/extensions/package-manager/src/AllValidatorRules.php new file mode 100644 index 0000000000..f9dccbf18e --- /dev/null +++ b/extensions/package-manager/src/AllValidatorRules.php @@ -0,0 +1,22 @@ +getRules(); + + $validator = $this->validator->make($attributes, $rules, $this->getMessages()); + + foreach ($this->configuration as $callable) { + $callable($this, $validator); + } + + return $validator; + } +} diff --git a/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php b/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php index 59b4ae9166..3244abcbb7 100755 --- a/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php +++ b/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php @@ -9,6 +9,7 @@ namespace Flarum\ExtensionManager\Api\Controller; +use Flarum\ExtensionManager\ConfigureAuthValidator; use Flarum\Foundation\Paths; use Flarum\Http\RequestUtil; use Flarum\ExtensionManager\Composer\ComposerJson; @@ -36,7 +37,12 @@ class ConfigureComposerController implements RequestHandlerInterface /** * @var ConfigureComposerValidator */ - protected $validator; + protected $composerValidator; + + /** + * @var ConfigureComposerValidator + */ + protected $authValidator; /** * @var Paths @@ -53,9 +59,10 @@ class ConfigureComposerController implements RequestHandlerInterface */ protected $filesystem; - public function __construct(ConfigureComposerValidator $validator, Paths $paths, ComposerJson $composerJson, Filesystem $filesystem) + public function __construct(ConfigureComposerValidator $composerValidator, ConfigureAuthValidator $authValidator, Paths $paths, ComposerJson $composerJson, Filesystem $filesystem) { - $this->validator = $validator; + $this->composerValidator = $composerValidator; + $this->authValidator = $authValidator; $this->paths = $paths; $this->composerJson = $composerJson; $this->filesystem = $filesystem; @@ -89,7 +96,7 @@ protected function composerConfig(ServerRequestInterface $request): array { $data = Arr::only(Arr::get($request->getParsedBody(), 'data') ?? [], $this->configurable); - $this->validator->assertValid(['composer' => $data]); + $this->composerValidator->assertValid($data); $composerJson = $this->composerJson->get(); if (! empty($data)) { @@ -105,10 +112,15 @@ protected function composerConfig(ServerRequestInterface $request): array $default = [ 'minimum-stability' => 'stable', + 'repositories' => [], ]; foreach ($this->configurable as $key) { $composerJson[$key] = Arr::get($composerJson, $key, Arr::get($default, $key)); + + if (is_null($composerJson[$key]) && ! is_null($default[$key])) { + $composerJson[$key] = $default[$key]; + } } $composerJson = Arr::sortRecursive($composerJson); @@ -120,7 +132,7 @@ protected function authConfig(ServerRequestInterface $request): array { $data = Arr::get($request->getParsedBody(), 'data'); - $this->validator->assertValid(['auth' => $data]); + $this->authValidator->assertValid($data ?? []); try { $authJson = json_decode($this->filesystem->get($this->paths->base.'/auth.json'), true); diff --git a/extensions/package-manager/src/ConfigureAuthValidator.php b/extensions/package-manager/src/ConfigureAuthValidator.php new file mode 100644 index 0000000000..e7544cf5eb --- /dev/null +++ b/extensions/package-manager/src/ConfigureAuthValidator.php @@ -0,0 +1,28 @@ + ['sometimes', 'array'], + 'github-oauth.*' => ['sometimes', 'string'], + 'gitlab-oauth' => ['sometimes', 'array'], + 'gitlab-oauth.*' => ['sometimes', 'string'], + 'gitlab-token' => ['sometimes', 'array'], + 'gitlab-token.*' => ['sometimes', 'string'], + 'bearer' => ['sometimes', 'array'], + 'bearer.*' => ['sometimes', 'string'], + ]; +} diff --git a/extensions/package-manager/src/ConfigureComposerValidator.php b/extensions/package-manager/src/ConfigureComposerValidator.php index 3c0fbaccb8..7caf3cc171 100644 --- a/extensions/package-manager/src/ConfigureComposerValidator.php +++ b/extensions/package-manager/src/ConfigureComposerValidator.php @@ -13,22 +13,13 @@ class ConfigureComposerValidator extends AbstractValidator { + use AllValidatorRules; + protected $rules = [ - 'composer' => [ - 'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'], - 'repositories' => ['sometimes', 'array'], - 'repositories.*.type' => ['sometimes', 'in:composer,vcs,path'], - 'repositories.*.url' => ['sometimes', 'string'], - ], - 'auth' => [ - 'github-oauth' => ['sometimes', 'array'], - 'github-oauth.*' => ['sometimes', 'string'], - 'gitlab-oauth' => ['sometimes', 'array'], - 'gitlab-oauth.*' => ['sometimes', 'string'], - 'gitlab-token' => ['sometimes', 'array'], - 'gitlab-token.*' => ['sometimes', 'string'], - 'bearer' => ['sometimes', 'array'], - 'bearer.*' => ['sometimes', 'string'], - ], + 'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'], + 'repositories' => ['sometimes', 'array'], + 'repositories.*' => ['sometimes', 'array', 'required_array_keys:type,url'], + 'repositories.*.type' => ['in:composer,vcs,path'], + 'repositories.*.url' => ['string', 'filled'], ]; }