Commit 6485dccb authored by Felix Herrmann's avatar Felix Herrmann Committed by Stefan Busemann
Browse files

Add DI and remove @inject usages

* Add Constants.php to store extension constants
* Add Services.yaml
* Update backend module mainModule from `voting` to `web`
* Update deprecated usage of `GeneralUtility::verifyFilenameAgainstDenyPattern` to `GeneralUtility::makeInstance(FileNameValidator::class)->isValid`
parent ab5ed30d
/Documentation-GENERATED-temp
.Build
composer.lock
<?php
$extensionKey = basename(__DIR__);
$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/Classes/',
__DIR__ . '/Tests/',
__DIR__ . '/Configuration/TCA/'
])
->exclude(__DIR__ . '/.Build/');
$date = date('Y');
$authors = 'Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz';
$header = <<<EOF
This file is part of the "$extensionKey" Extension for TYPO3 CMS.
For the full copyright and license information, please read the
LICENSE.txt file that was distributed with this source code.
(c) $date TYPO3 Association $authors
EOF;
return PhpCsFixer\Config::create()
->setUsingCache(false)
->setRules([
'@PSR1' => true,
'@PSR2' => true,
'@Symfony' => true,
'header_comment' => [
'header' => $header,
'location' => 'after_open',
'separate' => 'both',
'commentType' => 'PHPDoc',
],
'no_useless_else' => true,
'no_useless_return' => true,
'no_unused_imports' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'phpdoc_order' => true,
'phpdoc_summary' => false,
'blank_line_after_opening_tag' => false,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'yoda_style' => true,
'declare_strict_types' => true,
'psr4' => true,
'no_php4_constructor' => true,
'no_short_echo_tag' => true,
'semicolon_after_instruction' => true,
'align_multiline_comment' => true,
'general_phpdoc_annotation_remove' => ['annotations' => ["author", "package"]],
'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
])
->setFinder($finder);
<?php
/**
* This file is part of the "election" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021 TYPO3 Association Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz
*/
declare(strict_types=1);
namespace T3o\Election;
class Constants
{
public const EXTENSION_KEY = 'election';
public const EXTENSION_NAME = 'Election';
public const VENDOR = 'T3o';
}
<?php
namespace T3o\Election\Controller;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
/**
* This file is part of the "election" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
* (c) 2021 TYPO3 Association Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz
*/
declare(strict_types=1);
namespace T3o\Election\Controller;
use T3o\Election\Domain\Model\Configuration;
use T3o\Election\Domain\Repository\ConfigurationRepository;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
use T3o\Election\Domain\Model\Configuration;
/**
* Class AbstractBeController
*/
abstract class AbstractBeController extends ActionController
{
/**
* @var BackendUserAuthentication
*/
protected $backendUser = null;
protected $backendUser;
/**
* @var Configuration
*/
protected $configuration = null;
protected $configuration;
/**
* @var \T3o\Election\Domain\Repository\ConfigurationRepository
* @inject
* @var ConfigurationRepository
*/
protected $configurationRepository = null;
protected $configurationRepository;
/**
* Sets the current configuration
*/
public function initializeObject()
public function __construct(ConfigurationRepository $configurationRepository)
{
$this->configuration = $this->configurationRepository->findCurrent();
$this->configurationRepository = $configurationRepository;
$this->backendUser = self::getBackendUserAuthentication();
}
/**
* ModuleController constructor.
* Sets the current configuration
*/
public function __construct()
public function initializeObject(): void
{
parent::__construct();
$this->backendUser = self::getBackendUserAuthentication();
$this->configuration = $this->configurationRepository->findCurrent();
}
/**
* @param ViewInterface $view
*/
public function initializeView(ViewInterface $view)
public function initializeView(ViewInterface $view): void
{
$view->assign('backendUserData', $this->backendUser->user);
$view->assign('configuration', $this->configuration);
}
/**
* @return BackendUserAuthentication
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected static function getBackendUserAuthentication()
{
return $GLOBALS['BE_USER'];
}
/**
* @param string $messageBody
* @param string $messageTitle
* @param int $severity
* @param bool $storeInSession
* {@inheritDoc}
*/
public function addFlashMessage(
$messageBody,
$messageTitle = '',
$severity = AbstractMessage::OK,
$storeInSession = true
) {
): void {
if (null === $this->controllerContext) {
$this->controllerContext = $this->buildControllerContext();
}
......@@ -96,11 +74,14 @@ abstract class AbstractBeController extends ActionController
}
/**
* @param $argument
* @param array $properties
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function setDateTimeConverterOptions($argument, array $properties)
protected static function getBackendUserAuthentication(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
protected function setDateTimeConverterOptions(string $argument, array $properties): void
{
$propertyMappingConfiguration = $this->arguments->getArgument($argument)->getPropertyMappingConfiguration();
foreach ($properties as $property) {
......@@ -112,20 +93,17 @@ abstract class AbstractBeController extends ActionController
}
}
/**
* @param int $requiredUserGroup UID of the user group
* @return bool
*/
protected function backendUserHasUserGroup($requiredUserGroup=0)
protected function backendUserHasUserGroup(int $requiredUserGroup = 0): bool
{
if (Configuration::AUTH_NONE === $requiredUserGroup) {
return true;
}
foreach ($this->backendUser->userGroups as $userGroup) {
if ((int)$userGroup['uid'] === $requiredUserGroup) {
if ((int) $userGroup['uid'] === $requiredUserGroup) {
return true;
}
}
return false;
}
}
<?php
namespace T3o\Election\Controller;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
/**
* This file is part of the "election" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
* (c) 2021 TYPO3 Association Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz
*/
use Doctrine\Common\Util\Debug;
declare(strict_types=1);
namespace T3o\Election\Controller;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Class AbstractProtectedBeController
*/
abstract class AbstractProtectedBeController extends AbstractBeController
{
/**
*
*/
protected function initializeAction()
protected function initializeAction(): void
{
if (!$this->backendUserHasUserGroup($this->getRequiredUserGroup())) {
$this->addFlashMessage(
......@@ -38,41 +30,41 @@ abstract class AbstractProtectedBeController extends AbstractBeController
}
}
/**
* @return int
*/
abstract protected function getRequiredUserGroup();
abstract protected function getRequiredUserGroup(): int;
protected function errorAction()
protected function errorAction(): string
{
$this->clearCacheOnError();
$message="";
foreach ($this->arguments->getValidationResults()->getFlattenedErrors() as $propertyPath => $errors) {
$message = '';
foreach ($this->arguments->validate()->getFlattenedErrors() as $propertyPath => $errors) {
foreach ($errors as $error) {
$message .= PHP_EOL. 'Error for ' . $propertyPath . ': ' . $error->render();
$message .= PHP_EOL . 'Error for ' . $propertyPath . ': ' . $error->render();
}
}
$errorFlashMessage = $this->getErrorFlashMessage();
if ($errorFlashMessage !== FALSE) {
if (false !== $errorFlashMessage) {
$errorFlashMessageObject = new \TYPO3\CMS\Core\Messaging\FlashMessage(
$errorFlashMessage." ".$message,
$errorFlashMessage . ' ' . $message,
'',
\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
);
$this->controllerContext->getFlashMessageQueue()->enqueue($errorFlashMessageObject);
}
$referringRequest = $this->request->getReferringRequest();
if ($referringRequest !== NULL) {
if (null !== $referringRequest) {
$originalRequest = clone $this->request;
$this->request->setOriginalRequest($originalRequest);
$this->request->setOriginalRequestMappingResults($this->arguments->getValidationResults());
$this->forward($referringRequest->getControllerActionName(), $referringRequest->getControllerName(), $referringRequest->getControllerExtensionName(), $referringRequest->getArguments());
$this->request->setOriginalRequestMappingResults($this->arguments->validate());
$this->forward(
$referringRequest->getControllerActionName(),
$referringRequest->getControllerName(),
$referringRequest->getControllerExtensionName(),
$referringRequest->getArguments()
);
}
$message = 'An error occurred while trying to call ' . get_class($this) . '->' . $this->actionMethodName . '().' . PHP_EOL;
return $message;
return $message;
}
}
<?php
namespace T3o\Election\Controller;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
/**
* This file is part of the "election" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
* (c) 2021 TYPO3 Association Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz
*/
declare(strict_types=1);
namespace T3o\Election\Controller;
use T3o\Election\Domain\Model\Configuration;
use T3o\Election\Domain\Repository\ConfigurationRepository;
use TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
/**
* Class BeConfigurationController
*/
class BeConfigurationController extends AbstractProtectedBeController
{
const CONTROLLER_NAME = 'BeConfiguration';
const ACTION_EDIT = 'edit';
const ACTION_UPDATE = 'update';
public const CONTROLLER_NAME = 'BeConfiguration';
/**
* @var \TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository
* @inject
*/
protected $backendUserGroupRepository = null;
public const ACTION_EDIT = 'edit';
public const ACTION_UPDATE = 'update';
/**
*
* @var BackendUserGroupRepository
*/
public function editAction()
protected $backendUserGroupRepository;
public function __construct(
BackendUserGroupRepository $backendUserGroupRepository,
ConfigurationRepository $configurationRepository
) {
parent::__construct($configurationRepository);
$this->backendUserGroupRepository = $backendUserGroupRepository;
}
public function editAction(): void
{
if (null === $this->configuration) {
$this->configuration = new Configuration();
......@@ -43,31 +48,24 @@ class BeConfigurationController extends AbstractProtectedBeController
$this->view->assign('beUserGroups', $this->backendUserGroupRepository->findAll());
}
/**
* @param Configuration $configuration
*/
public function updateAction(Configuration $configuration)
public function updateAction(Configuration $configuration): void
{
$this->configurationRepository->update($configuration);
$this->objectManager->get(PersistenceManager::class)->persistAll();
$this->redirect(BeDashboardController::ACTION_INDEX, BeDashboardController::CONTROLLER_NAME);
}
/**
* @return string
*/
public static function getActionsForModuleConfiguration()
public static function getActionsForModuleConfiguration(): string
{
return implode(',', [self::ACTION_EDIT, self::ACTION_UPDATE]);
}
/**
* @return int
*/
protected function getRequiredUserGroup()
protected function getRequiredUserGroup(): int
{
if (null === $this->configuration) {
return Configuration::AUTH_NONE;
}
return $this->configuration->getAdministrationGroup();
}
}
<?php
namespace T3o\Election\Controller;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
/**
* This file is part of the "election" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
* (c) 2021 TYPO3 Association Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz
*/
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
declare(strict_types=1);
namespace T3o\Election\Controller;
use T3o\Election\Domain\Repository\ConfigurationRepository;
use T3o\Election\Domain\Repository\ElectionCircularRepository;
use T3o\Election\Domain\Repository\ElectionRepository;
use T3o\Election\Domain\Repository\ElectorImportRepository;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Dashboard controller
*/
class BeDashboardController extends AbstractBeController
{
const CONTROLLER_NAME = 'BeDashboard';
const ACTION_INDEX = 'index';
public const CONTROLLER_NAME = 'BeDashboard';
/**
* @var \T3o\Election\Domain\Repository\ElectionRepository
* @inject
*/
protected $electionRepository = null;
public const ACTION_INDEX = 'index';
/**
*
* @var ElectionRepository
*/
public function indexAction()
protected $electionRepository;
public function __construct(
ElectionRepository $electionRepository,
ConfigurationRepository $configurationRepository
) {
parent::__construct($configurationRepository);
$this->electionRepository = $electionRepository;
}
public function indexAction(): void
{
$this->view->assignMultiple(
[
......@@ -46,28 +50,32 @@ class BeDashboardController extends AbstractBeController
if ($this->configuration) {
if ($this->configuration->isGroupConfigComplete()) {
$electionManagerGroup = $this->configuration->getElectionManagerGroup();
}
else {
} else {
$this->addFlashMessage(
LocalizationUtility::translate('view.be.dashboard.index.no_proper_configuration_msg_body', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_title', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_proper_configuration_msg_body',
\T3o\Election\Constants::EXTENSION_NAME),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_title',
\T3o\Election\Constants::EXTENSION_NAME),
AbstractMessage::ERROR
);
if ($GLOBALS['BE_USER']->isAdmin()) {
$this->redirect('edit','BeConfiguration');
$this->redirect('edit', 'BeConfiguration');
} else {
$this->addFlashMessage(
LocalizationUtility::translate('view.be.dashboard.index.no_proper_configuration_msg_body_admin', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_title', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_proper_configuration_msg_body_admin',
\T3o\Election\Constants::EXTENSION_NAME),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_title',
\T3o\Election\Constants::EXTENSION_NAME),
AbstractMessage::ERROR
);
}
}
}
else {
} else {
$this->addFlashMessage(
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_body', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_title', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_body',
\T3o\Election\Constants::EXTENSION_NAME),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_title',
\T3o\Election\Constants::EXTENSION_NAME),
AbstractMessage::ERROR
);
}
......@@ -83,21 +91,18 @@ class BeDashboardController extends AbstractBeController
$this->objectManager->get(ElectionCircularRepository::class)->findAll()
);
}
}
else {
} else {
$this->addFlashMessage(
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_body', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_electionmanagergroup_msg_body', $this->extensionName),
LocalizationUtility::translate('view.be.dashboard.index.no_configuration_msg_body',
\T3o\Election\Constants::EXTENSION_NAME),
LocalizationUtility::translate('view.be.dashboard.index.no_electionmanagergroup_msg_body',
\T3o\Election\Constants::EXTENSION_NAME),
AbstractMessage::WARNING
);
}
}
/**
* @return string
*/
public static function getActionsForModuleConfiguration()
public static function getActionsForModuleConfiguration(): string
{
return self::ACTION_INDEX;
}
......
<?php
namespace T3o\Election\Controller;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
/**
* This file is part of the "election" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
* (c) 2021 TYPO3 Association Oliver Eglseder, Stefan Busemann, Christoph Pascher, Felix Herrmann, Wolf Utz
*/
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Utility\GeneralUtility;
declare(strict_types=1);