public function testJsonSerialization()
 {
     $backend = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Backend\\Backend')->disableOriginalConstructor()->getMock();
     $parameter = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\DefinitionParameter')->disableOriginalConstructor()->getMock();
     $parameter->expects($this->once())->method('getType')->willReturn(1);
     $backend->expects($this->once())->method('getParameters')->willReturn(['secure' => $parameter]);
     $backend->method('getIdentifier')->willReturn('storage::identifier');
     $authMech = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\AuthMechanism')->disableOriginalConstructor()->getMock();
     $authMech->method('getIdentifier')->willReturn('auth::identifier');
     $storageConfig = new StorageConfig(1);
     $storageConfig->setMountPoint('test');
     $storageConfig->setBackend($backend);
     $storageConfig->setAuthMechanism($authMech);
     $storageConfig->setBackendOptions(['user' => 'test', 'password' => 'password123', 'secure' => '1']);
     $storageConfig->setPriority(128);
     $storageConfig->setApplicableUsers(['user1', 'user2']);
     $storageConfig->setApplicableGroups(['group1', 'group2']);
     $storageConfig->setMountOptions(['preview' => false]);
     $json = $storageConfig->jsonSerialize();
     $this->assertSame(1, $json['id']);
     $this->assertSame('/test', $json['mountPoint']);
     $this->assertSame('storage::identifier', $json['backend']);
     $this->assertSame('auth::identifier', $json['authMechanism']);
     $this->assertSame('test', $json['backendOptions']['user']);
     $this->assertSame('password123', $json['backendOptions']['password']);
     $this->assertSame(true, $json['backendOptions']['secure']);
     $this->assertSame(128, $json['priority']);
     $this->assertSame(['user1', 'user2'], $json['applicableUsers']);
     $this->assertSame(['group1', 'group2'], $json['applicableGroups']);
     $this->assertSame(['preview' => false], $json['mountOptions']);
 }
Beispiel #2
0
 /**
  * @param StorageConfig $storage
  * @param IUser $user
  */
 public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null)
 {
     $user = $storage->getBackendOption('user');
     if ($domain = $storage->getBackendOption('domain')) {
         $storage->setBackendOption('user', $domain . '\\' . $user);
     }
 }
Beispiel #3
0
 /**
  * Triggers signal_create_mount or signal_delete_mount to
  * accomodate for additions/deletions in applicableUsers
  * and applicableGroups fields.
  *
  * @param StorageConfig $oldStorage old storage config
  * @param StorageConfig $newStorage new storage config
  */
 protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage)
 {
     // if mount point changed, it's like a deletion + creation
     if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
         $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
         $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
         return;
     }
     $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
     $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
     $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
     $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
     // FIXME: Use as expression in empty once PHP 5.4 support is dropped
     // if no applicable were set, raise a signal for "all"
     $oldApplicableUsers = $oldStorage->getApplicableUsers();
     $oldApplicableGroups = $oldStorage->getApplicableGroups();
     if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) {
         $this->triggerApplicableHooks(Filesystem::signal_delete_mount, $oldStorage->getMountPoint(), \OC_Mount_Config::MOUNT_TYPE_USER, ['all']);
     }
     // trigger delete for removed users
     $this->triggerApplicableHooks(Filesystem::signal_delete_mount, $oldStorage->getMountPoint(), \OC_Mount_Config::MOUNT_TYPE_USER, $userDeletions);
     // trigger delete for removed groups
     $this->triggerApplicableHooks(Filesystem::signal_delete_mount, $oldStorage->getMountPoint(), \OC_Mount_Config::MOUNT_TYPE_GROUP, $groupDeletions);
     // and now add the new users
     $this->triggerApplicableHooks(Filesystem::signal_create_mount, $newStorage->getMountPoint(), \OC_Mount_Config::MOUNT_TYPE_USER, $userAdditions);
     // and now add the new groups
     $this->triggerApplicableHooks(Filesystem::signal_create_mount, $newStorage->getMountPoint(), \OC_Mount_Config::MOUNT_TYPE_GROUP, $groupAdditions);
     // FIXME: Use as expression in empty once PHP 5.4 support is dropped
     // if no applicable, raise a signal for "all"
     $newApplicableUsers = $newStorage->getApplicableUsers();
     $newApplicableGroups = $newStorage->getApplicableGroups();
     if (empty($newApplicableUsers) && empty($newApplicableGroups)) {
         $this->triggerApplicableHooks(Filesystem::signal_create_mount, $newStorage->getMountPoint(), \OC_Mount_Config::MOUNT_TYPE_USER, ['all']);
     }
 }
 /**
  * Triggers signal_create_mount or signal_delete_mount to
  * accomodate for additions/deletions in applicableUsers
  * and applicableGroups fields.
  *
  * @param StorageConfig $oldStorage old storage data
  * @param StorageConfig $newStorage new storage data
  */
 protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage)
 {
     // if mount point changed, it's like a deletion + creation
     if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
         $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
         $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
     }
 }
