Enable.php 4.9 KB

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