Completed
Push — master ( e35451...cc9073 )
by Morris
53:33 queued 20:44
created

MountProviderCollection::filterMounts()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Files\Config;
25
26
use OC\Hooks\Emitter;
27
use OC\Hooks\EmitterTrait;
28
use OCP\Files\Config\IHomeMountProvider;
29
use OCP\Files\Config\IMountProviderCollection;
30
use OCP\Files\Config\IMountProvider;
31
use OCP\Files\Config\IUserMountCache;
32
use OCP\Files\Mount\IMountManager;
33
use OCP\Files\Mount\IMountPoint;
34
use OCP\Files\Storage\IStorageFactory;
35
use OCP\IUser;
36
37
class MountProviderCollection implements IMountProviderCollection, Emitter {
38
	use EmitterTrait;
39
40
	/**
41
	 * @var \OCP\Files\Config\IHomeMountProvider[]
42
	 */
43
	private $homeProviders = [];
44
45
	/**
46
	 * @var \OCP\Files\Config\IMountProvider[]
47
	 */
48
	private $providers = array();
49
50
	/**
51
	 * @var \OCP\Files\Storage\IStorageFactory
52
	 */
53
	private $loader;
54
55
	/**
56
	 * @var \OCP\Files\Config\IUserMountCache
57
	 */
58
	private $mountCache;
59
60
	/** @var callable[] */
61
	private $mountFilters = [];
62
63
	/**
64
	 * @param \OCP\Files\Storage\IStorageFactory $loader
65
	 * @param IUserMountCache $mountCache
66
	 */
67
	public function __construct(IStorageFactory $loader, IUserMountCache $mountCache) {
68
		$this->loader = $loader;
69
		$this->mountCache = $mountCache;
70
	}
71
72
	/**
73
	 * Get all configured mount points for the user
74
	 *
75
	 * @param \OCP\IUser $user
76
	 * @return \OCP\Files\Mount\IMountPoint[]
77
	 */
78
	public function getMountsForUser(IUser $user) {
79
		$loader = $this->loader;
80
		$mounts = array_map(function (IMountProvider $provider) use ($user, $loader) {
81
			return $provider->getMountsForUser($user, $loader);
82
		}, $this->providers);
83
		$mounts = array_filter($mounts, function ($result) {
84
			return is_array($result);
85
		});
86
		$mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
87
			return array_merge($mounts, $providerMounts);
88
		}, array());
89
		return $this->filterMounts($user, $mounts);
90
	}
91
92
	public function addMountForUser(IUser $user, IMountManager $mountManager) {
93
		// shared mount provider gets to go last since it needs to know existing files
94
		// to check for name collisions
95
		$firstMounts = [];
96
		$firstProviders = array_filter($this->providers, function (IMountProvider $provider) {
97
			return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
98
		});
99
		$lastProviders = array_filter($this->providers, function (IMountProvider $provider) {
100
			return (get_class($provider) === 'OCA\Files_Sharing\MountProvider');
101
		});
102
		foreach ($firstProviders as $provider) {
103
			$mounts = $provider->getMountsForUser($user, $this->loader);
104
			if (is_array($mounts)) {
105
				$firstMounts = array_merge($firstMounts, $mounts);
106
			}
107
		}
108
		$firstMounts = $this->filterMounts($user, $firstMounts);
109
		array_walk($firstMounts, [$mountManager, 'addMount']);
110
111
		$lateMounts = [];
112
		foreach ($lastProviders as $provider) {
113
			$mounts = $provider->getMountsForUser($user, $this->loader);
114
			if (is_array($mounts)) {
115
				$lateMounts = array_merge($lateMounts, $mounts);
116
			}
117
		}
118
119
		$lateMounts = $this->filterMounts($user, $lateMounts);
120
		array_walk($lateMounts, [$mountManager, 'addMount']);
121
122
		return array_merge($lateMounts, $firstMounts);
123
	}
124
125
	/**
126
	 * Get the configured home mount for this user
127
	 *
128
	 * @param \OCP\IUser $user
129
	 * @return \OCP\Files\Mount\IMountPoint
130
	 * @since 9.1.0
131
	 */
132
	public function getHomeMountForUser(IUser $user) {
133
		/** @var \OCP\Files\Config\IHomeMountProvider[] $providers */
134
		$providers = array_reverse($this->homeProviders); // call the latest registered provider first to give apps an opportunity to overwrite builtin
135
		foreach ($providers as $homeProvider) {
136
			if ($mount = $homeProvider->getHomeMountForUser($user, $this->loader)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mount is correct as $homeProvider->getHomeMo...r($user, $this->loader) (which targets OCP\Files\Config\IHomeMo...::getHomeMountForUser()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
				$mount->setMountPoint('/' . $user->getUID()); //make sure the mountpoint is what we expect
138
				return $mount;
139
			}
140
		}
141
		throw new \Exception('No home storage configured for user ' . $user);
142
	}
143
144
	/**
145
	 * Add a provider for mount points
146
	 *
147
	 * @param \OCP\Files\Config\IMountProvider $provider
148
	 */
149
	public function registerProvider(IMountProvider $provider) {
150
		$this->providers[] = $provider;
151
152
		$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
153
	}
154
155
	public function registerMountFilter(callable $filter) {
156
		$this->mountFilters[] = $filter;
157
	}
158
159
	private function filterMounts(IUser $user, array $mountPoints) {
160
		return array_filter($mountPoints, function (IMountPoint $mountPoint) use ($user) {
161
			foreach ($this->mountFilters as $filter) {
162
				if ($filter($mountPoint, $user) === false) {
163
					return false;
164
				}
165
			}
166
			return true;
167
		});
168
	}
169
170
	/**
171
	 * Add a provider for home mount points
172
	 *
173
	 * @param \OCP\Files\Config\IHomeMountProvider $provider
174
	 * @since 9.1.0
175
	 */
176
	public function registerHomeProvider(IHomeMountProvider $provider) {
177
		$this->homeProviders[] = $provider;
178
		$this->emit('\OC\Files\Config', 'registerHomeMountProvider', [$provider]);
179
	}
180
181
	/**
182
	 * Cache mounts for user
183
	 *
184
	 * @param IUser $user
185
	 * @param IMountPoint[] $mountPoints
186
	 */
187
	public function registerMounts(IUser $user, array $mountPoints) {
188
		$this->mountCache->registerMounts($user, $mountPoints);
189
	}
190
191
	/**
192
	 * Get the mount cache which can be used to search for mounts without setting up the filesystem
193
	 *
194
	 * @return IUserMountCache
195
	 */
196
	public function getMountCache() {
197
		return $this->mountCache;
198
	}
199
}
200