Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop the route select to use a toggle to enable admin routes #40

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
]);
}
}
32 changes: 7 additions & 25 deletions src/Kernel/SyliusNoCommerceKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
}
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
Loading