Просмотр исходного кода

remove the resetAllPerms from controller and improve logging

Olivier Massot 4 лет назад
Родитель
Сommit
4c887a1fa2

+ 22 - 1
ot_admin/Classes/Command/ResetBeUserPermsCommand.php

@@ -10,6 +10,7 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Style\SymfonyStyle;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Object\ObjectManager;
 
@@ -79,7 +80,27 @@ class ResetBeUserPermsCommand extends Command
         $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
 
         if ($all) {
-            $siteController->resetAllBeUserPermsAction($create);
+            $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
+            $queryBuilder = $connectionPool->getQueryBuilderForTable('pages');
+            $sites = $queryBuilder
+                ->select('tx_opentalent_structure_id')
+                ->from('pages')
+                ->where('is_siteroot=1')
+                ->andWhere($queryBuilder->expr()->gt('tx_opentalent_structure_id', 0))
+                ->execute()
+                ->fetchAll();
+
+            $io->progressStart(count($sites));
+
+            foreach ($sites as $site) {
+                try {
+                    $siteController->resetBeUserPermsAction($site['tx_opentalent_structure_id'], $create);
+                } catch (\Throwable $e) {
+                    $io->error(sprintf($e));
+                }
+                $io->progressAdvance(1);
+            }
+
             $io->success(sprintf("Be users permissions were reset for every website"));
         } else {
             $rootUid = $siteController->resetBeUserPermsAction($org_id, $create);

+ 40 - 44
ot_admin/Classes/Controller/SiteController.php

@@ -720,7 +720,15 @@ class SiteController extends ActionController
                 "'/user_upload/" . $organizationId . "/Forms'",
                 $hard);
 
-            $this->delete('be_users', 'db_mountpoints', $rootUid, $hard);
+            $adminBeUserUid = $this->findAdminBeUserUid($rootUid);
+            if ($adminBeUserUid !== null) {
+                $this->delete('be_users', 'uid', $adminBeUserUid, $hard);
+            }
+
+            $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid);
+            if ($editorsGroupUid !== null) {
+                $this->delete('be_groups', 'uid', $editorsGroupUid, $hard);
+            }
 
             // Look up for the config.yaml file of the website
             $configMainDir = $_ENV['TYPO3_PATH_APP'] . '/config/sites';
@@ -948,6 +956,26 @@ class SiteController extends ActionController
                 ->where($queryBuilder->expr()->eq('db_mountpoints', $rootUid))
                 ->execute();
 
+            $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid, false);
+            if ($editorsGroupUid !== null) {
+                $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
+                $queryBuilder
+                    ->update('be_groups')
+                    ->set('deleted', 0)
+                    ->where($queryBuilder->expr()->eq('uid', $editorsGroupUid))
+                    ->execute();
+            }
+
+            $adminBeUserUid = $this->findAdminBeUserUid($rootUid, false);
+            if ($adminBeUserUid !== null) {
+                $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
+                $queryBuilder
+                    ->update('be_users')
+                    ->set('deleted', 0)
+                    ->where($queryBuilder->expr()->eq('uid', $adminBeUserUid))
+                    ->execute();
+            }
+
             // Look up for the config.yaml file of the website
             $configMainDir = $_ENV['TYPO3_PATH_APP'] . '/config/sites';
             $configYamlFile = "";
@@ -1574,43 +1602,6 @@ class SiteController extends ActionController
         }
     }
 
-    /**
-     * CLI action for resetting the rights of admin and editors of every website
-     * on all of the existing pages, including deleted ones
-     *
-     * @param bool $createIfMissing
-     * @throws NoSuchRecordException
-     * @throws NoSuchWebsiteException
-     * @throws \Throwable
-     */
-    public function resetAllBeUserPermsAction(bool $createIfMissing = false) {
-        $this->connectionPool->getConnectionByName('Default')->beginTransaction();
-        try {
-            $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
-            $sites = $queryBuilder
-                ->select('tx_opentalent_structure_id')
-                ->from('pages')
-                ->where('is_siteroot=1')
-                ->andWhere($queryBuilder->expr()->gt('tx_opentalent_structure_id', 0))
-                ->execute()
-                ->fetchAll();
-
-            foreach ($sites as $site) {
-                $this->setBeUserPerms($site['tx_opentalent_structure_id'], $createIfMissing);
-            }
-
-            $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
-            if (!$commitSuccess) {
-                throw new \RuntimeException('Something went wrong while committing the result');
-            }
-        }
-        catch (\Throwable $e) {
-            // rollback
-            $this->connectionPool->getConnectionByName('Default')->rollback();
-            throw $e;
-        }
-    }
-
     /**
      * Retrieve the Organization object from the repository and then,
      * from the Opentalent API
@@ -2242,22 +2233,25 @@ class SiteController extends ActionController
      * for this website
      *
      * @param int $rootUid
+     * @param bool $withRestrictions
      * @return int
      * @throws NoSuchRecordException
      */
-    protected function findEditorsBeGroupUid(int $rootUid): int {
+    protected function findEditorsBeGroupUid(int $rootUid, bool $withRestrictions=true): int {
         $editorsGroups = [
             $this->getBaseBeGroupUid(self::BEGROUP_EDITOR_STANDARD),
             $this->getBaseBeGroupUid(self::BEGROUP_EDITOR_PREMIUM)
         ];
 
         $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
-        $queryBuilder->getRestrictions()->removeAll();
+        if (!$withRestrictions) {
+            $queryBuilder->getRestrictions()->removeAll();
+        }
         $beGroups = $queryBuilder
             ->select('uid', 'subgroup')
             ->from('be_groups')
             ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
-            ->andWhere($queryBuilder->expr()->eq('deleted', 0))
+//            ->andWhere($queryBuilder->expr()->eq('deleted', 0))
             ->execute()
             ->fetchAll();
 
@@ -2285,7 +2279,7 @@ class SiteController extends ActionController
      * @return int
      * @throws NoSuchRecordException
      */
-    protected function findAdminBeUserUid(int $rootUid): int {
+    protected function findAdminBeUserUid(int $rootUid, bool $withRestrictions=true): int {
         $adminGroups = [
             $this->getBaseBeGroupUid(self::BEGROUP_ADMIN_STANDARD),
             $this->getBaseBeGroupUid(self::BEGROUP_ADMIN_PREMIUM)
@@ -2294,12 +2288,14 @@ class SiteController extends ActionController
         $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid);
 
         $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
-        $queryBuilder->getRestrictions()->removeAll();
+        if (!$withRestrictions) {
+            $queryBuilder->getRestrictions()->removeAll();
+        }
         $beUsers = $queryBuilder
             ->select('uid', 'usergroup')
             ->from('be_users')
             ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
-            ->andWhere($queryBuilder->expr()->eq('deleted', 0))
+//            ->andWhere($queryBuilder->expr()->eq('deleted', 0))
             ->execute()
             ->fetchAll();