ResetBeUserPermsCommand.php 4.0 KB

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