Passed
Push — master ( fd284f...fa2878 )
by Morris
13:09 queued 12s
created
lib/private/Files/Cache/Storage.php 1 patch
Indentation   +191 added lines, -191 removed lines patch added patch discarded remove patch
@@ -45,218 +45,218 @@
 block discarded – undo
45 45
  * @package OC\Files\Cache
46 46
  */
47 47
 class Storage {
48
-	/** @var StorageGlobal|null */
49
-	private static $globalCache = null;
50
-	private $storageId;
51
-	private $numericId;
48
+    /** @var StorageGlobal|null */
49
+    private static $globalCache = null;
50
+    private $storageId;
51
+    private $numericId;
52 52
 
53
-	/**
54
-	 * @return StorageGlobal
55
-	 */
56
-	public static function getGlobalCache() {
57
-		if (is_null(self::$globalCache)) {
58
-			self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
59
-		}
60
-		return self::$globalCache;
61
-	}
53
+    /**
54
+     * @return StorageGlobal
55
+     */
56
+    public static function getGlobalCache() {
57
+        if (is_null(self::$globalCache)) {
58
+            self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
59
+        }
60
+        return self::$globalCache;
61
+    }
62 62
 
63
-	/**
64
-	 * @param \OC\Files\Storage\Storage|string $storage
65
-	 * @param bool $isAvailable
66
-	 * @throws \RuntimeException
67
-	 */
68
-	public function __construct($storage, $isAvailable = true) {
69
-		if ($storage instanceof IStorage) {
70
-			$this->storageId = $storage->getId();
71
-		} else {
72
-			$this->storageId = $storage;
73
-		}
74
-		$this->storageId = self::adjustStorageId($this->storageId);
63
+    /**
64
+     * @param \OC\Files\Storage\Storage|string $storage
65
+     * @param bool $isAvailable
66
+     * @throws \RuntimeException
67
+     */
68
+    public function __construct($storage, $isAvailable = true) {
69
+        if ($storage instanceof IStorage) {
70
+            $this->storageId = $storage->getId();
71
+        } else {
72
+            $this->storageId = $storage;
73
+        }
74
+        $this->storageId = self::adjustStorageId($this->storageId);
75 75
 
76
-		if ($row = self::getStorageById($this->storageId)) {
77
-			$this->numericId = (int)$row['numeric_id'];
78
-		} else {
79
-			$connection = \OC::$server->getDatabaseConnection();
80
-			$available = $isAvailable ? 1 : 0;
81
-			if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
82
-				$this->numericId = $connection->lastInsertId('*PREFIX*storages');
83
-			} else {
84
-				if ($row = self::getStorageById($this->storageId)) {
85
-					$this->numericId = (int)$row['numeric_id'];
86
-				} else {
87
-					throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
88
-				}
89
-			}
90
-		}
91
-	}
76
+        if ($row = self::getStorageById($this->storageId)) {
77
+            $this->numericId = (int)$row['numeric_id'];
78
+        } else {
79
+            $connection = \OC::$server->getDatabaseConnection();
80
+            $available = $isAvailable ? 1 : 0;
81
+            if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
82
+                $this->numericId = $connection->lastInsertId('*PREFIX*storages');
83
+            } else {
84
+                if ($row = self::getStorageById($this->storageId)) {
85
+                    $this->numericId = (int)$row['numeric_id'];
86
+                } else {
87
+                    throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
88
+                }
89
+            }
90
+        }
91
+    }
92 92
 
93
-	/**
94
-	 * @param string $storageId
95
-	 * @return array
96
-	 */
97
-	public static function getStorageById($storageId) {
98
-		return self::getGlobalCache()->getStorageInfo($storageId);
99
-	}
93
+    /**
94
+     * @param string $storageId
95
+     * @return array
96
+     */
97
+    public static function getStorageById($storageId) {
98
+        return self::getGlobalCache()->getStorageInfo($storageId);
99
+    }
100 100
 
101
-	/**
102
-	 * Adjusts the storage id to use md5 if too long
103
-	 * @param string $storageId storage id
104
-	 * @return string unchanged $storageId if its length is less than 64 characters,
105
-	 * else returns the md5 of $storageId
106
-	 */
107
-	public static function adjustStorageId($storageId) {
108
-		if (strlen($storageId) > 64) {
109
-			return md5($storageId);
110
-		}
111
-		return $storageId;
112
-	}
101
+    /**
102
+     * Adjusts the storage id to use md5 if too long
103
+     * @param string $storageId storage id
104
+     * @return string unchanged $storageId if its length is less than 64 characters,
105
+     * else returns the md5 of $storageId
106
+     */
107
+    public static function adjustStorageId($storageId) {
108
+        if (strlen($storageId) > 64) {
109
+            return md5($storageId);
110
+        }
111
+        return $storageId;
112
+    }
113 113
 
114
-	/**
115
-	 * Get the numeric id for the storage
116
-	 *
117
-	 * @return int
118
-	 */
119
-	public function getNumericId() {
120
-		return $this->numericId;
121
-	}
114
+    /**
115
+     * Get the numeric id for the storage
116
+     *
117
+     * @return int
118
+     */
119
+    public function getNumericId() {
120
+        return $this->numericId;
121
+    }
122 122
 
