Exemplo n.º 1
0
 private function serializeAuthBackend(\JsonSerializable $backend)
 {
     $data = $backend->jsonSerialize();
     $result = ['name' => $data['name'], 'identifier' => $data['identifier'], 'configuration' => array_map(function (DefinitionParameter $parameter) {
         return $parameter->getTypeName();
     }, $data['configuration'])];
     if ($backend instanceof Backend) {
         $result['storage_class'] = $backend->getStorageClass();
         $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
         $result['supported_authentication_backends'] = array_keys($authBackends);
     }
     return $result;
 }
 public function testGetAvailableBackends()
 {
     $service = new BackendService($this->config, $this->l10n);
     $backendAvailable = $this->getBackendMock('\\Backend\\Available');
     $backendAvailable->expects($this->once())->method('checkDependencies')->will($this->returnValue([]));
     $backendNotAvailable = $this->getBackendMock('\\Backend\\NotAvailable');
     $backendNotAvailable->expects($this->once())->method('checkDependencies')->will($this->returnValue([$this->getMockBuilder('\\OCA\\Files_External\\Lib\\MissingDependency')->disableOriginalConstructor()->getMock()]));
     $service->registerBackend($backendAvailable);
     $service->registerBackend($backendNotAvailable);
     $availableBackends = $service->getAvailableBackends();
     $this->assertArrayHasKey('identifier:\\Backend\\Available', $availableBackends);
     $this->assertArrayNotHasKey('identifier:\\Backend\\NotAvailable', $availableBackends);
 }
Exemplo n.º 3
0
 private function serializeAuthBackend(\JsonSerializable $backend)
 {
     $data = $backend->jsonSerialize();
     $result = ['name' => $data['name'], 'identifier' => $data['identifier'], 'configuration' => $this->formatConfiguration($data['configuration'])];
     if ($backend instanceof Backend) {
         $result['storage_class'] = $backend->getStorageClass();
         $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
         $result['supported_authentication_backends'] = array_keys($authBackends);
         $authConfig = array_map(function (AuthMechanism $auth) {
             return $this->serializeAuthBackend($auth)['configuration'];
         }, $authBackends);
         $result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig);
     }
     return $result;
 }
Exemplo n.º 4
0
 /**
  * Create a storage from its parameters
  *
  * @param string $mountPoint storage mount point
  * @param string $backendIdentifier backend identifier
  * @param string $authMechanismIdentifier authentication mechanism identifier
  * @param array $backendOptions backend-specific options
  * @param array|null $mountOptions mount-specific options
  * @param array|null $applicableUsers users for which to mount the storage
  * @param array|null $applicableGroups groups for which to mount the storage
  * @param int|null $priority priority
  *
  * @return StorageConfig
  */
 public function createStorage($mountPoint, $backendIdentifier, $authMechanismIdentifier, $backendOptions, $mountOptions = null, $applicableUsers = null, $applicableGroups = null, $priority = null)
 {
     $backend = $this->backendService->getBackend($backendIdentifier);
     if (!$backend) {
         throw new \InvalidArgumentException('Unable to get backend for ' . $backendIdentifier);
     }
     $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier);
     if (!$authMechanism) {
         throw new \InvalidArgumentException('Unable to get authentication mechanism for ' . $authMechanismIdentifier);
     }
     $newStorage = new StorageConfig();
     $newStorage->setMountPoint($mountPoint);
     $newStorage->setBackend($backend);
     $newStorage->setAuthMechanism($authMechanism);
     $newStorage->setBackendOptions($backendOptions);
     if (isset($mountOptions)) {
         $newStorage->setMountOptions($mountOptions);
     }
     if (isset($applicableUsers)) {
         $newStorage->setApplicableUsers($applicableUsers);
     }
     if (isset($applicableGroups)) {
         $newStorage->setApplicableGroups($applicableGroups);
     }
     if (isset($priority)) {
         $newStorage->setPriority($priority);
     }
     return $newStorage;
 }
Exemplo n.º 5
0
 private function getBackendByClass($className)
 {
     $backends = $this->backendService->getBackends();
     foreach ($backends as $backend) {
         if ($backend->getStorageClass() === $className) {
             return $backend;
         }
     }
 }
 /**
  * Copy legacy storage options into the given storage config object.
  *
  * @param StorageConfig $storageConfig storage config to populate
  * @param string $mountType mount type
  * @param string $applicable applicable user or group
  * @param array $storageOptions legacy storage options
  *
  * @return StorageConfig populated storage config
  */
 protected function populateStorageConfigWithLegacyOptions(&$storageConfig, $mountType, $applicable, $storageOptions)
 {
     $backend = $this->backendService->getBackend($storageOptions['backend']);
     if (!$backend) {
         throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']);
     }
     $storageConfig->setBackend($backend);
     if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') {
         $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']);
     } else {
         $authMechanism = $backend->getLegacyAuthMechanism($storageOptions);
         $storageOptions['authMechanism'] = 'null';
         // to make error handling easier
     }
     if (!$authMechanism) {
         throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']);
     }
     $storageConfig->setAuthMechanism($authMechanism);
     $storageConfig->setBackendOptions($storageOptions['options']);
     if (isset($storageOptions['mountOptions'])) {
         $storageConfig->setMountOptions($storageOptions['mountOptions']);
     }
     if (!isset($storageOptions['priority'])) {
         $storageOptions['priority'] = $backend->getPriority();
     }
     $storageConfig->setPriority($storageOptions['priority']);
     if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) {
         $applicableUsers = $storageConfig->getApplicableUsers();
         if ($applicable !== 'all') {
             $applicableUsers[] = $applicable;
             $storageConfig->setApplicableUsers($applicableUsers);
         }
     } else {
         if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) {
             $applicableGroups = $storageConfig->getApplicableGroups();
             $applicableGroups[] = $applicable;
             $storageConfig->setApplicableGroups($applicableGroups);
         }
     }
     return $storageConfig;
 }
