Skip to content

Commit 323922a

Browse files
authored
Merge pull request #37 from etienne-monsieurbiz/feature/allow-admin-url
Allow to enabled some routes for admin through configuration in BO
2 parents ea9dc54 + 0f3fd31 commit 323922a

File tree

8 files changed

+168
-13
lines changed

8 files changed

+168
-13
lines changed

src/Form/Type/Settings/NoCommerceType.php

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

1616
use MonsieurBiz\SyliusNoCommercePlugin\Firewall\RegistryInterface;
17+
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProvider;
1718
use MonsieurBiz\SyliusSettingsPlugin\Form\AbstractSettingsType;
1819
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
1920
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -60,5 +61,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
6061
'multiple' => true,
6162
'choices' => $choices,
6263
]);
64+
$this->addWithDefaultCheckbox($builder, 're_enabled_admin_routes', ChoiceType::class, [
65+
'label' => 'monsieurbiz.nocommerce.ui.form.field.re_enabled_admin_routes.label',
66+
'required' => false,
67+
'multiple' => true,
68+
'choices' => [
69+
'sylius.ui.countries' => FeaturesProvider::COUNTRIES_KEY,
70+
'sylius.ui.currencies' => FeaturesProvider::CURRENCIES_KEY,
71+
'sylius.ui.inventory' => FeaturesProvider::INVENTORY_KEY,
72+
'sylius.ui.payment' => FeaturesProvider::PAYMENT_KEY,
73+
'sylius.menu.admin.main.catalog.header' => FeaturesProvider::CATALOG_KEY,
74+
'sylius.ui.shipping' => FeaturesProvider::SHIPPING_KEY,
75+
'sylius.ui.tax' => FeaturesProvider::TAX_KEY,
76+
'sylius.ui.zones' => FeaturesProvider::ZONES_KEY,
77+
],
78+
]);
6379
}
6480
}

src/Kernel/SyliusNoCommerceKernelTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
216216
foreach ($collection as $name => $route) {
217217
foreach ($routesToRemove as $routeToRemove) {
218218
if (false !== strpos($name, $routeToRemove)) {
219-
$route->setCondition('not context.checkNoCommerce()');
219+
$route->setCondition('not context.checkNoCommerce(params)');
220220
}
221221
}
222222
}

src/Menu/AdminMenuListener.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function __invoke(MenuBuilderEvent $event): void
4141
}
4242

4343
$menu->removeChild('sales');
44-
$menu->removeChild('catalog');
44+
45+
$this->handleCatalogMenu($menu);
4546
$menu->removeChild('marketing');
4647

4748
if (!$this->config->areCustomersAllowed()) {
@@ -55,19 +56,64 @@ public function __invoke(MenuBuilderEvent $event): void
5556

5657
private function removeConfigurationChildren(ItemInterface $configuration): void
5758
{
58-
$configuration->removeChild('currencies');
59+
$this->removeChildIfRoutesDisabled($configuration, 'currencies');
5960

6061
if (!$this->config->areZonesAllowed() && !$this->config->areCountriesAllowed()) {
61-
$configuration->removeChild('countries');
62+
$this->removeChildIfRoutesDisabled($configuration, 'countries');
6263
}
6364
if (!$this->config->areZonesAllowed()) {
64-
$configuration->removeChild('zones');
65+
$this->removeChildIfRoutesDisabled($configuration, 'zones');
6566
}
67+
6668
$configuration->removeChild('exchange_rates');
67-
$configuration->removeChild('payment_methods');
68-
$configuration->removeChild('shipping_methods');
69-
$configuration->removeChild('shipping_categories');
70-
$configuration->removeChild('tax_categories');
71-
$configuration->removeChild('tax_rates');
69+
$this->removeChildIfRoutesDisabled($configuration, 'payment_methods');
70+
$this->removeChildIfRoutesDisabled($configuration, 'shipping_methods');
71+
$this->removeChildIfRoutesDisabled($configuration, 'shipping_categories');
72+
$this->removeChildIfRoutesDisabled($configuration, 'tax_categories');
73+
$this->removeChildIfRoutesDisabled($configuration, 'tax_rates');
74+
}
75+
76+
/**
77+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
78+
*/
79+
private function removeChildIfRoutesDisabled(ItemInterface $menu, string $menuName): void
80+
{
81+
$menuItem = $menu->getChild($menuName);
82+
if (!$menuItem || null === $menuItem->getExtra('routes')) {
83+
return;
84+
}
85+
86+
foreach ($menuItem->getExtra('routes') as $route) {
87+
if (!isset($route['route'])) {
88+
continue;
89+
}
90+
// If one route does not match the forced enabled routes, we remove the menu item
91+
if (!$this->featuresProvider->isRouteForcedEnabled(['_route' => $route['route']])) {
92+
$menu->removeChild($menuName);
93+
}
94+
}
95+
}
96+
97+
private function handleCatalogMenu(ItemInterface $menu): void
98+
{
99+
$catalogMenu = $menu->getChild('catalog');
100+
101+
if (null === $catalogMenu) {
102+
return;
103+
}
104+
105+
$this->removeChildIfRoutesDisabled($catalogMenu, 'taxons');
106+
$this->removeChildIfRoutesDisabled($catalogMenu, 'products');
107+
$this->removeChildIfRoutesDisabled($catalogMenu, 'inventory');
108+
$this->removeChildIfRoutesDisabled($catalogMenu, 'attributes');
109+
$this->removeChildIfRoutesDisabled($catalogMenu, 'options');
110+
$this->removeChildIfRoutesDisabled($catalogMenu, 'association_types');
111+
112+
// We remove the catalog menu if it has no children
113+
if ($catalogMenu->hasChildren()) {
114+
return;
115+
}
116+
117+
$menu->removeChild('catalog');
72118
}
73119
}

