Skip to content

Commit 7ad9433

Browse files
authored
Merge pull request #43 from guavestudios/feature/allow-admins
Feature/allow admins
2 parents a62126f + 06dc208 commit 7ad9433

17 files changed

+177
-12
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@
7676
``` shell
7777
php bin/console maintenance:enable 172.16.254.1 255.255.255.255 192.0.0.255
7878
```
79-
3. Disable the plugin
79+
3.Enable the plugin and disable admin access
80+
81+
``` shell
82+
php bin/console maintenance:enable --disable-admin
83+
```
84+
4.Disable the plugin
8085

8186
``` shell
8287
php bin/console maintenance:disable
8388
```
8489

85-
4. Remove configuration file using CLI
90+
5.Remove configuration file using CLI
8691

8792
By default, **maintenance.yaml** configuration file remains when running `maintenance:disable` or via admin panel using toggle disable
8893
Nevertheless passing option `[-c|--clear]` to command line above will reset previous saved configuration
@@ -91,9 +96,10 @@ Nevertheless passing option `[-c|--clear]` to command line above will reset prev
9196

9297
- Enable/disable the plugin
9398
- Allow access for one or multiple ips addresses (optional)
94-
- Allow access for secret token (optional)
99+
- Allow access for secret token (session and request) (optional)
95100
- Create your custom message (optional)
96101
- Grant access to search bots during maintenance (optional)
102+
- Grant access to admins during maintenance (optional)
97103

98104
### If you want to put the **maintenance.yaml** in a directory, please add your directory in .env:
99105

