diff --git a/src/Form/Type/Settings/NoCommerceType.php b/src/Form/Type/Settings/NoCommerceType.php index fed2ab5..3ad96c3 100644 --- a/src/Form/Type/Settings/NoCommerceType.php +++ b/src/Form/Type/Settings/NoCommerceType.php @@ -14,7 +14,6 @@ namespace MonsieurBiz\SyliusNoCommercePlugin\Form\Type\Settings; use MonsieurBiz\SyliusNoCommercePlugin\Firewall\RegistryInterface; -use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface; use MonsieurBiz\SyliusSettingsPlugin\Form\AbstractSettingsType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; @@ -56,35 +55,19 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => 'monsieurbiz.nocommerce.ui.form.field.enabled.label', 'required' => false, ]); - $this->addWithDefaultCheckbox($builder, 'disabled_firewall_contexts', ChoiceType::class, [ - 'label' => 'monsieurbiz.nocommerce.ui.form.field.disabled_firewall_contexts.label', - 'required' => false, - 'multiple' => true, - 'choices' => $choices, - ]); if ($this->isDefaultForm($builder)) { - $this->addWithDefaultCheckbox($builder, 'routes_to_enable', ChoiceType::class, [ - 'label' => 'monsieurbiz.nocommerce.ui.form.field.routes_to_enable.label', + $builder->add('allow_admin', CheckboxType::class, [ + 'label' => 'monsieurbiz.nocommerce.ui.form.field.allow_admin.label', 'required' => false, - 'multiple' => true, - 'expanded' => false, - 'choices' => $this->getEnabledRouteChoices(), ]); } - } - - private function getEnabledRouteChoices(): array - { - $allRoutes = ConfigInterface::ROUTES_BY_GROUP; - $choices = []; - foreach ($allRoutes as $group => $routes) { - foreach ($routes as $route) { - $choices[$group][$route] = $route; - } - } - - return $choices; + $this->addWithDefaultCheckbox($builder, 'disabled_firewall_contexts', ChoiceType::class, [ + 'label' => 'monsieurbiz.nocommerce.ui.form.field.disabled_firewall_contexts.label', + 'required' => false, + 'multiple' => true, + 'choices' => $choices, + ]); } } diff --git a/src/Kernel/SyliusNoCommerceKernelTrait.php b/src/Kernel/SyliusNoCommerceKernelTrait.php index 292b94c..4a9128a 100644 --- a/src/Kernel/SyliusNoCommerceKernelTrait.php +++ b/src/Kernel/SyliusNoCommerceKernelTrait.php @@ -87,42 +87,24 @@ public function getRoutesToRemove(): array unset($routesByGroup['country_admin'], $routesByGroup['country_api']); } - // Loop on settings to add routes /** @var FeaturesProviderInterface $featuresProvider */ $featuresProvider = $this->container->get('monsieurbiz.no_commerce.provider.features_provider'); try { - $routesToEnable = $featuresProvider->getRoutesToEnable(); + $allowAdmin = $featuresProvider->allowAdmin(); } catch (Exception $e) { - $routesToEnable = []; + $allowAdmin = false; } - foreach ($routesToEnable as $route) { - $this->enableRoute($route, $routesByGroup); - } + foreach ($routesByGroup as $routesKey => $routes) { + // Allow admin group routes + if (true === $allowAdmin && false !== strpos($routesKey, ConfigInterface::ADMIN_ROUTE_GROUP_MATCHER)) { + continue; + } - foreach ($routesByGroup as $routes) { $routesToRemove = array_merge($routesToRemove, $routes); } return $routesToRemove; } - - /** - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - private function enableRoute(string $route, array &$routesByGroup): void - { - foreach ($routesByGroup as $group => $routes) { - // Remove route from group - if (false !== ($key = array_search($route, $routes, true)) && isset($routesByGroup[$group][$key])) { - unset($routesByGroup[$group][$key]); - } - - // Remove group if empty - if (empty($routesByGroup[$group])) { - unset($routesByGroup[$group]); - } - } - } } diff --git a/src/Model/ConfigInterface.php b/src/Model/ConfigInterface.php index e13d0f0..204ee94 100644 --- a/src/Model/ConfigInterface.php +++ b/src/Model/ConfigInterface.php @@ -15,6 +15,8 @@ interface ConfigInterface { + public const ADMIN_ROUTE_GROUP_MATCHER = '_admin'; + public const ROUTES_BY_GROUP = [ /** * Customers & Account & Users. diff --git a/src/Provider/FeaturesProvider.php b/src/Provider/FeaturesProvider.php index 0ae1751..936e71a 100644 --- a/src/Provider/FeaturesProvider.php +++ b/src/Provider/FeaturesProvider.php @@ -49,8 +49,8 @@ public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null) return (bool) $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled'); } - public function getRoutesToEnable(): array + public function allowAdmin(): bool { - return $this->nocommerceSettings->getCurrentValue(null, null, 'routes_to_enable') ?? []; + return (bool) $this->nocommerceSettings->getCurrentValue(null, null, 'allow_admin'); } } diff --git a/src/Provider/FeaturesProviderInterface.php b/src/Provider/FeaturesProviderInterface.php index 3cc139a..c77b66b 100644 --- a/src/Provider/FeaturesProviderInterface.php +++ b/src/Provider/FeaturesProviderInterface.php @@ -19,5 +19,5 @@ interface FeaturesProviderInterface { public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool; - public function getRoutesToEnable(): array; + public function allowAdmin(): bool; } diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 7978200..f4e90d8 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -7,5 +7,5 @@ monsieurbiz: label: Firewalls to be disabled enabled: label: Enabled, clear cache after modification - routes_to_enable: - label: 'Route to enable, clear cache after modification (⚠️ Beta: this feature is under development)' + allow_admin: + label: Allow all routes in admin, clear cache after modification diff --git a/src/Resources/translations/messages.fr.yaml b/src/Resources/translations/messages.fr.yaml index ad9ebab..e9b3434 100644 --- a/src/Resources/translations/messages.fr.yaml +++ b/src/Resources/translations/messages.fr.yaml @@ -7,5 +7,5 @@ monsieurbiz: label: Firewalls à désactiver enabled: label: Activer, vider le cache après modification - routes_to_enable: - label: 'Routes à réactiver, vider le cache après modification (⚠️ Bêta : cette fonctionnalité est en cours de développement)' + allow_admin: + label: Autoriser toutes les routes dans l'admin, vider le cache après modification