123
-	/**
124
-	 * Get the string id for the storage
125
-	 *
126
-	 * @param int $numericId
127
-	 * @return string|null either the storage id string or null if the numeric id is not known
128
-	 */
129
-	public static function getStorageId($numericId) {
130
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
131
-		$query->select('id')
132
-			->from('storages')
133
-			->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
134
-		$result = $query->execute();
135
-		$row = $result->fetch();
136
-		$result->closeCursor();
137
-		if ($row) {
138
-			return $row['id'];
139
-		} else {
140
-			return null;
141
-		}
142
-	}
123
+    /**
124
+     * Get the string id for the storage
125
+     *
126
+     * @param int $numericId
127
+     * @return string|null either the storage id string or null if the numeric id is not known
128
+     */
129
+    public static function getStorageId($numericId) {
130
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
131
+        $query->select('id')
132
+            ->from('storages')
133
+            ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
134
+        $result = $query->execute();
135
+        $row = $result->fetch();
136
+        $result->closeCursor();
137
+        if ($row) {
138
+            return $row['id'];
139
+        } else {
140
+            return null;
141
+        }
142
+    }
143 143
 
144
-	/**
145
-	 * Get the numeric of the storage with the provided string id
146
-	 *
147
-	 * @param $storageId
148
-	 * @return int|null either the numeric storage id or null if the storage id is not knwon
149
-	 */
150
-	public static function getNumericStorageId($storageId) {
151
-		$storageId = self::adjustStorageId($storageId);
144
+    /**
145
+     * Get the numeric of the storage with the provided string id
146
+     *
147
+     * @param $storageId
148
+     * @return int|null either the numeric storage id or null if the storage id is not knwon
149
+     */
150
+    public static function getNumericStorageId($storageId) {
151
+        $storageId = self::adjustStorageId($storageId);
152 152
 
153
-		if ($row = self::getStorageById($storageId)) {
154
-			return (int)$row['numeric_id'];
155
-		} else {
156
-			return null;
157
-		}
158
-	}
153
+        if ($row = self::getStorageById($storageId)) {
154
+            return (int)$row['numeric_id'];
155
+        } else {
156
+            return null;
157
+        }
158
+    }
159 159
 
160
-	/**
161
-	 * @return array|null [ available, last_checked ]
162
-	 */
163
-	public function getAvailability() {
164
-		if ($row = self::getStorageById($this->storageId)) {
165
-			return [
166
-				'available' => (int)$row['available'] === 1,
167
-				'last_checked' => $row['last_checked']
168
-			];
169
-		} else {
170
-			return null;
171
-		}
172
-	}
160
+    /**
161
+     * @return array|null [ available, last_checked ]
162
+     */
163
+    public function getAvailability() {
164
+        if ($row = self::getStorageById($this->storageId)) {
165
+            return [
166
+                'available' => (int)$row['available'] === 1,
167
+                'last_checked' => $row['last_checked']
168
+            ];
169
+        } else {
170
+            return null;
171
+        }
172
+    }
173 173
 
174
-	/**
175
-	 * @param bool $isAvailable
176
-	 * @param int $delay amount of seconds to delay reconsidering that storage further
177
-	 */
178
-	public function setAvailability($isAvailable, int $delay = 0) {
179
-		$available = $isAvailable ? 1 : 0;
180
-		if (!$isAvailable) {
181
-			\OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
182
-		}
174
+    /**
175
+     * @param bool $isAvailable
176
+     * @param int $delay amount of seconds to delay reconsidering that storage further
177
+     */
178
+    public function setAvailability($isAvailable, int $delay = 0) {
179
+        $available = $isAvailable ? 1 : 0;
180
+        if (!$isAvailable) {
181
+            \OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
182
+        }
183 183
 
184
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
185
-		$query->update('storages')
186
-			->set('available', $query->createNamedParameter($available))
187
-			->set('last_checked', $query->createNamedParameter(time() + $delay))
188
-			->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
189
-		$query->execute();
190
-	}
184
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
185
+        $query->update('storages')
186
+            ->set('available', $query->createNamedParameter($available))
187
+            ->set('last_checked', $query->createNamedParameter(time() + $delay))
188
+            ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
189
+        $query->execute();
190
+    }
191 191
 
192
-	/**
193
-	 * Check if a string storage id is known
194
-	 *
195
-	 * @param string $storageId
196
-	 * @return bool
197
-	 */
198
-	public static function exists($storageId) {
199
-		return !is_null(self::getNumericStorageId($storageId));
200
-	}
192
+    /**
193
+     * Check if a string storage id is known
194
+     *
195
+     * @param string $storageId
196
+     * @return bool
197
+     */
198
+    public static function exists($storageId) {
199
+        return !is_null(self::getNumericStorageId($storageId));
200
+    }
201 201
 
202
-	/**
203
-	 * remove the entry for the storage
204
-	 *
205
-	 * @param string $storageId
206
-	 */
207
-	public static function remove($storageId) {
208
-		$storageId = self::adjustStorageId($storageId);
209
-		$numericId = self::getNumericStorageId($storageId);
202
+    /**
203
+     * remove the entry for the storage
204
+     *
205
+     * @param string $storageId
206
+     */
207
+    public static function remove($storageId) {
208
+        $storageId = self::adjustStorageId($storageId);
209
+        $numericId = self::getNumericStorageId($storageId);
210 210
 
211
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
212
-		$query->delete('storages')
213
-			->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
214
-		$query->execute();
211
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
212
+        $query->delete('storages')
213
+            ->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
214
+        $query->execute();
215 215
 
216
-		if (!is_null($numericId)) {
217
-			$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
218
-			$query->delete('filecache')
219
-				->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
220
-			$query->execute();
221
-		}
222
-	}
216
+        if (!is_null($numericId)) {
217
+            $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
218
+            $query->delete('filecache')
219
+                ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
220
+            $query->execute();
221
+        }
222
+    }
223 223
 
