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

Fix false firewall name and validate webspace in community admin #87

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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ install:
script:
- ./vendor/bin/phpunit --coverage-clover=coverage.clover
- ./Tests/app/console lint:twig Resources/views --env test
- ./Tests/app/console sulu:community:init -vvv --env test
- if [[ $PHPSTAN == 'true' ]]; then ./vendor/bin/phpstan analyse ./ --level 2 -c phpstan.neon ; fi

after_script:
Expand Down
30 changes: 28 additions & 2 deletions Admin/CommunityAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,35 @@ public function getSecurityContexts()
{
$systems = [];

$webspaceCollection = $this->webspaceManager->getWebspaceCollection();

$webspaceKeys = array_keys($webspaceCollection->getWebspaces());

foreach ($this->webspacesConfiguration as $webspaceKey => $webspaceConfig) {
$webspace = $this->webspaceManager->getWebspaceCollection()->getWebspace($webspaceKey);
$system = $webspace->getSecurity()->getSystem();
$webspace = $webspaceCollection->getWebspace($webspaceKey);

if (!$webspace) {
throw new \InvalidArgumentException(
sprintf(
'Webspace "%s" not found for "sulu_community" expected one of %s.',
$webspaceKey,
'"' . implode('", "', $webspaceKeys) . '"'
)
);
}

$security = $webspace->getSecurity();

if (!$security) {
throw new \InvalidArgumentException(
sprintf(
'Missing "<security><system>Website</system><security>" configuration in webspace "%s" for "sulu_community".',
$webspaceKey
)
);
}

$system = $security->getSystem();
$systems[$system] = [];
}

Expand Down
3 changes: 2 additions & 1 deletion Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\CommunityBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\CommunityBundle\DependencyInjection\CompilerPass\Normalizer;
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface;
use Sulu\Bundle\SecurityBundle\Entity\Role;
Expand Down Expand Up @@ -78,7 +79,7 @@ protected function initWebspace($webspace, OutputInterface $output)
{
$webspaceKey = $webspace->getKey();

$communityServiceName = sprintf('sulu_community.%s.community_manager', $webspaceKey);
$communityServiceName = sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey));

if (!$webspace->getSecurity() || !$this->getContainer()->has($communityServiceName)) {
return;
Expand Down
3 changes: 2 additions & 1 deletion Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\CompilerPass\Normalizer;
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface;
use Sulu\Bundle\SecurityBundle\Entity\User;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected function getCommunityManager($webspaceKey)
{
if (!isset($this->communityManagers[$webspaceKey])) {
$this->communityManagers[$webspaceKey] = $this->get(
sprintf('sulu_community.%s.community_manager', $webspaceKey)
sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ public function process(ContainerBuilder $container)
$definition->replaceArgument(1, $webspaceKey);

$container->setDefinition(
sprintf('sulu_community.%s.community_manager', $webspaceKey),
sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey)),
$definition
);

if (false !== strpos($webspaceKey, '-')) {
$container->setAlias(
sprintf('sulu_community.%s.community_manager', $webspaceKey),
sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey))
);
}
}

$container->setParameter('sulu_community.webspaces_config', $webspacesConfig);
Expand All @@ -60,6 +67,9 @@ private function updateWebspaceConfig($webspaceKey, array $webspaceConfig)
$webspaceConfig[Configuration::FIREWALL] = $webspaceKey;
}

// TODO currently symfony normalize the security firewalls key which will replace "-" with "_".
$webspaceConfig[Configuration::FIREWALL] = Normalizer::normalize($webspaceConfig[Configuration::FIREWALL]);

// Set role by webspace key
if (null === $webspaceConfig[Configuration::ROLE]) {
$webspaceConfig[Configuration::ROLE] = ucfirst($webspaceKey) . 'User';
Expand Down
25 changes: 25 additions & 0 deletions DependencyInjection/CompilerPass/Normalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\DependencyInjection\CompilerPass;

/**
* Only used for internal usages.
*
* @internal
*/
class Normalizer
{
public static function normalize($text)
{
return str_replace('-', '_', $text);
}
}
4 changes: 0 additions & 4 deletions Resources/config/routing_website.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
<default key="_controller">SuluCommunityBundle:Login:index</default>
</route>

<route id="sulu_community.login" path="/login">
<default key="_controller">SuluCommunityBundle:Login:index</default>
</route>

<route id="sulu_community.profile" path="/profile">
<default key="_controller">SuluCommunityBundle:Profile:index</default>
</route>
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Controller/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ protected function setUp()
$entityManager = $this->getEntityManager();

$role = new Role();
$role->setName('Sulu_ioUser');
$role->setSystem('Sulu');
$role->setName('Sulu-ioUser');
$role->setSystem('Website');

$emailType = new EmailType();
$emailType->setName('private');
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Listener/BlacklistListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testValidateEmail()
$user->getEmail()->willReturn('[email protected]');

$event = $this->prophesize(CommunityEvent::class);
$event->getConfigProperty(Configuration::WEBSPACE_KEY)->willReturn('sulu_io');
$event->getConfigProperty(Configuration::WEBSPACE_KEY)->willReturn('sulu-io');
$event->getConfigProperty(Configuration::EMAIL_TO)->willReturn(['[email protected]' => '[email protected]']);
$event->getConfigProperty(Configuration::EMAIL_FROM)->willReturn(['[email protected]' => '[email protected]']);
$event->getConfigTypeProperty(Configuration::TYPE_BLACKLISTED, Configuration::EMAIL)->willReturn(
Expand All @@ -92,7 +92,7 @@ public function testValidateEmail()
Argument::that(
function (BlacklistUser $item) use ($user) {
return '123-123-123' === $item->getToken()
&& 'sulu_io' === $item->getWebspaceKey()
&& 'sulu-io' === $item->getWebspaceKey()
&& $item->getUser() === $user->reveal();
}
)
Expand Down
6 changes: 5 additions & 1 deletion Tests/app/Resources/webspaces/sulu.io.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
xsi:schemaLocation="http://schemas.sulu.io/webspace/webspace http://schemas.sulu.io/webspace/webspace-1.1.xsd">

<name>Sulu CMF</name>
<key>sulu_io</key>
<key>sulu-io</key>

<security>
<system>Website</system>
</security>

<localizations>
<localization language="de" default="true"/>
Expand Down
5 changes: 5 additions & 0 deletions Tests/app/config/config_admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ parameters:

framework:
router: { resource: "%kernel.root_dir%/config/routing_admin.yml" }

sulu_community:
webspaces:
sulu-io:
from: '[email protected]'
4 changes: 2 additions & 2 deletions Tests/app/config/config_website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ framework:

sulu_community:
webspaces:
sulu_io:
to: '[email protected]'
sulu-io:
from: '[email protected]'

sulu_security:
checker:
Expand Down
26 changes: 18 additions & 8 deletions Tests/app/config/config_website_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ imports:
- { resource: config_website.yml }

security:
acl:
connection: default
session_fixation_strategy: none

access_decision_manager:
strategy: affirmative
Expand All @@ -16,14 +15,25 @@ security:
id: sulu_security.user_provider

access_control:
- { path: /login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /profile, roles: ROLE_USER }
- { path: /completion, roles: ROLE_USER }

firewalls:
test:
sulu-io:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login
login_path: sulu_community.login
check_path: sulu_community.login
default_target_path: sulu_community.profile
http_basic: ~
anonymous: ~
logout:
path: sulu_community.logout
target: /
remember_me:
secret: "%secret%"
lifetime: 604800 # 1 week in seconds
path: /

sulu_security:
checker:
enabled: true