Enable.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\App;
  8. use OC\Installer;
  9. use OCP\App\AppPathNotFoundException;
  10. use OCP\App\IAppManager;
  11. use OCP\IGroup;
  12. use OCP\IGroupManager;
  13. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  14. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  15. use Symfony\Component\Console\Command\Command;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. class Enable extends Command implements CompletionAwareInterface {
  21. protected int $exitCode = 0;
  22. public function __construct(
  23. protected IAppManager $appManager,
  24. protected IGroupManager $groupManager,
  25. ) {
  26. parent::__construct();
  27. }
  28. protected function configure(): void {
  29. $this
  30. ->setName('app:enable')
  31. ->setDescription('enable an app')
  32. ->addArgument(
  33. 'app-id',
  34. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  35. 'enable the specified app'
  36. )
  37. ->addOption(
  38. 'groups',
  39. 'g',
  40. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  41. 'enable the app only for a list of groups'
  42. )
  43. ->addOption(
  44. 'force',
  45. 'f',
  46. InputOption::VALUE_NONE,
  47. 'enable the app regardless of the Nextcloud version requirement'
  48. );
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $appIds = $input->getArgument('app-id');
  52. $groups = $this->resolveGroupIds($input->getOption('groups'));
  53. $forceEnable = (bool)$input->getOption('force');
  54. foreach ($appIds as $appId) {
  55. $this->enableApp($appId, $groups, $forceEnable, $output);
  56. }
  57. return $this->exitCode;
  58. }
  59. /**
  60. * @param string $appId
  61. * @param array $groupIds
  62. * @param bool $forceEnable
  63. * @param OutputInterface $output
  64. */
  65. private function enableApp(string $appId, array $groupIds, bool $forceEnable, OutputInterface $output): void {
  66. $groupNames = array_map(function (IGroup $group) {
  67. return $group->getDisplayName();
  68. }, $groupIds);
  69. if ($this->appManager->isInstalled($appId) && $groupIds === []) {
  70. $output->writeln($appId . ' already enabled');
  71. return;
  72. }
  73. try {
  74. /** @var Installer $installer */
  75. $installer = \OC::$server->query(Installer::class);
  76. if ($installer->isDownloaded($appId) === false) {
  77. $installer->downloadApp($appId);
  78. }
  79. $installer->installApp($appId, $forceEnable);
  80. $appVersion = $this->appManager->getAppVersion($appId);
  81. if ($groupIds === []) {
  82. $this->appManager->enableApp($appId, $forceEnable);
  83. $output->writeln($appId . ' ' . $appVersion . ' enabled');
  84. } else {
  85. $this->appManager->enableAppForGroups($appId, $groupIds, $forceEnable);
  86. $output->writeln($appId . ' ' . $appVersion . ' enabled for groups: ' . implode(', ', $groupNames));
  87. }
  88. } catch (AppPathNotFoundException $e) {
  89. $output->writeln($appId . ' not found');
  90. $this->exitCode = 1;
  91. } catch (\Exception $e) {
  92. $output->writeln($e->getMessage());
  93. $this->exitCode = 1;
  94. }
  95. }
  96. /**
  97. * @param array $groupIds
  98. * @return array
  99. */
  100. private function resolveGroupIds(array $groupIds): array {
  101. $groups = [];
  102. foreach ($groupIds as $groupId) {
  103. $group = $this->groupManager->get($groupId);
  104. if ($group instanceof IGroup) {
  105. $groups[] = $group;
  106. }
  107. }
  108. return $groups;
  109. }
  110. /**
  111. * @param string $optionName
  112. * @param CompletionContext $context
  113. * @return string[]
  114. */
  115. public function completeOptionValues($optionName, CompletionContext $context): array {
  116. if ($optionName === 'groups') {
  117. return array_map(function (IGroup $group) {
  118. return $group->getGID();
  119. }, $this->groupManager->search($context->getCurrentWord()));
  120. }
  121. return [];
  122. }
  123. /**
  124. * @param string $argumentName
  125. * @param CompletionContext $context
  126. * @return string[]
  127. */
  128. public function completeArgumentValues($argumentName, CompletionContext $context): array {
  129. if ($argumentName === 'app-id') {
  130. $allApps = $this->appManager->getAllAppsInAppsFolders();
  131. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  132. }
  133. return [];
  134. }
  135. }