Die Klasse kannst du nicht überschreiben. Aber eine alternative Lösung wäre folgende:
PHP-Code:
// contao/dca/tl_news.php
use Contao\CoreBundle\DataContainer\PaletteManipulator;
use Doctrine\DBAL\Platforms\MySQLPlatform;
$GLOBALS['TL_DCA']['tl_news']['fields']['protected'] = [
'exclude' => true,
'filter' => true,
'inputType' => 'checkbox',
'eval' => ['submitOnChange' => true],
'sql' => ['type' => 'boolean', 'default' => false],
];
$GLOBALS['TL_DCA']['tl_news']['fields']['groups'] = [
'exclude' => true,
'inputType' => 'checkbox',
'foreignKey' => 'tl_member_group.name',
'eval' => ['mandatory' => true, 'multiple' => true],
'sql' => ['type' => 'blob', 'length' => MySQLPlatform::LENGTH_LIMIT_BLOB, 'notnull' => false],
'relation' => ['type' => 'hasMany', 'load' => 'lazy'],
];
$GLOBALS['TL_DCA']['tl_news']['palettes']['__selector__'][] = 'protected';
$GLOBALS['TL_DCA']['tl_news']['subpalettes']['protected'] = 'groups';
$pm = PaletteManipulator::create()
->addLegend('protected_legend', 'publish_legend', PaletteManipulator::POSITION_BEFORE)
->addField('protected', 'protected_legend', PaletteManipulator::POSITION_APPEND)
;
foreach ($GLOBALS['TL_DCA']['tl_news']['palettes'] as $name => $palette) {
if (!\is_string($palette)) {
continue;
}
$pm->applyToPalette($name, 'tl_news');
}
PHP-Code:
// src/EventListener/ProtectNewsEntryListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\CoreBundle\Exception\AccessDeniedException;
use Contao\CoreBundle\Exception\InsufficientAuthenticationException;
use Contao\FrontendTemplate;
use Contao\Module;
use Contao\StringUtil;
use Symfony\Component\Security\Core\Security;
#[AsHook('parseArticles', priority: 10000)]
class ProtectNewsEntryListener
{
public function __construct(private readonly Security $security)
{
}
public function __invoke(FrontendTemplate $template, array $news, Module $module): void
{
if ('newsreader' !== $module->type || !$news['protected']) {
return;
}
if ($this->security->isGranted(ContaoCorePermissions::MEMBER_IN_GROUPS, StringUtil::deserialize($news['groups'], true))) {
return;
}
if ($this->security->isGranted('ROLE_MEMBER')) {
throw new AccessDeniedException();
} else {
throw new InsufficientAuthenticationException();
}
}
}
(ungetestet)
Lesezeichen