2013-11-25 16:20:16 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-11-25 16:20:16 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-11-25 16:20:16 +04:00
|
|
|
namespace OC\Core\Command\App;
|
|
|
|
|
2019-01-27 23:53:09 +03:00
|
|
|
use OC\Installer;
|
2019-01-27 14:28:57 +03:00
|
|
|
use OCP\App\AppPathNotFoundException;
|
2016-01-14 18:10:17 +03:00
|
|
|
use OCP\App\IAppManager;
|
2016-09-21 01:48:05 +03:00
|
|
|
use OCP\IGroup;
|
2019-01-27 14:28:57 +03:00
|
|
|
use OCP\IGroupManager;
|
2016-09-21 01:48:05 +03:00
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
|
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
|
2013-11-25 16:20:16 +04:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2016-01-14 18:10:17 +03:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2013-11-25 16:20:16 +04:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2016-09-21 01:48:05 +03:00
|
|
|
class Enable extends Command implements CompletionAwareInterface {
|
2016-01-14 18:10:17 +03:00
|
|
|
|
|
|
|
/** @var IAppManager */
|
2019-01-27 14:28:57 +03:00
|
|
|
protected $appManager;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
protected $groupManager;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $exitCode = 0;
|
2016-01-14 18:10:17 +03:00
|
|
|
|
|
|
|
/**
|
2019-01-27 14:28:57 +03:00
|
|
|
* @param IAppManager $appManager
|
|
|
|
* @param IGroupManager $groupManager
|
2016-01-14 18:10:17 +03:00
|
|
|
*/
|
2019-01-27 14:28:57 +03:00
|
|
|
public function __construct(IAppManager $appManager, IGroupManager $groupManager) {
|
2016-01-14 18:10:17 +03:00
|
|
|
parent::__construct();
|
2019-01-27 14:28:57 +03:00
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->groupManager = $groupManager;
|
2016-01-14 18:10:17 +03:00
|
|
|
}
|
|
|
|
|
2019-01-27 14:28:57 +03:00
|
|
|
protected function configure(): void {
|
2013-11-25 16:20:16 +04:00
|
|
|
$this
|
|
|
|
->setName('app:enable')
|
|
|
|
->setDescription('enable an app')
|
|
|
|
->addArgument(
|
2013-11-25 20:28:01 +04:00
|
|
|
'app-id',
|
2019-01-27 14:28:57 +03:00
|
|
|
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
|
2013-11-25 16:20:16 +04:00
|
|
|
'enable the specified app'
|
2016-01-14 18:10:17 +03:00
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'groups',
|
|
|
|
'g',
|
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
|
|
|
|
'enable the app only for a list of groups'
|
2019-09-05 13:55:24 +03:00
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'force',
|
|
|
|
'f',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'enable the app regardless of the Nextcloud version requirement'
|
2019-01-27 14:28:57 +03:00
|
|
|
);
|
2013-11-25 16:20:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) {
|
2019-01-27 14:28:57 +03:00
|
|
|
$appIds = $input->getArgument('app-id');
|
|
|
|
$groups = $this->resolveGroupIds($input->getOption('groups'));
|
2019-09-05 13:55:24 +03:00
|
|
|
$forceEnable = (bool) $input->getOption('force');
|
2019-01-27 14:28:57 +03:00
|
|
|
|
|
|
|
foreach ($appIds as $appId) {
|
2019-09-05 13:55:24 +03:00
|
|
|
$this->enableApp($appId, $groups, $forceEnable, $output);
|
2019-01-27 14:28:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->exitCode;
|
|
|
|
}
|
2016-01-14 18:10:17 +03:00
|
|
|
|
2019-01-27 14:28:57 +03:00
|
|
|
/**
|
|
|
|
* @param string $appId
|
|
|
|
* @param array $groupIds
|
2019-09-05 13:55:24 +03:00
|
|
|
* @param bool $forceEnable
|
2019-01-27 14:28:57 +03:00
|
|
|
* @param OutputInterface $output
|
|
|
|
*/
|
2019-09-05 13:55:24 +03:00
|
|
|
private function enableApp(string $appId, array $groupIds, bool $forceEnable, OutputInterface $output): void {
|
2019-01-27 14:28:57 +03:00
|
|
|
$groupNames = array_map(function (IGroup $group) {
|
|
|
|
return $group->getDisplayName();
|
|
|
|
}, $groupIds);
|
|
|
|
|
2019-01-27 23:53:09 +03:00
|
|
|
|
2019-01-27 14:28:57 +03:00
|
|
|
try {
|
2019-01-27 23:53:09 +03:00
|
|
|
/** @var Installer $installer */
|
|
|
|
$installer = \OC::$server->query(Installer::class);
|
|
|
|
|
|
|
|
if (false === $installer->isDownloaded($appId)) {
|
|
|
|
$installer->downloadApp($appId);
|
|
|
|
}
|
|
|
|
|
2019-09-05 13:55:24 +03:00
|
|
|
$installer->installApp($appId, $forceEnable);
|
2019-01-27 23:53:09 +03:00
|
|
|
|
2019-01-27 14:28:57 +03:00
|
|
|
if ($groupIds === []) {
|
2019-09-05 13:55:24 +03:00
|
|
|
$this->appManager->enableApp($appId, $forceEnable);
|
2019-01-27 14:28:57 +03:00
|
|
|
$output->writeln($appId . ' enabled');
|
|
|
|
} else {
|
2019-09-05 13:55:24 +03:00
|
|
|
$this->appManager->enableAppForGroups($appId, $groupIds, $forceEnable);
|
2019-01-27 14:28:57 +03:00
|
|
|
$output->writeln($appId . ' enabled for groups: ' . implode(', ', $groupNames));
|
|
|
|
}
|
|
|
|
} catch (AppPathNotFoundException $e) {
|
2013-11-25 16:20:16 +04:00
|
|
|
$output->writeln($appId . ' not found');
|
2019-01-27 14:28:57 +03:00
|
|
|
$this->exitCode = 1;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$output->writeln($e->getMessage());
|
|
|
|
$this->exitCode = 1;
|
2016-01-14 18:10:17 +03:00
|
|
|
}
|
2019-01-27 14:28:57 +03:00
|
|
|
}
|
2016-01-14 18:10:17 +03:00
|
|
|
|
2019-01-27 14:28:57 +03:00
|
|
|
/**
|
|
|
|
* @param array $groupIds
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function resolveGroupIds(array $groupIds): array {
|
|
|
|
$groups = [];
|
|
|
|
foreach ($groupIds as $groupId) {
|
|
|
|
$group = $this->groupManager->get($groupId);
|
|
|
|
if ($group instanceof IGroup) {
|
|
|
|
$groups[] = $group;
|
|
|
|
}
|
2013-11-25 16:20:16 +04:00
|
|
|
}
|
2019-01-27 14:28:57 +03:00
|
|
|
return $groups;
|
2013-11-25 16:20:16 +04:00
|
|
|
}
|
2016-09-21 01:48:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $optionName
|
|
|
|
* @param CompletionContext $context
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function completeOptionValues($optionName, CompletionContext $context) {
|
|
|
|
if ($optionName === 'groups') {
|
2019-01-27 14:28:57 +03:00
|
|
|
return array_map(function (IGroup $group) {
|
2016-09-21 01:48:05 +03:00
|
|
|
return $group->getGID();
|
2019-02-20 20:05:21 +03:00
|
|
|
}, $this->groupManager->search($context->getCurrentWord()));
|
2016-09-21 01:48:05 +03:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $argumentName
|
|
|
|
* @param CompletionContext $context
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function completeArgumentValues($argumentName, CompletionContext $context) {
|
|
|
|
if ($argumentName === 'app-id') {
|
|
|
|
$allApps = \OC_App::getAllApps();
|
|
|
|
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2013-11-25 16:20:16 +04:00
|
|
|
}
|