Beispiel #5
0
 public function manipulateStorageConfig(StorageConfig &$storage)
 {
     $username_as_share = $storage->getBackendOption('username_as_share') === true;
     if ($username_as_share) {
         $share = '/' . $storage->getBackendOption('user');
         $storage->setBackendOption('share', $share);
     }
 }
 protected function manipulateStorageConfig(StorageConfig $storage)
 {
     /** @var AuthMechanism */
     $authMechanism = $storage->getAuthMechanism();
     $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
     /** @var Backend */
     $backend = $storage->getBackend();
     $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
 }
Beispiel #7
0
 private function manipulateStorageConfig(StorageConfig $storage)
 {
     /** @var AuthMechanism */
     $authMechanism = $storage->getAuthMechanism();
     $authMechanism->manipulateStorageConfig($storage);
     /** @var Backend */
     $backend = $storage->getBackend();
     $backend->manipulateStorageConfig($storage);
 }
Beispiel #8
0
 public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null)
 {
     $auth = new RSACrypt();
     $auth->setPassword($this->config->getSystemValue('secret', ''));
     if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
         throw new \RuntimeException('unable to load private key');
     }
     $storage->setBackendOption('public_key_auth', $auth);
 }
Beispiel #9
0
 /**
  * @param StorageConfig $mount
  * @param string $key
  * @param string $value
  * @param OutputInterface $output
  */
 protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output)
 {
     $decoded = json_decode($value, true);
     if (!is_null($decoded)) {
         $value = $decoded;
     }
     $mount->setMountOption($key, $value);
     $this->globalService->updateStorage($mount);
 }
Beispiel #10
0
 /**
  * Construct the storage implementation
  *
  * @param StorageConfig $storageConfig
  * @return Storage
  */
 private function constructStorage(StorageConfig $storageConfig)
 {
     $class = $storageConfig->getBackend()->getStorageClass();
     $storage = new $class($storageConfig->getBackendOptions());
     // auth mechanism should fire first
     $storage = $storageConfig->getBackend()->wrapStorage($storage);
     $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
     return $storage;
 }
Beispiel #11
0
 public function manipulateStorageConfig(StorageConfig &$storage)
 {
     $encrypted = $this->session->get('password::sessioncredentials/credentials');
     if (!isset($encrypted)) {
         throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved');
     }
     $credentials = json_decode($this->crypto->decrypt($encrypted), true);
     $storage->setBackendOption('user', $this->session->get('loginname'));
     $storage->setBackendOption('password', $credentials['password']);
 }
Beispiel #12
0
 public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null)
 {
     if ($storage->getType() === StorageConfig::MOUNT_TYPE_ADMIN) {
         $uid = '';
     } else {
         $uid = $user->getUID();
     }
     $credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
     if (is_array($credentials)) {
         $storage->setBackendOption('user', $credentials['user']);
         $storage->setBackendOption('password', $credentials['password']);
     }
 }
Beispiel #13
0
 public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null)
 {
     if (!isset($user)) {
         throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
     }
     $uid = $user->getUID();
     $credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
     if (!isset($credentials)) {
         throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
     }
     $storage->setBackendOption('user', $credentials['user']);
     $storage->setBackendOption('password', $credentials['password']);
 }
