1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Morris Jobke <[email protected]> |
8
|
|
|
* @author Robin Appelman <[email protected]> |
9
|
|
|
* @author Vincent Petry <[email protected]> |
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
|
|
|
|
27
|
|
|
namespace OC\Core\Command\App; |
28
|
|
|
|
29
|
|
|
use OC\Installer; |
30
|
|
|
use OCP\App\AppPathNotFoundException; |
31
|
|
|
use OCP\App\IAppManager; |
32
|
|
|
use OCP\IGroup; |
33
|
|
|
use OCP\IGroupManager; |
34
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface; |
35
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
36
|
|
|
use Symfony\Component\Console\Command\Command; |
37
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
38
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
39
|
|
|
use Symfony\Component\Console\Input\InputOption; |
40
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
41
|
|
|
|
42
|
|
|
class Enable extends Command implements CompletionAwareInterface { |
43
|
|
|
|
44
|
|
|
/** @var IAppManager */ |
45
|
|
|
protected $appManager; |
46
|
|
|
|
47
|
|
|
/** @var IGroupManager */ |
48
|
|
|
protected $groupManager; |
49
|
|
|
|
50
|
|
|
/** @var int */ |
51
|
|
|
protected $exitCode = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param IAppManager $appManager |
55
|
|
|
* @param IGroupManager $groupManager |
56
|
|
|
*/ |
57
|
|
|
public function __construct(IAppManager $appManager, IGroupManager $groupManager) { |
58
|
|
|
parent::__construct(); |
59
|
|
|
$this->appManager = $appManager; |
60
|
|
|
$this->groupManager = $groupManager; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function configure(): void { |
64
|
|
|
$this |
65
|
|
|
->setName('app:enable') |
66
|
|
|
->setDescription('enable an app') |
67
|
|
|
->addArgument( |
68
|
|
|
'app-id', |
69
|
|
|
InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
70
|
|
|
'enable the specified app' |
71
|
|
|
) |
72
|
|
|
->addOption( |
73
|
|
|
'groups', |
74
|
|
|
'g', |
75
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
76
|
|
|
'enable the app only for a list of groups' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
81
|
|
|
$appIds = $input->getArgument('app-id'); |
82
|
|
|
$groups = $this->resolveGroupIds($input->getOption('groups')); |
83
|
|
|
|
84
|
|
|
foreach ($appIds as $appId) { |
85
|
|
|
$this->enableApp($appId, $groups, $output); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->exitCode; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $appId |
93
|
|
|
* @param array $groupIds |
94
|
|
|
* @param OutputInterface $output |
95
|
|
|
*/ |
96
|
|
|
private function enableApp(string $appId, array $groupIds, OutputInterface $output): void { |
97
|
|
|
$groupNames = array_map(function (IGroup $group) { |
98
|
|
|
return $group->getDisplayName(); |
99
|
|
|
}, $groupIds); |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
/** @var Installer $installer */ |
104
|
|
|
$installer = \OC::$server->query(Installer::class); |
105
|
|
|
|
106
|
|
|
if (false === $installer->isDownloaded($appId)) { |
107
|
|
|
$installer->downloadApp($appId); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$installer->installApp($appId); |
111
|
|
|
|
112
|
|
|
if ($groupIds === []) { |
113
|
|
|
$this->appManager->enableApp($appId); |
114
|
|
|
$output->writeln($appId . ' enabled'); |
115
|
|
|
} else { |
116
|
|
|
$this->appManager->enableAppForGroups($appId, $groupIds); |
117
|
|
|
$output->writeln($appId . ' enabled for groups: ' . implode(', ', $groupNames)); |
118
|
|
|
} |
119
|
|
|
} catch (AppPathNotFoundException $e) { |
120
|
|
|
$output->writeln($appId . ' not found'); |
121
|
|
|
$this->exitCode = 1; |
122
|
|
|
} catch (\Exception $e) { |
123
|
|
|
$output->writeln($e->getMessage()); |
124
|
|
|
$this->exitCode = 1; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $groupIds |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
private function resolveGroupIds(array $groupIds): array { |
133
|
|
|
$groups = []; |
134
|
|
|
foreach ($groupIds as $groupId) { |
135
|
|
|
$group = $this->groupManager->get($groupId); |
136
|
|
|
if ($group instanceof IGroup) { |
137
|
|
|
$groups[] = $group; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
return $groups; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $optionName |
145
|
|
|
* @param CompletionContext $context |
146
|
|
|
* @return string[] |
147
|
|
|
*/ |
148
|
|
|
public function completeOptionValues($optionName, CompletionContext $context) { |
149
|
|
|
if ($optionName === 'groups') { |
150
|
|
|
return array_map(function (IGroup $group) { |
151
|
|
|
return $group->getGID(); |
152
|
|
|
}, $this->groupManager->search($context->getCurrentWord())); |
153
|
|
|
} |
154
|
|
|
return []; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $argumentName |
159
|
|
|
* @param CompletionContext $context |
160
|
|
|
* @return string[] |
161
|
|
|
*/ |
162
|
|
|
public function completeArgumentValues($argumentName, CompletionContext $context) { |
163
|
|
|
if ($argumentName === 'app-id') { |
164
|
|
|
$allApps = \OC_App::getAllApps(); |
165
|
|
|
return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
166
|
|
|
} |
167
|
|
|
return []; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|