@@ -123,6 +129,11 @@ framework:
123129
adapter: cache.adapter.redis
124130
```
125131

132+
### Precisions for access token
133+
134+
Once token is generated, disallowing maintenance will be available thought request as well.
135+
So you can use it as query parameter `?synolia_maintenance_token={$token}` or in headers `HTTP_SYNOLIA_MAINTENANCE_TOKEN: token` for a particular request to bypass maintenance mode.
136+
126137
## Development
127138

128139
See [How to contribute](CONTRIBUTING.md).

src/Checker/AdminChecker.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ public function isMaintenance(MaintenanceConfiguration $configuration, Request $
4040
$request = $this->requestStack->getMainRequest();
4141
Assert::isInstanceOf($request, Request::class);
4242

43-
if ($request === $this->requestStack->getCurrentRequest()) {
44-
$flashBag = $request->getSession()->getBag('flashes');
45-
if ($flashBag instanceof FlashBagInterface) {
46-
$flashBag->add('info', $this->translator->trans('maintenance.ui.message_info_admin'));
43+
if ($configuration->isAllowAdmins()) {
44+
if ($request === $this->requestStack->getCurrentRequest()) {
45+
$flashBag = $request->getSession()->getBag('flashes');
46+
if ($flashBag instanceof FlashBagInterface) {
47+
$flashBag->add('info', $this->translator->trans('maintenance.ui.message_info_admin'));
48+
}
4749
}
48-
}
4950

50-
return IsMaintenanceVoterInterface::ACCESS_GRANTED;
51+
return IsMaintenanceVoterInterface::ACCESS_GRANTED;
52+
}
5153
}
5254

5355
return IsMaintenanceVoterInterface::ACCESS_DENIED;

src/Checker/TokenChecker.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public static function getDefaultPriority(): int
2020

2121
public function isMaintenance(MaintenanceConfiguration $configuration, Request $request): bool
2222
{
23+
if ($request->query->get(TokenStorage::MAINTENANCE_TOKEN_NAME) === $configuration->getToken()) {
24+
return IsMaintenanceVoterInterface::ACCESS_GRANTED;
25+
}
26+
27+
if ($request->headers->get(TokenStorage::MAINTENANCE_TOKEN_NAME) === $configuration->getToken()) {
28+
return IsMaintenanceVoterInterface::ACCESS_GRANTED;
29+
}
30+
2331
if ($request->getSession()->get(TokenStorage::MAINTENANCE_TOKEN_NAME) === $configuration->getToken()) {
2432
return IsMaintenanceVoterInterface::ACCESS_GRANTED;
2533
}

src/Command/EnableMaintenanceCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Console\Command\Command;
1111
use Symfony\Component\Console\Input\InputArgument;
1212
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
1314
use Symfony\Component\Console\Output\OutputInterface;
1415
use Symfony\Contracts\Cache\CacheInterface;
1516
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -35,6 +36,7 @@ protected function configure(): void
3536
{
3637
$this
3738
->addArgument('ips_address', InputArgument::IS_ARRAY, 'Add ips addresses (separate multiple ips with a space)')
39+
->addOption('disable-admin', null, InputOption::VALUE_NONE, 'disable admin access')
3840
->setHelp('This command allows you to create the maintenance.yaml and also allows you to put the ips into this file.')
3941
;
4042
}
@@ -68,6 +70,9 @@ private function getMaintenanceConfiguration(InputInterface $input): Maintenance
6870
$maintenanceConfiguration->setChannels($this->getChannels());
6971
$maintenanceConfiguration->setEnabled(true);
7072

73+
$disableAdmin = (bool) $input->getOption('disable-admin');
74+
$maintenanceConfiguration->setAllowAdmins(!$disableAdmin);
75+
7176
/** @var array $ipsAddress */
7277
$ipsAddress = $input->getArgument('ips_address');
7378
if ([] !== $ipsAddress) {

src/Controller/MaintenanceConfigurationController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function __invoke(Request $request): Response
5656
}
5757

5858
return $this->render('@SynoliaSyliusMaintenancePlugin/Admin/maintenanceConfiguration.html.twig', [
59-
'form' => $form,
59+
'maintenanceConfiguration' => $maintenanceConfiguration,
60+
'form' => $form->createView(),
6061
]);
6162
}
6263
}

src/EventSubscriber/MaintenanceEventsubscriber.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Symfony\Component\DependencyInjection\Attribute\Autowire;
88
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
use Symfony\Component\HttpFoundation\JsonResponse;
910
use Symfony\Component\HttpFoundation\Response;
1011
use Symfony\Component\HttpKernel\Event\RequestEvent;
1112
use Symfony\Contracts\Cache\CacheInterface;
@@ -51,6 +52,15 @@ public function handle(RequestEvent $event): void
5152
'custom_message' => $configuration->getCustomMessage(),
5253
]);
5354

55+
if ('json' === $event->getRequest()->getContentTypeFormat()) {
56+
$event->setResponse(new JsonResponse([
57+
'key' => 'maintenance',
58+
'message' => $configuration->getCustomMessage(),
59+
], Response::HTTP_SERVICE_UNAVAILABLE));
60+
61+
return;
62+
}
63+
5464
$event->setResponse(new Response($responseContent, Response::HTTP_SERVICE_UNAVAILABLE));
5565
}
5666

src/Exporter/MaintenanceConfigurationExporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function export(MaintenanceConfiguration $configuration): void
3838
}
3939
$dataToExport['enabled'] = $configuration->isEnabled();
4040
$scheduler = $this->getSchedulerArray($configuration->getStartDate(), $configuration->getEndDate());
41+
$dataToExport['allow_admins'] = $configuration->isAllowAdmins();
4142

4243
$this->configurationFileManager->createMaintenanceFile(array_merge(
4344
$scheduler,

src/Factory/MaintenanceConfigurationFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function map(MaintenanceConfiguration $maintenanceConfiguration, ?
4646
'custom_message' => '',
4747
'token' => '',
4848
'allow_bots' => false,
49+
'allow_admins' => true,
4950
'enabled' => true,
5051
]);
5152
/**
@@ -56,6 +57,7 @@ public static function map(MaintenanceConfiguration $maintenanceConfiguration, ?
5657
* 'custom_message': string,
5758
* 'token': string,
5859
* 'allow_bots': bool,
60+
* 'allow_admins': bool,
5961
* 'enabled': bool,
6062
* } $options
6163
*/
@@ -79,6 +81,7 @@ public static function map(MaintenanceConfiguration $maintenanceConfiguration, ?
7981
->setCustomMessage($options['custom_message'])
8082
->setToken($options['token'])
8183
->setAllowBots($options['allow_bots'])
84+
->setAllowAdmins($options['allow_admins'])
8285
->setEnabled($options['enabled'])
8386
;
8487
}

src/Form/Type/MaintenanceConfigurationType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
6767
'label' => 'maintenance.ui.form.allow_bots',
6868
'required' => true,
6969
])
70+
->add('allowAdmins', CheckboxType::class, [
71+
'label' => 'maintenance.ui.form.allow_admins',
72+
'required' => true,
73+
])
7074
;
7175

7276
if ($this->channelRepository->count([]) > 1) {

src/Model/MaintenanceConfiguration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class MaintenanceConfiguration
2424

2525
private bool $allowBots = false;
2626

27+
private bool $allowAdmins = true;
28+
2729
public function __construct()
2830
{
2931
$this->token = bin2hex(random_bytes(16));
@@ -158,4 +160,16 @@ public function setAllowBots(bool $allowBots): self
158160

159161
return $this;
160162
}
163+
164+
public function isAllowAdmins(): bool
165+
{
166+
return $this->allowAdmins;
167+
}
168+
169+
public function setAllowAdmins(bool $allowAdmins): self
170+
{
171+
$this->allowAdmins = $allowAdmins;
172+
173+
return $this;
174+
}
161175
}

0 commit comments

Comments
 (0)