diff options
author | Joas Schilling <coding@schilljs.com> | 2022-04-08 11:40:05 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-04-08 11:54:44 +0200 |
commit | cfd2e8cc2026222866dd78a77fecd8ed44532634 (patch) | |
tree | cb2685ad69694c7948e06e07b04a2ba068ee0a80 /core/Command/Group | |
parent | 69378e15347630cdf4c3c7465a951857926e52ec (diff) | |
download | nextcloud-server-cfd2e8cc2026222866dd78a77fecd8ed44532634.tar.gz nextcloud-server-cfd2e8cc2026222866dd78a77fecd8ed44532634.zip |
Allow to autocomplete user and group ids in commands
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'core/Command/Group')
-rw-r--r-- | core/Command/Group/AddUser.php | 26 | ||||
-rw-r--r-- | core/Command/Group/Delete.php | 14 | ||||
-rw-r--r-- | core/Command/Group/Info.php | 13 | ||||
-rw-r--r-- | core/Command/Group/RemoveUser.php | 23 |
4 files changed, 76 insertions, 0 deletions
diff --git a/core/Command/Group/AddUser.php b/core/Command/Group/AddUser.php index b5d0068acc6..141916953ab 100644 --- a/core/Command/Group/AddUser.php +++ b/core/Command/Group/AddUser.php @@ -24,8 +24,11 @@ namespace OC\Core\Command\Group; use OC\Core\Command\Base; +use OCP\IGroup; use OCP\IGroupManager; +use OCP\IUser; use OCP\IUserManager; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -75,4 +78,27 @@ class AddUser extends Base { $group->addUser($user); return 0; } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'group') { + return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord())); + } + if ($argumentName === 'user') { + $groupId = $context->getWordAtIndex($context->getWordIndex() - 1); + $group = $this->groupManager->get($groupId); + if ($group === null) { + return []; + } + + $members = array_map(static fn (IUser $user) => $user->getUID(), $group->searchUsers($context->getCurrentWord())); + $users = array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord())); + return array_diff($users, $members); + } + return []; + } } diff --git a/core/Command/Group/Delete.php b/core/Command/Group/Delete.php index be97be83407..2596b461d17 100644 --- a/core/Command/Group/Delete.php +++ b/core/Command/Group/Delete.php @@ -27,7 +27,9 @@ declare(strict_types=1); namespace OC\Core\Command\Group; use OC\Core\Command\Base; +use OCP\IGroup; use OCP\IGroupManager; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -74,4 +76,16 @@ class Delete extends Base { } return 0; } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'groupid') { + return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord())); + } + return []; + } } diff --git a/core/Command/Group/Info.php b/core/Command/Group/Info.php index 180055e83ea..5e9fa661130 100644 --- a/core/Command/Group/Info.php +++ b/core/Command/Group/Info.php @@ -28,6 +28,7 @@ namespace OC\Core\Command\Group; use OC\Core\Command\Base; use OCP\IGroup; use OCP\IGroupManager; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -79,4 +80,16 @@ class Info extends Base { return 0; } } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'groupid') { + return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord())); + } + return []; + } } diff --git a/core/Command/Group/RemoveUser.php b/core/Command/Group/RemoveUser.php index 4af66480f28..2b9c4eaec52 100644 --- a/core/Command/Group/RemoveUser.php +++ b/core/Command/Group/RemoveUser.php @@ -24,8 +24,11 @@ namespace OC\Core\Command\Group; use OC\Core\Command\Base; +use OCP\IGroup; use OCP\IGroupManager; +use OCP\IUser; use OCP\IUserManager; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -75,4 +78,24 @@ class RemoveUser extends Base { $group->removeUser($user); return 0; } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'group') { + return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord())); + } + if ($argumentName === 'user') { + $groupId = $context->getWordAtIndex($context->getWordIndex() - 1); + $group = $this->groupManager->get($groupId); + if ($group === null) { + return []; + } + return array_map(static fn (IUser $user) => $user->getUID(), $group->searchUsers($context->getCurrentWord())); + } + return []; + } } |