remove depricated methods from MountConfig

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/26319/head
Robin Appelman 5 years ago committed by Arthur Schiwon
parent 7917f4cf76
commit 1052feabed
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
  1. 6
      apps/files_external/appinfo/app.php
  2. 59
      apps/files_external/appinfo/routes.php
  3. 53
      apps/files_external/lib/Controller/ApiController.php
  4. 12
      apps/files_external/lib/Lib/StorageConfig.php
  5. 113
      apps/files_external/lib/MountConfig.php
  6. 7
      apps/files_external/tests/Service/StoragesServiceTest.php
  7. 11
      lib/private/Encryption/File.php
  8. 28
      lib/private/Encryption/Util.php

@ -31,10 +31,10 @@ use OCA\Files_External\Config\ConfigAdapter;
require_once __DIR__ . '/../3rdparty/autoload.php';
// register Application object singleton
\OCA\Files_External\MountConfig::$app = \OC::$server->query(\OCA\Files_External\AppInfo\Application::class);
\OCA\Files_External\MountConfig::$app->registerListeners();
$app = \OC::$server->query(\OCA\Files_External\AppInfo\Application::class);
$app->registerListeners();
$appContainer = \OCA\Files_External\MountConfig::$app->getContainer();
$appContainer = $app->getContainer();
\OCA\Files\App::getNavigationManager()->add(function () {
$l = \OC::$server->getL10N('files_external');

@ -26,36 +26,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
\OCA\Files_External\MountConfig::$app->registerRoutes(
$this,
[
'resources' => [
'global_storages' => ['url' => '/globalstorages'],
'user_storages' => ['url' => '/userstorages'],
'user_global_storages' => ['url' => '/userglobalstorages'],
],
'routes' => [
[
'name' => 'Ajax#getSshKeys',
'url' => '/ajax/public_key.php',
'verb' => 'POST',
'requirements' => [],
],
[
'name' => 'Ajax#saveGlobalCredentials',
'url' => '/globalcredentials',
'verb' => 'POST',
],
],
'ocs' => [
[
'name' => 'Api#getUserMounts',
'url' => '/api/v1/mounts',
'verb' => 'GET',
],
],
]
);
$this->create('files_external_oauth1', 'apps/files_external/ajax/oauth1.php')
->actionInclude('files_external/ajax/oauth1.php');
@ -65,3 +36,31 @@ $this->create('files_external_oauth2', 'apps/files_external/ajax/oauth2.php')
$this->create('files_external_list_applicable', '/apps/files_external/applicable')
->actionInclude('files_external/ajax/applicable.php');
return [
'resources' => [
'global_storages' => ['url' => '/globalstorages'],
'user_storages' => ['url' => '/userstorages'],
'user_global_storages' => ['url' => '/userglobalstorages'],
],
'routes' => [
[
'name' => 'Ajax#getSshKeys',
'url' => '/ajax/public_key.php',
'verb' => 'POST',
'requirements' => [],
],
[
'name' => 'Ajax#saveGlobalCredentials',
'url' => '/globalcredentials',
'verb' => 'POST',
],
],
'ocs' => [
[
'name' => 'Api#getUserMounts',
'url' => '/api/v1/mounts',
'verb' => 'GET',
],
],
];

@ -29,44 +29,55 @@ declare(strict_types=1);
*/
namespace OCA\Files_External\Controller;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\MountConfig;
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
class ApiController extends OCSController {
/** @var IUserSession */
private $userSession;
/** @var UserGlobalStoragesService */
private $userGlobalStoragesService;
/** @var UserStoragesService */
private $userStoragesService;
public function __construct(string $appName,
IRequest $request,
IUserSession $userSession) {
public function __construct(
string $appName,
IRequest $request,
IUserSession $userSession,
UserGlobalStoragesService $userGlobalStorageService,
UserStoragesService $userStorageService
) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->userGlobalStoragesService = $userGlobalStorageService;
$this->userStoragesService = $userStorageService;
}
/**
* Formats the given mount config to a mount entry.
*
* @param string $mountPoint mount point name, relative to the data dir
* @param array $mountConfig mount config to format
* @param StorageConfig $mountConfig mount config to format
*
* @return array entry
*/
private function formatMount(string $mountPoint, array $mountConfig): array {
// strip "/$user/files" from mount point
$mountPoint = explode('/', trim($mountPoint, '/'), 3);
$mountPoint = $mountPoint[2] ?? '';
private function formatMount(string $mountPoint, StorageConfig $mountConfig): array {
// split path from mount point
$path = \dirname($mountPoint);
if ($path === '.') {
if ($path === '.' || $path === '/') {
$path = '';
}
$isSystemMount = !$mountConfig['personal'];
$isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN;
$permissions = \OCP\Constants::PERMISSION_READ;
// personal mounts can be deleted
@ -78,11 +89,11 @@ class ApiController extends OCSController {
'name' => basename($mountPoint),
'path' => $path,
'type' => 'dir',
'backend' => $mountConfig['backend'],
'backend' => $mountConfig->getBackend()->getText(),
'scope' => $isSystemMount ? 'system' : 'personal',
'permissions' => $permissions,
'id' => $mountConfig['id'],
'class' => $mountConfig['class']
'id' => $mountConfig->getId(),
'class' => $mountConfig->getBackend()->getIdentifier(),
];
return $entry;
}
@ -96,10 +107,18 @@ class ApiController extends OCSController {
*/
public function getUserMounts(): DataResponse {
$entries = [];
$user = $this->userSession->getUser()->getUID();
$mountPoints = [];
foreach ($this->userGlobalStoragesService->getStorages() as $storage) {
$mountPoint = $storage->getMountPoint();
$mountPoints[$mountPoint] = $storage;
}
$mounts = \OCA\Files_External\MountConfig::getAbsoluteMountPoints($user);
foreach ($mounts as $mountPoint => $mount) {
foreach ($this->userStoragesService->getStorages() as $storage) {
$mountPoint = $storage->getMountPoint();
$mountPoints[$mountPoint] = $storage;
}
foreach ($mountPoints as $mountPoint => $mount) {
$entries[] = $this->formatMount($mountPoint, $mount);
}

@ -98,14 +98,14 @@ class StorageConfig implements \JsonSerializable {
/**
* List of users who have access to this storage
*
* @var array
* @var string[]
*/
private $applicableUsers = [];
/**
* List of groups that have access to this storage
*
* @var array
* @var string[]
*/
private $applicableGroups = [];
@ -272,7 +272,7 @@ class StorageConfig implements \JsonSerializable {
/**
* Returns the users for which to mount this storage
*
* @return array applicable users
* @return string[] applicable users
*/
public function getApplicableUsers() {
return $this->applicableUsers;
@ -281,7 +281,7 @@ class StorageConfig implements \JsonSerializable {
/**
* Sets the users for which to mount this storage
*
* @param array|null $applicableUsers applicable users
* @param string[]|null $applicableUsers applicable users
*/
public function setApplicableUsers($applicableUsers) {
if (is_null($applicableUsers)) {
@ -293,7 +293,7 @@ class StorageConfig implements \JsonSerializable {
/**
* Returns the groups for which to mount this storage
*
* @return array applicable groups
* @return string[] applicable groups
*/
public function getApplicableGroups() {
return $this->applicableGroups;
@ -302,7 +302,7 @@ class StorageConfig implements \JsonSerializable {
/**
* Sets the groups for which to mount this storage
*
* @param array|null $applicableGroups applicable groups
* @param string[]|null $applicableGroups applicable groups
*/
public function setApplicableGroups($applicableGroups) {
if (is_null($applicableGroups)) {

@ -66,102 +66,21 @@ class MountConfig {
// whether to skip backend test (for unit tests, as this static class is not mockable)
public static $skipTest = false;
/** @var Application */
public static $app;
/**
* Returns the mount points for the given user.
* The mount point is relative to the data directory.
*
* @param string $uid user
* @return array of mount point string as key, mountpoint config as value
*
* @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages()
*/
public static function getAbsoluteMountPoints($uid) {
$mountPoints = [];
$userGlobalStoragesService = self::$app->getContainer()->query(UserGlobalStoragesService::class);
$userStoragesService = self::$app->getContainer()->query(UserStoragesService::class);
$user = self::$app->getContainer()->query(IUserManager::class)->get($uid);
$userGlobalStoragesService->setUser($user);
$userStoragesService->setUser($user);
foreach ($userGlobalStoragesService->getStorages() as $storage) {
/** @var \OCA\Files_External\Lib\StorageConfig $storage */
$mountPoint = '/'.$uid.'/files'.$storage->getMountPoint();
$mountEntry = self::prepareMountPointEntry($storage, false);
foreach ($mountEntry['options'] as &$option) {
$option = self::substitutePlaceholdersInConfig($option, $uid);
}
$mountPoints[$mountPoint] = $mountEntry;
}
foreach ($userStoragesService->getStorages() as $storage) {
$mountPoint = '/'.$uid.'/files'.$storage->getMountPoint();
$mountEntry = self::prepareMountPointEntry($storage, true);
foreach ($mountEntry['options'] as &$option) {
$option = self::substitutePlaceholdersInConfig($option, $uid);
}
$mountPoints[$mountPoint] = $mountEntry;
}
$userGlobalStoragesService->resetUser();
$userStoragesService->resetUser();
return $mountPoints;
}
/**
* Get the system mount points
*
* @return array
*
* @deprecated 8.2.0 use GlobalStoragesService::getStorages()
*/
public static function getSystemMountPoints() {
$mountPoints = [];
$service = self::$app->getContainer()->query(GlobalStoragesService::class);
foreach ($service->getStorages() as $storage) {
$mountPoints[] = self::prepareMountPointEntry($storage, false);
}
return $mountPoints;
}
/**
* Convert a StorageConfig to the legacy mountPoints array format
* There's a lot of extra information in here, to satisfy all of the legacy functions
*
* @param StorageConfig $storage
* @param bool $isPersonal
* @return array
*/
private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) {
$mountEntry = [];
$mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash
$mountEntry['class'] = $storage->getBackend()->getIdentifier();
$mountEntry['backend'] = $storage->getBackend()->getText();
$mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier();
$mountEntry['personal'] = $isPersonal;
$mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions());
$mountEntry['mountOptions'] = $storage->getMountOptions();
$mountEntry['priority'] = $storage->getPriority();
$mountEntry['applicable'] = [
'groups' => $storage->getApplicableGroups(),
'users' => $storage->getApplicableUsers(),
];
// if mountpoint is applicable to all users the old API expects ['all']
if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) {
$mountEntry['applicable']['users'] = ['all'];
}
$mountEntry['id'] = $storage->getId();
return $mountEntry;
/** @var UserGlobalStoragesService */
private $userGlobalStorageService;
/** @var UserStoragesService */
private $userStorageService;
/** @var GlobalStoragesService */
private $globalStorageService;
public function __construct(
UserGlobalStoragesService $userGlobalStorageService,
UserStoragesService $userStorageService,
GlobalStoragesService $globalStorageService
) {
$this->userGlobalStorageService = $userGlobalStorageService;
$this->userStorageService = $userStorageService;
$this->globalStorageService = $globalStorageService;
}
/**
@ -173,7 +92,7 @@ class MountConfig {
*/
public static function substitutePlaceholdersInConfig($input, string $userId = null) {
/** @var BackendService $backendService */
$backendService = self::$app->getContainer()->query(BackendService::class);
$backendService = \OC::$server->get(BackendService::class);
/** @var IConfigHandler[] $handlers */
$handlers = $backendService->getConfigHandlers();
foreach ($handlers as $handler) {

@ -154,6 +154,7 @@ abstract class StoragesServiceTest extends \Test\TestCase {
});
$this->backendService->method('getBackends')
->willReturn($backends);
$this->overwriteService(BackendService::class, $this->backendService);
\OCP\Util::connectHook(
Filesystem::CLASSNAME,
@ -171,12 +172,6 @@ abstract class StoragesServiceTest extends \Test\TestCase {
return $this->backendService;
}
});
\OCA\Files_External\MountConfig::$app = $this->getMockBuilder('\OCA\Files_External\Appinfo\Application')
->disableOriginalConstructor()
->getMock();
\OCA\Files_External\MountConfig::$app->method('getContainer')
->willReturn($containerMock);
}
protected function tearDown(): void {

@ -28,6 +28,7 @@
namespace OC\Encryption;
use OC\Cache\CappedMemoryCache;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Share\IManager;
@ -110,10 +111,12 @@ class File implements \OCP\Encryption\IFile {
// check if it is a group mount
if (\OCP\App::isEnabled("files_external")) {
$mounts = \OCA\Files_External\MountConfig::getSystemMountPoints();
foreach ($mounts as $mount) {
if ($mount['mountpoint'] == substr($ownerPath, 1, strlen($mount['mountpoint']))) {
$mountedFor = $this->util->getUserWithAccessToMountPoint($mount['applicable']['users'], $mount['applicable']['groups']);
/** @var GlobalStoragesService $storageService */
$storageService = \OC::$server->get(GlobalStoragesService::class);
$storages = $storageService->getAllStorages();
foreach ($storages as $storage) {
if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) {
$mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups());
$userIds = array_merge($userIds, $mountedFor);
}
}

@ -32,6 +32,8 @@ use OC\Encryption\Exceptions\EncryptionHeaderToLargeException;
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
use OC\Files\Filesystem;
use OC\Files\View;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\Encryption\IEncryptionModule;
use OCP\IConfig;
use OCP\IUser;
@ -265,7 +267,7 @@ class Util {
public function getUserWithAccessToMountPoint($users, $groups) {
$result = [];
if (in_array('all', $users)) {
if ($users === [] && $groups === []) {
$users = $this->userManager->search('', null, null);
$result = array_map(function (IUser $user) {
return $user->getUID();
@ -298,10 +300,12 @@ class Util {
*/
public function isSystemWideMountPoint($path, $uid) {
if (\OCP\App::isEnabled("files_external")) {
$mounts = \OCA\Files_External\MountConfig::getSystemMountPoints();
foreach ($mounts as $mount) {
if (strpos($path, '/files/' . $mount['mountpoint']) === 0) {
if ($this->isMountPointApplicableToUser($mount, $uid)) {
/** @var GlobalStoragesService $storageService */
$storageService = \OC::$server->get(GlobalStoragesService::class);
$storages = $storageService->getAllStorages();
foreach ($storages as $storage) {
if (strpos($path, '/files/' . $storage->getMountPoint()) === 0) {
if ($this->isMountPointApplicableToUser($storage, $uid)) {
return true;
}
}
@ -313,19 +317,21 @@ class Util {
/**
* check if mount point is applicable to user
*
* @param array $mount contains $mount['applicable']['users'], $mount['applicable']['groups']
* @param StorageConfig $mount
* @param string $uid
* @return boolean
*/
private function isMountPointApplicableToUser($mount, $uid) {
$acceptedUids = ['all', $uid];
private function isMountPointApplicableToUser(StorageConfig $mount, string $uid) {
if ($mount->getApplicableUsers() === [] && $mount->getApplicableGroups() === []) {
// applicable for everyone
return true;
}
// check if mount point is applicable for the user
$intersection = array_intersect($acceptedUids, $mount['applicable']['users']);
if (!empty($intersection)) {
if (array_search($uid, $mount->getApplicableUsers()) !== false) {
return true;
}
// check if mount point is applicable for group where the user is a member
foreach ($mount['applicable']['groups'] as $gid) {
foreach ($mount->getApplicableGroups() as $gid) {
if ($this->groupManager->isInGroup($uid, $gid)) {
return true;
}

Loading…
Cancel
Save