Skip to content

Commit

Permalink
Drop the route select to use a toggle to enable admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Oct 1, 2024
1 parent d49d6e5 commit e907649
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 62 deletions.
33 changes: 8 additions & 25 deletions src/Form/Type/Settings/NoCommerceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
}
}
36 changes: 6 additions & 30 deletions src/Kernel/SyliusNoCommerceKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace MonsieurBiz\SyliusNoCommercePlugin\Kernel;

use Exception;
use MonsieurBiz\SyliusNoCommercePlugin\Model\Config;
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
Expand Down Expand Up @@ -87,42 +86,19 @@ 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');
$allowAdmin = $featuresProvider->allowAdmin();

try {
$routesToEnable = $featuresProvider->getRoutesToEnable();
} catch (Exception $e) {
$routesToEnable = [];
}

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]);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Model/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

interface ConfigInterface
{
public const ADMIN_ROUTE_GROUP_MATCHER = '_admin';

public const ROUTES_BY_GROUP = [
/**
* Customers & Account & Users.
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/FeaturesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Provider/FeaturesProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ interface FeaturesProviderInterface
{
public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool;

public function getRoutesToEnable(): array;
public function allowAdmin(): bool;
}
4 changes: 2 additions & 2 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e907649

Please sign in to comment.