224
-	/**
225
-	 * remove the entry for the storage by the mount id
226
-	 *
227
-	 * @param int $mountId
228
-	 */
229
-	public static function cleanByMountId(int $mountId) {
230
-		$db = \OC::$server->getDatabaseConnection();
224
+    /**
225
+     * remove the entry for the storage by the mount id
226
+     *
227
+     * @param int $mountId
228
+     */
229
+    public static function cleanByMountId(int $mountId) {
230
+        $db = \OC::$server->getDatabaseConnection();
231 231
 
232
-		try {
233
-			$db->beginTransaction();
232
+        try {
233
+            $db->beginTransaction();
234 234
 
235
-			$query = $db->getQueryBuilder();
236
-			$query->select('storage_id')
237
-				->from('mounts')
238
-				->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
239
-			$storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
235
+            $query = $db->getQueryBuilder();
236
+            $query->select('storage_id')
237
+                ->from('mounts')
238
+                ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
239
+            $storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
240 240
 
241
-			$query = $db->getQueryBuilder();
242
-			$query->delete('filecache')
243
-				->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
244
-			$query->executeStatement();
241
+            $query = $db->getQueryBuilder();
242
+            $query->delete('filecache')
243
+                ->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
244
+            $query->executeStatement();
245 245
 
246
-			$query = $db->getQueryBuilder();
247
-			$query->delete('storages')
248
-				->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
249
-			$query->executeStatement();
246
+            $query = $db->getQueryBuilder();
247
+            $query->delete('storages')
248
+                ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
249
+            $query->executeStatement();
250 250
 
251
-			$query = $db->getQueryBuilder();
252
-			$query->delete('mounts')
253
-				->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
254
-			$query->executeStatement();
251
+            $query = $db->getQueryBuilder();
252
+            $query->delete('mounts')
253
+                ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
254
+            $query->executeStatement();
255 255
 
256
-			$db->commit();
257
-		} catch (\Exception $e) {
258
-			$db->rollBack();
259
-			throw $e;
260
-		}
261
-	}
256
+            $db->commit();
257
+        } catch (\Exception $e) {
258
+            $db->rollBack();
259
+            throw $e;
260
+        }
261
+    }
262 262
 }
Please login to merge, or discard this patch.
apps/files_external/lib/Service/StoragesService.php 1 patch
Indentation   +454 added lines, -454 removed lines patch added patch discarded remove patch
@@ -49,458 +49,458 @@
 block discarded – undo
49 49
  */
