Skip to content

Commit 58e9be9

Browse files
committed
Drop the route select to use a toggle to enable admin routes
1 parent d49d6e5 commit 58e9be9

File tree

7 files changed

+24
-57
lines changed

7 files changed

+24
-57
lines changed

src/Form/Type/Settings/NoCommerceType.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace MonsieurBiz\SyliusNoCommercePlugin\Form\Type\Settings;
1515

1616
use MonsieurBiz\SyliusNoCommercePlugin\Firewall\RegistryInterface;
17-
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
1817
use MonsieurBiz\SyliusSettingsPlugin\Form\AbstractSettingsType;
1918
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
2019
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -56,35 +55,19 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5655
'label' => 'monsieurbiz.nocommerce.ui.form.field.enabled.label',
5756
'required' => false,
5857
]);
59-
$this->addWithDefaultCheckbox($builder, 'disabled_firewall_contexts', ChoiceType::class, [
60-
'label' => 'monsieurbiz.nocommerce.ui.form.field.disabled_firewall_contexts.label',
61-
'required' => false,
62-
'multiple' => true,
63-
'choices' => $choices,
64-
]);
6558

6659
if ($this->isDefaultForm($builder)) {
67-
$this->addWithDefaultCheckbox($builder, 'routes_to_enable', ChoiceType::class, [
68-
'label' => 'monsieurbiz.nocommerce.ui.form.field.routes_to_enable.label',
60+
$builder->add('allow_admin', CheckboxType::class, [
61+
'label' => 'monsieurbiz.nocommerce.ui.form.field.allow_admin.label',
6962
'required' => false,
70-
'multiple' => true,
71-
'expanded' => false,
72-
'choices' => $this->getEnabledRouteChoices(),
7363
]);
7464
}
75-
}
76-
77-
private function getEnabledRouteChoices(): array
78-
{
79-
$allRoutes = ConfigInterface::ROUTES_BY_GROUP;
80-
$choices = [];
8165

82-
foreach ($allRoutes as $group => $routes) {
83-
foreach ($routes as $route) {
84-
$choices[$group][$route] = $route;
85-
}
86-
}
87-
88-
return $choices;
66+
$this->addWithDefaultCheckbox($builder, 'disabled_firewall_contexts', ChoiceType::class, [
67+
'label' => 'monsieurbiz.nocommerce.ui.form.field.disabled_firewall_contexts.label',
68+
'required' => false,
69+
'multiple' => true,
70+
'choices' => $choices,
71+
]);
8972
}
9073
}

src/Kernel/SyliusNoCommerceKernelTrait.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,42 +87,24 @@ public function getRoutesToRemove(): array
8787
unset($routesByGroup['country_admin'], $routesByGroup['country_api']);
8888
}
8989

90-
// Loop on settings to add routes
9190
/** @var FeaturesProviderInterface $featuresProvider */
9291
$featuresProvider = $this->container->get('monsieurbiz.no_commerce.provider.features_provider');
9392

9493
try {
95-
$routesToEnable = $featuresProvider->getRoutesToEnable();
94+
$allowAdmin = $featuresProvider->allowAdmin();
9695
} catch (Exception $e) {
97-
$routesToEnable = [];
96+
$allowAdmin = false;
9897
}
9998

100-
foreach ($routesToEnable as $route) {
101-
$this->enableRoute($route, $routesByGroup);
102-
}
99+
foreach ($routesByGroup as $routesKey => $routes) {
100+
// Allow admin group routes
101+
if (true === $allowAdmin && false !== strpos($routesKey, ConfigInterface::ADMIN_ROUTE_GROUP_MATCHER)) {
102+
continue;
103+
}
103104

104-
foreach ($routesByGroup as $routes) {
105105
$routesToRemove = array_merge($routesToRemove, $routes);
106106
}
107107

108108
return $routesToRemove;
109109
}
110-
111-
/**
112-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
113-
*/
114-
private function enableRoute(string $route, array &$routesByGroup): void
115-
{
116-
foreach ($routesByGroup as $group => $routes) {
117-
// Remove route from group
118-
if (false !== ($key = array_search($route, $routes, true)) && isset($routesByGroup[$group][$key])) {
119-
unset($routesByGroup[$group][$key]);
120-
}
121-
122-
// Remove group if empty
123-
if (empty($routesByGroup[$group])) {
124-
unset($routesByGroup[$group]);
125-
}
126-
}
127-
}
128110
}

src/Model/ConfigInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
interface ConfigInterface
1717
{
18+
public const ADMIN_ROUTE_GROUP_MATCHER = '_admin';
19+
1820
public const ROUTES_BY_GROUP = [
1921
/**
2022
* Customers & Account & Users.

src/Provider/FeaturesProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null)
4949
return (bool) $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled');
5050
}
5151

52-
public function getRoutesToEnable(): array
52+
public function allowAdmin(): bool
5353
{
54-
return $this->nocommerceSettings->getCurrentValue(null, null, 'routes_to_enable') ?? [];
54+
return (bool) $this->nocommerceSettings->getCurrentValue(null, null, 'allow_admin');
5555
}
5656
}

src/Provider/FeaturesProviderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ interface FeaturesProviderInterface
1919
{
2020
public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool;
2121

22-
public function getRoutesToEnable(): array;
22+
public function allowAdmin(): bool;
2323
}

src/Resources/translations/messages.en.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ monsieurbiz:
77
label: Firewalls to be disabled
88
enabled:
99
label: Enabled, clear cache after modification
10-
routes_to_enable:
11-
label: 'Route to enable, clear cache after modification (⚠️ Beta: this feature is under development)'
10+
allow_admin:
11+
label: Allow all routes in admin, clear cache after modification

src/Resources/translations/messages.fr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ monsieurbiz:
77
label: Firewalls à désactiver
88
enabled:
99
label: Activer, vider le cache après modification
10-
routes_to_enable:
11-
label: 'Routes à réactiver, vider le cache après modification (⚠️ Bêta : cette fonctionnalité est en cours de développement)'
10+
allow_admin:
11+
label: Autoriser toutes les routes dans l'admin, vider le cache après modification

0 commit comments

Comments
 (0)