From e979daa537a43aedf1fde651c0d366ee5d0ce208 Mon Sep 17 00:00:00 2001 From: fritzmg Date: Thu, 5 Dec 2024 18:42:31 +0000 Subject: [PATCH] Fix DI for Contao 5+ --- .php-cs-fixer.dist.php | 79 ------------------- composer.json | 12 +-- .../config/routing.yml => config/routing.yaml | 0 {src/Resources/config => config}/services.yml | 0 .../contao => contao}/dca/tl_files.php | 0 .../contao => contao}/dca/tl_member.php | 0 .../languages/de/modules.php | 0 .../languages/de/tl_files.php | 0 .../languages/de/tl_member.php | 0 .../languages/en/modules.php | 0 .../languages/en/tl_files.php | 0 .../languages/en/tl_member.php | 0 ecs.php | 29 +++++++ rector.php | 20 +++++ src/ContaoFileAccessBundle.php | 5 ++ .../Plugin.php} | 7 +- src/Controller/AbstractFilesController.php | 19 ++++- 17 files changed, 79 insertions(+), 92 deletions(-) delete mode 100644 .php-cs-fixer.dist.php rename src/Resources/config/routing.yml => config/routing.yaml (100%) rename {src/Resources/config => config}/services.yml (100%) rename {src/Resources/contao => contao}/dca/tl_files.php (100%) rename {src/Resources/contao => contao}/dca/tl_member.php (100%) rename {src/Resources/contao => contao}/languages/de/modules.php (100%) rename {src/Resources/contao => contao}/languages/de/tl_files.php (100%) rename {src/Resources/contao => contao}/languages/de/tl_member.php (100%) rename {src/Resources/contao => contao}/languages/en/modules.php (100%) rename {src/Resources/contao => contao}/languages/en/tl_files.php (100%) rename {src/Resources/contao => contao}/languages/en/tl_member.php (100%) create mode 100644 ecs.php create mode 100644 rector.php rename src/{ContaoManagerPlugin.php => ContaoManager/Plugin.php} (81%) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index af426ca..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,79 +0,0 @@ -exclude('Resources/contao/templates') - ->in([ - __DIR__.'/src', - ]) -; - -return (new PhpCsFixer\Config()) - ->setRules([ - '@Symfony' => true, - '@Symfony:risky' => true, - '@PHP71Migration' => true, - '@PHP71Migration:risky' => true, - '@PHPUnit60Migration:risky' => true, - 'align_multiline_comment' => true, - 'array_indentation' => true, - 'array_syntax' => ['syntax' => 'short'], - 'combine_consecutive_issets' => true, - 'combine_consecutive_unsets' => true, - 'comment_to_phpdoc' => true, - 'compact_nullable_typehint' => true, - 'escape_implicit_backslashes' => true, - 'fully_qualified_strict_types' => true, - 'general_phpdoc_annotation_remove' => [ - 'annotations' => [ - 'author', - 'expectedException', - 'expectedExceptionMessage', - ], - ], - 'header_comment' => ['header' => $header], - 'heredoc_to_nowdoc' => true, - 'linebreak_after_opening_tag' => true, - 'list_syntax' => ['syntax' => 'short'], - 'multiline_comment_opening_closing' => true, - 'multiline_whitespace_before_semicolons' => [ - 'strategy' => 'new_line_for_chained_calls', - ], - 'native_function_invocation' => [ - 'include' => ['@compiler_optimized'], - ], - 'no_alternative_syntax' => true, - 'no_binary_string' => true, - 'no_null_property_initialization' => true, - 'no_superfluous_elseif' => true, - 'no_superfluous_phpdoc_tags' => true, - 'no_unreachable_default_argument_value' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_class_elements' => true, - 'ordered_imports' => true, - 'php_unit_strict' => true, - 'phpdoc_add_missing_param_annotation' => true, - 'phpdoc_order' => true, - 'phpdoc_trim_consecutive_blank_line_separation' => true, - 'phpdoc_types_order' => [ - 'null_adjustment' => 'always_last', - 'sort_algorithm' => 'none', - ], - 'return_assignment' => true, - 'strict_comparison' => true, - 'strict_param' => true, - 'string_line_ending' => true, - 'void_return' => true, - ]) - ->setFinder($finder) - ->setRiskyAllowed(true) - ->setUsingCache(false) -; diff --git a/composer.json b/composer.json index 4cb92fb..88a0668 100644 --- a/composer.json +++ b/composer.json @@ -26,14 +26,14 @@ } ], "require": { - "php": "^7.4 || ^8.0", + "php": ">=8.1", "contao/core-bundle": "^4.13 || ^5.0", "contao/image": "^1.2", - "symfony/config": "^5.2 || ^6.0", - "symfony/dependency-injection": "^5.2 || ^6.0", - "symfony/http-foundation": "^5.2 || ^6.0", - "symfony/http-kernel": "^5.2 || ^6.0", - "symfony/security-core": "^5.2 || ^6.0", + "symfony/config": "^5.4 || ^6.4 || ^7.1", + "symfony/dependency-injection": "^5.4 || ^6.4 || ^7.1", + "symfony/http-foundation": "^5.4 || ^6.4 || ^7.1", + "symfony/http-kernel": "^5.4 || ^6.4 || ^7.1", + "symfony/security-core": "^5.4 || ^6.4 || ^7.1", "webmozart/path-util": "^2.3", "symfony/service-contracts": "^2.5 || ^3.0" }, diff --git a/src/Resources/config/routing.yml b/config/routing.yaml similarity index 100% rename from src/Resources/config/routing.yml rename to config/routing.yaml diff --git a/src/Resources/config/services.yml b/config/services.yml similarity index 100% rename from src/Resources/config/services.yml rename to config/services.yml diff --git a/src/Resources/contao/dca/tl_files.php b/contao/dca/tl_files.php similarity index 100% rename from src/Resources/contao/dca/tl_files.php rename to contao/dca/tl_files.php diff --git a/src/Resources/contao/dca/tl_member.php b/contao/dca/tl_member.php similarity index 100% rename from src/Resources/contao/dca/tl_member.php rename to contao/dca/tl_member.php diff --git a/src/Resources/contao/languages/de/modules.php b/contao/languages/de/modules.php similarity index 100% rename from src/Resources/contao/languages/de/modules.php rename to contao/languages/de/modules.php diff --git a/src/Resources/contao/languages/de/tl_files.php b/contao/languages/de/tl_files.php similarity index 100% rename from src/Resources/contao/languages/de/tl_files.php rename to contao/languages/de/tl_files.php diff --git a/src/Resources/contao/languages/de/tl_member.php b/contao/languages/de/tl_member.php similarity index 100% rename from src/Resources/contao/languages/de/tl_member.php rename to contao/languages/de/tl_member.php diff --git a/src/Resources/contao/languages/en/modules.php b/contao/languages/en/modules.php similarity index 100% rename from src/Resources/contao/languages/en/modules.php rename to contao/languages/en/modules.php diff --git a/src/Resources/contao/languages/en/tl_files.php b/contao/languages/en/tl_files.php similarity index 100% rename from src/Resources/contao/languages/en/tl_files.php rename to contao/languages/en/tl_files.php diff --git a/src/Resources/contao/languages/en/tl_member.php b/contao/languages/en/tl_member.php similarity index 100% rename from src/Resources/contao/languages/en/tl_member.php rename to contao/languages/en/tl_member.php diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..6de7bc2 --- /dev/null +++ b/ecs.php @@ -0,0 +1,29 @@ +withSets([SetList::CONTAO]) + ->withPaths([ + __DIR__.'/contao', + __DIR__.'/src', + ]) + ->withSkip([ + MethodChainingIndentationFixer::class => [ + '*/DependencyInjection/Configuration.php', + ], + CommentLengthFixer::class => ['*'], + ]) + ->withConfiguredRule(HeaderCommentFixer::class, [ + 'header' => "This file is part of the Contao File Access extension.\n\n(c) INSPIRED MINDS\n\n@license LGPL-3.0-or-later", + ]) + ->withParallel() + ->withSpacing(lineEnding: "\n") + ->withCache(sys_get_temp_dir().'/ecs_default_cache') +; diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..c4cad96 --- /dev/null +++ b/rector.php @@ -0,0 +1,20 @@ +withSets([SetList::CONTAO]) + ->withPaths([ + __DIR__.'/contao', + __DIR__.'/src', + ]) + ->withSkip([ + FirstClassCallableRector::class, + ]) + ->withParallel() + ->withCache(sys_get_temp_dir().'/rector_cache') +; diff --git a/src/ContaoFileAccessBundle.php b/src/ContaoFileAccessBundle.php index f58d391..1641690 100644 --- a/src/ContaoFileAccessBundle.php +++ b/src/ContaoFileAccessBundle.php @@ -24,4 +24,9 @@ public function build(ContainerBuilder $container): void $container->addCompilerPass(new AdjustProtectedResizerServicePass()); $container->addCompilerPass(new AdjustResizerServicePass()); } + + public function getPath(): string + { + return \dirname(__DIR__); + } } diff --git a/src/ContaoManagerPlugin.php b/src/ContaoManager/Plugin.php similarity index 81% rename from src/ContaoManagerPlugin.php rename to src/ContaoManager/Plugin.php index 127c743..13844e5 100644 --- a/src/ContaoManagerPlugin.php +++ b/src/ContaoManager/Plugin.php @@ -10,13 +10,14 @@ * @license LGPL-3.0-or-later */ -namespace InspiredMinds\ContaoFileAccessBundle; +namespace InspiredMinds\ContaoFileAccessBundle\ContaoManager; use Contao\CoreBundle\ContaoCoreBundle; use Contao\ManagerPlugin\Bundle\BundlePluginInterface; use Contao\ManagerPlugin\Bundle\Config\BundleConfig; use Contao\ManagerPlugin\Bundle\Parser\ParserInterface; use Contao\ManagerPlugin\Routing\RoutingPluginInterface; +use InspiredMinds\ContaoFileAccessBundle\ContaoFileAccessBundle; use Symfony\Component\Config\Loader\LoaderResolverInterface; use Symfony\Component\HttpKernel\KernelInterface; @@ -42,8 +43,8 @@ public function getBundles(ParserInterface $parser) public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel) { return $resolver - ->resolve(__DIR__.'/Resources/config/routing.yml') - ->load(__DIR__.'/Resources/config/routing.yml') + ->resolve(__DIR__.'/../../config/routing.yaml') + ->load(__DIR__.'/../../config/routing.yaml') ; } } diff --git a/src/Controller/AbstractFilesController.php b/src/Controller/AbstractFilesController.php index a8837df..c2f022f 100644 --- a/src/Controller/AbstractFilesController.php +++ b/src/Controller/AbstractFilesController.php @@ -23,7 +23,9 @@ use Contao\PageModel; use Doctrine\DBAL\Connection; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Security\Core\Security; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; +use Symfony\Contracts\Service\Attribute\SubscribedService; use Symfony\Contracts\Service\ServiceSubscriberInterface; use Symfony\Contracts\Service\ServiceSubscriberTrait; @@ -43,7 +45,7 @@ protected function checkFilePermissions(FilesModel $filesModel): void $allowAccess = false; // Get the current user - $user = $this->security()->getUser(); + $user = $this->tokenStorage()->getToken()?->getUser(); // Check if the current user can access their home directory $canAccessHomeDir = $user instanceof FrontendUser && !empty($user->homeDir) && $user->accessHomeDir; @@ -84,7 +86,7 @@ protected function checkFilePermissions(FilesModel $filesModel): void // Deny access if (!$allowAccess) { // If a user is authenticated or the 401 exception does not exist, throw 403 exception - if ($this->security()->isGranted('ROLE_MEMBER')) { + if ($this->authChecker()->isGranted('ROLE_MEMBER')) { throw new AccessDeniedException(); } @@ -104,16 +106,25 @@ protected function setRootPage(Request $request): void } } + #[SubscribedService] protected function connection(): Connection { return $this->container->get(__METHOD__); } - protected function security(): Security + #[SubscribedService] + protected function tokenStorage(): TokenStorageInterface { return $this->container->get(__METHOD__); } + #[SubscribedService] + protected function authChecker(): AuthorizationCheckerInterface + { + return $this->container->get(__METHOD__); + } + + #[SubscribedService] protected function tokenChecker(): TokenChecker { return $this->container->get(__METHOD__);