50 50
 abstract class StoragesService {
51 51
 
52
-	/** @var BackendService */
53
-	protected $backendService;
54
-
55
-	/**
56
-	 * @var DBConfigService
57
-	 */
58
-	protected $dbConfig;
59
-
60
-	/**
61
-	 * @var IUserMountCache
62
-	 */
63
-	protected $userMountCache;
64
-
65
-	/**
66
-	 * @param BackendService $backendService
67
-	 * @param DBConfigService $dbConfigService
68
-	 * @param IUserMountCache $userMountCache
69
-	 */
70
-	public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) {
71
-		$this->backendService = $backendService;
72
-		$this->dbConfig = $dbConfigService;
73
-		$this->userMountCache = $userMountCache;
74
-	}
75
-
76
-	protected function readDBConfig() {
77
-		return $this->dbConfig->getAdminMounts();
78
-	}
79
-
80
-	protected function getStorageConfigFromDBMount(array $mount) {
81
-		$applicableUsers = array_filter($mount['applicable'], function ($applicable) {
82
-			return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER;
83
-		});
84
-		$applicableUsers = array_map(function ($applicable) {
85
-			return $applicable['value'];
86
-		}, $applicableUsers);
87
-
88
-		$applicableGroups = array_filter($mount['applicable'], function ($applicable) {
89
-			return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP;
90
-		});
91
-		$applicableGroups = array_map(function ($applicable) {
92
-			return $applicable['value'];
93
-		}, $applicableGroups);
94
-
95
-		try {
96
-			$config = $this->createStorage(
97
-				$mount['mount_point'],
98
-				$mount['storage_backend'],
99
-				$mount['auth_backend'],
100
-				$mount['config'],
101
-				$mount['options'],
102
-				array_values($applicableUsers),
103
-				array_values($applicableGroups),
104
-				$mount['priority']
105
-			);
106
-			$config->setType($mount['type']);
107
-			$config->setId((int)$mount['mount_id']);
108
-			return $config;
109
-		} catch (\UnexpectedValueException $e) {
110
-			// don't die if a storage backend doesn't exist
111
-			\OC::$server->getLogger()->logException($e, [
112
-				'message' => 'Could not load storage.',
113
-				'level' => ILogger::ERROR,
114
-				'app' => 'files_external',
115
-			]);
116
-			return null;
117
-		} catch (\InvalidArgumentException $e) {
118
-			\OC::$server->getLogger()->logException($e, [
119
-				'message' => 'Could not load storage.',
120
-				'level' => ILogger::ERROR,
121
-				'app' => 'files_external',
122
-			]);
123
-			return null;
124
-		}
125
-	}
126
-
127
-	/**
128
-	 * Read the external storages config
129
-	 *
130
-	 * @return array map of storage id to storage config
131
-	 */
132
-	protected function readConfig() {
133
-		$mounts = $this->readDBConfig();
134
-		$configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
135
-		$configs = array_filter($configs, function ($config) {
136
-			return $config instanceof StorageConfig;
137
-		});
138
-
139
-		$keys = array_map(function (StorageConfig $config) {
140
-			return $config->getId();
141
-		}, $configs);
142
-
143
-		return array_combine($keys, $configs);
144
-	}
145
-
146
-	/**
147
-	 * Get a storage with status
148
-	 *
149
-	 * @param int $id storage id
150
-	 *
151
-	 * @return StorageConfig
152
-	 * @throws NotFoundException if the storage with the given id was not found
153
-	 */
154
-	public function getStorage($id) {
155
-		$mount = $this->dbConfig->getMountById($id);
156
-
157
-		if (!is_array($mount)) {
158
-			throw new NotFoundException('Storage with ID "' . $id . '" not found');
159
-		}
160
-
161
-		$config = $this->getStorageConfigFromDBMount($mount);
162
-		if ($this->isApplicable($config)) {
163
-			return $config;
164
-		} else {
165
-			throw new NotFoundException('Storage with ID "' . $id . '" not found');
166
-		}
167
-	}
168
-
169
-	/**
170
-	 * Check whether this storage service should provide access to a storage
171
-	 *
172
-	 * @param StorageConfig $config
173
-	 * @return bool
174
-	 */
175
-	abstract protected function isApplicable(StorageConfig $config);
176
-
177
-	/**
178
-	 * Gets all storages, valid or not
179
-	 *
180
-	 * @return StorageConfig[] array of storage configs
181
-	 */
182
-	public function getAllStorages() {
183
-		return $this->readConfig();
184
-	}
185
-
186
-	/**
187
-	 * Gets all valid storages
188
-	 *
189
-	 * @return StorageConfig[]
190
-	 */
191
-	public function getStorages() {
192
-		return array_filter($this->getAllStorages(), [$this, 'validateStorage']);
193
-	}
194
-
195
-	/**
196
-	 * Validate storage
197
-	 * FIXME: De-duplicate with StoragesController::validate()
198
-	 *
199
-	 * @param StorageConfig $storage
200
-	 * @return bool
201
-	 */
202
-	protected function validateStorage(StorageConfig $storage) {
203
-		/** @var Backend */
204
-		$backend = $storage->getBackend();
205
-		/** @var AuthMechanism */
206
-		$authMechanism = $storage->getAuthMechanism();
207
-
208
-		if (!$backend->isVisibleFor($this->getVisibilityType())) {
209
-			// not permitted to use backend
210
-			return false;
211
-		}
212
-		if (!$authMechanism->isVisibleFor($this->getVisibilityType())) {
213
-			// not permitted to use auth mechanism
214
-			return false;
215
-		}
216
-
217
-		return true;
218
-	}
219
-
220
-	/**
221
-	 * Get the visibility type for this controller, used in validation
222
-	 *
223
-	 * @return string BackendService::VISIBILITY_* constants
224
-	 */
225
-	abstract public function getVisibilityType();
226
-
227
-	/**
228
-	 * @return integer
229
-	 */
230
-	protected function getType() {
231
-		return DBConfigService::MOUNT_TYPE_ADMIN;
232
-	}
233
-
234
-	/**
235
-	 * Add new storage to the configuration
236
-	 *
237
-	 * @param StorageConfig $newStorage storage attributes
238
-	 *
239
-	 * @return StorageConfig storage config, with added id
240
-	 */
241
-	public function addStorage(StorageConfig $newStorage) {
242
-		$allStorages = $this->readConfig();
243
-
244
-		$configId = $this->dbConfig->addMount(
245
-			$newStorage->getMountPoint(),
246
-			$newStorage->getBackend()->getIdentifier(),
247
-			$newStorage->getAuthMechanism()->getIdentifier(),
248
-			$newStorage->getPriority(),
249
-			$this->getType()
250
-		);
251
-
252
-		$newStorage->setId($configId);
253
-
254
-		foreach ($newStorage->getApplicableUsers() as $user) {
255
-			$this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user);
256
-		}
257
-		foreach ($newStorage->getApplicableGroups() as $group) {
258
-			$this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
259
-		}
260
-		foreach ($newStorage->getBackendOptions() as $key => $value) {
261
-			$this->dbConfig->setConfig($configId, $key, $value);
262
-		}
263
-		foreach ($newStorage->getMountOptions() as $key => $value) {
264
-			$this->dbConfig->setOption($configId, $key, $value);
265
-		}
266
-
267
-		if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) {
268
-			$this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
269
-		}
270
-
271
-		// add new storage
272
-		$allStorages[$configId] = $newStorage;
273
-
274
-		$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
275
-
276
-		$newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS);
277
-		return $newStorage;
278
-	}
279
-
280
-	/**
281
-	 * Create a storage from its parameters
282
-	 *
283
-	 * @param string $mountPoint storage mount point
284
-	 * @param string $backendIdentifier backend identifier
285
-	 * @param string $authMechanismIdentifier authentication mechanism identifier
286
-	 * @param array $backendOptions backend-specific options
287
-	 * @param array|null $mountOptions mount-specific options
288
-	 * @param array|null $applicableUsers users for which to mount the storage
289
-	 * @param array|null $applicableGroups groups for which to mount the storage
290
-	 * @param int|null $priority priority
291
-	 *
292
-	 * @return StorageConfig
293
-	 */
294
-	public function createStorage(
295
-		$mountPoint,
296
-		$backendIdentifier,
297
-		$authMechanismIdentifier,
298
-		$backendOptions,
299
-		$mountOptions = null,
300
-		$applicableUsers = null,
301
-		$applicableGroups = null,
302
-		$priority = null
303
-	) {
304
-		$backend = $this->backendService->getBackend($backendIdentifier);
305
-		if (!$backend) {
306
-			$backend = new InvalidBackend($backendIdentifier);
307
-		}
308
-		$authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier);
309
-		if (!$authMechanism) {
310
-			$authMechanism = new InvalidAuth($authMechanismIdentifier);
311
-		}
312
-		$newStorage = new StorageConfig();
313
-		$newStorage->setMountPoint($mountPoint);
314
-		$newStorage->setBackend($backend);
315
-		$newStorage->setAuthMechanism($authMechanism);
316
-		$newStorage->setBackendOptions($backendOptions);
317
-		if (isset($mountOptions)) {
318
-			$newStorage->setMountOptions($mountOptions);
319
-		}
320
-		if (isset($applicableUsers)) {
321
-			$newStorage->setApplicableUsers($applicableUsers);
322
-		}
323
-		if (isset($applicableGroups)) {
324
-			$newStorage->setApplicableGroups($applicableGroups);
325
-		}
326
-		if (isset($priority)) {
327
-			$newStorage->setPriority($priority);
328
-		}
329
-
330
-		return $newStorage;
331
-	}
332
-
333
-	/**
334
-	 * Triggers the given hook signal for all the applicables given
335
-	 *
336
-	 * @param string $signal signal
337
-	 * @param string $mountPoint hook mount pount param
338
-	 * @param string $mountType hook mount type param
339
-	 * @param array $applicableArray array of applicable users/groups for which to trigger the hook
340
-	 */
341
-	protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) {
342
-		foreach ($applicableArray as $applicable) {
343
-			\OCP\Util::emitHook(
344
-				Filesystem::CLASSNAME,
345
-				$signal,
346
-				[
347
-					Filesystem::signal_param_path => $mountPoint,
348
-					Filesystem::signal_param_mount_type => $mountType,
349
-					Filesystem::signal_param_users => $applicable,
350
-				]
351
-			);
352
-		}
353
-	}
354
-
355
-	/**
356
-	 * Triggers $signal for all applicable users of the given
357
-	 * storage
358
-	 *
359
-	 * @param StorageConfig $storage storage data
360
-	 * @param string $signal signal to trigger
361
-	 */
362
-	abstract protected function triggerHooks(StorageConfig $storage, $signal);
363
-
364
-	/**
365
-	 * Triggers signal_create_mount or signal_delete_mount to
366
-	 * accommodate for additions/deletions in applicableUsers
367
-	 * and applicableGroups fields.
368
-	 *
369
-	 * @param StorageConfig $oldStorage old storage data
370
-	 * @param StorageConfig $newStorage new storage data
371
-	 */
372
-	abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage);
373
-
374
-	/**
375
-	 * Update storage to the configuration
376
-	 *
377
-	 * @param StorageConfig $updatedStorage storage attributes
378
-	 *
379
-	 * @return StorageConfig storage config
380
-	 * @throws NotFoundException if the given storage does not exist in the config
381
-	 */
382
-	public function updateStorage(StorageConfig $updatedStorage) {
383
-		$id = $updatedStorage->getId();
384
-
385
-		$existingMount = $this->dbConfig->getMountById($id);
386
-
387
-		if (!is_array($existingMount)) {
388
-			throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage');
389
-		}
390
-
391
-		$oldStorage = $this->getStorageConfigFromDBMount($existingMount);
392
-
393
-		if ($oldStorage->getBackend() instanceof InvalidBackend) {
394
-			throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend');
395
-		}
396
-
397
-		$removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers());
398
-		$removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups());
399
-		$addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
400
-		$addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
401
-
402
-		$oldUserCount = count($oldStorage->getApplicableUsers());
403
-		$oldGroupCount = count($oldStorage->getApplicableGroups());
404
-		$newUserCount = count($updatedStorage->getApplicableUsers());
405
-		$newGroupCount = count($updatedStorage->getApplicableGroups());
406
-		$wasGlobal = ($oldUserCount + $oldGroupCount) === 0;
407
-		$isGlobal = ($newUserCount + $newGroupCount) === 0;
408
-
409
-		foreach ($removedUsers as $user) {
410
-			$this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user);
411
-		}
412
-		foreach ($removedGroups as $group) {
413
-			$this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
414
-		}
415
-		foreach ($addedUsers as $user) {
416
-			$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user);
417
-		}
418
-		foreach ($addedGroups as $group) {
419
-			$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
420
-		}
421
-
422
-		if ($wasGlobal && !$isGlobal) {
423
-			$this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
424
-		} elseif (!$wasGlobal && $isGlobal) {
425
-			$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
426
-		}
427
-
428
-		$changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions());
429
-		$changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions());
430
-
431
-		foreach ($changedConfig as $key => $value) {
432
-			if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) {
433
-				$this->dbConfig->setConfig($id, $key, $value);
434
-			}
435
-		}
436
-		foreach ($changedOptions as $key => $value) {
437
-			$this->dbConfig->setOption($id, $key, $value);
438
-		}
439
-
440
-		if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) {
441
-			$this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint());
442
-		}
443
-
444
-		if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) {
445
-			$this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier());
446
-		}
447
-
448
-		$this->triggerChangeHooks($oldStorage, $updatedStorage);
449
-
450
-		if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly
451
-			$this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage));
452
-		} else {
453
-			$storageId = $this->getStorageId($updatedStorage);
454
-			foreach ($removedUsers as $userId) {
455
-				$this->userMountCache->removeUserStorageMount($storageId, $userId);
456
-			}
457
-		}
458
-
459
-		return $this->getStorage($id);
460
-	}
461
-
462
-	/**
463
-	 * Delete the storage with the given id.
464
-	 *
465
-	 * @param int $id storage id
466
-	 *
467
-	 * @throws NotFoundException if no storage was found with the given id
468
-	 */
469
-	public function removeStorage($id) {
470
-		$existingMount = $this->dbConfig->getMountById($id);
471
-
472
-		if (!is_array($existingMount)) {
473
-			throw new NotFoundException('Storage with ID "' . $id . '" not found');
474
-		}
475
-
476
-		$this->dbConfig->removeMount($id);
477
-
478
-		$deletedStorage = $this->getStorageConfigFromDBMount($existingMount);
479
-		$this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount);
480
-
481
-		// delete oc_storages entries and oc_filecache
482
-		\OC\Files\Cache\Storage::cleanByMountId($id);
483
-	}
484
-
485
-	/**
486
-	 * Construct the storage implementation
487
-	 *
488
-	 * @param StorageConfig $storageConfig
489
-	 * @return int
490
-	 */
491
-	private function getStorageId(StorageConfig $storageConfig) {
492
-		try {
493
-			$class = $storageConfig->getBackend()->getStorageClass();
494
-			/** @var \OC\Files\Storage\Storage $storage */
495
-			$storage = new $class($storageConfig->getBackendOptions());
496
-
497
-			// auth mechanism should fire first
498
-			$storage = $storageConfig->getBackend()->wrapStorage($storage);
499
-			$storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
500
-
501
-			return $storage->getStorageCache()->getNumericId();
502
-		} catch (\Exception $e) {
503
-			return -1;
504
-		}
505
-	}
52
+    /** @var BackendService */
53
+    protected $backendService;
54
+
55
+    /**
56
+     * @var DBConfigService
57
+     */
58
+    protected $dbConfig;
59
+
60
+    /**
61
+     * @var IUserMountCache
62
+     */
63
+    protected $userMountCache;
64
+
65
+    /**
66
+     * @param BackendService $backendService
67
+     * @param DBConfigService $dbConfigService
68
+     * @param IUserMountCache $userMountCache
69
+     */
70
+    public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) {
71
+        $this->backendService = $backendService;
72
+        $this->dbConfig = $dbConfigService;
73
+        $this->userMountCache = $userMountCache;
74
+    }
75
+
76
+    protected function readDBConfig() {
77
+        return $this->dbConfig->getAdminMounts();
78
+    }
79
+
80
+    protected function getStorageConfigFromDBMount(array $mount) {
81
+        $applicableUsers = array_filter($mount['applicable'], function ($applicable) {
82
+            return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER;
83
+        });
84
+        $applicableUsers = array_map(function ($applicable) {
85
+            return $applicable['value'];
86
+        }, $applicableUsers);
87
+
88
+        $applicableGroups = array_filter($mount['applicable'], function ($applicable) {
89
+            return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP;
90
+        });
91
+        $applicableGroups = array_map(function ($applicable) {
92
+            return $applicable['value'];
93
+        }, $applicableGroups);
94
+
95
+        try {
96
+            $config = $this->createStorage(
97
+                $mount['mount_point'],
98
+                $mount['storage_backend'],
99
+                $mount['auth_backend'],
100
+                $mount['config'],
101
+                $mount['options'],
102
+                array_values($applicableUsers),
103
+                array_values($applicableGroups),
104
+                $mount['priority']
105
+            );
106
+            $config->setType($mount['type']);
107
+            $config->setId((int)$mount['mount_id']);
108
+            return $config;
109
+        } catch (\UnexpectedValueException $e) {
110
+            // don't die if a storage backend doesn't exist
111
+            \OC::$server->getLogger()->logException($e, [
112
+                'message' => 'Could not load storage.',
113
+                'level' => ILogger::ERROR,
114
+                'app' => 'files_external',
115
+            ]);
116
+            return null;
117
+        } catch (\InvalidArgumentException $e) {
118
+            \OC::$server->getLogger()->logException($e, [
119
+                'message' => 'Could not load storage.',
120
+                'level' => ILogger::ERROR,
121
+                'app' => 'files_external',
122
+            ]);
123
+            return null;
124
+        }
125
+    }
126
+
127
+    /**
128
+     * Read the external storages config
129
+     *
130
+     * @return array map of storage id to storage config
131
+     */
132
+    protected function readConfig() {
133
+        $mounts = $this->readDBConfig();
134
+        $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
135
+        $configs = array_filter($configs, function ($config) {
136
+            return $config instanceof StorageConfig;
137
+        });
138
+
139
+        $keys = array_map(function (StorageConfig $config) {
140
+            return $config->getId();
141
+        }, $configs);
142
+
143
+        return array_combine($keys, $configs);
144
+    }
145
+
146
+    /**
147
+     * Get a storage with status
148
+     *
149
+     * @param int $id storage id
150
+     *
151
+     * @return StorageConfig
152
+     * @throws NotFoundException if the storage with the given id was not found
153
+     */
154
+    public function getStorage($id) {
155
+        $mount = $this->dbConfig->getMountById($id);
156
+
157
+        if (!is_array($mount)) {
158
+            throw new NotFoundException('Storage with ID "' . $id . '" not found');
159
+        }
160
+
161
+        $config = $this->getStorageConfigFromDBMount($mount);
162
+        if ($this->isApplicable($config)) {
163
+            return $config;
164
+        } else {
165
+            throw new NotFoundException('Storage with ID "' . $id . '" not found');
166
+        }
167
+    }
168
+
169
+    /**
170
+     * Check whether this storage service should provide access to a storage
171
+     *
172
+     * @param StorageConfig $config
173
+     * @return bool
174
+     */
175
+    abstract protected function isApplicable(StorageConfig $config);
176
+
177
+    /**
178
+     * Gets all storages, valid or not
179
+     *
180
+     * @return StorageConfig[] array of storage configs
181
+     */
182
+    public function getAllStorages() {
183
+        return $this->readConfig();
184
+    }
185
+
186
+    /**
187
+     * Gets all valid storages
188
+     *
189
+     * @return StorageConfig[]
190
+     */
191
+    public function getStorages() {
192
+        return array_filter($this->getAllStorages(), [$this, 'validateStorage']);
193
+    }
194
+
195
+    /**
196
+     * Validate storage
197
+     * FIXME: De-duplicate with StoragesController::validate()
198
+     *
199
+     * @param StorageConfig $storage
200
+     * @return bool
201
+     */
202
+    protected function validateStorage(StorageConfig $storage) {
203
+        /** @var Backend */
204
+        $backend = $storage->getBackend();
205
+        /** @var AuthMechanism */
206
+        $authMechanism = $storage->getAuthMechanism();
207
+
208
+        if (!$backend->isVisibleFor($this->getVisibilityType())) {
209
+            // not permitted to use backend
210
+            return false;
211
+        }
212
+        if (!$authMechanism->isVisibleFor($this->getVisibilityType())) {
213
+            // not permitted to use auth mechanism
214
+            return false;
215
+        }
216
+
217
+        return true;
218
+    }
219
+
220
+    /**
221
+     * Get the visibility type for this controller, used in validation
222
+     *
223
+     * @return string BackendService::VISIBILITY_* constants
224
+     */
225
+    abstract public function getVisibilityType();
226
+
227
+    /**
228
+     * @return integer
229
+     */
230
+    protected function getType() {
231
+        return DBConfigService::MOUNT_TYPE_ADMIN;
232
+    }
233
+
234
+    /**
235
+     * Add new storage to the configuration
236
+     *
237
+     * @param StorageConfig $newStorage storage attributes
238
+     *
239
+     * @return StorageConfig storage config, with added id
240
+     */
241
+    public function addStorage(StorageConfig $newStorage) {
242
+        $allStorages = $this->readConfig();
243
+
244
+        $configId = $this->dbConfig->addMount(
245
+            $newStorage->getMountPoint(),
246
+            $newStorage->getBackend()->getIdentifier(),
247
+            $newStorage->getAuthMechanism()->getIdentifier(),
248
+            $newStorage->getPriority(),
249
+            $this->getType()
250
+        );
251
+
252
+        $newStorage->setId($configId);
253
+
254
+        foreach ($newStorage->getApplicableUsers() as $user) {
255
+            $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user);
256
+        }
257
+        foreach ($newStorage->getApplicableGroups() as $group) {
258
+            $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
259
+        }
260
+        foreach ($newStorage->getBackendOptions() as $key => $value) {
261
+            $this->dbConfig->setConfig($configId, $key, $value);
262
+        }
263
+        foreach ($newStorage->getMountOptions() as $key => $value) {
264
+            $this->dbConfig->setOption($configId, $key, $value);
265
+        }
266
+
267
+        if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) {
268
+            $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
269
+        }
270
+
271
+        // add new storage
272
+        $allStorages[$configId] = $newStorage;
273
+
274
+        $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
275
+
276
+        $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS);
277
+        return $newStorage;
278
+    }
279
+
280
+    /**
281
+     * Create a storage from its parameters
282
+     *
283
+     * @param string $mountPoint storage mount point
284
+     * @param string $backendIdentifier backend identifier
285
+     * @param string $authMechanismIdentifier authentication mechanism identifier
286
+     * @param array $backendOptions backend-specific options
287
+     * @param array|null $mountOptions mount-specific options
288
+     * @param array|null $applicableUsers users for which to mount the storage
289
+     * @param array|null $applicableGroups groups for which to mount the storage
290
+     * @param int|null $priority priority
291
+     *
292
+     * @return StorageConfig
293
+     */
294
+    public function createStorage(
295
+        $mountPoint,
296
+        $backendIdentifier,
297
+        $authMechanismIdentifier,
298
+        $backendOptions,
299
+        $mountOptions = null,
300
+        $applicableUsers = null,
301
+        $applicableGroups = null,
302
+        $priority = null
303
+    ) {
304
+        $backend = $this->backendService->getBackend($backendIdentifier);
305
+        if (!$backend) {
306
+            $backend = new InvalidBackend($backendIdentifier);
307
+        }
308
+        $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier);
309
+        if (!$authMechanism) {
310
+            $authMechanism = new InvalidAuth($authMechanismIdentifier);
311
+        }
312
+        $newStorage = new StorageConfig();
313
+        $newStorage->setMountPoint($mountPoint);
314
+        $newStorage->setBackend($backend);
315
+        $newStorage->setAuthMechanism($authMechanism);
316
+        $newStorage->setBackendOptions($backendOptions);
317
+        if (isset($mountOptions)) {
318
+            $newStorage->setMountOptions($mountOptions);
319
+        }
320
+        if (isset($applicableUsers)) {
321
+            $newStorage->setApplicableUsers($applicableUsers);
322
+        }
323
+        if (isset($applicableGroups)) {
324
+            $newStorage->setApplicableGroups($applicableGroups);
325
+        }
326
+        if (isset($priority)) {
327
+            $newStorage->setPriority($priority);
328
+        }
329
+
330
+        return $newStorage;
331
+    }
332
+
333
+    /**
334
+     * Triggers the given hook signal for all the applicables given
335
+     *
336
+     * @param string $signal signal
337
+     * @param string $mountPoint hook mount pount param
338
+     * @param string $mountType hook mount type param
339
+     * @param array $applicableArray array of applicable users/groups for which to trigger the hook
340
+     */
341
+    protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) {
342
+        foreach ($applicableArray as $applicable) {
343
+            \OCP\Util::emitHook(
344
+                Filesystem::CLASSNAME,
345
+                $signal,
346
+                [
347
+                    Filesystem::signal_param_path => $mountPoint,
348
+                    Filesystem::signal_param_mount_type => $mountType,
349
+                    Filesystem::signal_param_users => $applicable,
350
+                ]
351
+            );
352
+        }
353
+    }
354
+
355
+    /**
356
+     * Triggers $signal for all applicable users of the given
357
+     * storage
358
+     *
359
+     * @param StorageConfig $storage storage data
360
+     * @param string $signal signal to trigger
361
+     */
362
+    abstract protected function triggerHooks(StorageConfig $storage, $signal);
363
+
364
+    /**
365
+     * Triggers signal_create_mount or signal_delete_mount to
366
+     * accommodate for additions/deletions in applicableUsers
367
+     * and applicableGroups fields.
368
+     *
369
+     * @param StorageConfig $oldStorage old storage data
370
+     * @param StorageConfig $newStorage new storage data
371
+     */
372
+    abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage);
373
+
374
+    /**
375
+     * Update storage to the configuration
376
+     *
377
+     * @param StorageConfig $updatedStorage storage attributes
378
+     *
379
+     * @return StorageConfig storage config
380
+     * @throws NotFoundException if the given storage does not exist in the config
381
+     */
382
+    public function updateStorage(StorageConfig $updatedStorage) {
383
+        $id = $updatedStorage->getId();
384
+
385
+        $existingMount = $this->dbConfig->getMountById($id);
386
+
387
+        if (!is_array($existingMount)) {
388
+            throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage');
389
+        }
390
+
391
+        $oldStorage = $this->getStorageConfigFromDBMount($existingMount);
392
+
393
+        if ($oldStorage->getBackend() instanceof InvalidBackend) {
394
+            throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend');
395
+        }
396
+
397
+        $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers());
398
+        $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups());
399
+        $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
400
+        $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
401
+
402
+        $oldUserCount = count($oldStorage->getApplicableUsers());
403
+        $oldGroupCount = count($oldStorage->getApplicableGroups());
404
+        $newUserCount = count($updatedStorage->getApplicableUsers());
405
+        $newGroupCount = count($updatedStorage->getApplicableGroups());
406
+        $wasGlobal = ($oldUserCount + $oldGroupCount) === 0;
407
+        $isGlobal = ($newUserCount + $newGroupCount) === 0;
408
+
409
+        foreach ($removedUsers as $user) {
410
+            $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user);
411
+        }
412
+        foreach ($removedGroups as $group) {
413
+            $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
414
+        }
415
+        foreach ($addedUsers as $user) {
416
+            $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user);
417
+        }
418
+        foreach ($addedGroups as $group) {
419
+            $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
420
+        }
421
+
422
+        if ($wasGlobal && !$isGlobal) {
423
+            $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
424
+        } elseif (!$wasGlobal && $isGlobal) {
425
+            $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
426
+        }
427
+
428
+        $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions());
429
+        $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions());
430
+
431
+        foreach ($changedConfig as $key => $value) {
432
+            if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) {
433
+                $this->dbConfig->setConfig($id, $key, $value);
434
+            }
435
+        }
436
+        foreach ($changedOptions as $key => $value) {
437
+            $this->dbConfig->setOption($id, $key, $value);
438
+        }
439
+
440
+        if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) {
441
+            $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint());
442
+        }
443
+
444
+        if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) {
445
+            $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier());
446
+        }
447
+
448
+        $this->triggerChangeHooks($oldStorage, $updatedStorage);
449
+
450
+        if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly
451
+            $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage));
452
+        } else {
453
+            $storageId = $this->getStorageId($updatedStorage);
454
+            foreach ($removedUsers as $userId) {
455
+                $this->userMountCache->removeUserStorageMount($storageId, $userId);
456
+            }
457
+        }
458
+
459
+        return $this->getStorage($id);
460
+    }
461
+
462
+    /**
463
+     * Delete the storage with the given id.
464
+     *
465
+     * @param int $id storage id
466
+     *
467
+     * @throws NotFoundException if no storage was found with the given id
468
+     */
469
+    public function removeStorage($id) {
470
+        $existingMount = $this->dbConfig->getMountById($id);
471
+
472
+        if (!is_array($existingMount)) {
473
+            throw new NotFoundException('Storage with ID "' . $id . '" not found');
474
+        }
475
+
476
+        $this->dbConfig->removeMount($id);
477
+
478
+        $deletedStorage = $this->getStorageConfigFromDBMount($existingMount);
479
+        $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount);
480
+
481
+        // delete oc_storages entries and oc_filecache
482
+        \OC\Files\Cache\Storage::cleanByMountId($id);
483
+    }
484
+
485
+    /**
486
+     * Construct the storage implementation
487
+     *
488
+     * @param StorageConfig $storageConfig
489
+     * @return int
490
+     */
491
+    private function getStorageId(StorageConfig $storageConfig) {
492
+        try {
493
+            $class = $storageConfig->getBackend()->getStorageClass();
494
+            /** @var \OC\Files\Storage\Storage $storage */
495
+            $storage = new $class($storageConfig->getBackendOptions());
496
+
497
+            // auth mechanism should fire first
498
+            $storage = $storageConfig->getBackend()->wrapStorage($storage);
499
+            $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
500
+
501
+            return $storage->getStorageCache()->getNumericId();
502
+        } catch (\Exception $e) {
503
+            return -1;
504
+        }
505
+    }
506 506
 }
Please login to merge, or discard this patch.