Beispiel #14
0
 public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null)
 {
     if ($storage->getType() === StorageConfig::MOUNT_TYPE_ADMIN) {
         $uid = '';
     } elseif (is_null($user)) {
         throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
     } else {
         $uid = $user->getUID();
     }
     $credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
     if (is_array($credentials)) {
         $storage->setBackendOption('user', $credentials['user']);
         $storage->setBackendOption('password', $credentials['password']);
     }
 }
 public function testDeleteStorage()
 {
     $storage = new StorageConfig(255);
     $storage->setMountPoint('mountpoint');
     $storage->setBackendClass('\\OC\\Files\\Storage\\SMB');
     $storage->setBackendOptions(['password' => 'testPassword']);
     $newStorage = $this->service->addStorage($storage);
     $this->assertEquals(1, $newStorage->getId());
     $newStorage = $this->service->removeStorage(1);
     $caught = false;
     try {
         $this->service->getStorage(1);
     } catch (NotFoundException $e) {
         $caught = true;
     }
     $this->assertTrue($caught);
 }
Beispiel #16
0
 public function testListAuthIdentifier()
 {
     $l10n = $this->getMock('\\OC_L10N', null, [], '', false);
     $credentialsManager = $this->getMock('\\OCP\\Security\\ICredentialsManager');
     $instance = $this->getInstance();
     $mount1 = new StorageConfig();
     $mount1->setAuthMechanism(new Password($l10n));
     $mount1->setBackend(new Local($l10n, new NullMechanism($l10n)));
     $mount2 = new StorageConfig();
     $mount2->setAuthMechanism(new UserProvided($l10n, $credentialsManager));
     $mount2->setBackend(new Local($l10n, new NullMechanism($l10n)));
     $input = $this->getInput($instance, [], ['output' => 'json']);
     $output = new BufferedOutput();
     $instance->listMounts('', [$mount1, $mount2], $input, $output);
     $output = json_decode($output->fetch(), true);
     $this->assertNotEquals($output[0]['authentication_type'], $output[1]['authentication_type']);
 }
Beispiel #17
0
 /**
  * @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;
 }
Beispiel #18
0
 public function testJsonSerialization()
 {
     $storageConfig = new StorageConfig(1);
     $storageConfig->setMountPoint('test');
     $storageConfig->setBackendClass('\\OC\\Files\\Storage\\SMB');
     $storageConfig->setBackendOptions(['user' => 'test', 'password' => 'password123']);
     $storageConfig->setPriority(128);
     $storageConfig->setApplicableUsers(['user1', 'user2']);
     $storageConfig->setApplicableGroups(['group1', 'group2']);
     $storageConfig->setMountOptions(['preview' => false]);
     $json = $storageConfig->jsonSerialize();
     $this->assertEquals(1, $json['id']);
     $this->assertEquals('/test', $json['mountPoint']);
     $this->assertEquals('\\OC\\Files\\Storage\\SMB', $json['backendClass']);
     $this->assertEquals('test', $json['backendOptions']['user']);
     $this->assertEquals('password123', $json['backendOptions']['password']);
     $this->assertEquals(128, $json['priority']);
     $this->assertEquals(['user1', 'user2'], $json['applicableUsers']);
     $this->assertEquals(['group1', 'group2'], $json['applicableGroups']);
     $this->assertEquals(['preview' => false], $json['mountOptions']);
 }
 /**
  * Update an external storage entry.
  *
  * @param int $id storage id
  * @param string $mountPoint storage mount point
  * @param string $backendClass backend class name
  * @param array $backendOptions backend-specific options
  * @param array $mountOptions mount-specific options
  * @param array $applicableUsers users for which to mount the storage
  * @param array $applicableGroups groups for which to mount the storage
  * @param int $priority priority
  *
  * @return DataResponse
  */
 public function update($id, $mountPoint, $backendClass, $backendOptions, $mountOptions, $applicableUsers, $applicableGroups, $priority)
 {
     $storage = new StorageConfig($id);
     $storage->setMountPoint($mountPoint);
     $storage->setBackendClass($backendClass);
     $storage->setBackendOptions($backendOptions);
     $storage->setMountOptions($mountOptions);
     $storage->setApplicableUsers($applicableUsers);
     $storage->setApplicableGroups($applicableGroups);
     $storage->setPriority($priority);
     $response = $this->validate($storage);
     if (!empty($response)) {
         return $response;
     }
     try {
         $storage = $this->service->updateStorage($storage);
     } catch (NotFoundException $e) {
         return new DataResponse(['message' => (string) $this->l10n->t('Storage with id "%i" not found', array($id))], Http::STATUS_NOT_FOUND);
     }
     $this->updateStorageStatus($storage);
     return new DataResponse($storage, Http::STATUS_OK);
 }
 public function testAddOrUpdateStorageDisallowedBackend()
 {
     $backend = $this->getBackendMock();
     $backend->method('isVisibleFor')->with(BackendService::VISIBILITY_PERSONAL)->willReturn(false);
     $authMech = $this->getAuthMechMock();
     $storageConfig = new StorageConfig(1);
     $storageConfig->setMountPoint('mount');
     $storageConfig->setBackend($backend);
     $storageConfig->setAuthMechanism($authMech);
     $storageConfig->setBackendOptions([]);
     $this->service->expects($this->exactly(2))->method('createStorage')->will($this->returnValue($storageConfig));
     $this->service->expects($this->never())->method('addStorage');
     $this->service->expects($this->never())->method('updateStorage');
     $response = $this->controller->create('mount', '\\OC\\Files\\Storage\\SMB', '\\Auth\\Mechanism', array(), [], [], [], null);
     $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
     $response = $this->controller->update(1, 'mount', '\\OC\\Files\\Storage\\SMB', '\\Auth\\Mechanism', array(), [], [], [], null);
     $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
 }
