Enable.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  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 OCP\App\IAppManager;
  27. use OCP\IGroup;
  28. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  29. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class Enable extends Command implements CompletionAwareInterface {
  36. /** @var IAppManager */
  37. protected $manager;
  38. /**
  39. * @param IAppManager $manager
  40. */
  41. public function __construct(IAppManager $manager) {
  42. parent::__construct();
  43. $this->manager = $manager;
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('app:enable')
  48. ->setDescription('enable an app')
  49. ->addArgument(
  50. 'app-id',
  51. InputArgument::REQUIRED,
  52. 'enable the specified app'
  53. )
  54. ->addOption(
  55. 'groups',
  56. 'g',
  57. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  58. 'enable the app only for a list of groups'
  59. )
  60. ;
  61. }
  62. protected function execute(InputInterface $input, OutputInterface $output) {
  63. $appId = $input->getArgument('app-id');
  64. if (!\OC_App::getAppPath($appId)) {
  65. $output->writeln($appId . ' not found');
  66. return 1;
  67. }
  68. $groups = $input->getOption('groups');
  69. $appClass = new \OC_App();
  70. if (empty($groups)) {
  71. $appClass->enable($appId);
  72. $output->writeln($appId . ' enabled');
  73. } else {
  74. $appClass->enable($appId, $groups);
  75. $output->writeln($appId . ' enabled for groups: ' . implode(', ', $groups));
  76. }
  77. return 0;
  78. }
  79. /**
  80. * @param string $optionName
  81. * @param CompletionContext $context
  82. * @return string[]
  83. */
  84. public function completeOptionValues($optionName, CompletionContext $context) {
  85. if ($optionName === 'groups') {
  86. return array_map(function(IGroup $group) {
  87. return $group->getGID();
  88. }, \OC::$server->getGroupManager()->search($context->getCurrentWord()));
  89. }
  90. return [];
  91. }
  92. /**
  93. * @param string $argumentName
  94. * @param CompletionContext $context
  95. * @return string[]
  96. */
  97. public function completeArgumentValues($argumentName, CompletionContext $context) {
  98. if ($argumentName === 'app-id') {
  99. $allApps = \OC_App::getAllApps();
  100. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  101. }
  102. return [];
  103. }
  104. }