Skip to content

Commit

Permalink
Fix DI for Contao 5+
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Dec 5, 2024
1 parent 6705162 commit e979daa
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 92 deletions.
79 changes: 0 additions & 79 deletions .php-cs-fixer.dist.php

This file was deleted.

12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Contao\EasyCodingStandard\Fixer\CommentLengthFixer;
use Contao\EasyCodingStandard\Set\SetList;
use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->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')
;
20 changes: 20 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Contao\Rector\Set\SetList;
use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;

return RectorConfig::configure()
->withSets([SetList::CONTAO])
->withPaths([
__DIR__.'/contao',
__DIR__.'/src',
])
->withSkip([
FirstClassCallableRector::class,
])
->withParallel()
->withCache(sys_get_temp_dir().'/rector_cache')
;
5 changes: 5 additions & 0 deletions src/ContaoFileAccessBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}
}
7 changes: 4 additions & 3 deletions src/ContaoManagerPlugin.php → src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')
;
}
}
19 changes: 15 additions & 4 deletions src/Controller/AbstractFilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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__);
Expand Down

0 comments on commit e979daa

Please sign in to comment.