Skip to content

Commit

Permalink
refactor: Removed deprecated AppController.php and Handler methods. R…
Browse files Browse the repository at this point in the history
…emoved FrontendController.php.
  • Loading branch information
ambroisemaupate committed Mar 14, 2024
1 parent ba52619 commit 4c72de2
Show file tree
Hide file tree
Showing 25 changed files with 58 additions and 721 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"nelmio/cors-bundle": "^2.2",
"phpdocumentor/reflection-docblock": "^5.2",
"phpoffice/phpspreadsheet": "^1.15",
"pimple/pimple": "^3.3.1",
"psr/cache": ">=1.0.1",
"psr/log": ">=1.1",
"ramsey/uuid": "^4.7",
Expand Down
4 changes: 0 additions & 4 deletions lib/Documents/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#
# Roadiz
#
/pimple.json

# Ignore Google webmaster tool verification
/google*.html
Expand Down
4 changes: 0 additions & 4 deletions lib/Models/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#
# Roadiz
#
/pimple.json

# Ignore Google webmaster tool verification
/google*.html
Expand Down
3 changes: 3 additions & 0 deletions lib/Models/src/Core/AbstractEntities/LeafTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ trait LeafTrait
{
use PositionedTrait;

/**
* @return Collection<int, LeafInterface>
*/
public function getChildren(): Collection
{
return $this->children;
Expand Down
1 change: 0 additions & 1 deletion lib/RoadizCompatBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"prefer-stable": true,
"require": {
"php": ">=8.1",
"pimple/pimple": "^3.3.1",
"roadiz/core-bundle": "2.3.x-dev",
"roadiz/openid": "2.3.x-dev",
"symfony/framework-bundle": "6.4.*"
Expand Down
1 change: 0 additions & 1 deletion lib/RoadizCompatBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ services:
# Explicit declaration
RZ\Roadiz\CompatBundle\Controller\AppController: ~
RZ\Roadiz\CompatBundle\Controller\Controller: ~
RZ\Roadiz\CompatBundle\Controller\FrontendController: ~

securityTokenStorage:
alias: security.token_storage
Expand Down
1 change: 0 additions & 1 deletion lib/RoadizCompatBundle/src/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public static function getAliases(): array
return [
\RZ\Roadiz\CompatBundle\Controller\AppController::class => \RZ\Roadiz\CMS\Controllers\AppController::class,
\RZ\Roadiz\CompatBundle\Controller\Controller::class => \RZ\Roadiz\CMS\Controllers\Controller::class,
\RZ\Roadiz\CompatBundle\Controller\FrontendController::class => \RZ\Roadiz\CMS\Controllers\FrontendController::class,
\RZ\Roadiz\CompatBundle\Theme\ThemeResolverInterface::class => \RZ\Roadiz\Utils\Theme\ThemeResolverInterface::class,
\RZ\Roadiz\CoreBundle\Bag\NodeTypes::class => \RZ\Roadiz\Core\Bags\NodeTypes::class,
\RZ\Roadiz\CoreBundle\Bag\Roles::class => \RZ\Roadiz\Core\Bags\Roles::class,
Expand Down
192 changes: 11 additions & 181 deletions lib/RoadizCompatBundle/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,20 @@

namespace RZ\Roadiz\CompatBundle\Controller;

use InvalidArgumentException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionClass;
use ReflectionException;
use RZ\Roadiz\CompatBundle\Theme\ThemeResolverInterface;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\Theme;
use RZ\Roadiz\CoreBundle\Entity\User;
use RZ\Roadiz\CoreBundle\EntityHandler\NodeHandler;
use RZ\Roadiz\CoreBundle\Exception\ThemeClassNotValidException;
use RZ\Roadiz\CoreBundle\Form\Error\FormErrorSerializer;
use RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootResolver;
use RZ\Roadiz\Documents\Packages;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\String\UnicodeString;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -93,10 +80,6 @@ abstract class AppController extends Controller
* Assignation for twig template engine.
*/
protected array $assignation = [];
/**
* @var Node|null
*/
private ?Node $homeNode = null;

/**
* @return string
Expand Down Expand Up @@ -146,34 +129,6 @@ public static function isBackendTheme(): bool
return static::$backendTheme;
}

/**
* @return RouteCollection
* @throws ReflectionException
*/
public static function getRoutes(): RouteCollection
{
$locator = static::getFileLocator();
$loader = new YamlFileLoader($locator);
return $loader->load('routes.yml');
}

/**
* Return a file locator with theme
* Resource folder.
*
* @return FileLocator
* @throws ReflectionException
*/
public static function getFileLocator(): FileLocator
{
$resourcesFolder = static::getResourcesFolder();
return new FileLocator([
$resourcesFolder,
$resourcesFolder . '/routing',
$resourcesFolder . '/config',
]);
}

/**
* Return theme Resource folder according to
* main theme class inheriting AppController.
Expand All @@ -183,6 +138,7 @@ public static function getFileLocator(): FileLocator
*
* @return string
* @throws ReflectionException
* @throws ThemeClassNotValidException
*/
public static function getResourcesFolder(): string
{
Expand Down Expand Up @@ -233,24 +189,6 @@ public static function getThemeMainClassName(): string
return static::getThemeDir() . 'App';
}

/**
* These routes are used to extend Roadiz back-office.
*
* @return RouteCollection|null
* @throws ReflectionException
*/
public static function getBackendRoutes(): ?RouteCollection
{
$locator = static::getFileLocator();

try {
$loader = new YamlFileLoader($locator);
return $loader->load('backend-routes.yml');
} catch (InvalidArgumentException $e) {
return null;
}
}

/**
* @return string
* @throws ReflectionException
Expand Down Expand Up @@ -317,8 +255,10 @@ public function getAssignation(): array
* - securityAuthorizationChecker
*
* @return $this
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function prepareBaseAssignation()
public function prepareBaseAssignation(): static
{
/** @var KernelInterface $kernel */
$kernel = $this->container->get('kernel');
Expand All @@ -344,7 +284,7 @@ public function prepareBaseAssignation()
* @throws RuntimeError
* @throws SyntaxError
*/
public function throw404($message = '')
public function throw404(string $message = ''): Response
{
$this->assignation['nodeName'] = 'error-404';
$this->assignation['nodeTypeName'] = 'error404';
Expand All @@ -363,6 +303,8 @@ public function throw404($message = '')
* Return the current Theme
*
* @return Theme|null
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getTheme(): ?Theme
{
Expand Down Expand Up @@ -455,72 +397,14 @@ public function publishErrorMessage(Request $request, string $msg, ?object $sour
$this->publishMessage($request, $msg, 'error', $source);
}

/**
* Validate a request against a given ROLE_*
* and check chroot
* and throws an AccessDeniedException exception.
*
* @param mixed $attributes
* @param mixed $nodeId
* @param bool|false $includeChroot
* @return void
*
* @throws AccessDeniedException
* @deprecated Use denyAccessUnlessGranted with NodeVoter attribute and a Node subject.
*/
public function validateNodeAccessForRole(mixed $attributes, mixed $nodeId = null, bool $includeChroot = false): void
{
/** @var Node|null $node */
$node = null;
/** @var User $user */
$user = $this->getUser();
/** @var NodeChrootResolver $chrootResolver */
$chrootResolver = $this->container->get(NodeChrootResolver::class);
$chroot = $chrootResolver->getChroot($user);

if ($this->isGranted($attributes) && $chroot === null) {
/*
* Already grant access if user is not chroot-ed.
*/
return;
}

if ($nodeId instanceof Node) {
$node = $nodeId;
} elseif (\is_scalar($nodeId)) {
/** @var Node|null $node */
$node = $this->em()->find(Node::class, (int) $nodeId);
}

if (null === $node) {
throw $this->createAccessDeniedException("You don't have access to this page");
}

$this->em()->refresh($node);

/** @var NodeHandler $nodeHandler */
$nodeHandler = $this->getHandlerFactory()->getHandler($node);
$parents = $nodeHandler->getParents();

if ($includeChroot) {
$parents[] = $node;
}

if (!$this->isGranted($attributes)) {
throw $this->createAccessDeniedException("You don't have access to this page");
}

if (null !== $user && $chroot !== null && !in_array($chroot, $parents, true)) {
throw $this->createAccessDeniedException("You don't have access to this page");
}
}

/**
* Generate a simple view to inform visitors that website is
* currently unavailable.
*
* @param Request $request
* @return Response
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function maintenanceAction(Request $request): Response
{
Expand Down Expand Up @@ -597,58 +481,4 @@ public function makeResponseCachable(

return $response;
}

/**
* Returns a fully qualified view path for Twig rendering.
*
* @param string $view
* @param string $namespace
* @return string
*/
protected function getNamespacedView(string $view, string $namespace = ''): string
{
if ($namespace !== "" && $namespace !== "/") {
$view = '@' . $namespace . '/' . $view;
} elseif (static::getThemeDir() !== "" && $namespace !== "/") {
// when no namespace is used
// use current theme directory
$view = '@' . static::getThemeDir() . '/' . $view;
}

return $view;
}

/**
* @param TranslationInterface|null $translation
* @return null|Node
*/
protected function getHome(?TranslationInterface $translation = null): ?Node
{
$this->getStopwatch()->start('getHome');
if (null === $this->homeNode) {
$nodeRepository = $this->em()->getRepository(Node::class);
if ($translation !== null) {
$this->homeNode = $nodeRepository->findHomeWithTranslation($translation);
} else {
$this->homeNode = $nodeRepository->findHomeWithDefaultTranslation();
}
}
$this->getStopwatch()->stop('getHome');

return $this->homeNode;
}

/**
* Return all Form errors as an array.
*
* @param FormInterface $form
* @return array
* @deprecated Use FormErrorSerializer::getErrorsAsArray instead
*/
protected function getErrorsAsArray(FormInterface $form): array
{
/** @var FormErrorSerializer $formErrorSerializer */
$formErrorSerializer = $this->container->get(FormErrorSerializer::class);
return $formErrorSerializer->getErrorsAsArray($form);
}
}
Loading

0 comments on commit 4c72de2

Please sign in to comment.