Beispiel #21
0
 /**
  * Convert a StorageConfig to the legacy mountPoints array format
  * There's a lot of extra information in here, to satisfy all of the legacy functions
  *
  * @param StorageConfig $storage
  * @param bool $isPersonal
  * @return array
  */
 private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal)
 {
     $mountEntry = [];
     $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1);
     // remove leading slash
     $mountEntry['class'] = $storage->getBackend()->getIdentifier();
     $mountEntry['backend'] = $storage->getBackend()->getText();
     $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier();
     $mountEntry['personal'] = $isPersonal;
     $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions());
     $mountEntry['mountOptions'] = $storage->getMountOptions();
     $mountEntry['priority'] = $storage->getPriority();
     $mountEntry['applicable'] = ['groups' => $storage->getApplicableGroups(), 'users' => $storage->getApplicableUsers()];
     // if mountpoint is applicable to all users the old API expects ['all']
     if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) {
         $mountEntry['applicable']['users'] = ['all'];
     }
     $mountEntry['id'] = $storage->getId();
     return $mountEntry;
 }
 /**
  * Read the external storages config
  *
  * @return StorageConfig[] map of storage id to storage config
  */
 public function getAllStorages()
 {
     $mountPoints = $this->readLegacyConfig();
     /**
      * Here is the how the horribly messy mount point array looks like
      * from the mount.json file:
      *
      * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath]
      *
      * - $mountType is either "user" or "group"
      * - $applicable is the name of a user or group (or the current user for personal mounts)
      * - $mountPath is the mount point path (where the storage must be mounted)
      * - $storageOptions is a map of storage options:
      *     - "priority": storage priority
      *     - "backend": backend identifier
      *     - "class": LEGACY backend class name
      *     - "options": backend-specific options
      *     - "authMechanism": authentication mechanism identifier
      *     - "mountOptions": mount-specific options (ex: disable previews, scanner, etc)
      */
     // group by storage id
     /** @var StorageConfig[] $storages */
     $storages = [];
     // for storages without id (legacy), group by config hash for
     // later processing
     $storagesWithConfigHash = [];
     foreach ($mountPoints as $mountType => $applicables) {
         foreach ($applicables as $applicable => $mountPaths) {
             foreach ($mountPaths as $rootMountPath => $storageOptions) {
                 $currentStorage = null;
                 /**
                  * Flag whether the config that was read already has an id.
                  * If not, it will use a config hash instead and generate
                  * a proper id later
                  *
                  * @var boolean
                  */
                 $hasId = false;
                 // the root mount point is in the format "/$user/files/the/mount/point"
                 // we remove the "/$user/files" prefix
                 $parts = explode('/', ltrim($rootMountPath, '/'), 3);
                 if (count($parts) < 3) {
                     // something went wrong, skip
                     \OCP\Util::writeLog('files_external', 'Could not parse mount point "' . $rootMountPath . '"', \OCP\Util::ERROR);
                     continue;
                 }
                 $relativeMountPath = rtrim($parts[2], '/');
                 // note: we cannot do this after the loop because the decrypted config
                 // options might be needed for the config hash
                 $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']);
                 if (!isset($storageOptions['backend'])) {
                     $storageOptions['backend'] = $storageOptions['class'];
                     // legacy compat
                 }
                 if (!isset($storageOptions['authMechanism'])) {
                     $storageOptions['authMechanism'] = null;
                     // ensure config hash works
                 }
                 if (isset($storageOptions['id'])) {
                     $configId = (int) $storageOptions['id'];
                     if (isset($storages[$configId])) {
                         $currentStorage = $storages[$configId];
                     }
                     $hasId = true;
                 } else {
                     // missing id in legacy config, need to generate
                     // but at this point we don't know the max-id, so use
                     // first group it by config hash
                     $storageOptions['mountpoint'] = $rootMountPath;
                     $configId = \OC_Mount_Config::makeConfigHash($storageOptions);
                     if (isset($storagesWithConfigHash[$configId])) {
                         $currentStorage = $storagesWithConfigHash[$configId];
                     }
                 }
                 if (is_null($currentStorage)) {
                     // create new
                     $currentStorage = new StorageConfig($configId);
                     $currentStorage->setMountPoint($relativeMountPath);
                 }
                 try {
                     $this->populateStorageConfigWithLegacyOptions($currentStorage, $mountType, $applicable, $storageOptions);
                     if ($hasId) {
                         $storages[$configId] = $currentStorage;
                     } else {
                         $storagesWithConfigHash[$configId] = $currentStorage;
                     }
                 } catch (\UnexpectedValueException $e) {
                     // don't die if a storage backend doesn't exist
                     \OCP\Util::writeLog('files_external', 'Could not load storage: "' . $e->getMessage() . '"', \OCP\Util::ERROR);
                 }
             }
         }
     }
     // convert parameter values
     foreach ($storages as $storage) {
         $storage->getBackend()->validateStorageDefinition($storage);
         $storage->getAuthMechanism()->validateStorageDefinition($storage);
     }
     return $storages;
 }
