Enable.php 5.2 KB

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