ResetBeUserPermsCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Opentalent\OtAdmin\Command;
  3. use Opentalent\OtAdmin\Controller\SiteController;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use Symfony\Component\Console\Style\SymfonyStyle;
  10. use TYPO3\CMS\Core\Database\ConnectionPool;
  11. use TYPO3\CMS\Core\Utility\GeneralUtility;
  12. use TYPO3\CMS\Extbase\Object\ObjectManager;
  13. /**
  14. * This CLI command resets the permissions granted to
  15. * this website be users
  16. *
  17. * @package Opentalent\OtAdmin\Command
  18. */
  19. class ResetBeUserPermsCommand extends Command
  20. {
  21. public function __construct(
  22. private readonly SiteController $siteController,
  23. private readonly ConnectionPool $connectionPool
  24. ) {
  25. parent::__construct();
  26. }
  27. /**
  28. * -- This method is expected by Typo3, do not rename ou remove --
  29. *
  30. * Allows to configure the command.
  31. * Allows to add a description, a help text, and / or define arguments.
  32. *
  33. */
  34. protected function configure(): void
  35. {
  36. $this
  37. ->setName("ot:site:reset-perms")
  38. ->setDescription("Reset the permissions granted to the be users")
  39. ->setHelp("This CLI command resets the permissions granted to
  40. either one website be users, or every websites be users")
  41. ->addArgument(
  42. 'organization-id',
  43. InputArgument::OPTIONAL,
  44. "The organization's id in the opentalent DB"
  45. )
  46. ->addOption(
  47. "all",
  48. null,
  49. InputOption::VALUE_NONE,
  50. "Reset all of the websites be_users permissions."
  51. )
  52. ->addOption(
  53. "create",
  54. 'c',
  55. InputOption::VALUE_NONE,
  56. "Create the be_user and/or be_group when they are missing"
  57. );
  58. }
  59. /**
  60. * -- This method is expected by Typo3, do not rename ou remove --
  61. *
  62. * @param InputInterface $input
  63. * @param OutputInterface $output
  64. * @return int
  65. * @throws \Exception
  66. */
  67. protected function execute(InputInterface $input, OutputInterface $output): int
  68. {
  69. $org_id = $input->getArgument('organization-id');
  70. $all = $input->getOption('all');
  71. $create = $input->getOption('create');
  72. if ($all && $org_id) {
  73. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  74. }
  75. if (!$all && !$org_id) {
  76. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  77. }
  78. $io = new SymfonyStyle($input, $output);
  79. if ($all) {
  80. $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
  81. $sites = $queryBuilder
  82. ->select('organization_id')
  83. ->from('ot_websites')
  84. ->where($queryBuilder->expr()->eq('deleted', 0))
  85. ->andWhere($queryBuilder->expr()->gt('organization_id', 0))
  86. ->execute()
  87. ->fetchAll();
  88. $io->progressStart(count($sites));
  89. foreach ($sites as $site) {
  90. $org_id = $site['organization_id'];
  91. try {
  92. $this->siteController->resetBeUserPermsAction($org_id, $create);
  93. } catch (\Throwable $e) {
  94. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  95. }
  96. $io->progressAdvance(1);
  97. }
  98. $io->progressFinish();
  99. $io->success(sprintf("Be users permissions were reset for every website"));
  100. } else {
  101. $rootUid = $this->siteController->resetBeUserPermsAction($org_id, $create);
  102. $io->success(sprintf("The website with root uid " . $rootUid . " had its be users permissions reset"));
  103. }
  104. return 0;
  105. }
  106. }