Skip to content

Commit

Permalink
Add maintenance mode (#80)
Browse files Browse the repository at this point in the history
* add maintenance flag to sulu community bundle

* update pull request template

* fix styleci

* restructure community manager compiler pass
  • Loading branch information
alexander-schranz authored and wachterjohannes committed Dec 11, 2017
1 parent 0faec9a commit 92359dc
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -35,4 +34,5 @@ Describe BC breaks/deprecations here. (remove this section if not needed)

#### To Do

- [ ] Create a documentation PR
- [ ] Add Documentation

1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ disabled:
- blankline_after_open_tag
- function_declaration
- single_line_class_definition
- const_separation
9 changes: 8 additions & 1 deletion Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
94 changes: 67 additions & 27 deletions DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}
}
19 changes: 18 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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()
Expand Down
33 changes: 33 additions & 0 deletions Resources/doc/13-maintenance.md
Original file line number Diff line number Diff line change
@@ -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:
<webspace_key>:
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 %}
```
5 changes: 4 additions & 1 deletion Resources/doc/3-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 22 additions & 18 deletions Resources/views/Login/login-embed.html.twig
Original file line number Diff line number Diff line change
@@ -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 %}

<a href="{{ path('sulu_community.profile') }}">
{% if media is not null %}
<img src="{{ media.thumbnails['50x50'] }}"/>
{{% 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) }}
</a>
<a href="{{ path('sulu_community.profile') }}">
{% if media is not null %}
<img src="{{ media.thumbnails['50x50'] }}"/>
{% endif %}

<a href="{{ path('sulu_community.logout') }}">
{{ 'Logout'|trans }}
</a>
{% else %}
<a href="{{ path('sulu_community.login') }}">
{{ 'Login'|trans }}
</a>
{{ app.user.username|default('No username'|trans) }}
</a>

<a href="{{ path('sulu_community.logout') }}">
{{ 'Logout'|trans }}
</a>
{% else %}
<a href="{{ path('sulu_community.login') }}">
{{ 'Login'|trans }}
</a>
{% endif %}
{% endif %}
9 changes: 9 additions & 0 deletions Resources/views/Maintenance/maintenance.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "SuluCommunityBundle::master.html.twig" %}

{% block content %}
<h1>{{ 'Maintenance'|trans }}</h1>

<p>
{{ 'maintenance_message'|trans }}
</p>
{% endblock %}

0 comments on commit 92359dc

Please sign in to comment.