diff options
author | Robin Appelman <robin@icewind.nl> | 2017-04-13 18:45:49 +0300 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-04-13 18:45:49 +0300 |
commit | e38d9bf08246bad7821d21e787d32243365e2f97 (patch) | |
tree | cfe246289d72f1da5e02da77c822bb7161057efb /lib | |
parent | e8e63c2b4a081d22e8d3fb76b0665df77c8bc2e8 (diff) |
mount group folders and wip ui
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 18 | ||||
-rw-r--r-- | lib/Controller/FolderController.php | 5 | ||||
-rw-r--r-- | lib/Folder/FolderManager.php | 4 | ||||
-rw-r--r-- | lib/Mount/MountProvider.php | 94 |
4 files changed, 117 insertions, 4 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index e1ce2a6c..f6cab7ec 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -22,6 +22,7 @@ namespace OCA\GroupFolders\AppInfo; use OCA\GroupFolders\Folder\FolderManager; +use OCA\GroupFolders\Mount\MountProvider; use OCP\AppFramework\App; use OCP\AppFramework\IAppContainer; @@ -34,10 +35,27 @@ class Application extends App { $container->registerService(FolderManager::class, function (IAppContainer $c) { return new FolderManager($c->getServer()->getDatabaseConnection()); }); + + $container->registerService(MountProvider::class, function (IAppContainer $c) { + $config = $c->getServer()->getConfig(); + $rootDir = $config->getAppValue( + 'groupfolders', + 'datadir', + $config->getSystemValue('datadirectory') . '/__groupfolders' + ); + + return new MountProvider( + $c->getServer()->getGroupManager(), + rtrim($rootDir, '/') . '/', + $c->query(FolderManager::class) + ); + }); } public function register() { $container = $this->getContainer(); + + $container->getServer()->getMountProviderCollection()->registerProvider($container->query(MountProvider::class)); } } diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index 4329834b..62f250df 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -23,6 +23,7 @@ namespace OCA\GroupFolders\Controller; use OCA\GroupFolders\Folder\FolderManager; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; class FolderController extends Controller { @@ -48,10 +49,10 @@ class FolderController extends Controller { /** * @param string $mountpoint - * @return int */ public function addFolder($mountpoint) { - return $this->manager->createFolder($mountpoint); + $id = $this->manager->createFolder($mountpoint); + return new JSONResponse(['id' => $id]); } /** diff --git a/lib/Folder/FolderManager.php b/lib/Folder/FolderManager.php index 5bbf9fe3..371441e7 100644 --- a/lib/Folder/FolderManager.php +++ b/lib/Folder/FolderManager.php @@ -84,13 +84,13 @@ class FolderManager { public function getFoldersForGroup($groupId) { $query = $this->connection->getQueryBuilder(); - $query->select('mount_point', 'permissions') + $query->select('f.folder_id', 'mount_point', 'permissions') ->from('group_folders', 'f') ->innerJoin('f', 'group_folders_applicable', 'a', $query->expr()->eq('f.folder_id', 'a.folder_id')) ->where($query->expr()->eq('a.group_id', $query->createNamedParameter($groupId))); - return $query->execute()->fetchAll(\PDO::FETCH_KEY_PAIR); + return $query->execute()->fetchAll(); } public function createFolder($mountPoint) { diff --git a/lib/Mount/MountProvider.php b/lib/Mount/MountProvider.php new file mode 100644 index 00000000..21246fbb --- /dev/null +++ b/lib/Mount/MountProvider.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\GroupFolders\Mount; + +use OC\Files\Mount\MountPoint; +use OC\Files\Storage\Local; +use OC\Files\Storage\Wrapper\PermissionsMask; +use OCA\GroupFolders\Folder\FolderManager; +use OCP\Files\Config\IMountProvider; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\Storage\IStorageFactory; +use OCP\IGroupManager; +use OCP\IUser; + +class MountProvider implements IMountProvider { + /** @var IGroupManager */ + private $groupProvider; + + /** @var string */ + private $rootDirectory; + + /** @var FolderManager */ + private $folderManager; + + /** + * @param IGroupManager $groupProvider + * @param string $rootDirectory + * @param FolderManager $folderManager + */ + public function __construct(IGroupManager $groupProvider, $rootDirectory, FolderManager $folderManager) { + $this->groupProvider = $groupProvider; + $this->rootDirectory = $rootDirectory; + $this->folderManager = $folderManager; + } + + public function getMountsForUser(IUser $user, IStorageFactory $loader) { + $groups = $this->groupProvider->getUserGroupIds($user); + $folders = array_reduce($groups, function ($folders, $groupId) { + return array_merge($folders, $this->folderManager->getFoldersForGroup($groupId)); + }, []); + + return array_map(function ($folder) use ($user, $loader) { + return $this->getMount($folder['folder_id'], '/' . $user->getUID() . '/files/' . $folder['mount_point'], $folder['permissions'], $loader); + }, $folders); + } + + /** + * @param $id + * @param $mountPoint + * @param $permissions + * @return IMountPoint + */ + private function getMount($id, $mountPoint, $permissions, IStorageFactory $loader) { + $baseStorage = new Local(['datadir' => $this->createFolder($id)]); + $maskedStore = new PermissionsMask([ + 'storage' => $baseStorage, + 'mask' => $permissions + ]); + + return new MountPoint( + $maskedStore, + $mountPoint, + null, + $loader + ); + } + + private function createFolder($id) { + $path = $this->rootDirectory . $id; + if (!is_dir($path)) { + mkdir($path, 0755, true); + } + return $path; + } +} |