Skip to content

Commit

Permalink
Allow to autocomplete user and group ids in commands
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Apr 8, 2022
1 parent 69378e1 commit 3f40b6d
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 9 deletions.
26 changes: 26 additions & 0 deletions core/Command/Group/AddUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,4 +78,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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 [];
}
}
14 changes: 14 additions & 0 deletions core/Command/Group/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
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;
Expand Down Expand Up @@ -74,4 +76,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
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 [];
}
}
13 changes: 13 additions & 0 deletions core/Command/Group/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -79,4 +80,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
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 [];
}
}
23 changes: 23 additions & 0 deletions core/Command/Group/RemoveUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,4 +78,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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 [];
}
}
18 changes: 16 additions & 2 deletions core/Command/User/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
*/
namespace OC\Core\Command\User;

use OC\Core\Command\Base;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
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;

class Delete extends Command {
class Delete extends Base {
/** @var IUserManager */
protected $userManager;

Expand Down Expand Up @@ -68,4 +70,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('<error>The specified user could not be deleted. Please check the logs.</error>');
return 1;
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'uid') {
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
}
return [];
}
}
24 changes: 22 additions & 2 deletions core/Command/User/Disable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
*/
namespace OC\Core\Command\User;

use OC\Core\Command\Base;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
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;

class Disable extends Command {
class Disable extends Base {
/** @var IUserManager */
protected $userManager;

Expand Down Expand Up @@ -63,4 +65,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('<info>The specified user is disabled</info>');
return 0;
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'uid') {
return array_map(
static fn (IUser $user) => $user->getUID(),
array_filter(
$this->userManager->search($context->getCurrentWord()),
static fn (IUser $user) => $user->isEnabled()
)
);
}
return [];
}
}
24 changes: 22 additions & 2 deletions core/Command/User/Enable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
*/
namespace OC\Core\Command\User;

use OC\Core\Command\Base;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
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;

class Enable extends Command {
class Enable extends Base {
/** @var IUserManager */
protected $userManager;

Expand Down Expand Up @@ -63,4 +65,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('<info>The specified user is enabled</info>');
return 0;
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'uid') {
return array_map(
static fn (IUser $user) => $user->getUID(),
array_filter(
$this->userManager->search($context->getCurrentWord()),
static fn (IUser $user) => !$user->isEnabled()
)
);
}
return [];
}
}
13 changes: 13 additions & 0 deletions core/Command/User/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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\Input\InputOption;
Expand Down Expand Up @@ -110,4 +111,16 @@ protected function getStorageInfo(IUser $user): array {
'quota' => $storage['quota'],
];
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'user') {
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
}
return [];
}
}
17 changes: 16 additions & 1 deletion core/Command/User/LastSeen.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
*/
namespace OC\Core\Command\User;

use OC\Core\Command\Base;
use OCP\IUser;
use OCP\IUserManager;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class LastSeen extends Command {
class LastSeen extends Base {
/** @var IUserManager */
protected $userManager;

Expand Down Expand Up @@ -73,4 +76,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
return 0;
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'uid') {
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
}
return [];
}
}
18 changes: 16 additions & 2 deletions core/Command/User/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
*/
namespace OC\Core\Command\User;

use OC\Core\Command\Base;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -37,7 +39,7 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

class ResetPassword extends Command {
class ResetPassword extends Base {

/** @var IUserManager */
protected $userManager;
Expand Down Expand Up @@ -133,4 +135,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
return 0;
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'user') {
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
}
return [];
}
}
24 changes: 24 additions & 0 deletions core/Command/User/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\IConfig;
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\Input\InputOption;
Expand Down Expand Up @@ -255,4 +256,27 @@ protected function getUserSettings($uid, $app) {

return $settings;
}

/**
* @param string $argumentName
* @param CompletionContext $context
* @return string[]
*/
public function completeArgumentValues($argumentName, CompletionContext $context) {
if ($argumentName === 'uid') {
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
}
if ($argumentName === 'app') {
$userId = $context->getWordAtIndex($context->getWordIndex() - 1);
$settings = $this->getUserSettings($userId, '');
return array_keys($settings);
}
if ($argumentName === 'key') {
$userId = $context->getWordAtIndex($context->getWordIndex() - 2);
$app = $context->getWordAtIndex($context->getWordIndex() - 1);
$settings = $this->getUserSettings($userId, $app);
return array_keys($settings[$app]);
}
return [];
}
}

0 comments on commit 3f40b6d

Please sign in to comment.