Enable.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Sander Ruitenbeek <s.ruitenbeek@getgoing.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command\App;
  27. use OC\Installer;
  28. use OCP\App\AppPathNotFoundException;
  29. use OCP\App\IAppManager;
  30. use OCP\IGroup;
  31. use OCP\IGroupManager;
  32. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  33. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  34. use Symfony\Component\Console\Command\Command;
  35. use Symfony\Component\Console\Input\InputArgument;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Input\InputOption;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. class Enable extends Command implements CompletionAwareInterface {
  40. protected int $exitCode = 0;
  41. public function __construct(
  42. protected IAppManager $appManager,
  43. protected IGroupManager $groupManager,
  44. ) {
  45. parent::__construct();
  46. }
  47. protected function configure(): void {
  48. $this
  49. ->setName('app:enable')
  50. ->setDescription('enable an app')
  51. ->addArgument(
  52. 'app-id',
  53. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  54. 'enable the specified app'
  55. )
  56. ->addOption(
  57. 'groups',
  58. 'g',
  59. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  60. 'enable the app only for a list of groups'
  61. )
  62. ->addOption(
  63. 'force',
  64. 'f',
  65. InputOption::VALUE_NONE,
  66. 'enable the app regardless of the Nextcloud version requirement'
  67. );
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output): int {
  70. $appIds = $input->getArgument('app-id');
  71. $groups = $this->resolveGroupIds($input->getOption('groups'));
  72. $forceEnable = (bool) $input->getOption('force');
  73. foreach ($appIds as $appId) {
  74. $this->enableApp($appId, $groups, $forceEnable, $output);
  75. }
  76. return $this->exitCode;
  77. }
  78. /**
  79. * @param string $appId
  80. * @param array $groupIds
  81. * @param bool $forceEnable
  82. * @param OutputInterface $output
  83. */
  84. private function enableApp(string $appId, array $groupIds, bool $forceEnable, OutputInterface $output): void {
  85. $groupNames = array_map(function (IGroup $group) {
  86. return $group->getDisplayName();
  87. }, $groupIds);
  88. if ($this->appManager->isInstalled($appId) && $groupIds === []) {
  89. $output->writeln($appId . ' already enabled');
  90. return;
  91. }
  92. try {
  93. /** @var Installer $installer */
  94. $installer = \OC::$server->query(Installer::class);
  95. if ($installer->isDownloaded($appId) === false) {
  96. $installer->downloadApp($appId);
  97. }
  98. $installer->installApp($appId, $forceEnable);
  99. $appVersion = $this->appManager->getAppVersion($appId);
  100. if ($groupIds === []) {
  101. $this->appManager->enableApp($appId, $forceEnable);
  102. $output->writeln($appId . ' ' . $appVersion . ' enabled');
  103. } else {
  104. $this->appManager->enableAppForGroups($appId, $groupIds, $forceEnable);
  105. $output->writeln($appId . ' ' . $appVersion . ' enabled for groups: ' . implode(', ', $groupNames));
  106. }
  107. } catch (AppPathNotFoundException $e) {
  108. $output->writeln($appId . ' not found');
  109. $this->exitCode = 1;
  110. } catch (\Exception $e) {
  111. $output->writeln($e->getMessage());
  112. $this->exitCode = 1;
  113. }
  114. }
  115. /**
  116. * @param array $groupIds
  117. * @return array
  118. */
  119. private function resolveGroupIds(array $groupIds): array {
  120. $groups = [];
  121. foreach ($groupIds as $groupId) {
  122. $group = $this->groupManager->get($groupId);
  123. if ($group instanceof IGroup) {
  124. $groups[] = $group;
  125. }
  126. }
  127. return $groups;
  128. }
  129. /**
  130. * @param string $optionName
  131. * @param CompletionContext $context
  132. * @return string[]
  133. */
  134. public function completeOptionValues($optionName, CompletionContext $context): array {
  135. if ($optionName === 'groups') {
  136. return array_map(function (IGroup $group) {
  137. return $group->getGID();
  138. }, $this->groupManager->search($context->getCurrentWord()));
  139. }
  140. return [];
  141. }
  142. /**
  143. * @param string $argumentName
  144. * @param CompletionContext $context
  145. * @return string[]
  146. */
  147. public function completeArgumentValues($argumentName, CompletionContext $context): array {
  148. if ($argumentName === 'app-id') {
  149. $allApps = \OC_App::getAllApps();
  150. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  151. }
  152. return [];
  153. }
  154. }