From 51b517983930fcbcf144703fe81a003132e727d5 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 18 Jan 2016 11:58:17 +0100 Subject: [PATCH] Add occ command to manage applicables --- .../appinfo/register_command.php | 3 + apps/files_external/command/applicable.php | 157 ++++++++++++++++ .../tests/command/applicabletest.php | 168 ++++++++++++++++++ .../tests/command/commandtest.php | 104 +++++++++++ 4 files changed, 432 insertions(+) create mode 100644 apps/files_external/command/applicable.php create mode 100644 apps/files_external/tests/command/applicabletest.php create mode 100644 apps/files_external/tests/command/commandtest.php diff --git a/apps/files_external/appinfo/register_command.php b/apps/files_external/appinfo/register_command.php index d85906e383..929becce77 100644 --- a/apps/files_external/appinfo/register_command.php +++ b/apps/files_external/appinfo/register_command.php @@ -23,12 +23,14 @@ use OCA\Files_External\Command\ListCommand; use OCA\Files_External\Command\Config; use OCA\Files_External\Command\Option; +use OCA\Files_External\Command\Applicable; use OCA\Files_External\Command\Import; use OCA\Files_External\Command\Export; use OCA\Files_External\Command\Delete; $userManager = OC::$server->getUserManager(); $userSession = OC::$server->getUserSession(); +$groupManager = OC::$server->getGroupManager(); $app = \OC_Mount_Config::$app; @@ -41,6 +43,7 @@ $backendService = $app->getContainer()->query('OCA\Files_External\Service\Backen $application->add(new ListCommand($globalStorageService, $userStorageService, $userSession, $userManager)); $application->add(new Config($globalStorageService)); $application->add(new Option($globalStorageService)); +$application->add(new Applicable($globalStorageService, $userManager, $groupManager)); $application->add(new Import($globalStorageService, $userStorageService, $userSession, $userManager, $importLegacyStorageService, $backendService)); $application->add(new Export($globalStorageService, $userStorageService, $userSession, $userManager)); $application->add(new Delete($globalStorageService, $userStorageService, $userSession, $userManager)); diff --git a/apps/files_external/command/applicable.php b/apps/files_external/command/applicable.php new file mode 100644 index 0000000000..7e6c99d291 --- /dev/null +++ b/apps/files_external/command/applicable.php @@ -0,0 +1,157 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_external\NotFoundException; +use OCA\Files_external\Service\GlobalStoragesService; +use OCP\IGroupManager; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableHelper; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Applicable extends Base { + /** + * @var GlobalStoragesService + */ + protected $globalService; + + /** + * @var IUserManager + */ + private $userManager; + + /** + * @var IGroupManager + */ + private $groupManager; + + function __construct( + GlobalStoragesService $globalService, + IUserManager $userManager, + IGroupManager $groupManager + ) { + parent::__construct(); + $this->globalService = $globalService; + $this->userManager = $userManager; + $this->groupManager = $groupManager; + } + + protected function configure() { + $this + ->setName('files_external:applicable') + ->setDescription('Manage applicable users and groups for a mount') + ->addArgument( + 'mount_id', + InputArgument::REQUIRED, + 'The id of the mount to edit' + )->addOption( + 'add-user', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'user to add as applicable' + )->addOption( + 'remove-user', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'user to remove as applicable' + )->addOption( + 'add-group', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'group to add as applicable' + )->addOption( + 'remove-group', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'group to remove as applicable' + )->addOption( + 'remove-all', + null, + InputOption::VALUE_NONE, + 'Set the mount to be globally applicable' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mountId = $input->getArgument('mount_id'); + try { + $mount = $this->globalService->getStorage($mountId); + } catch (NotFoundException $e) { + $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts'); + return 404; + } + + if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { + $output->writeln('Can\'t change applicables on personal mounts'); + return 1; + } + + $addUsers = $input->getOption('add-user'); + $removeUsers = $input->getOption('remove-user'); + $addGroups = $input->getOption('add-group'); + $removeGroups = $input->getOption('remove-group'); + + $applicableUsers = $mount->getApplicableUsers(); + $applicableGroups = $mount->getApplicableGroups(); + + if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { + foreach ($addUsers as $addUser) { + if (!$this->userManager->userExists($addUser)) { + $output->writeln('User "' . $addUser . '" not found'); + return 404; + } + } + foreach ($addGroups as $addGroup) { + if (!$this->groupManager->groupExists($addGroup)) { + $output->writeln('Group "' . $addGroup . '" not found'); + return 404; + } + } + + if ($input->getOption('remove-all')) { + $applicableUsers = []; + $applicableGroups = []; + } else { + $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); + $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); + $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); + $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); + } + $mount->setApplicableUsers($applicableUsers); + $mount->setApplicableGroups($applicableGroups); + $this->globalService->updateStorage($mount); + } + + $this->writeArrayInOutputFormat($input, $output, [ + 'users' => $applicableUsers, + 'groups' => $applicableGroups + ]); + } +} diff --git a/apps/files_external/tests/command/applicabletest.php b/apps/files_external/tests/command/applicabletest.php new file mode 100644 index 0000000000..64d41f6f24 --- /dev/null +++ b/apps/files_external/tests/command/applicabletest.php @@ -0,0 +1,168 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 + * + */ + +namespace OCA\Files_External\Tests\Command; + +use OCA\Files_External\Command\Applicable; + +class ApplicableTest extends CommandTest { + private function getInstance($storageService) { + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */ + $userManager = $this->getMock('\OCP\IUserManager'); + /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */ + $groupManager = $this->getMock('\OCP\IGroupManager'); + + $userManager->expects($this->any()) + ->method('userExists') + ->will($this->returnValue(true)); + + $groupManager->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + return new Applicable($storageService, $userManager, $groupManager); + } + + public function testListEmpty() { + $mount = $this->getMount(1, '', ''); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json' + ]); + + $result = json_decode($this->executeCommand($command, $input), true); + + $this->assertEquals(['users' => [], 'groups' => []], $result); + } + + public function testList() { + $mount = $this->getMount(1, '', '', '', [], [], ['test', 'asd']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json' + ]); + + $result = json_decode($this->executeCommand($command, $input), true); + + $this->assertEquals(['users' => ['test', 'asd'], 'groups' => []], $result); + } + + public function testAddSingle() { + $mount = $this->getMount(1, '', '', '', [], [], []); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'add-user' => ['foo'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo'], $mount->getApplicableUsers()); + } + + public function testAddDuplicate() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'add-user' => ['foo', 'bar'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo', 'bar'], $mount->getApplicableUsers()); + } + + public function testRemoveSingle() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'remove-user' => ['bar'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo'], $mount->getApplicableUsers()); + } + + public function testRemoveNonExisting() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'remove-user' => ['bar', 'asd'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo'], $mount->getApplicableUsers()); + } + + public function testRemoveAddRemove() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'remove-user' => ['bar', 'asd'], + 'add-user' => ['test'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo', 'test'], $mount->getApplicableUsers()); + } +} diff --git a/apps/files_external/tests/command/commandtest.php b/apps/files_external/tests/command/commandtest.php new file mode 100644 index 0000000000..9a0afbd368 --- /dev/null +++ b/apps/files_external/tests/command/commandtest.php @@ -0,0 +1,104 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 + * + */ + +namespace OCA\Files_External\Tests\Command; + +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_external\NotFoundException; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\Input; +use Symfony\Component\Console\Output\BufferedOutput; +use Test\TestCase; + +abstract class CommandTest extends TestCase { + /** + * @param StorageConfig[] $mounts + * @return \OCA\Files_external\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getGlobalStorageService(array $mounts = []) { + $mock = $this->getMockBuilder('OCA\Files_external\Service\GlobalStoragesService') + ->disableOriginalConstructor() + ->getMock(); + + $this->bindMounts($mock, $mounts); + + return $mock; + } + + /** + * @param \PHPUnit_Framework_MockObject_MockObject $mock + * @param StorageConfig[] $mounts + */ + protected function bindMounts(\PHPUnit_Framework_MockObject_MockObject $mock, array $mounts) { + $mock->expects($this->any()) + ->method('getStorage') + ->will($this->returnCallback(function ($id) use ($mounts) { + foreach ($mounts as $mount) { + if ($mount->getId() === $id) { + return $mount; + } + } + throw new NotFoundException(); + })); + } + + /** + * @param $id + * @param $mountPoint + * @param $backendClass + * @param string $applicableIdentifier + * @param array $config + * @param array $options + * @param array $users + * @param array $groups + * @return StorageConfig + */ + protected function getMount($id, $mountPoint, $backendClass, $applicableIdentifier = 'password::password', $config = [], $options = [], $users = [], $groups = []) { + $mount = new StorageConfig($id); + + $mount->setMountPoint($mountPoint); + $mount->setBackendOptions($config); + $mount->setMountOptions($options); + $mount->setApplicableUsers($users); + $mount->setApplicableGroups($groups); + + return $mount; + } + + protected function getInput(Command $command, array $arguments = [], array $options = []) { + $input = new ArrayInput([]); + $input->bind($command->getDefinition()); + foreach ($arguments as $key => $value) { + $input->setArgument($key, $value); + } + foreach ($options as $key => $value) { + $input->setOption($key, $value); + } + return $input; + } + + protected function executeCommand(Command $command, Input $input) { + $output = new BufferedOutput(); + $this->invokePrivate($command, 'execute', [$input, $output]); + return $output->fetch(); + } +}