diff --git a/Deleter/Deleter.php b/Deleter/Deleter.php index e49eda08..63037960 100644 --- a/Deleter/Deleter.php +++ b/Deleter/Deleter.php @@ -2,9 +2,10 @@ namespace Ornicar\MessageBundle\Deleter; -use Ornicar\MessageBundle\Authorizer\AuthorizerInterface; +use Ornicar\MessageBundle\Security\AuthorizerInterface; use Ornicar\MessageBundle\Model\ThreadInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; +use Ornicar\MessageBundle\Security\ParticipantProviderInterface; /** * Marks threads as deleted @@ -20,9 +21,17 @@ class Deleter implements ReaderInterface */ protected $authorizer; - public function __construct(AuthorizerInterface $authorizer) + /** + * The participant provider instance + * + * @var ParticipantProviderInterface + */ + protected $participantProvider; + + public function __construct(AuthorizerInterface $authorizer, ParticipantProviderInterface $participantProvider) { $this->authorizer = $authorizer; + $this->participantProvider = $participantProvider; } /** @@ -58,6 +67,6 @@ public function markAsUndeleted(ThreadInterface $thread) */ protected function getAuthenticatedParticipant() { - return $this->authorizer->getAuthenticatedParticipant(); + return $this->participantProvider->getAuthenticatedParticipant(); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 27dc4963..b29cd800 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -31,6 +31,7 @@ public function getConfigTreeBuilder() ->scalarNode('sender')->defaultValue('ornicar_message.sender.default')->cannotBeEmpty()->end() ->scalarNode('composer')->defaultValue('ornicar_message.composer.default')->cannotBeEmpty()->end() ->scalarNode('provider')->defaultValue('ornicar_message.provider.default')->cannotBeEmpty()->end() + ->scalarNode('participant_provider')->defaultValue('ornicar_message.participant_provider.default')->cannotBeEmpty()->end() ->scalarNode('authorizer')->defaultValue('ornicar_message.authorizer.default')->cannotBeEmpty()->end() ->scalarNode('message_reader')->defaultValue('ornicar_message.message_reader.default')->cannotBeEmpty()->end() ->scalarNode('thread_reader')->defaultValue('ornicar_message.thread_reader.default')->cannotBeEmpty()->end() diff --git a/DependencyInjection/OrnicarMessageExtension.php b/DependencyInjection/OrnicarMessageExtension.php index 201ca433..5f9b025c 100644 --- a/DependencyInjection/OrnicarMessageExtension.php +++ b/DependencyInjection/OrnicarMessageExtension.php @@ -40,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setAlias('ornicar_message.sender', $config['sender']); $container->setAlias('ornicar_message.composer', $config['composer']); $container->setAlias('ornicar_message.provider', $config['provider']); + $container->setAlias('ornicar_message.participant_provider', $config['participant_provider']); $container->setAlias('ornicar_message.authorizer', $config['authorizer']); $container->setAlias('ornicar_message.message_reader', $config['message_reader']); $container->setAlias('ornicar_message.thread_reader', $config['thread_reader']); diff --git a/FormHandler/AbstractMessageFormHandler.php b/FormHandler/AbstractMessageFormHandler.php index 91521bd0..31ff3127 100644 --- a/FormHandler/AbstractMessageFormHandler.php +++ b/FormHandler/AbstractMessageFormHandler.php @@ -6,7 +6,7 @@ use Symfony\Component\HttpFoundation\Request; use Ornicar\MessageBundle\Composer\ComposerInterface; use Ornicar\MessageBundle\FormModel\AbstractMessage; -use Ornicar\MessageBundle\Authorizer\AuthorizerInterface; +use Ornicar\MessageBundle\Security\ParticipantProviderInterface; use Ornicar\MessageBundle\Model\ParticipantInterface; abstract class AbstractMessageFormHandler @@ -14,13 +14,13 @@ abstract class AbstractMessageFormHandler protected $form; protected $request; protected $composer; - protected $authorizer; + protected $participantProvider; - public function __construct(Request $request, ComposerInterface $composer, AuthorizerInterface $authorizer) + public function __construct(Request $request, ComposerInterface $composer, ParticipantProviderInterface $participantProvider) { $this->request = $request; $this->composer = $composer; - $this->authorizer = $authorizer; + $this->participantProvider = $participantProvider; } /** @@ -58,6 +58,6 @@ abstract protected function composeAndSend(AbstractMessage $message); */ public function getAuthenticatedParticipant() { - return $this->authorizer->getAuthenticatedParticipant(); + return $this->participantProvider->getAuthenticatedParticipant(); } } diff --git a/Provider/Provider.php b/Provider/Provider.php index 33f59dc0..41be33db 100644 --- a/Provider/Provider.php +++ b/Provider/Provider.php @@ -5,8 +5,9 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Ornicar\MessageBundle\ModelManager\ThreadManagerInterface; -use Ornicar\MessageBundle\Authorizer\AuthorizerInterface; +use Ornicar\MessageBundle\Security\AuthorizerInterface; use Ornicar\MessageBundle\Reader\ReaderInterface; +use Ornicar\MessageBundle\Security\ParticipantProviderInterface; /** * Provides threads for the current authenticated user @@ -15,13 +16,6 @@ */ class Provider implements ProviderInterface { - /** - * The authorizer manager - * - * @var authorizerInterface - */ - protected $authorizer; - /** * The thread manager * @@ -36,11 +30,26 @@ class Provider implements ProviderInterface */ protected $threadReader; - public function __construct(ThreadManagerInterface $threadManager, AuthorizerInterface $authorizer, ReaderInterface $threadReader) + /** + * The authorizer manager + * + * @var authorizerInterface + */ + protected $authorizer; + + /** + * The participant provider instance + * + * @var ParticipantProviderInterface + */ + protected $participantProvider; + + public function __construct(ThreadManagerInterface $threadManager, ReaderInterface $threadReader, AuthorizerInterface $authorizer, ParticipantProviderInterface $participantProvider) { - $this->authorizer = $authorizer; $this->threadManager = $threadManager; $this->threadReader = $threadReader; + $this->authorizer = $authorizer; + $this->participantProvider = $participantProvider; } /** @@ -98,6 +107,6 @@ public function getThread($threadId) */ protected function getAuthenticatedParticipant() { - return $this->authorizer->getAuthenticatedParticipant(); + return $this->participantProvider->getAuthenticatedParticipant(); } } diff --git a/Reader/Reader.php b/Reader/Reader.php index 1c57f47c..9db2b990 100644 --- a/Reader/Reader.php +++ b/Reader/Reader.php @@ -2,7 +2,7 @@ namespace Ornicar\MessageBundle\Reader; -use Ornicar\MessageBundle\Authorizer\AuthorizerInterface; +use Ornicar\MessageBundle\Security\ParticipantProviderInterface; use Ornicar\MessageBundle\Model\ReadableInterface; use Ornicar\MessageBundle\ModelManager\ReadableManagerInterface; @@ -14,11 +14,11 @@ class Reader implements ReaderInterface { /** - * The authorizer instance + * The participantProvider instance * - * @var AuthorizerInterface + * @var ParticipantProviderInterface */ - protected $authorizer; + protected $participantProvider; /** * The readable manager @@ -27,9 +27,9 @@ class Reader implements ReaderInterface */ protected $readableManager; - public function __construct(AuthorizerInterface $authorizer, ReadableManagerInterface $readableManager) + public function __construct(ParticipantProviderInterface $participantProvider, ReadableManagerInterface $readableManager) { - $this->authorizer = $authorizer; + $this->participantProvider = $participantProvider; $this->readableManager = $readableManager; } @@ -60,6 +60,6 @@ public function markAsUnread(ReadableInterface $readable) */ protected function getAuthenticatedParticipant() { - return $this->authorizer->getAuthenticatedParticipant(); + return $this->participantProvider->getAuthenticatedParticipant(); } } diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 5abf9a33..69c7a4da 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -16,32 +16,38 @@ - + + + + + - + + - + - + + - + @@ -51,7 +57,7 @@ - + diff --git a/Resources/config/form.xml b/Resources/config/form.xml index 3ff6ee44..6708cea2 100644 --- a/Resources/config/form.xml +++ b/Resources/config/form.xml @@ -27,13 +27,13 @@ - + - + diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst index ca72a410..76399b4a 100644 --- a/Resources/doc/index.rst +++ b/Resources/doc/index.rst @@ -281,32 +281,33 @@ All configuration options are listed below:: # app/config/config.yml ornicar_message - db_driver: mongodb - thread_class: Acme\MessageBundle\Document\Thread - message_class: Acme\MessageBundle\Document\Message - message_manager: ornicar_message.message_manager # See ModelManager\MessageManagerInterface - thread_manager: ornicar_message.thread_manager # See ModelManager\ThreadManagerInterface - sender: ornicar_message.sender # See Sender\SenderInterface - composer: ornicar_message.composer # See Composer\ComposerInterface - provider: ornicar_message.provider # See Provider\ProviderInterface - authorizer: ornicar_message.authorizer # See Authorizer\AuthorizerInterface - message_reader: ornicar_message.message_reader # See Reader\ReaderInterface - thread_reader: ornicar_message.thread_reader # See Reader\ReaderInterface - deleter: ornicar_message.deleter # See Deleter\DeleterInterface + db_driver: mongodb + thread_class: Acme\MessageBundle\Document\Thread + message_class: Acme\MessageBundle\Document\Message + message_manager: ornicar_message.message_manager # See ModelManager\MessageManagerInterface + thread_manager: ornicar_message.thread_manager # See ModelManager\ThreadManagerInterface + sender: ornicar_message.sender # See Sender\SenderInterface + composer: ornicar_message.composer # See Composer\ComposerInterface + provider: ornicar_message.provider # See Provider\ProviderInterface + participant_provider: ornicar_message.participant_provider # See Security\ParticipantProviderInterface + authorizer: ornicar_message.authorizer # See Security\AuthorizerInterface + message_reader: ornicar_message.message_reader # See Reader\ReaderInterface + thread_reader: ornicar_message.thread_reader # See Reader\ReaderInterface + deleter: ornicar_message.deleter # See Deleter\DeleterInterface search: - finder: ornicar_message.search_finder # See Finder\FinderInterface - query_factory: ornicar_message.search_query_factory # See Finder\QueryFactoryInterface - query_parameter: 'q' # Request query parameter containing the term + finder: ornicar_message.search_finder # See Finder\FinderInterface + query_factory: ornicar_message.search_query_factory # See Finder\QueryFactoryInterface + query_parameter: 'q' # Request query parameter containing the term new_thread_form: - factory: ornicar_message.new_thread_form.factory # See FormFactory\NewThreadMessageFormFactory - type: ornicar_message.new_thread_form.type # See FormType\NewThreadMessageFormType - handler: ornicar_message.new_thread_form.handler # See FormHandler\NewThreadMessageFormHandler - name: message + factory: ornicar_message.new_thread_form.factory # See FormFactory\NewThreadMessageFormFactory + type: ornicar_message.new_thread_form.type # See FormType\NewThreadMessageFormType + handler: ornicar_message.new_thread_form.handler # See FormHandler\NewThreadMessageFormHandler + name: message reply_form: - factory: ornicar_message.reply_form.factory # See FormFactory\ReplyMessageFormFactory - type: ornicar_message.reply_form.type # See FormType\ReplyMessageFormType - handler: ornicar_message.reply_form.handler # See FormHandler\ReplyMessageFormHandler - name: message + factory: ornicar_message.reply_form.factory # See FormFactory\ReplyMessageFormFactory + type: ornicar_message.reply_form.type # See FormType\ReplyMessageFormType + handler: ornicar_message.reply_form.handler # See FormHandler\ReplyMessageFormHandler + name: message Implement a new persistence backend =================================== diff --git a/Search/Finder.php b/Search/Finder.php index 9eff837b..b4d09d17 100644 --- a/Search/Finder.php +++ b/Search/Finder.php @@ -3,7 +3,7 @@ namespace Ornicar\MessageBundle\Search; use Ornicar\MessageBundle\ModelManager\ThreadManagerInterface; -use Ornicar\MessageBundle\Authorizer\AuthorizerInterface; +use Ornicar\MessageBundle\Security\ParticipantProviderInterface; /** * Finds threads of a participant, matching a given query @@ -13,11 +13,11 @@ class Finder implements FinderInterface { /** - * The authorizer instance + * The participant provider instance * - * @var AuthorizerInterface + * @var ParticipantProviderInterface */ - protected $authorizer; + protected $participantProvider; /** * The thread manager @@ -26,9 +26,9 @@ class Finder implements FinderInterface */ protected $threadManager; - public function __construct(AuthorizerInterface $authorizer, ThreadManagerInterface $threadManager) + public function __construct(ParticipantProviderInterface $participantProvider, ThreadManagerInterface $threadManager) { - $this->authorizer = $authorizer; + $this->participantProvider = $participantProvider; $this->threadManager = $threadManager; } @@ -61,6 +61,6 @@ public function getQueryBuilder(Query $query) */ protected function getAuthenticatedParticipant() { - return $this->authorizer->getAuthenticatedParticipant(); + return $this->participantProvider->getAuthenticatedParticipant(); } } diff --git a/Authorizer/Authorizer.php b/Security/Authorizer.php similarity index 53% rename from Authorizer/Authorizer.php rename to Security/Authorizer.php index 304658a8..92bd9d4f 100644 --- a/Authorizer/Authorizer.php +++ b/Security/Authorizer.php @@ -1,30 +1,29 @@ */ class Authorizer implements AuthorizerInterface { /** - * The security context + * The participant provider * - * @var SecurityContextInterface + * @var ParticipantProviderInterface */ - protected $securityContext; + protected $participantProvider; - public function __construct(SecurityContextInterface $securityContext) + public function __construct(ParticipantProviderInterface $participantProvider) { - $this->securityContext = $securityContext; + $this->participantProvider = $participantProvider; } /** @@ -36,7 +35,7 @@ public function __construct(SecurityContextInterface $securityContext) */ public function canSeeThread(ThreadInterface $thread) { - return $this->isAuthenticated() && $thread->isParticipant($this->getAuthenticatedParticipant()); + return $this->getAuthenticatedParticipant() && $thread->isParticipant($this->getAuthenticatedParticipant()); } /** @@ -68,27 +67,8 @@ public function canMessageParticipant(ParticipantInterface $participant) * * @return ParticipantInterface */ - public function getAuthenticatedParticipant() + protected function getAuthenticatedParticipant() { - if (!$this->isAuthenticated()) { - return null; - } - $participant = $this->securityContext->getToken()->getUser(); - - if (!$participant instanceof ParticipantInterface) { - throw new AccessDeniedException('Must be logged in with a ParticipantInterface instance'); - } - - return $participant; - } - - /** - * Tells if there is an authenticated user - * - * @return boolean - */ - protected function isAuthenticated() - { - return $this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED'); + return $this->participantProvider->getAuthenticatedParticipant(); } } diff --git a/Authorizer/AuthorizerInterface.php b/Security/AuthorizerInterface.php similarity index 86% rename from Authorizer/AuthorizerInterface.php rename to Security/AuthorizerInterface.php index 0fba10d6..f7094c6d 100644 --- a/Authorizer/AuthorizerInterface.php +++ b/Security/AuthorizerInterface.php @@ -1,13 +1,12 @@ */ diff --git a/Security/ParticipantProvider.php b/Security/ParticipantProvider.php new file mode 100644 index 00000000..960fffef --- /dev/null +++ b/Security/ParticipantProvider.php @@ -0,0 +1,43 @@ + + */ +class ParticipantProvider implements ParticipantProviderInterface +{ + /** + * The security context + * + * @var SecurityContextInterface + */ + protected $securityContext; + + public function __construct(SecurityContextInterface $securityContext) + { + $this->securityContext = $securityContext; + } + + /** + * Gets the current authenticated user + * + * @return ParticipantInterface + */ + public function getAuthenticatedParticipant() + { + $participant = $this->securityContext->getToken()->getUser(); + + if (!$participant instanceof ParticipantInterface) { + throw new AccessDeniedException('Must be logged in with a ParticipantInterface instance'); + } + + return $participant; + } +} diff --git a/Security/ParticipantProviderInterface.php b/Security/ParticipantProviderInterface.php new file mode 100644 index 00000000..fc6de19d --- /dev/null +++ b/Security/ParticipantProviderInterface.php @@ -0,0 +1,20 @@ + + */ +interface ParticipantProviderInterface +{ + /** + * Gets the current authenticated user + * + * @return ParticipantInterface + */ + function getAuthenticatedParticipant(); +} diff --git a/Twig/Extension/MessageExtension.php b/Twig/Extension/MessageExtension.php index 783dade5..efff9257 100644 --- a/Twig/Extension/MessageExtension.php +++ b/Twig/Extension/MessageExtension.php @@ -2,16 +2,16 @@ namespace Ornicar\MessageBundle\Twig\Extension; -use Ornicar\MessageBundle\Authorizer\AuthorizerInterface; +use Ornicar\MessageBundle\Security\ParticipantProviderInterface; use Ornicar\MessageBundle\Model\ReadableInterface; class MessageExtension extends \Twig_Extension { - protected $authorizer; + protected $participantProvider; - public function __construct(AuthorizerInterface $authorizer) + public function __construct(ParticipantProviderInterface $participantProvider) { - $this->authorizer = $authorizer; + $this->participantProvider = $participantProvider; } /** @@ -43,7 +43,7 @@ public function isRead(ReadableInterface $readable) */ protected function getAuthenticatedParticipant() { - return $this->authorizer->getAuthenticatedParticipant(); + return $this->participantProvider->getAuthenticatedParticipant(); } /**