From 92359dc4e1b3ee87f99c8ba4bb1dc8286455b2c0 Mon Sep 17 00:00:00 2001 From: L91 Date: Mon, 11 Dec 2017 16:45:53 +0100 Subject: [PATCH] Add maintenance mode (#80) * add maintenance flag to sulu community bundle * update pull request template * fix styleci * restructure community manager compiler pass --- .github/PULL_REQUEST_TEMPLATE.md | 4 +- .styleci.yml | 1 + Controller/LoginController.php | 9 +- .../CommunityManagerCompilerPass.php | 94 +++++++++++++------ DependencyInjection/Configuration.php | 19 +++- Resources/doc/13-maintenance.md | 33 +++++++ Resources/doc/3-customization.md | 5 +- Resources/views/Login/login-embed.html.twig | 40 ++++---- .../views/Maintenance/maintenance.html.twig | 9 ++ 9 files changed, 164 insertions(+), 50 deletions(-) create mode 100644 Resources/doc/13-maintenance.md create mode 100644 Resources/views/Maintenance/maintenance.html.twig diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index bbdeadd9..a4d94507 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -7,7 +7,6 @@ | Fixed tickets | fixes #issuenum | Related issues/PRs | #issuenum | License | MIT -| Documentation PR | sulu/sulu-docs#prnum #### What's in this PR? @@ -35,4 +34,5 @@ Describe BC breaks/deprecations here. (remove this section if not needed) #### To Do -- [ ] Create a documentation PR +- [ ] Add Documentation + diff --git a/.styleci.yml b/.styleci.yml index 400102fb..f61f8b54 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -13,3 +13,4 @@ disabled: - blankline_after_open_tag - function_declaration - single_line_class_definition + - const_separation diff --git a/Controller/LoginController.php b/Controller/LoginController.php index bae132b4..f2dc6727 100644 --- a/Controller/LoginController.php +++ b/Controller/LoginController.php @@ -57,7 +57,14 @@ public function embedAction(Request $request) { $communityManager = $this->getCommunityManager($this->getWebspaceKey()); - $response = $this->render($communityManager->getConfigTypeProperty(self::TYPE, Configuration::EMBED_TEMPLATE)); + $maintenance = $communityManager->getConfigTypeProperty(Configuration::MAINTENANCE, Configuration::ENABLED); + + $response = $this->render( + $communityManager->getConfigTypeProperty(self::TYPE, Configuration::EMBED_TEMPLATE), + [ + 'maintenanceMode' => $maintenance, + ] + ); $response->setPrivate(); $response->setMaxAge(0); diff --git a/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php b/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php index abc5751f..5e86b918 100644 --- a/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php +++ b/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php @@ -29,33 +29,7 @@ public function process(ContainerBuilder $container) $webspacesConfig = $container->getParameter('sulu_community.webspaces_config'); foreach ($webspacesConfig as $webspaceKey => $webspaceConfig) { - // Set firewall by webspace key - if (null === $webspaceConfig[Configuration::FIREWALL]) { - $webspaceConfig[Configuration::FIREWALL] = $webspaceKey; - } - - // Set role by webspace key - if (null === $webspaceConfig[Configuration::ROLE]) { - $webspaceConfig[Configuration::ROLE] = ucfirst($webspaceKey) . 'User'; - } - - if (isset($webspaceConfig[Configuration::EMAIL_FROM])) { - $webspaceConfig[Configuration::EMAIL_FROM] = [ - $webspaceConfig[Configuration::EMAIL_FROM][Configuration::EMAIL_FROM_EMAIL] => $webspaceConfig[Configuration::EMAIL_FROM][Configuration::EMAIL_FROM_NAME], - ]; - } else { - $webspaceConfig[Configuration::EMAIL_FROM] = null; - } - - if (isset($webspaceConfig[Configuration::EMAIL_TO])) { - $webspaceConfig[Configuration::EMAIL_TO] = [ - $webspaceConfig[Configuration::EMAIL_TO][Configuration::EMAIL_TO_EMAIL] => $webspaceConfig[Configuration::EMAIL_TO][Configuration::EMAIL_TO_NAME], - ]; - } else { - $webspaceConfig[Configuration::EMAIL_TO] = null; - } - - $webspaceConfig[Configuration::WEBSPACE_KEY] = $webspaceKey; + $webspaceConfig = $this->updateWebspaceConfig($webspaceKey, $webspaceConfig); $webspacesConfig[$webspaceKey] = $webspaceConfig; $definition = new DefinitionDecorator('sulu_community.community_manager'); @@ -70,4 +44,70 @@ public function process(ContainerBuilder $container) $container->setParameter('sulu_community.webspaces_config', $webspacesConfig); } + + /** + * Update webspace config. + * + * @param string $webspaceKey + * @param array $webspaceConfig + * + * @return array + */ + private function updateWebspaceConfig($webspaceKey, array $webspaceConfig) + { + // Set firewall by webspace key + if (null === $webspaceConfig[Configuration::FIREWALL]) { + $webspaceConfig[Configuration::FIREWALL] = $webspaceKey; + } + + // Set role by webspace key + if (null === $webspaceConfig[Configuration::ROLE]) { + $webspaceConfig[Configuration::ROLE] = ucfirst($webspaceKey) . 'User'; + } + + // Set email from + if (isset($webspaceConfig[Configuration::EMAIL_FROM])) { + $webspaceConfig[Configuration::EMAIL_FROM] = [ + $webspaceConfig[Configuration::EMAIL_FROM][Configuration::EMAIL_FROM_EMAIL] => $webspaceConfig[Configuration::EMAIL_FROM][Configuration::EMAIL_FROM_NAME], + ]; + } else { + $webspaceConfig[Configuration::EMAIL_FROM] = null; + } + + // Set email to + if (isset($webspaceConfig[Configuration::EMAIL_TO])) { + $webspaceConfig[Configuration::EMAIL_TO] = [ + $webspaceConfig[Configuration::EMAIL_TO][Configuration::EMAIL_TO_EMAIL] => $webspaceConfig[Configuration::EMAIL_TO][Configuration::EMAIL_TO_NAME], + ]; + } else { + $webspaceConfig[Configuration::EMAIL_TO] = null; + } + + // Set maintenance mode + if ($webspaceConfig[Configuration::MAINTENANCE][Configuration::ENABLED]) { + $webspaceConfig = $this->activateMaintenanceMode($webspaceConfig); + } + + $webspaceConfig[Configuration::WEBSPACE_KEY] = $webspaceKey; + + return $webspaceConfig; + } + + /** + * Activate Maintenance mode. + * + * @param array $webspaceConfig + * + * @return array + */ + private function activateMaintenanceMode(array $webspaceConfig) + { + foreach (Configuration::$TYPES as $type) { + if (isset($webspaceConfig[$type][Configuration::TEMPLATE])) { + $webspaceConfig[$type][Configuration::TEMPLATE] = $webspaceConfig[Configuration::MAINTENANCE][Configuration::TEMPLATE]; + } + } + + return $webspaceConfig; + } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f2e0f85b..8af96699 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -38,6 +38,7 @@ class Configuration implements ConfigurationInterface const ROLE = 'role'; const WEBSPACE_KEY = 'webspace_key'; const FIREWALL = 'firewall'; + const MAINTENANCE = 'maintenance'; // Form types const TYPE_LOGIN = 'login'; @@ -54,11 +55,20 @@ class Configuration implements ConfigurationInterface public static $TYPES = [ self::TYPE_LOGIN, - self::TYPE_REGISTRATION, + self::TYPE_COMPLETION, self::TYPE_CONFIRMATION, + self::TYPE_REGISTRATION, + self::TYPE_PASSWORD_FORGET, + self::TYPE_PASSWORD_RESET, + self::TYPE_BLACKLISTED, + self::TYPE_BLACKLIST_CONFIRMED, + self::TYPE_BLACKLIST_DENIED, + self::TYPE_PROFILE, + self::TYPE_EMAIL_CONFIRMATION, ]; // Type configurations + const ENABLED = 'enabled'; const TEMPLATE = 'template'; const SERVICE = 'service'; const EMBED_TEMPLATE = 'embed_template'; @@ -129,6 +139,13 @@ public function getConfigTreeBuilder() ->end() ->scalarNode(self::ROLE)->defaultValue(null)->end() ->scalarNode(self::FIREWALL)->defaultValue(null)->end() + // Maintenance + ->arrayNode(self::MAINTENANCE) + ->canBeEnabled() + ->children() + ->scalarNode(self::TEMPLATE)->defaultValue('SuluCommunityBundle:Maintenance:maintenance.html.twig')->end() + ->end() + ->end() // Login ->arrayNode(self::TYPE_LOGIN) ->addDefaultsIfNotSet() diff --git a/Resources/doc/13-maintenance.md b/Resources/doc/13-maintenance.md new file mode 100644 index 00000000..30e34b2d --- /dev/null +++ b/Resources/doc/13-maintenance.md @@ -0,0 +1,33 @@ +# Maintenance + +You can temporarily change the community bundle into a maintenace mode +this will disable all form renderings. + +## Config + +```yml +# app/config/config.yml + +sulu_community: + webspaces: + : + maintenance: + enabled: false + template: AppBundle:template:community/Maintenance/maintenance.html.twig +``` + +The configured maintenance template will be rendered instead of configured +login, registration, completion, password forget, password reset, ... template. + +## Disable login embed + +In the `login-embed.html.twig` a variable `maintenanceMode` is available so you can render +in the login embed another text instead. + +```twig +{% if maintenanceMode %} + Maintenace mode active +{% else %} + {# ... #} +{% endif %} +``` diff --git a/Resources/doc/3-customization.md b/Resources/doc/3-customization.md index eb7eacd7..fe6e6c1d 100644 --- a/Resources/doc/3-customization.md +++ b/Resources/doc/3-customization.md @@ -19,7 +19,10 @@ sulu_community: email: "%sulu_admin.email%" role: CustomRoleName firewall: CustomFirewallName - + # Maintenance + maintenance: + enabled: false + template: AppBundle:template:community/Maintenance/maintenance.html.twig # Login login: embed_template: AppBundle:templates:community/Login/login-embed.html.twig diff --git a/Resources/views/Login/login-embed.html.twig b/Resources/views/Login/login-embed.html.twig index 0a2ccaa8..c1fb8040 100644 --- a/Resources/views/Login/login-embed.html.twig +++ b/Resources/views/Login/login-embed.html.twig @@ -1,22 +1,26 @@ -{% if app.user %} - {% set media = null %} - {% if app.user.contact.avatar is not null %} - {% set media = sulu_resolve_media(app.user.contact.avatar, request.locale) %} - {% endif %} - - - {% if media is not null %} - +{{% if maintenanceMode %} + {# Show nothing in maintenance mode #} +{% else %} + {% if app.user %} + {% set media = null %} + {% if app.user.contact.avatar is not null %} + {% set media = sulu_resolve_media(app.user.contact.avatar, request.locale) %} {% endif %} - {{ app.user.username|default('No username'|trans) }} - + + {% if media is not null %} + + {% endif %} - - {{ 'Logout'|trans }} - -{% else %} - - {{ 'Login'|trans }} - + {{ app.user.username|default('No username'|trans) }} + + + + {{ 'Logout'|trans }} + + {% else %} + + {{ 'Login'|trans }} + + {% endif %} {% endif %} diff --git a/Resources/views/Maintenance/maintenance.html.twig b/Resources/views/Maintenance/maintenance.html.twig new file mode 100644 index 00000000..42d70d00 --- /dev/null +++ b/Resources/views/Maintenance/maintenance.html.twig @@ -0,0 +1,9 @@ +{% extends "SuluCommunityBundle::master.html.twig" %} + +{% block content %} +

{{ 'Maintenance'|trans }}

+ +

+ {{ 'maintenance_message'|trans }} +

+{% endblock %}