Exemplo n.º 7
0
 public function testGetStoragesAuthMechanismNotVisible()
 {
     $backend = $this->backendService->getBackend('identifier:\\OCA\\Files_External\\Lib\\Backend\\SMB');
     $backend->method('isVisibleFor')->with($this->service->getVisibilityType())->willReturn(true);
     $authMechanism = $this->backendService->getAuthMechanism('identifier:\\Auth\\Mechanism');
     $authMechanism->expects($this->once())->method('isVisibleFor')->with($this->service->getVisibilityType())->willReturn(false);
     $storage = new StorageConfig(255);
     $storage->setMountPoint('mountpoint');
     $storage->setBackend($backend);
     $storage->setAuthMechanism($authMechanism);
     $storage->setBackendOptions(['password' => 'testPassword']);
     $newStorage = $this->service->addStorage($storage);
     $this->assertCount(1, $this->service->getAllStorages());
     $this->assertEmpty($this->service->getStorages());
 }
Exemplo n.º 8
0
 public function testUpdateStorageMountPoint()
 {
     $backend = $this->backendService->getBackend('identifier:\\OCA\\Files_External\\Lib\\Backend\\SMB');
     $authMechanism = $this->backendService->getAuthMechanism('identifier:\\Auth\\Mechanism');
     $storage = new StorageConfig();
     $storage->setMountPoint('mountpoint');
     $storage->setBackend($backend);
     $storage->setAuthMechanism($authMechanism);
     $storage->setBackendOptions(['password' => 'testPassword']);
     $savedStorage = $this->service->addStorage($storage);
     $newAuthMechanism = $this->backendService->getAuthMechanism('identifier:\\Other\\Auth\\Mechanism');
     $updatedStorage = new StorageConfig($savedStorage->getId());
     $updatedStorage->setMountPoint('mountpoint2');
     $updatedStorage->setBackend($backend);
     $updatedStorage->setAuthMechanism($newAuthMechanism);
     $updatedStorage->setBackendOptions(['password' => 'password2']);
     $this->service->updateStorage($updatedStorage);
     $savedStorage = $this->service->getStorage($updatedStorage->getId());
     $this->assertEquals('/mountpoint2', $savedStorage->getMountPoint());
     $this->assertEquals($newAuthMechanism, $savedStorage->getAuthMechanism());
     $this->assertEquals('password2', $savedStorage->getBackendOption('password'));
 }
Exemplo n.º 9
0
 public function testGetUserBackends()
 {
     $service = new BackendService($this->config, $this->l10n);
     $backendAllowed = $this->getBackendMock('\\User\\Mount\\Allowed');
     $backendAllowed->expects($this->once())->method('isVisibleFor')->with(BackendService::VISIBILITY_PERSONAL)->will($this->returnValue(true));
     $backendNotAllowed = $this->getBackendMock('\\User\\Mount\\NotAllowed');
     $backendNotAllowed->expects($this->once())->method('isVisibleFor')->with(BackendService::VISIBILITY_PERSONAL)->will($this->returnValue(false));
     $service->registerBackend($backendAllowed);
     $service->registerBackend($backendNotAllowed);
     $userBackends = $service->getBackendsVisibleFor(BackendService::VISIBILITY_PERSONAL);
     $this->assertArrayHasKey('identifier:\\User\\Mount\\Allowed', $userBackends);
     $this->assertArrayNotHasKey('identifier:\\User\\Mount\\NotAllowed', $userBackends);
 }
Exemplo n.º 10
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $user = $input->getOption('user');
     $mountPoint = $input->getArgument('mount_point');
     $storageIdentifier = $input->getArgument('storage_backend');
     $authIdentifier = $input->getArgument('authentication_backend');
     $configInput = $input->getOption('config');
     $storageBackend = $this->backendService->getBackend($storageIdentifier);
     $authBackend = $this->backendService->getAuthMechanism($authIdentifier);
     if (!Filesystem::isValidPath($mountPoint)) {
         $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
         return 1;
     }
     if (is_null($storageBackend)) {
         $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
         return 404;
     }
     if (is_null($authBackend)) {
         $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
         return 404;
     }
     $supportedSchemes = array_keys($storageBackend->getAuthSchemes());
     if (!in_array($authBackend->getScheme(), $supportedSchemes)) {
         $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>');
         return 1;
     }
     $config = [];
     foreach ($configInput as $configOption) {
         if (!strpos($configOption, '=')) {
             $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
             return 1;
         }
         list($key, $value) = explode('=', $configOption, 2);
         if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) {
             $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>');
             return 1;
         }
         $config[$key] = $value;
     }
     $mount = new StorageConfig();
     $mount->setMountPoint($mountPoint);
     $mount->setBackend($storageBackend);
     $mount->setAuthMechanism($authBackend);
     $mount->setBackendOptions($config);
     if ($user) {
         if (!$this->userManager->userExists($user)) {
             $output->writeln('<error>User "' . $user . '" not found</error>');
             return 1;
         }
         $mount->setApplicableUsers([$user]);
     }
     if ($input->getOption('dry')) {
         $this->showMount($user, $mount, $input, $output);
     } else {
         $this->getStorageService($user)->addStorage($mount);
         if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
             $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>');
         } else {
             $output->writeln($mount->getId());
         }
     }
     return 0;
 }