-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature completion listener and form (#11)
* add completion listener service * add completion form and handling * register completion listener in service xml and set or unset in compilerpass * do not redirect to confirmation page after completion form and fix tests
- Loading branch information
1 parent
c502d99
commit b346e0a
Showing
18 changed files
with
649 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?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\Controller; | ||
|
||
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration; | ||
use Sulu\Bundle\CommunityBundle\EventListener\CompletionListener; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
/** | ||
* Handles the completion page. | ||
*/ | ||
class CompletionController extends AbstractController | ||
{ | ||
const TYPE = Configuration::TYPE_COMPLETION; | ||
|
||
/** | ||
* Handle registration form. | ||
* | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function indexAction(Request $request) | ||
{ | ||
$user = $this->getUser(); | ||
|
||
if (!$user) { | ||
throw new HttpException(403, 'You need to be logged in for completion form.'); | ||
} | ||
|
||
$communityManager = $this->getCommunityManager($this->getWebspaceKey()); | ||
|
||
// Create Form | ||
$form = $this->createForm( | ||
$communityManager->getConfigTypeProperty(self::TYPE, Configuration::FORM_TYPE), | ||
$user, | ||
$communityManager->getConfigTypeProperty(self::TYPE, Configuration::FORM_TYPE_OPTIONS) | ||
); | ||
|
||
$form->handleRequest($request); | ||
$success = false; | ||
|
||
// Handle Form Success | ||
if ($form->isSubmitted() && $form->isValid()) { | ||
$user = $form->getData(); | ||
|
||
// Completion User | ||
$communityManager->completion($user); | ||
|
||
// Save User | ||
$this->saveEntities(); | ||
|
||
// Redirect | ||
$session = $request->getSession(); | ||
$redirectTo = $session->get(CompletionListener::SESSION_STORE); | ||
|
||
if (!$redirectTo) { | ||
$redirectTo = $communityManager->getConfigTypeProperty(self::TYPE, Configuration::REDIRECT_TO); | ||
} else { | ||
$session->remove(CompletionListener::SESSION_STORE); | ||
} | ||
|
||
if ($redirectTo) { | ||
return $this->redirect($redirectTo); | ||
} | ||
|
||
$success = true; | ||
} | ||
|
||
return $this->renderTemplate( | ||
self::TYPE, | ||
[ | ||
'form' => $form->createView(), | ||
'success' => $success, | ||
] | ||
); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
DependencyInjection/CompilerPass/CommunityValidatorCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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; | ||
|
||
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Register the completion listener when validators are configured. | ||
*/ | ||
class CommunityValidatorCompilerPass implements CompilerPassInterface | ||
{ | ||
const COMPLETION_LISTENER_SERVICE_ID = 'sulu_community.completion_listener'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
// If no listener exists do nothing | ||
if (!$container->hasDefinition(self::COMPLETION_LISTENER_SERVICE_ID)) { | ||
return; | ||
} | ||
|
||
// Create Validator References | ||
$config = $container->getParameter('sulu_community.config'); | ||
|
||
foreach ($config[Configuration::WEBSPACES] as $webspaceKey => $webspaceConfig) { | ||
// Get Completion Validator | ||
$validatorId = $webspaceConfig[Configuration::TYPE_COMPLETION][Configuration::SERVICE]; | ||
|
||
if ($validatorId) { | ||
$validators[$webspaceKey] = new Reference($validatorId); | ||
} | ||
} | ||
|
||
// Register request listener only when validator exists. | ||
if (!empty($validators)) { | ||
$definition = $container->getDefinition(self::COMPLETION_LISTENER_SERVICE_ID); | ||
$definition->replaceArgument(3, $validators); | ||
} else { | ||
$container->removeDefinition(self::COMPLETION_LISTENER_SERVICE_ID); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?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\EventListener; | ||
|
||
use Sulu\Bundle\CommunityBundle\Validator\User\CompletionInterface; | ||
use Sulu\Bundle\SecurityBundle\Entity\User; | ||
use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
|
||
/** | ||
* Validates the current user entity. | ||
*/ | ||
class CompletionListener | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const SESSION_STORE = 'sulu_community/completion/redirect_to'; | ||
|
||
/** | ||
* @var RequestAnalyzerInterface | ||
*/ | ||
protected $requestAnalyzer; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
protected $router; | ||
|
||
/** | ||
* @var TokenStorage | ||
*/ | ||
protected $tokenStorage; | ||
|
||
/** | ||
* @var CompletionInterface[] | ||
*/ | ||
protected $validators; | ||
|
||
/** | ||
* CompletionListener constructor. | ||
* | ||
* @param RequestAnalyzerInterface $requestAnalyzer | ||
* @param RouterInterface $router | ||
* @param TokenStorage $tokenStorage | ||
* @param array $validators | ||
*/ | ||
public function __construct( | ||
RequestAnalyzerInterface $requestAnalyzer, | ||
RouterInterface $router, | ||
TokenStorage $tokenStorage, | ||
array $validators | ||
) { | ||
$this->requestAnalyzer = $requestAnalyzer; | ||
$this->router = $router; | ||
$this->tokenStorage = $tokenStorage; | ||
$this->validators = $validators; | ||
} | ||
|
||
/** | ||
* Will call a specific user completion validator of a webspace. | ||
* | ||
* @param GetResponseEvent $event | ||
*/ | ||
public function onRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
$completionUrl = $this->router->generate('sulu_community.completion'); | ||
|
||
if (!$event->isMasterRequest() | ||
|| $request->isMethod('post') | ||
|| $request->isXmlHttpRequest() | ||
|| $request->getPathInfo() === $completionUrl | ||
) { | ||
return; | ||
} | ||
|
||
$token = $this->tokenStorage->getToken(); | ||
|
||
/** @var User $user */ | ||
$user = $token->getUser(); | ||
|
||
if (!$user instanceof User) { | ||
// don't do anything if no user is login | ||
return; | ||
} | ||
|
||
if ($request->attributes->get('_route') !== 'sulu_community.confirmation') { | ||
$session = $request->getSession(); | ||
$session->set(self::SESSION_STORE, $request->getUri()); | ||
} | ||
|
||
$webspaceKey = $this->requestAnalyzer->getWebspace()->getKey(); | ||
$validator = $this->getValidator($webspaceKey); | ||
|
||
if ($validator && !$validator->validate($user, $webspaceKey)) { | ||
$response = new RedirectResponse($completionUrl); | ||
$event->setResponse($response); | ||
} | ||
} | ||
|
||
/** | ||
* @param CompletionInterface $validator | ||
* @param string $webspaceKey | ||
*/ | ||
public function addValidator(CompletionInterface $validator, $webspaceKey) | ||
{ | ||
$this->validators[$webspaceKey] = $validator; | ||
} | ||
|
||
/** | ||
* @param string $webspaceKey | ||
* | ||
* @return CompletionInterface | ||
*/ | ||
protected function getValidator($webspaceKey) | ||
{ | ||
if (!isset($this->validators[$webspaceKey])) { | ||
return; | ||
} | ||
|
||
return $this->validators[$webspaceKey]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.