Skip to content

Commit

Permalink
Only add the global operation if user can configure page routing
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Sep 6, 2024
1 parent 627d007 commit 669e92f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ services:
- '@database_connection'
- '@contao.intl.countries'
- '@translator'
- '@security.authorization_checker'
tags:
- { name: contao.callback, table: tl_page, target: config.onload, method: addGlobalOperation }
- { name: contao.callback, table: tl_page_geoip, target: list.label.label, method: generateLabel }
- { name: contao.callback, table: tl_page_geoip, target: fields.country.options, method: getCountryOptions }
- { name: contao.callback, table: tl_page_geoip, target: fields.pages.options, method: getPagesOptions }
Expand Down
10 changes: 0 additions & 10 deletions contao/dca/tl_page.php

This file was deleted.

1 change: 1 addition & 0 deletions contao/dca/tl_page_geoip.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'config' => [
'dataContainer' => DC_Table::class,
'enableVersioning' => true,
'backlink' => 'do=page',
'sql' => [
'keys' => [
'id' => 'primary',
Expand Down
23 changes: 23 additions & 0 deletions src/EventListener/PageRoutingDataContainerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Terminal42\Geoip2CountryBundle\EventListener;

use Contao\ArrayUtil;
use Contao\CoreBundle\Intl\Countries;
use Contao\CoreBundle\Security\ContaoCorePermissions;
use Contao\DataContainer;
use Contao\DC_Table;
use Doctrine\DBAL\Connection;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class PageRoutingDataContainerListener
Expand All @@ -20,9 +24,28 @@ public function __construct(
private readonly Connection $connection,
private readonly Countries $countries,
private readonly TranslatorInterface $translator,
private readonly AuthorizationCheckerInterface $authorizationChecker,
) {
}

public function addGlobalOperation(DataContainer $dc): void
{
if (!$this->authorizationChecker->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELDS_OF_TABLE, 'tl_page_geoip')) {
return;
}

ArrayUtil::arrayInsert($GLOBALS['TL_DCA']['tl_page']['list']['global_operations'], 0, [
'geoip_routing' => [
'label' => [
$this->translator->trans('tl_page.geoip_routing.0', [], 'contao_tl_page'),
$this->translator->trans('tl_page.geoip_routing.1', [], 'contao_tl_page'),
],
'icon' => '/bundles/terminal42geoip2country/crosshair.svg',
'href' => 'table=tl_page_geoip',
],
]);
}

/**
* @param array<string, string> $row
* @param array<int, string> $args
Expand Down

0 comments on commit 669e92f

Please sign in to comment.