2013-11-25 12:20:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 10:44:34 +00: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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-11-25 12:20:16 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-11-25 12:20:16 +00:00
|
|
|
namespace OC\Core\Command\App;
|
|
|
|
|
2019-01-27 20:53:09 +00:00
|
|
|
use OC\Installer;
|
2019-01-27 11:28:57 +00:00
|
|
|
use OCP\App\AppPathNotFoundException;
|
2016-01-14 15:10:17 +00:00
|
|
|
use OCP\App\IAppManager;
|
2016-09-20 22:48:05 +00:00
|
|
|
use OCP\IGroup;
|
2019-01-27 11:28:57 +00:00
|
|
|
use OCP\IGroupManager;
|
2016-09-20 22:48:05 +00:00
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
|
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
|
2013-11-25 12:20:16 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2016-01-14 15:10:17 +00:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2013-11-25 12:20:16 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2016-09-20 22:48:05 +00:00
|
|
|
class Enable extends Command implements CompletionAwareInterface {
|
2016-01-14 15:10:17 +00:00
|
|
|
|
|
|
|
/** @var IAppManager */
|
2019-01-27 11:28:57 +00:00
|
|
|
protected $appManager;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
protected $groupManager;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $exitCode = 0;
|
2016-01-14 15:10:17 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-27 11:28:57 +00:00
|
|
|
* @param IAppManager $appManager
|
|
|
|
* @param IGroupManager $groupManager
|
2016-01-14 15:10:17 +00:00
|
|
|
*/
|
2019-01-27 11:28:57 +00:00
|
|
|
public function __construct(IAppManager $appManager, IGroupManager $groupManager) {
|
2016-01-14 15:10:17 +00:00
|
|
|
parent::__construct();
|
2019-01-27 11:28:57 +00:00
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->groupManager = $groupManager;
|
2016-01-14 15:10:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 11:28:57 +00:00
|
|
|
protected function configure(): void {
|
2013-11-25 12:20:16 +00:00
|
|
|
$this
|
|
|
|
->setName('app:enable')
|
|
|
|
->setDescription('enable an app')
|
|
|
|
->addArgument(
|
2013-11-25 16:28:01 +00:00
|
|
|
'app-id',
|
2019-01-27 11:28:57 +00:00
|
|
|
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
|
2013-11-25 12:20:16 +00:00
|
|
|
'enable the specified app'
|
2016-01-14 15:10:17 +00:00
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'groups',
|
|
|
|
'g',
|
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
|
|
|
|
'enable the app only for a list of groups'
|
2019-01-27 11:28:57 +00:00
|
|
|
);
|
2013-11-25 12:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) {
|
2019-01-27 11:28:57 +00:00
|
|
|
$appIds = $input->getArgument('app-id');
|
|
|
|
$groups = $this->resolveGroupIds($input->getOption('groups'));
|
|
|
|
|
|
|
|
foreach ($appIds as $appId) {
|
|
|
|
$this->enableApp($appId, $groups, $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->exitCode;
|
|
|
|
}
|
2016-01-14 15:10:17 +00:00
|
|
|
|
2019-01-27 11:28:57 +00:00
|
|
|
/**
|
|
|
|
* @param string $appId
|
|
|
|
* @param array $groupIds
|
|
|
|
* @param OutputInterface $output
|
|
|
|
*/
|
|
|
|
private function enableApp(string $appId, array $groupIds, OutputInterface $output): void {
|
|
|
|
$groupNames = array_map(function (IGroup $group) {
|
|
|
|
return $group->getDisplayName();
|
|
|
|
}, $groupIds);
|
|
|
|
|
2019-01-27 20:53:09 +00:00
|
|
|
|
2019-01-27 11:28:57 +00:00
|
|
|
try {
|
2019-01-27 20:53:09 +00:00
|
|
|
/** @var Installer $installer */
|
|
|
|
$installer = \OC::$server->query(Installer::class);
|
|
|
|
|
|
|
|
if (false === $installer->isDownloaded($appId)) {
|
|
|
|
$installer->downloadApp($appId);
|
|
|
|
}
|
|
|
|
|
|
|
|
$installer->installApp($appId);
|
|
|
|
|
2019-01-27 11:28:57 +00:00
|
|
|
if ($groupIds === []) {
|
|
|
|
$this->appManager->enableApp($appId);
|
|
|
|
$output->writeln($appId . ' enabled');
|
|
|
|
} else {
|
|
|
|
$this->appManager->enableAppForGroups($appId, $groupIds);
|
|
|
|
$output->writeln($appId . ' enabled for groups: ' . implode(', ', $groupNames));
|
|
|
|
}
|
|
|
|
} catch (AppPathNotFoundException $e) {
|
2013-11-25 12:20:16 +00:00
|
|
|
$output->writeln($appId . ' not found');
|
2019-01-27 11:28:57 +00:00
|
|
|
$this->exitCode = 1;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$output->writeln($e->getMessage());
|
|
|
|
$this->exitCode = 1;
|
2016-01-14 15:10:17 +00:00
|
|
|
}
|
2019-01-27 11:28:57 +00:00
|
|
|
}
|
2016-01-14 15:10:17 +00:00
|
|
|
|
2019-01-27 11:28:57 +00: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 12:20:16 +00:00
|
|
|
}
|
2019-01-27 11:28:57 +00:00
|
|
|
return $groups;
|
2013-11-25 12:20:16 +00:00
|
|
|
}
|
2016-09-20 22:48:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $optionName
|
|
|
|
* @param CompletionContext $context
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function completeOptionValues($optionName, CompletionContext $context) {
|
|
|
|
if ($optionName === 'groups') {
|
2019-01-27 11:28:57 +00:00
|
|
|
return array_map(function (IGroup $group) {
|
2016-09-20 22:48:05 +00:00
|
|
|
return $group->getGID();
|
2019-02-20 17:05:21 +00:00
|
|
|
}, $this->groupManager->search($context->getCurrentWord()));
|
2016-09-20 22:48:05 +00: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 12:20:16 +00:00
|
|
|
}
|