Enable.php 3.3 KB

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