Enable.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * @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. /** @var IAppManager */
  41. protected $appManager;
  42. /** @var IGroupManager */
  43. protected $groupManager;
  44. /** @var int */
  45. protected $exitCode = 0;
  46. /**
  47. * @param IAppManager $appManager
  48. * @param IGroupManager $groupManager
  49. */
  50. public function __construct(IAppManager $appManager, IGroupManager $groupManager) {
  51. parent::__construct();
  52. $this->appManager = $appManager;
  53. $this->groupManager = $groupManager;
  54. }
  55. protected function configure(): void {
  56. $this
  57. ->setName('app:enable')
  58. ->setDescription('enable an app')
  59. ->addArgument(
  60. 'app-id',
  61. InputArgument::REQUIRED | InputArgument::IS_ARRAY,
  62. 'enable the specified app'
  63. )
  64. ->addOption(
  65. 'groups',
  66. 'g',
  67. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  68. 'enable the app only for a list of groups'
  69. )
  70. ->addOption(
  71. 'force',
  72. 'f',
  73. InputOption::VALUE_NONE,
  74. 'enable the app regardless of the Nextcloud version requirement'
  75. );
  76. }
  77. protected function execute(InputInterface $input, OutputInterface $output): int {
  78. $appIds = $input->getArgument('app-id');
  79. $groups = $this->resolveGroupIds($input->getOption('groups'));
  80. $forceEnable = (bool) $input->getOption('force');
  81. foreach ($appIds as $appId) {
  82. $this->enableApp($appId, $groups, $forceEnable, $output);
  83. }
  84. return $this->exitCode;
  85. }
  86. /**
  87. * @param string $appId
  88. * @param array $groupIds
  89. * @param bool $forceEnable
  90. * @param OutputInterface $output
  91. */
  92. private function enableApp(string $appId, array $groupIds, bool $forceEnable, OutputInterface $output): void {
  93. $groupNames = array_map(function (IGroup $group) {
  94. return $group->getDisplayName();
  95. }, $groupIds);
  96. if ($this->appManager->isInstalled($appId) && $groupIds === []) {
  97. $output->writeln($appId . ' already enabled');
  98. return;
  99. }
  100. try {
  101. /** @var Installer $installer */
  102. $installer = \OC::$server->query(Installer::class);
  103. if (false === $installer->isDownloaded($appId)) {
  104. $installer->downloadApp($appId);
  105. }
  106. $installer->installApp($appId, $forceEnable);
  107. $appVersion = \OC_App::getAppVersion($appId);
  108. if ($groupIds === []) {
  109. $this->appManager->enableApp($appId, $forceEnable);
  110. $output->writeln($appId . ' ' . $appVersion . ' enabled');
  111. } else {
  112. $this->appManager->enableAppForGroups($appId, $groupIds, $forceEnable);
  113. $output->writeln($appId . ' ' . $appVersion . ' enabled for groups: ' . implode(', ', $groupNames));
  114. }
  115. } catch (AppPathNotFoundException $e) {
  116. $output->writeln($appId . ' not found');
  117. $this->exitCode = 1;
  118. } catch (\Exception $e) {
  119. $output->writeln($e->getMessage());
  120. $this->exitCode = 1;
  121. }
  122. }
  123. /**
  124. * @param array $groupIds
  125. * @return array
  126. */
  127. private function resolveGroupIds(array $groupIds): array {
  128. $groups = [];
  129. foreach ($groupIds as $groupId) {
  130. $group = $this->groupManager->get($groupId);
  131. if ($group instanceof IGroup) {
  132. $groups[] = $group;
  133. }
  134. }
  135. return $groups;
  136. }
  137. /**
  138. * @param string $optionName
  139. * @param CompletionContext $context
  140. * @return string[]
  141. */
  142. public function completeOptionValues($optionName, CompletionContext $context) {
  143. if ($optionName === 'groups') {
  144. return array_map(function (IGroup $group) {
  145. return $group->getGID();
  146. }, $this->groupManager->search($context->getCurrentWord()));
  147. }
  148. return [];
  149. }
  150. /**
  151. * @param string $argumentName
  152. * @param CompletionContext $context
  153. * @return string[]
  154. */
  155. public function completeArgumentValues($argumentName, CompletionContext $context) {
  156. if ($argumentName === 'app-id') {
  157. $allApps = \OC_App::getAllApps();
  158. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  159. }
  160. return [];
  161. }
  162. }