Beispiel #23
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());
 }
Beispiel #24
0
 /**
  * Returns the rusty storage id from oc_storages from the given storage config.
  *
  * @param StorageConfig $storageConfig
  * @return string rusty storage id
  */
 private function getRustyStorageIdFromConfig(StorageConfig $storageConfig)
 {
     // if any of the storage options contains $user, it is not possible
     // to compute the possible storage id as we don't know which users
     // mounted it already (and we certainly don't want to iterate over ALL users)
     foreach ($storageConfig->getBackendOptions() as $value) {
         if (strpos($value, '$user') !== false) {
             throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration');
         }
     }
     // note: similar to ConfigAdapter->prepateStorageConfig()
     $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig);
     $storageConfig->getBackend()->manipulateStorageConfig($storageConfig);
     $class = $storageConfig->getBackend()->getStorageClass();
     $storageImpl = new $class($storageConfig->getBackendOptions());
     return $storageImpl->getId();
 }
 protected function isApplicable(StorageConfig $config)
 {
     $applicableUsers = $config->getApplicableUsers();
     $applicableGroups = $config->getApplicableGroups();
     if (count($applicableUsers) === 0 && count($applicableGroups) === 0) {
         return true;
     }
     if (in_array($this->getUser()->getUID(), $applicableUsers, true)) {
         return true;
     }
     $groupIds = $this->groupManager->getUserGroupIds($this->getUser());
     foreach ($groupIds as $groupId) {
         if (in_array($groupId, $applicableGroups, true)) {
             return true;
         }
     }
     return false;
 }
 /**
  * Remove sensitive data from a StorageConfig before returning it to the user
  *
  * @param StorageConfig $storage
  */
 protected function sanitizeStorage(StorageConfig $storage)
 {
     $storage->setBackendOptions([]);
     $storage->setMountOptions([]);
 }
 /**
  * Get a priority 'type', where a bigger number means higher priority
  * user applicable > group applicable > 'all'
  *
  * @param StorageConfig $storage
  * @return int
  */
 protected function getPriorityType(StorageConfig $storage)
 {
     $applicableUsers = $storage->getApplicableUsers();
     $applicableGroups = $storage->getApplicableGroups();
     if ($applicableUsers && $applicableUsers[0] !== 'all') {
         return 2;
     }
     if ($applicableGroups) {
         return 1;
     }
     return 0;
 }
 /**
  * Construct the storage implementation
  *
  * @param StorageConfig $storageConfig
  * @return int
  */
 private function getStorageId(StorageConfig $storageConfig)
 {
     try {
         $class = $storageConfig->getBackend()->getStorageClass();
         /** @var \OC\Files\Storage\Storage $storage */
         $storage = new $class($storageConfig->getBackendOptions());
         // auth mechanism should fire first
         $storage = $storageConfig->getBackend()->wrapStorage($storage);
         $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
         return $storage->getStorageCache()->getNumericId();
     } catch (\Exception $e) {
         return -1;
     }
 }
