浏览代码

create the ot:site:delete-user-created-pages command

Olivier Massot 1 年之前
父节点
当前提交
396105ebe2

+ 71 - 0
ot_admin/Classes/Command/DeleteUserCreatedPagesCommand.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Opentalent\OtAdmin\Command;
+
+use Opentalent\OtAdmin\Controller\SiteController;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+/**
+ * This CLI command provoke the definitive deletion of all the pages
+ * created by the users, leaving only the starting mandatory pages.
+ *
+ * /!\ Warning: this is a destructive operation
+ *
+ * @package Opentalent\OtAdmin\Command
+ */
+class DeleteUserCreatedPagesCommand extends Command
+{
+    public function __construct(
+        private readonly SiteController $siteController
+    ) {
+        parent::__construct();
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * Allows to configure the command.
+     * Allows to add a description, a help text, and / or define arguments.
+     *
+     */
+    protected function configure(): void
+    {
+        $this
+            ->setName("ot:site:delete-user-created-pages")
+            ->setDescription("Delete all the pages created by the users, leaving only the starting mandatory pages.")
+            ->setHelp("This CLI command provoke the definitive deletion of all the pages 
+                            created by the users, leaving only the starting mandatory pages. 
+                            /!\ Warning: this is a destructive operation.")
+            ->addArgument(
+                'organization-id',
+                InputArgument::OPTIONAL,
+                "The organization's id in the opentalent DB"
+            );
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @return int
+     * @throws \Exception
+     */
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $org_id = $input->getArgument('organization-id');
+
+        $io = new SymfonyStyle($input, $output);
+
+        $rootUid = $this->siteController->deleteUserCreatedPagesAction($org_id);
+
+        $io->success(sprintf("The website with root uid " . $rootUid . " had its user-created pages deleted."));
+
+        return 0;
+    }
+
+}

+ 35 - 17
ot_admin/Classes/Controller/SiteController.php

@@ -20,6 +20,7 @@ use Opentalent\OtCore\Utility\FileUtility;
 use PDO;
 use RuntimeException;
 use Symfony\Component\Yaml\Yaml;
+use Throwable;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
 use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
@@ -1745,7 +1746,7 @@ class SiteController extends ActionController
         // @see https://ressources.opentalent.fr/display/EX/Droits+des+BE+Users
         foreach ($pages as $page) {
 
-            if ($page['is_siteroot']) {
+            if ($page['ot_page_type'] === OtPageTypeEnum::ROOT) {
 
                 $adminPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE;
                 if ($isPremium)  {
@@ -1753,26 +1754,14 @@ class SiteController extends ActionController
                 }
                 $editorsPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT;
 
-            } else if (
-                $page['slug'] === '/footer' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->legal' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->contact' ||
-                $page['slug'] === '/plan-du-site'
-            ) {
+            } else if ($page['ot_page_type'] === OtPageTypeEnum::MANDATORY_NON_EDITABLE) {
                 $adminPerms = self::PERM_SHOW;
                 if ($isPremium)  {
                     $adminPerms += self::PERM_NEW;
                 }
                 $editorsPerms = self::PERM_SHOW;
 
-            } else if (
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->members' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->membersCa' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->structures' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->events' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->structuresEvents' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->structureDetails'
-            ) {
+            } else if ($page['ot_page_type'] === OtPageTypeEnum::MANDATORY_EDITABLE) {
                 $adminPerms = self::PERM_SHOW;
                 if ($isPremium)  {
                     $adminPerms += self::PERM_NEW + self::PERM_EDIT_PAGE;
@@ -1836,11 +1825,40 @@ class SiteController extends ActionController
         }
     }
 
-    public function deleteNonMandatoryPages(int $organizationId): void
+    /**
+     * Delete all the pages created by the users, leaving only the starting mandatory pages.
+     *
+     * @param int $organizationId
+     * @return int
+     * @throws NoSuchRecordException
+     * @throws NoSuchWebsiteException
+     * @throws \Doctrine\DBAL\Exception
+     * @throws Throwable
+     */
+    public function deleteUserCreatedPagesAction(int $organizationId): int
     {
-        $pages = $this->otPageRepository->getPageWithSubpages($organizationId);
+        $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
+        $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
+
+        $pages = $this->otPageRepository->getPageWithSubpages($rootUid);
 
+        $this->connectionPool->getConnectionByName('Default')->beginTransaction();
+
+        try {
+            foreach($pages as $page) {
+                if ($page['ot_page_type'] === OtPageTypeEnum::USER_CREATED->value) {
+                    $this->delete('tt_content', 'pid', $page['uid'], true);
+                    $this->delete('pages', 'uid', $page['uid'], true);
+                }
+            }
 
+            $this->connectionPool->getConnectionByName('Default')->commit();
+        } catch (\Throwable $e) {
+            $this->connectionPool->getConnectionByName('Default')->rollBack();
+            throw $e;
+        }
+
+        return $rootUid;
     }
 
     /**

+ 6 - 0
ot_admin/Configuration/Services.yaml

@@ -72,3 +72,9 @@ services:
       - name: console.command
         command: 'ot:site:scan'
         schedulable: true
+
+  Opentalent\OtAdmin\Command\DeleteUserCreatedPagesCommand:
+    tags:
+      - name: console.command
+        command: 'ot:site:delete-user-created-pages'
+        schedulable: false