From d7fbc361d7a8c6c9e356da5e3e89edaad8869e68 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Wed, 5 Dec 2018 09:52:59 +0100 Subject: [PATCH] Implement permission configuration. --- composer.json | 3 +- src/Bundle/Resources/config/listeners.yml | 18 +++ src/Bundle/Resources/contao/config/config.php | 14 ++- src/Bundle/Resources/contao/dca/tl_user.php | 68 +++++++++++ .../Resources/contao/dca/tl_user_group.php | 67 +++++++++++ .../Resources/contao/languages/en/modules.php | 2 + .../Resources/contao/languages/en/tl_user.php | 31 +++++ .../contao/languages/en/tl_user_group.php | 31 +++++ src/Listener/Dca/AbstractUserDcaListener.php | 109 ++++++++++++++++++ src/Listener/Dca/UserDcaListener.php | 28 +++++ src/Listener/Dca/UserGroupDcaListener.php | 28 +++++ 11 files changed, 396 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/Resources/contao/dca/tl_user.php create mode 100644 src/Bundle/Resources/contao/dca/tl_user_group.php create mode 100644 src/Bundle/Resources/contao/languages/en/tl_user.php create mode 100644 src/Bundle/Resources/contao/languages/en/tl_user_group.php create mode 100644 src/Listener/Dca/AbstractUserDcaListener.php create mode 100644 src/Listener/Dca/UserDcaListener.php create mode 100644 src/Listener/Dca/UserGroupDcaListener.php diff --git a/composer.json b/composer.json index 5a69266..c0e85ef 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "netzmacht/contao-page-context": "~1.0", "contao-community-alliance/meta-palettes": "^2.0 || ^1.5", "menatwork/contao-multicolumnwizard": "^3.2", - "doctrine/cache": "^1.0" + "doctrine/cache": "^1.0", + "codefog/contao-widget_tree_picker": "^2.4" }, "require-dev": { "phpcq/all-tasks": "^1.2", diff --git a/src/Bundle/Resources/config/listeners.yml b/src/Bundle/Resources/config/listeners.yml index 692bc00..199fad5 100644 --- a/src/Bundle/Resources/config/listeners.yml +++ b/src/Bundle/Resources/config/listeners.yml @@ -83,6 +83,24 @@ services: public: true arguments: - '%netzmacht.contao_leaflet.styles%' + - + netzmacht.contao_leaflet.listeners.dca.user: + class: Netzmacht\Contao\Leaflet\Listener\Dca\UserDcaListener + public: true + arguments: + - '@netzmacht.contao_toolkit.dca.manager' + - '@netzmacht.contao_leaflet.layer_label_renderer' + - '@translator' + - '%netzmacht.contao_leaflet.layers%' + - + netzmacht.contao_leaflet.listeners.dca.user_group: + class: Netzmacht\Contao\Leaflet\Listener\Dca\UserGroupDcaListener + public: true + arguments: + - '@netzmacht.contao_toolkit.dca.manager' + - '@netzmacht.contao_leaflet.layer_label_renderer' + - '@translator' + - '%netzmacht.contao_leaflet.layers%' netzmacht.contao_leaflet.listeners.geo_json_listener: class: Netzmacht\Contao\Leaflet\Listener\GeoJsonListener diff --git a/src/Bundle/Resources/contao/config/config.php b/src/Bundle/Resources/contao/config/config.php index 9f4431c..39e6713 100644 --- a/src/Bundle/Resources/contao/config/config.php +++ b/src/Bundle/Resources/contao/config/config.php @@ -11,7 +11,7 @@ */ /* - * Backend module. + * Backend modules */ array_insert( @@ -48,8 +48,9 @@ if (TL_MODE === 'BE') { $GLOBALS['TL_CSS'][] = 'bundles/netzmachtcontaoleaflet/css/backend_global.css'; } + /* - * Models. + * Models */ $GLOBALS['TL_MODELS']['tl_leaflet_control'] = \Netzmacht\Contao\Leaflet\Model\ControlModel::class; @@ -60,3 +61,12 @@ $GLOBALS['TL_MODELS']['tl_leaflet_marker'] = \Netzmacht\Contao\Leaflet\Model\Ma $GLOBALS['TL_MODELS']['tl_leaflet_popup'] = \Netzmacht\Contao\Leaflet\Model\PopupModel::class; $GLOBALS['TL_MODELS']['tl_leaflet_style'] = \Netzmacht\Contao\Leaflet\Model\StyleModel::class; $GLOBALS['TL_MODELS']['tl_leaflet_vector'] = \Netzmacht\Contao\Leaflet\Model\VectorModel::class; + + +/* + * Permissions + */ + +$GLOBALS['TL_PERMISSIONS'][] = 'leaflet_layers'; +$GLOBALS['TL_PERMISSIONS'][] = 'leaflet_layer_permissions'; +$GLOBALS['TL_PERMISSIONS'][] = 'leaflet_tables'; diff --git a/src/Bundle/Resources/contao/dca/tl_user.php b/src/Bundle/Resources/contao/dca/tl_user.php new file mode 100644 index 0000000..3b92ecd --- /dev/null +++ b/src/Bundle/Resources/contao/dca/tl_user.php @@ -0,0 +1,68 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +use Contao\CoreBundle\DataContainer\PaletteManipulator; + +PaletteManipulator::create() + ->addLegend('leaflet_legend', 'amg_legend', PaletteManipulator::POSITION_BEFORE) + ->addField( + ['leaflet_layers', 'leaflet_layer_permissions', 'leaflet_tables'], + 'leaflet_legend', + PaletteManipulator::POSITION_APPEND + ) + ->applyToPalette('extend', 'tl_user') + ->applyToPalette('custom', 'tl_user'); + + +$GLOBALS['TL_DCA']['tl_user']['fields']['leaflet_layers'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_user']['leaflet_layers'], + 'exclude' => true, + 'inputType' => 'treePicker', + 'eval' => [ + 'foreignTable' => 'tl_leaflet_layer', + 'titleField' => 'title', + 'searchField' => 'title', + 'managerHref' => 'do=leaflet_layer', + 'fieldType' => 'checkbox', + 'selectParents' => true, + 'multiple' => true, + 'pickerCallback' => ['netzmacht.contao_leaflet.listeners.dca.user', 'generateLayersRowLabel'] + ], + 'sql' => 'blob NULL', +]; + +$GLOBALS['TL_DCA']['tl_user']['fields']['leaflet_tables'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_user']['leaflet_tables'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'options' => ['tl_leaflet_style', 'tl_leaflet_icon', 'tl_leaflet_popup'], + 'reference' => &$GLOBALS['TL_LANG']['MOD'], + 'eval' => [ + 'multiple' => true, + ], + 'sql' => 'mediumblob NULL', +]; + +$GLOBALS['TL_DCA']['tl_user']['fields']['leaflet_layer_permissions'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'options' => ['create', 'edit', 'delete', 'data'], + 'reference' => &$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options'], + 'eval' => [ + 'helpwizard' => true, + 'multiple' => true, + ], + 'sql' => 'mediumblob NULL', +]; diff --git a/src/Bundle/Resources/contao/dca/tl_user_group.php b/src/Bundle/Resources/contao/dca/tl_user_group.php new file mode 100644 index 0000000..2d8d842 --- /dev/null +++ b/src/Bundle/Resources/contao/dca/tl_user_group.php @@ -0,0 +1,67 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +use Contao\CoreBundle\DataContainer\PaletteManipulator; + +PaletteManipulator::create() + ->addLegend('leaflet_legend', 'amg_legend', PaletteManipulator::POSITION_BEFORE) + ->addField( + ['leaflet_layers', 'leaflet_layer_permissions', 'leaflet_tables'], + 'leaflet_legend', + PaletteManipulator::POSITION_APPEND + ) + ->applyToPalette('default', 'tl_user_group'); + + +$GLOBALS['TL_DCA']['tl_user_group']['fields']['leaflet_layers'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layers'], + 'exclude' => true, + 'inputType' => 'treePicker', + 'eval' => [ + 'foreignTable' => 'tl_leaflet_layer', + 'titleField' => 'title', + 'searchField' => 'title', + 'managerHref' => 'do=leaflet_layer', + 'fieldType' => 'checkbox', + 'selectParents' => true, + 'multiple' => true, + 'pickerCallback' => ['netzmacht.contao_leaflet.listeners.dca.user_group', 'generateLayersRowLabel'] + ], + 'sql' => 'blob NULL', +]; + +$GLOBALS['TL_DCA']['tl_user_group']['fields']['leaflet_tables'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_user_group']['leaflet_tables'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'options' => ['tl_leaflet_style', 'tl_leaflet_icon', 'tl_leaflet_popup'], + 'reference' => &$GLOBALS['TL_LANG']['MOD'], + 'eval' => [ + 'multiple' => true, + ], + 'sql' => 'mediumblob NULL', +]; + +$GLOBALS['TL_DCA']['tl_user_group']['fields']['leaflet_layer_permissions'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'options' => ['create', 'edit', 'delete', 'data'], + 'reference' => &$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options'], + 'eval' => [ + 'helpwizard' => true, + 'multiple' => true, + ], + 'sql' => 'mediumblob NULL', +]; diff --git a/src/Bundle/Resources/contao/languages/en/modules.php b/src/Bundle/Resources/contao/languages/en/modules.php index f2b1520..fae115a 100644 --- a/src/Bundle/Resources/contao/languages/en/modules.php +++ b/src/Bundle/Resources/contao/languages/en/modules.php @@ -22,6 +22,8 @@ $GLOBALS['TL_LANG']['MOD']['leaflet_about'][1] = 'About the Leaflet integration $GLOBALS['TL_LANG']['MOD']['tl_leaflet_map'] = 'Maps'; $GLOBALS['TL_LANG']['MOD']['tl_leaflet_layer'] = 'Layers'; $GLOBALS['TL_LANG']['MOD']['tl_leaflet_icon'] = 'Icons'; +$GLOBALS['TL_LANG']['MOD']['tl_leaflet_style'] = 'Styles'; +$GLOBALS['TL_LANG']['MOD']['tl_leaflet_popup'] = 'Popup'; $GLOBALS['TL_LANG']['MOD']['tl_leaflet_marker'] = 'Markers'; $GLOBALS['TL_LANG']['MOD']['tl_leaflet_control'] = 'Controls'; diff --git a/src/Bundle/Resources/contao/languages/en/tl_user.php b/src/Bundle/Resources/contao/languages/en/tl_user.php new file mode 100644 index 0000000..6d04bff --- /dev/null +++ b/src/Bundle/Resources/contao/languages/en/tl_user.php @@ -0,0 +1,31 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +$GLOBALS['TL_LANG']['tl_user']['leaflet_legend'] = 'Leaflet Maps Permissions'; + +$GLOBALS['TL_LANG']['tl_user']['leaflet_layers'][0] = 'Allowed map layers'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layers'][1] = 'Please choose the allowed map layers.'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions'][0] = 'Layer permissions'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions'][1] = 'Please choose which permissions are allowed for the map layer.'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_tables'][0] = 'Allows map tables'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_tables'][1] = 'Please choose which tables are allowed.'; + +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['create'][0] = 'Create layers'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['create'][1] = 'Grant permission to create new layers.'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['edit'][0] = 'Edit layers'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['edit'][1] = 'Grant permission to edit layers.'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['delete'][0] = 'Delete layers'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['delete'][1] = 'Grant permission to delete layers.'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['data'][0] = 'Edit data'; +$GLOBALS['TL_LANG']['tl_user']['leaflet_layer_permissions_options']['data'][1] = 'Grant permission to edit data of a layer.'; diff --git a/src/Bundle/Resources/contao/languages/en/tl_user_group.php b/src/Bundle/Resources/contao/languages/en/tl_user_group.php new file mode 100644 index 0000000..01f77b3 --- /dev/null +++ b/src/Bundle/Resources/contao/languages/en/tl_user_group.php @@ -0,0 +1,31 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_legend'] = 'Leaflet Maps Permissions'; + +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layers'][0] = 'Allowed map layers'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layers'][1] = 'Please choose the allowed map layers.'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions'][0] = 'Layer permissions'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions'][1] = 'Please choose which permissions are allowed for the map layer.'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_tables'][0] = 'Allows map tables'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_tables'][1] = 'Please choose which tables are allowed.'; + +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['create'][0] = 'Create layers'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['create'][1] = 'Grant permission to create new layers.'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['edit'][0] = 'Edit layers'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['edit'][1] = 'Grant permission to edit layers.'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['delete'][0] = 'Delete layers'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['delete'][1] = 'Grant permission to delete layers.'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['data'][0] = 'Edit data'; +$GLOBALS['TL_LANG']['tl_user_group']['leaflet_layer_permissions_options']['data'][1] = 'Grant permission to edit data of a layer.'; diff --git a/src/Listener/Dca/AbstractUserDcaListener.php b/src/Listener/Dca/AbstractUserDcaListener.php new file mode 100644 index 0000000..fe8d4c0 --- /dev/null +++ b/src/Listener/Dca/AbstractUserDcaListener.php @@ -0,0 +1,109 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Listener\Dca; + +use Contao\Image; +use Contao\StringUtil; +use Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\LayerLabelRenderer; +use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; +use Netzmacht\Contao\Toolkit\Dca\Manager; +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Class AbstractUserDcaListener + */ +abstract class AbstractUserDcaListener extends AbstractListener +{ + /** + * Layer label renderer. + * + * @var LayerLabelRenderer + */ + private $labelRenderer; + + /** + * Translator. + * + * @var Translator + */ + private $translator; + + /** + * Layers definitions. + * + * @var array + */ + private $layers; + + /** + * Constructor. + * + * @param Manager $dcaManager Dca manager. + * @param LayerLabelRenderer $labelRenderer Layer label renderer. + * @param Translator $translator Translator. + * @param array $layers Layers definition. + */ + public function __construct( + Manager $dcaManager, + LayerLabelRenderer $labelRenderer, + Translator $translator, + array $layers + ) { + parent::__construct($dcaManager); + + $this->labelRenderer = $labelRenderer; + $this->translator = $translator; + $this->layers = $layers; + } + + /** + * Generate the layers row label. + * + * @param array $row Layer data row. + * + * @return string + */ + public function generateLayersRowLabel(array $row): string + { + if (!empty($this->layers[$row['type']]['icon'])) { + $src = $this->layers[$row['type']]['icon']; + } else { + $src = 'iconPLAIN.gif'; + } + + $activeIcon = $src; + $disabledIcon = preg_replace('/(\.[^\.]+)$/', '_1$1', $src); + + if (!$row['active']) { + $src = $disabledIcon; + } + + $alt = $this->getFormatter(LayerModel::getTable())->formatValue('type', $row['type']); + $attributes = sprintf( + 'class="list-icon" title="%s" data-icon="%s" data-icon-disabled="%s"', + StringUtil::specialchars(strip_tags($alt)), + $activeIcon, + $disabledIcon + ); + + $label = $this->getFormatter(LayerModel::getTable())->formatValue('title', $row['title']); + $label .= sprintf(' [ID %s]', $row['id']); + $icon = Image::getHtml($src, $alt, $attributes); + $label = $this->labelRenderer->render($row, $label, $this->translator); + + return $icon . ' ' . $label; + } +} diff --git a/src/Listener/Dca/UserDcaListener.php b/src/Listener/Dca/UserDcaListener.php new file mode 100644 index 0000000..f993089 --- /dev/null +++ b/src/Listener/Dca/UserDcaListener.php @@ -0,0 +1,28 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Listener\Dca; + +/** + * Class UserDcaListener + */ +final class UserDcaListener extends AbstractUserDcaListener +{ + /** + * Data container name. + * + * @var string + */ + protected static $name = 'tl_user'; +} diff --git a/src/Listener/Dca/UserGroupDcaListener.php b/src/Listener/Dca/UserGroupDcaListener.php new file mode 100644 index 0000000..f073016 --- /dev/null +++ b/src/Listener/Dca/UserGroupDcaListener.php @@ -0,0 +1,28 @@ + + * @copyright 2014-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Listener\Dca; + +/** + * Class UserGroupDcaListener + */ +final class UserGroupDcaListener extends AbstractUserDcaListener +{ + /** + * Data container name. + * + * @var string + */ + protected static $name = 'tl_user_group'; +}