/** * 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; }
/** * 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; }
/** * Add new storage to the configuration * * @param StorageConfig $newStorage storage attributes * * @return StorageConfig storage config, with added id */ public function addStorage(StorageConfig $newStorage) { $allStorages = $this->readConfig(); $configId = $this->dbConfig->addMount($newStorage->getMountPoint(), $newStorage->getBackend()->getIdentifier(), $newStorage->getAuthMechanism()->getIdentifier(), $newStorage->getPriority(), $this->getType()); $newStorage->setId($configId); foreach ($newStorage->getApplicableUsers() as $user) { $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); } foreach ($newStorage->getApplicableGroups() as $group) { $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); } foreach ($newStorage->getBackendOptions() as $key => $value) { $this->dbConfig->setConfig($configId, $key, $value); } foreach ($newStorage->getMountOptions() as $key => $value) { $this->dbConfig->setOption($configId, $key, $value); } if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); } // add new storage $allStorages[$configId] = $newStorage; $this->triggerHooks($newStorage, Filesystem::signal_create_mount); $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); return $newStorage; }
/** * Validate storage * FIXME: De-duplicate with StoragesController::validate() * * @param StorageConfig $storage * @return bool */ protected function validateStorage(StorageConfig $storage) { /** @var Backend */ $backend = $storage->getBackend(); /** @var AuthMechanism */ $authMechanism = $storage->getAuthMechanism(); if (!$backend->isVisibleFor($this->getVisibilityType())) { // not permitted to use backend return false; } if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { // not permitted to use auth mechanism return false; } return true; }
/** * 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; } }
/** * Validate storage config * * @param StorageConfig $storage storage config * * @return DataResponse|null returns response in case of validation error */ protected function validate(StorageConfig $storage) { $mountPoint = $storage->getMountPoint(); if ($mountPoint === '' || $mountPoint === '/') { return new DataResponse(array('message' => (string) $this->l10n->t('Invalid mount point')), Http::STATUS_UNPROCESSABLE_ENTITY); } if ($storage->getBackendOption('objectstore')) { // objectstore must not be sent from client side return new DataResponse(array('message' => (string) $this->l10n->t('Objectstore forbidden')), Http::STATUS_UNPROCESSABLE_ENTITY); } /** @var Backend */ $backend = $storage->getBackend(); /** @var AuthMechanism */ $authMechanism = $storage->getAuthMechanism(); if ($backend->checkDependencies()) { // invalid backend return new DataResponse(array('message' => (string) $this->l10n->t('Invalid storage backend "%s"', [$backend->getIdentifier()])), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$backend->isVisibleFor($this->service->getVisibilityType())) { // not permitted to use backend return new DataResponse(array('message' => (string) $this->l10n->t('Not permitted to use backend "%s"', [$backend->getIdentifier()])), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) { // not permitted to use auth mechanism return new DataResponse(array('message' => (string) $this->l10n->t('Not permitted to use authentication mechanism "%s"', [$authMechanism->getIdentifier()])), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$backend->validateStorage($storage)) { // unsatisfied parameters return new DataResponse(array('message' => (string) $this->l10n->t('Unsatisfied backend parameters')), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$authMechanism->validateStorage($storage)) { // unsatisfied parameters return new DataResponse(['message' => (string) $this->l10n->t('Unsatisfied authentication mechanism parameters')], Http::STATUS_UNPROCESSABLE_ENTITY); } return null; }
/** * Check if parameters are satisfied in a StorageConfig * * @param StorageConfig $storage * @return bool */ public function validateStorage(StorageConfig $storage) { // does the backend actually support this scheme $supportedSchemes = $storage->getBackend()->getAuthSchemes(); if (!isset($supportedSchemes[$this->getScheme()])) { return false; } return $this->validateStorageDefinition($storage); }
/** * 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()]; $mountEntry['id'] = $storage->getId(); // $mountEntry['storage_id'] = null; // we don't store this! return $mountEntry; }
/** * Validate storage config * * @param StorageConfig $storage storage config * * @return DataResponse|null returns response in case of validation error */ protected function validate(StorageConfig $storage) { $mountPoint = $storage->getMountPoint(); if ($mountPoint === '' || $mountPoint === '/') { return new DataResponse(array('message' => (string) $this->l10n->t('Invalid mount point')), Http::STATUS_UNPROCESSABLE_ENTITY); } /** @var Backend */ $backend = $storage->getBackend(); /** @var AuthMechanism */ $authMechanism = $storage->getAuthMechanism(); if (!$backend || $backend->checkDependencies()) { // invalid backend return new DataResponse(array('message' => (string) $this->l10n->t('Invalid storage backend "%s"', [$storage->getBackend()->getIdentifier()])), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$backend->validateStorage($storage)) { // unsatisfied parameters return new DataResponse(array('message' => (string) $this->l10n->t('Unsatisfied backend parameters')), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$authMechanism->validateStorage($storage)) { // unsatisfied parameters return new DataResponse(['message' => (string) $this->l10n->t('Unsatisfied authentication mechanism parameters')], Http::STATUS_UNPROCESSABLE_ENTITY); } return null; }