From f64e3afa6348e641d4f8aa70fabca1b1a6ae79d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Wed, 16 Sep 2015 15:37:04 +0200 Subject: [PATCH] Use voters in workzone, actionbar and basket_actionbar --- .../Phrasea/Authorization/BaseVoter.php | 18 ++++++++++++------ .../Controller/Prod/BasketController.php | 6 ++++++ .../Phrasea/Controller/Prod/RootController.php | 9 +++++++++ .../Core/Provider/PluginServiceProvider.php | 18 ++++++++++++++++++ templates/web/prod/WorkZone/Basket.html.twig | 12 +++++------- templates/web/prod/index.html.twig | 12 ++---------- templates/web/prod/tab_headers.html.twig | 2 +- templates/web/prod/toolbar.html.twig | 10 +++++----- 8 files changed, 58 insertions(+), 29 deletions(-) diff --git a/lib/Alchemy/Phrasea/Authorization/BaseVoter.php b/lib/Alchemy/Phrasea/Authorization/BaseVoter.php index 19b1f38261..2257350590 100644 --- a/lib/Alchemy/Phrasea/Authorization/BaseVoter.php +++ b/lib/Alchemy/Phrasea/Authorization/BaseVoter.php @@ -16,16 +16,21 @@ use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; abstract class BaseVoter implements VoterInterface { private $supportedAttributes; - private $supportedClass; + private $supportedClasses; /** @var Application */ private $app; - public function __construct(Application $app, array $attributes, $supportedClass) + /** + * @param Application $app + * @param array $attributes + * @param string|array $supportedClasses + */ + public function __construct(Application $app, array $attributes, $supportedClasses) { $this->app = $app; $this->supportedAttributes = $attributes; - $this->supportedClass = $supportedClass; + $this->supportedClasses = is_array($supportedClasses) ? $supportedClasses : [$supportedClasses]; if (!is_callable([$this, 'isGranted'])) { throw new \LogicException('Subclasses should implement a "isGranted" method'); @@ -39,9 +44,10 @@ abstract class BaseVoter implements VoterInterface public function supportsClass($class) { - $supportedClass = $this->supportedClass; - if ($class == $supportedClass || is_subclass_of($class, $supportedClass)) { - return true; + foreach ($this->supportedClasses as $supportedClass) { + if ($class == $supportedClass || is_subclass_of($class, $supportedClass)) { + return true; + } } return false; diff --git a/lib/Alchemy/Phrasea/Controller/Prod/BasketController.php b/lib/Alchemy/Phrasea/Controller/Prod/BasketController.php index 56a76f6263..98e0438049 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/BasketController.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/BasketController.php @@ -36,9 +36,15 @@ class BasketController extends Controller } } + /** @var \Closure $filter */ + $filter = $this->app['plugin.filter_by_authorization']; + return $this->render('prod/WorkZone/Basket.html.twig', [ 'basket' => $basket, 'ordre' => $request->query->get('order'), + 'plugins' => [ + 'actionbar' => $filter('workzone.basket.actionbar'), + ], ]); } diff --git a/lib/Alchemy/Phrasea/Controller/Prod/RootController.php b/lib/Alchemy/Phrasea/Controller/Prod/RootController.php index 0c804b781c..99c550051d 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/RootController.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/RootController.php @@ -106,6 +106,14 @@ class RootController extends Controller $helper = new Helper\Prod($this->app, $request); + /** @var \Closure $filter */ + $filter = $this->app['plugin.filter_by_authorization']; + + $plugins = [ + 'workzone' => $filter('workzone'), + 'actionbar' => $filter('actionbar'), + ]; + return $this->render('prod/index.html.twig', [ 'module_name' => 'Production', 'WorkZone' => new Helper\WorkZone($this->app, $request), @@ -129,6 +137,7 @@ class RootController extends Controller 'thesau_json_sbas' => json_encode($sbas), 'thesau_json_bas2sbas' => json_encode($bas2sbas), 'thesau_languages' => $this->app['locales.available'], + 'plugins' => $plugins, ]); } /** diff --git a/lib/Alchemy/Phrasea/Core/Provider/PluginServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/PluginServiceProvider.php index 1110d7101e..e6bb654018 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/PluginServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/PluginServiceProvider.php @@ -11,6 +11,7 @@ namespace Alchemy\Phrasea\Core\Provider; +use Alchemy\Phrasea\Authorization\AuthorizationChecker; use Alchemy\Phrasea\Plugin\PluginManager; use Alchemy\Phrasea\Plugin\Schema\ManifestValidator; use Alchemy\Phrasea\Plugin\Schema\PluginValidator; @@ -57,6 +58,23 @@ class PluginServiceProvider implements ServiceProviderInterface $app['plugin.workzone'] = $app->share(function () { return new Pimple(); }); + $app['plugin.filter_by_authorization'] = $app->protect(function ($pluginZone, $attributes = 'VIEW') use ($app) { + /** @var \Pimple $container */ + $container = $app['plugin.' . $pluginZone]; + /** @var AuthorizationChecker $authorizationChecker */ + $authorizationChecker = $app['phraseanet.authorization_checker']; + + $plugins = []; + foreach ($container->keys() as $pluginKey) { + $plugin = $container[$pluginKey]; + + if ($authorizationChecker->isGranted($attributes, $plugin)) { + $plugins[$pluginKey] = $plugin; + } + } + + return $plugins; + }); $app['plugin.locale.textdomains'] = new ArrayObject(); diff --git a/templates/web/prod/WorkZone/Basket.html.twig b/templates/web/prod/WorkZone/Basket.html.twig index 035af334eb..38eddf5e10 100644 --- a/templates/web/prod/WorkZone/Basket.html.twig +++ b/templates/web/prod/WorkZone/Basket.html.twig @@ -58,15 +58,13 @@ - {% if app['plugin.workzone.basket.actionbar'].keys() is not empty %} - {% for pluginId in app['plugin.workzone.basket.actionbar'].keys() %} - {% for key,action in app['plugin.workzone.basket.actionbar'][pluginId].getBasketActionBar() %} + {% if plugins['actionbar'] is not empty %} + {% for plugin in plugins['actionbar'] %} + {% for key, action in plugin.getBasketActionBar() %} {% set label = action.label %} - - - {% endfor %} {% endfor %} {% endif %} diff --git a/templates/web/prod/index.html.twig b/templates/web/prod/index.html.twig index 61207bd585..0bd34f490b 100644 --- a/templates/web/prod/index.html.twig +++ b/templates/web/prod/index.html.twig @@ -160,14 +160,6 @@ {% endblock %} {% block content %} - - {% set workzone_plugins = [] %} - {% for plugin in app['plugin.workzone'].keys() %} - {% if app['phraseanet.authorization_checker'].isGranted('VIEW', app['plugin.workzone'][plugin]) %} - {% set workzone_plugins = workzone_plugins|merge({(plugin): app['plugin.workzone'][plugin]}) %} - {% endif %} - {% endfor %} - {% set search_datas = module_prod.get_search_datas() %}
@@ -203,8 +195,8 @@ {% include 'prod/tab_thesaurus.html.twig' with {has_access_to_module: app.getAclForUser(app.getAuthenticatedUser()).has_access_to_module('thesaurus')} %} {% endif %} {% endblock %} -
- {% for pluginId, plugin in workzone_plugins %} +
+ {% for pluginId, plugin in plugins.workzone %} {% include plugin.getWorkzoneTemplate() with {'app': app, 'plugin_id': pluginId} only %} {% endfor %}
diff --git a/templates/web/prod/tab_headers.html.twig b/templates/web/prod/tab_headers.html.twig index 05c9bdbc6a..d1e40e6a37 100644 --- a/templates/web/prod/tab_headers.html.twig +++ b/templates/web/prod/tab_headers.html.twig @@ -16,7 +16,7 @@ {% endif %} - {% if workzone_plugins is not empty %} + {% if plugins.workzone is not empty %}
  • diff --git a/templates/web/prod/toolbar.html.twig b/templates/web/prod/toolbar.html.twig index 1608bf9309..fe1f151370 100644 --- a/templates/web/prod/toolbar.html.twig +++ b/templates/web/prod/toolbar.html.twig @@ -154,18 +154,18 @@ {{ 'action : publier' | trans }}
  • - {% if app['plugin.actionbar'].keys() is not empty %} - {% for actionId in app['plugin.actionbar'].keys() %} - {% for key, action in app['plugin.actionbar'][actionId].getActionBar().push|default([]) %} + {% if plugins.actionbar is not empty %} + {% for plugin in plugins.actionbar %} + {% for key, action in plugin.getActionBar().push|default([]) %}
  • {% if action.icon %} - + {% endif %} {% set label = action.label %} - {% trans label app['plugin.actionbar'][actionId].PluginLocale %} + {% trans label plugin.PluginLocale %}
  • {% endfor %}