src/Provider/FeaturesProvider.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,60 @@
2020

2121
final class FeaturesProvider implements FeaturesProviderInterface
2222
{
23+
public const COUNTRIES_KEY = 'countries';
24+
25+
public const CURRENCIES_KEY = 'currencies';
26+
27+
public const INVENTORY_KEY = 'inventory';
28+
29+
public const PAYMENT_KEY = 'payment';
30+
31+
public const CATALOG_KEY = 'catalog';
32+
33+
public const SHIPPING_KEY = 'shipping';
34+
35+
public const TAX_KEY = 'tax';
36+
37+
public const ZONES_KEY = 'zones';
38+
39+
public const ADMIN_ROUTES_THAT_CAN_BE_RE_ENABLED = [
40+
self::COUNTRIES_KEY => [
41+
'sylius_admin_country',
42+
'sylius_admin_ajax_render_province_form',
43+
],
44+
self::CURRENCIES_KEY => [
45+
'sylius_admin_currency',
46+
],
47+
self::INVENTORY_KEY => [
48+
'sylius_admin_inventory',
49+
],
50+
self::PAYMENT_KEY => [
51+
'sylius_admin_payment_method',
52+
],
53+
self::CATALOG_KEY => [
54+
'sylius_admin_get_attribute_types',
55+
'sylius_admin_get_product_attributes',
56+
'sylius_admin_render_attribute_forms',
57+
'sylius_admin_product',
58+
'sylius_admin_ajax_product',
59+
'sylius_admin_partial_product',
60+
'sylius_admin_ajax_generate_product_slug',
61+
'sylius_admin_partial_taxon',
62+
'sylius_admin_ajax_taxon',
63+
'sylius_admin_taxon',
64+
'sylius_admin_ajax_generate_taxon_slug',
65+
],
66+
self::SHIPPING_KEY => [
67+
'sylius_admin_shipping',
68+
],
69+
self::TAX_KEY => [
70+
'sylius_admin_tax_',
71+
],
72+
self::ZONES_KEY => [
73+
'sylius_admin_zone',
74+
],
75+
];
76+
2377
private ChannelContextInterface $channelContext;
2478

2579
private SettingsInterface $nocommerceSettings;
@@ -38,7 +92,7 @@ public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null)
3892
if (null === $channel) {
3993
$channel = $this->channelContext->getChannel();
4094
}
41-
// In case we are getting a channel that does not exists yet we return null to have the channel set properly
95+
// In case we are getting a channel that does not exist yet, we return null to have the channel set properly
4296
if (null === $channel->getId()) {
4397
return true;
4498
}
@@ -48,4 +102,37 @@ public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null)
48102

49103
return (bool) $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled');
50104
}
105+
106+
/**
107+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
108+
*/
109+
public function isRouteForcedEnabled(array $params = []): bool
110+
{
111+
if (!isset($params['_route'])) {
112+
return false;
113+
}
114+
115+
$route = $params['_route'];
116+
$channel = $this->channelContext->getChannel();
117+
/** @var ?array $reEnabledAdminRoutes */
118+
$reEnabledAdminRoutes = $this->nocommerceSettings->getCurrentValue($channel, null, 're_enabled_admin_routes');
119+
120+
if (empty($reEnabledAdminRoutes)) {
121+
return false;
122+
}
123+
124+
// We are checking if we should re-enable the route
125+
foreach ($reEnabledAdminRoutes as $reEnabledAdminSection) {
126+
if (!isset(self::ADMIN_ROUTES_THAT_CAN_BE_RE_ENABLED[$reEnabledAdminSection])) {
127+
continue;
128+
}
129+
foreach (self::ADMIN_ROUTES_THAT_CAN_BE_RE_ENABLED[$reEnabledAdminSection] as $reEnabledAdminRoute) {
130+
if (false !== strpos($route, $reEnabledAdminRoute)) {
131+
return true;
132+
}
133+
}
134+
}
135+
136+
return false;
137+
}
51138
}

src/Provider/FeaturesProviderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
interface FeaturesProviderInterface
1919
{
2020
public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool;
21+
22+
public function isRouteForcedEnabled(array $params = []): bool;
2123
}

src/Resources/translations/messages.en.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ monsieurbiz:
77
label: Firewalls to be disabled
88
enabled:
99
label: Enabled
10+
re_enabled_admin_routes:
11+
label: Admin routes to re-enable

src/Resources/translations/messages.fr.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ monsieurbiz:
77
label: Firewalls à désactiver
88
enabled:
99
label: Activer
10+
re_enabled_admin_routes:
11+
label: Routes à réactiver pour l'admin

src/Routing/NoCommerceRequestContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public function __construct(
4141
$this->featuresProvider = $featuresProvider;
4242
}
4343

44-
public function checkNoCommerce(): bool
44+
public function checkNoCommerce(array $params = []): bool
4545
{
46-
return $this->featuresProvider->isNoCommerceEnabledForChannel();
46+
return $this->featuresProvider->isNoCommerceEnabledForChannel() && !$this->featuresProvider->isRouteForcedEnabled($params);
4747
}
4848

4949
/**

0 commit comments

Comments
 (0)