Beispiel #29
0
 /**
  * Update storage to the configuration
  *
  * @param StorageConfig $updatedStorage storage attributes
  *
  * @return StorageConfig storage config
  * @throws NotFoundException if the given storage does not exist in the config
  */
 public function updateStorage(StorageConfig $updatedStorage)
 {
     $allStorages = $this->readConfig();
     $id = $updatedStorage->getId();
     if (!isset($allStorages[$id])) {
         throw new NotFoundException('Storage with id "' . $id . '" not found');
     }
     $oldStorage = $allStorages[$id];
     // ensure objectstore is persistent
     if ($objectstore = $oldStorage->getBackendOption('objectstore')) {
         $updatedStorage->setBackendOption('objectstore', $objectstore);
     }
     $allStorages[$id] = $updatedStorage;
     $this->writeConfig($allStorages);
     $this->triggerChangeHooks($oldStorage, $updatedStorage);
     return $this->getStorage($id);
 }
 /**
  * @dataProvider getUniqueStoragesProvider
  */
 public function testGetUniqueStorages($priority1, $applicableUsers1, $applicableGroups1, $priority2, $applicableUsers2, $applicableGroups2, $expectedPrecedence)
 {
     $backend = $this->backendService->getBackend('identifier:\\OCA\\Files_External\\Lib\\Backend\\SMB');
     $authMechanism = $this->backendService->getAuthMechanism('identifier:\\Auth\\Mechanism');
     $storage1 = new StorageConfig();
     $storage1->setMountPoint('mountpoint');
     $storage1->setBackend($backend);
     $storage1->setAuthMechanism($authMechanism);
     $storage1->setBackendOptions(['password' => 'testPassword']);
     $storage1->setPriority($priority1);
     $storage1->setApplicableUsers($applicableUsers1);
     $storage1->setApplicableGroups($applicableGroups1);
     $storage1 = $this->globalStoragesService->addStorage($storage1);
     $storage2 = new StorageConfig();
     $storage2->setMountPoint('mountpoint');
     $storage2->setBackend($backend);
     $storage2->setAuthMechanism($authMechanism);
     $storage2->setBackendOptions(['password' => 'testPassword']);
     $storage2->setPriority($priority2);
     $storage2->setApplicableUsers($applicableUsers2);
     $storage2->setApplicableGroups($applicableGroups2);
     $storage2 = $this->globalStoragesService->addStorage($storage2);
     $storages = $this->service->getUniqueStorages();
     $this->assertCount(1, $storages);
     if ($expectedPrecedence === 1) {
         $this->assertArrayHasKey($storage1->getID(), $storages);
     } elseif ($expectedPrecedence === 2) {
         $this->assertArrayHasKey($storage2->getID(), $storages);
     }
 }