Merge pull request #10179 from nextcloud/mount-filter

Add the option to filter mounts for a user
This commit is contained in:
Morris Jobke 2018-07-11 16:29:09 +02:00 committed by GitHub
commit cc9073c251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -57,6 +57,9 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
*/
private $mountCache;
/** @var callable[] */
private $mountFilters = [];
/**
* @param \OCP\Files\Storage\IStorageFactory $loader
* @param IUserMountCache $mountCache
@ -80,9 +83,10 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
$mounts = array_filter($mounts, function ($result) {
return is_array($result);
});
return array_reduce($mounts, function (array $mounts, array $providerMounts) {
$mounts = array_reduce($mounts, function (array $mounts, array $providerMounts) {
return array_merge($mounts, $providerMounts);
}, array());
return $this->filterMounts($user, $mounts);
}
public function addMountForUser(IUser $user, IMountManager $mountManager) {
@ -101,6 +105,7 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
$firstMounts = array_merge($firstMounts, $mounts);
}
}
$firstMounts = $this->filterMounts($user, $firstMounts);
array_walk($firstMounts, [$mountManager, 'addMount']);
$lateMounts = [];
@ -111,6 +116,7 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
}
}
$lateMounts = $this->filterMounts($user, $lateMounts);
array_walk($lateMounts, [$mountManager, 'addMount']);
return array_merge($lateMounts, $firstMounts);
@ -146,6 +152,21 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
}
public function registerMountFilter(callable $filter) {
$this->mountFilters[] = $filter;
}
private function filterMounts(IUser $user, array $mountPoints) {
return array_filter($mountPoints, function (IMountPoint $mountPoint) use ($user) {
foreach ($this->mountFilters as $filter) {
if ($filter($mountPoint, $user) === false) {
return false;
}
}
return true;
});
}
/**
* Add a provider for home mount points
*

View File

@ -56,6 +56,14 @@ interface IMountProviderCollection {
*/
public function registerProvider(IMountProvider $provider);
/**
* Add a filter for mounts
*
* @param callable $filter (IMountPoint $mountPoint, IUser $user) => boolean
* @since 14.0.0
*/
public function registerMountFilter(callable $filter);
/**
* Add a provider for home mount points
*