diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-10-31 11:41:07 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2015-03-12 18:51:02 +0100 |
commit | ce94a998dd5a5801beef7874dd13752095e35de0 (patch) | |
tree | 8d91631f709549c40555dcb74e9976519f895ae2 /apps/files_external/lib | |
parent | 23cc3cc5f2f42166c37fbe03fa62d3dd1dbfe5ed (diff) | |
download | nextcloud-server-ce94a998dd5a5801beef7874dd13752095e35de0.tar.gz nextcloud-server-ce94a998dd5a5801beef7874dd13752095e35de0.zip |
Use storage id + appframework for ext storage CRUD
- Added StorageConfig class to replace ugly arrays
- Implemented StorageService and StorageController for Global and User
storages
- Async status checking for storages (from Xenopathic)
- Auto-generate id for external storage configs (not the same as
storage_id)
- Refactor JS classes for external storage settings, this mostly
moves/encapsulated existing global event handlers into the
MountConfigListView class.
- Added some JS unit tests for the external storage UI
Diffstat (limited to 'apps/files_external/lib')
-rw-r--r-- | apps/files_external/lib/config.php | 51 | ||||
-rw-r--r-- | apps/files_external/lib/notfoundexception.php | 15 | ||||
-rw-r--r-- | apps/files_external/lib/storageconfig.php | 243 |
3 files changed, 284 insertions, 25 deletions
diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index ddfab439879..deeedb98551 100644 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -32,6 +32,10 @@ class OC_Mount_Config { const MOUNT_TYPE_USER = 'user'; const MOUNT_TYPE_PERSONAL = 'personal'; + // getBackendStatus return types + const STATUS_SUCCESS = 0; + const STATUS_ERROR = 1; + // whether to skip backend test (for unit tests, as this static class is not mockable) public static $skipTest = false; @@ -143,15 +147,9 @@ class OC_Mount_Config { $mountPoints = array(); $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data"); - $mount_file = \OC_Config::getValue("mount_file", $datadir . "/mount.json"); $backends = self::getBackends(); - //move config file to it's new position - if (is_file(\OC::$SERVERROOT . '/config/mount.json')) { - rename(\OC::$SERVERROOT . '/config/mount.json', $mount_file); - } - // Load system mount points $mountConfig = self::readData(); @@ -349,6 +347,8 @@ class OC_Mount_Config { $mountPoint = substr($mountPoint, 13); $config = array( + 'id' => (int) $mount['id'], + 'storage_id' => (int) $mount['storage_id'], 'class' => $mount['class'], 'mountpoint' => $mountPoint, 'backend' => $backends[$mount['class']]['backend'], @@ -383,6 +383,8 @@ class OC_Mount_Config { // Remove '/$user/files/' from mount point $mountPoint = substr($mountPoint, 13); $config = array( + 'id' => (int) $mount['id'], + 'storage_id' => (int) $mount['storage_id'], 'class' => $mount['class'], 'mountpoint' => $mountPoint, 'backend' => $backends[$mount['class']]['backend'], @@ -425,6 +427,8 @@ class OC_Mount_Config { } $mount['options'] = self::decryptPasswords($mount['options']); $personal[] = array( + 'id' => (int) $mount['id'], + 'storage_id' => (int) $mount['storage_id'], 'class' => $mount['class'], // Remove '/uid/files/' from mount point 'mountpoint' => substr($mountPoint, strlen($uid) + 8), @@ -442,11 +446,11 @@ class OC_Mount_Config { * * @param string $class backend class name * @param array $options backend configuration options - * @return bool true if the connection succeeded, false otherwise + * @return int see self::STATUS_* */ - private static function getBackendStatus($class, $options, $isPersonal) { + public static function getBackendStatus($class, $options, $isPersonal) { if (self::$skipTest) { - return true; + return self::STATUS_SUCCESS; } foreach ($options as &$option) { $option = self::setUserVars(OCP\User::getUser(), $option); @@ -454,13 +458,14 @@ class OC_Mount_Config { if (class_exists($class)) { try { $storage = new $class($options); - return $storage->test($isPersonal); + if ($storage->test($isPersonal)) { + return self::STATUS_SUCCESS; + } } catch (Exception $exception) { \OCP\Util::logException('files_external', $exception); - return false; } } - return false; + return self::STATUS_ERROR; } /** @@ -474,6 +479,8 @@ class OC_Mount_Config { * @param bool $isPersonal Personal or system mount point i.e. is this being called from the personal or admin page * @param int|null $priority Mount point priority, null for default * @return boolean + * + * @deprecated use StoragesService#addStorage() instead */ public static function addMountPoint($mountPoint, $class, @@ -537,7 +544,7 @@ class OC_Mount_Config { self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints); $result = self::getBackendStatus($class, $classOptions, $isPersonal); - if ($result && $isNew) { + if ($result === self::STATUS_SUCCESS && $isNew) { \OC_Hook::emit( \OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create_mount, @@ -558,6 +565,8 @@ class OC_Mount_Config { * @param string $applicable User or group to remove mount from * @param bool $isPersonal Personal or system mount point * @return bool + * + * @deprecated use StoragesService#removeStorage() instead */ public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) { // Verify that the mount point applies for the current user @@ -622,13 +631,10 @@ class OC_Mount_Config { * @param string|null $user If not null, personal for $user, otherwise system * @return array */ - private static function readData($user = null) { - $parser = new \OC\ArrayParser(); + public static function readData($user = null) { if (isset($user)) { - $phpFile = OC_User::getHome($user) . '/mount.php'; $jsonFile = OC_User::getHome($user) . '/mount.json'; } else { - $phpFile = OC::$SERVERROOT . '/config/mount.php'; $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/'); $jsonFile = \OC_Config::getValue('mount_file', $datadir . '/mount.json'); } @@ -637,11 +643,6 @@ class OC_Mount_Config { if (is_array($mountPoints)) { return $mountPoints; } - } elseif (is_file($phpFile)) { - $mountPoints = $parser->parsePHP(file_get_contents($phpFile)); - if (is_array($mountPoints)) { - return $mountPoints; - } } return array(); } @@ -652,7 +653,7 @@ class OC_Mount_Config { * @param string|null $user If not null, personal for $user, otherwise system * @param array $data Mount points */ - private static function writeData($user, $data) { + public static function writeData($user, $data) { if (isset($user)) { $file = OC_User::getHome($user) . '/mount.json'; } else { @@ -769,7 +770,7 @@ class OC_Mount_Config { * @param array $options mount options * @return array updated options */ - private static function encryptPasswords($options) { + public static function encryptPasswords($options) { if (isset($options['password'])) { $options['password_encrypted'] = self::encryptPassword($options['password']); // do not unset the password, we want to keep the keys order @@ -785,7 +786,7 @@ class OC_Mount_Config { * @param array $options mount options * @return array updated options */ - private static function decryptPasswords($options) { + public static function decryptPasswords($options) { // note: legacy options might still have the unencrypted password in the "password" field if (isset($options['password_encrypted'])) { $options['password'] = self::decryptPassword($options['password_encrypted']); diff --git a/apps/files_external/lib/notfoundexception.php b/apps/files_external/lib/notfoundexception.php new file mode 100644 index 00000000000..d1d15309d5b --- /dev/null +++ b/apps/files_external/lib/notfoundexception.php @@ -0,0 +1,15 @@ +<?php +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_external; + +/** + * Storage is not found + */ +class NotFoundException extends \Exception { +} diff --git a/apps/files_external/lib/storageconfig.php b/apps/files_external/lib/storageconfig.php new file mode 100644 index 00000000000..f23b5cd86a9 --- /dev/null +++ b/apps/files_external/lib/storageconfig.php @@ -0,0 +1,243 @@ +<?php +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_external\Lib; + +/** + * External storage configuration + */ +class StorageConfig implements \JsonSerializable { + + /** + * @var int + */ + private $id; + + /** + * @var string + */ + private $backendClass; + + /** + * @var array + */ + private $backendOptions = []; + + /** + * @var string + */ + private $mountPoint; + + /** + * @var int + */ + private $status; + + /** + * @var int + */ + private $priority; + + /** + * @var array + */ + private $applicableUsers = []; + + /** + * @var array + */ + private $applicableGroups = []; + + /** + * @param int|null $id config id or null for a new config + */ + public function __construct($id = null) { + $this->id = $id; + } + + /** + * Returns the configuration id + * + * @return int + */ + public function getId() { + return $this->id; + } + + /** + * Sets the configuration id + * + * @param int configuration id + */ + public function setId($id) { + $this->id = $id; + } + + /** + * Returns mount point path relative to the user's + * "files" folder. + * + * @return string path + */ + public function getMountPoint() { + return $this->mountPoint; + } + + /** + * Sets mount point path relative to the user's + * "files" folder. + * The path will be normalized. + * + * @param string path + */ + public function setMountPoint($mountPoint) { + $this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint); + } + + /** + * Returns the external storage backend class name + * + * @return string external storage backend class name + */ + public function getBackendClass() { + return $this->backendClass; + } + + /** + * Sets the external storage backend class name + * + * @param string external storage backend class name + */ + public function setBackendClass($backendClass) { + $this->backendClass = $backendClass; + } + + /** + * Returns the external storage backend-specific options + * + * @return array backend options + */ + public function getBackendOptions() { + return $this->backendOptions; + } + + /** + * Sets the external storage backend-specific options + * + * @param array backend options + */ + public function setBackendOptions($backendOptions) { + $this->backendOptions = $backendOptions; + } + + /** + * Returns the mount priority + * + * @return int priority + */ + public function getPriority() { + return $this->priority; + } + + /** + * Sets the mount priotity + * + * @param int priority + */ + public function setPriority($priority) { + $this->priority = $priority; + } + + /** + * Returns the users for which to mount this storage + * + * @return array applicable users + */ + public function getApplicableUsers() { + return $this->applicableUsers; + } + + /** + * Sets the users for which to mount this storage + * + * @param array applicable users + */ + public function setApplicableUsers($applicableUsers) { + if (is_null($applicableUsers)) { + $applicableUsers = []; + } + $this->applicableUsers = $applicableUsers; + } + + /** + * Returns the groups for which to mount this storage + * + * @return array applicable groups + */ + public function getApplicableGroups() { + return $this->applicableGroups; + } + + /** + * Sets the groups for which to mount this storage + * + * @param array applicable groups + */ + public function setApplicableGroups($applicableGroups) { + if (is_null($applicableGroups)) { + $applicableGroups = []; + } + $this->applicableGroups = $applicableGroups; + } + + /** + * Sets the storage status, whether the config worked last time + * + * @return int $status status + */ + public function getStatus() { + return $this->status; + } + + /** + * Sets the storage status, whether the config worked last time + * + * @param int $status status + */ + public function setStatus($status) { + $this->status = $status; + } + + /** + * Serialize config to JSON + * + * @return array + */ + public function jsonSerialize() { + $result = []; + if (!is_null($this->id)) { + $result['id'] = $this->id; + } + $result['mountPoint'] = $this->mountPoint; + $result['backendClass'] = $this->backendClass; + $result['backendOptions'] = $this->backendOptions; + if (!is_null($this->priority)) { + $result['priority'] = $this->priority; + } + if (!empty($this->applicableUsers)) { + $result['applicableUsers'] = $this->applicableUsers; + } + if (!empty($this->applicableGroups)) { + $result['applicableGroups'] = $this->applicableGroups; + } + if (!is_null($this->status)) { + $result['status'] = $this->status; + } + return $result; + } +} |