Completed
Pull Request — master (#26700)
by Philipp
08:19
created

apps/files_external/lib/Config/ConfigAdapter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 * @author Vincent Petry <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2016, ownCloud GmbH.
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\Files_External\Config;
27
28
use OC\Files\Storage\Wrapper\Availability;
29
use OCA\Files_External\Migration\StorageMigrator;
30
use OCP\Files\Storage;
31
use OC\Files\Mount\MountPoint;
32
use OCP\Files\Storage\IStorageFactory;
33
use OCA\Files_External\Lib\PersonalMount;
34
use OCP\Files\Config\IMountProvider;
35
use OCP\IUser;
36
use OCA\Files_External\Service\UserStoragesService;
37
use OCA\Files_External\Service\UserGlobalStoragesService;
38
use OCA\Files_External\Lib\StorageConfig;
39
use OC\Files\Storage\FailedStorage;
40
use OCP\Files\StorageNotAvailableException;
41
42
/**
43
 * Make the old files_external config work with the new public mount config api
44
 */
45
class ConfigAdapter implements IMountProvider {
46
47
	/** @var UserStoragesService */
48
	private $userStoragesService;
49
50
	/** @var UserGlobalStoragesService */
51
	private $userGlobalStoragesService;
52
	/** @var StorageMigrator  */
53
	private $migrator;
54
55
	/**
56
	 * @param UserStoragesService $userStoragesService
57
	 * @param UserGlobalStoragesService $userGlobalStoragesService
58
	 * @param StorageMigrator $migrator
59
	 */
60
	public function __construct(
61
		UserStoragesService $userStoragesService,
62
		UserGlobalStoragesService $userGlobalStoragesService,
63
		StorageMigrator $migrator
64
	) {
65
		$this->userStoragesService = $userStoragesService;
66
		$this->userGlobalStoragesService = $userGlobalStoragesService;
67
		$this->migrator = $migrator;
68
	}
69
70
	/**
71
	 * Process storage ready for mounting
72
	 *
73
	 * @param StorageConfig $storage
74
	 * @param IUser $user
75
	 */
76
	private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
77
		foreach ($storage->getBackendOptions() as $option => $value) {
78
			$storage->setBackendOption($option, \OC_Mount_Config::setUserVars(
79
				$user->getUID(), $value
80
			));
81
		}
82
83
		$objectStore = $storage->getBackendOption('objectstore');
84
		if ($objectStore) {
85
			$objectClass = $objectStore['class'];
86
			if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
87
				throw new \InvalidArgumentException('Invalid object store');
88
			}
89
			$storage->setBackendOption('objectstore', new $objectClass($objectStore));
90
		}
91
92
		$storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
93
		$storage->getBackend()->manipulateStorageConfig($storage, $user);
94
	}
95
96
	/**
97
	 * Construct the storage implementation
98
	 *
99
	 * @param StorageConfig $storageConfig
100
	 * @return Storage
101
	 */
102
	private function constructStorage(StorageConfig $storageConfig) {
103
		$class = $storageConfig->getBackend()->getStorageClass();
104
		$storage = new $class($storageConfig->getBackendOptions());
105
106
		// auth mechanism should fire first
107
		$storage = $storageConfig->getBackend()->wrapStorage($storage);
108
		$storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
109
110
		return $storage;
111
	}
112
113
	/**
114
	 * Get all mountpoints applicable for the user
115
	 *
116
	 * @param \OCP\IUser $user
117
	 * @param \OCP\Files\Storage\IStorageFactory $loader
118
	 * @return \OCP\Files\Mount\IMountPoint[]
119
	 */
120
	public function getMountsForUser(IUser $user, IStorageFactory $loader) {
121
		$this->migrator->migrateUser($user);
122
123
		$mounts = [];
124
125
		$this->userStoragesService->setUser($user);
126
		$this->userGlobalStoragesService->setUser($user);
127
128
		foreach ($this->userGlobalStoragesService->getUniqueStorages() as $storage) {
129
			try {
130
				$this->prepareStorageConfig($storage, $user);
131
				$impl = $this->constructStorage($storage);
132
			} catch (\Exception $e) {
133
				// propagate exception into filesystem
134
				$impl = new FailedStorage(['exception' => $e]);
135
			}
136
137
			try {
138
				$availability = $impl->getAvailability();
139
				if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
140
					$impl = new FailedStorage([
141
						'exception' => new StorageNotAvailableException('Storage with mount id ' . $storage->getId() . ' is not available')
142
					]);
143
				}
144
			} catch (\Exception $e) {
145
				// propagate exception into filesystem
146
				$impl = new FailedStorage(['exception' => $e]);
147
			}
148
149
			$mount = new MountPoint(
150
				$impl,
0 ignored issues
show
$impl is of type object<OCP\Files\Storage>, but the function expects a string|object<OC\Files\Storage\Storage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
				'/' . $user->getUID() . '/files' . $storage->getMountPoint(),
152
				null,
153
				$loader,
154
				$storage->getMountOptions()
155
			);
156
			$mounts[$storage->getMountPoint()] = $mount;
157
		}
158
159
		foreach ($this->userStoragesService->getStorages() as $storage) {
160
			try {
161
				$this->prepareStorageConfig($storage, $user);
162
				$impl = $this->constructStorage($storage);
163
			} catch (\Exception $e) {
164
				// propagate exception into filesystem
165
				$impl = new FailedStorage(['exception' => $e]);
166
			}
167
168
			$mount = new PersonalMount(
169
				$this->userStoragesService,
170
				$storage->getId(),
171
				$impl,
172
				'/' . $user->getUID() . '/files' . $storage->getMountPoint(),
173
				null,
174
				$loader,
175
				$storage->getMountOptions()
176
			);
177
			$mounts[$storage->getMountPoint()] = $mount;
178
		}
179
180
		$this->userStoragesService->resetUser();
181
		$this->userGlobalStoragesService->resetUser();
182
183
		return $mounts;
184
	}
185
}
186