From 7bc24e20367761bf13ebaa5b6e978ba2a34aad50 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Tue, 12 Nov 2019 23:53:33 +0100 Subject: [PATCH 1/3] when a user was delete remove them from applicable list, unless it is the only user, then remove the mount Signed-off-by: Arthur Schiwon --- apps/files_external/appinfo/app.php | 2 ++ .../lib/AppInfo/Application.php | 17 +++++++++++++ .../lib/Service/DBConfigService.php | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php index 3f7094f4dca95..2b5ce1fdee066 100644 --- a/apps/files_external/appinfo/app.php +++ b/apps/files_external/appinfo/app.php @@ -34,6 +34,8 @@ // register Application object singleton \OC_Mount_Config::$app = new \OCA\Files_External\AppInfo\Application(); +\OC_Mount_Config::$app->registerListeners(); + $appContainer = \OC_Mount_Config::$app->getContainer(); \OCA\Files\App::getNavigationManager()->add(function () { diff --git a/apps/files_external/lib/AppInfo/Application.php b/apps/files_external/lib/AppInfo/Application.php index 57ef28f2ae07b..2e614dd366a34 100644 --- a/apps/files_external/lib/AppInfo/Application.php +++ b/apps/files_external/lib/AppInfo/Application.php @@ -32,6 +32,7 @@ use OCA\Files_External\Config\UserPlaceholderHandler; use OCA\Files_External\Lib\Auth\PublicKey\RSAPrivateKey; use OCA\Files_External\Lib\Auth\SMB\KerberosAuth; +use OCA\Files_External\Service\DBConfigService; use \OCP\AppFramework\App; use OCP\AppFramework\IAppContainer; use \OCA\Files_External\Service\BackendService; @@ -63,6 +64,8 @@ use OCA\Files_External\Lib\Backend\FTP; use OCA\Files_External\Lib\Backend\Local; use OCP\Files\Config\IUserMountCache; +use OCP\IUser; +use Symfony\Component\EventDispatcher\GenericEvent; /** * @package OCA\Files_External\AppInfo @@ -96,6 +99,20 @@ public function __construct(array $urlParams = []) { $this->getAuthMechanisms(); } + public function registerListeners() { + $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); + $dispatcher->addListener( + IUser::class . '::postDelete', + function (GenericEvent $event) { + /** @var IUser $user */ + $user = $event->getSubject(); + /** @var DBConfigService $config */ + $config = $this->getContainer()->query(DBConfigService::class); + $config->modifyMountsOnUserDelete($user->getUID()); + } + ); + } + /** * @{inheritdoc} */ diff --git a/apps/files_external/lib/Service/DBConfigService.php b/apps/files_external/lib/Service/DBConfigService.php index 65995b21cc40d..0d8d048fc1d9d 100644 --- a/apps/files_external/lib/Service/DBConfigService.php +++ b/apps/files_external/lib/Service/DBConfigService.php @@ -114,6 +114,30 @@ public function getMountsForUser($userId, $groupIds) { return $this->getMountsFromQuery($query); } + public function modifyMountsOnUserDelete(string $uid) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select(['a.mount_id', $builder->func()->count('a.mount_id', 'count')]) + ->from('external_applicable', 'a') + ->rightJoin('a', 'external_applicable', 'b', $builder->expr()->eq('a.mount_id', 'b.mount_id')) + ->where($builder->expr()->andX( // mounts for user + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), + $builder->expr()->eq('a.value', $builder->createNamedParameter($uid)) + ) + ) + ->groupBy(['a.mount_id']); + $stmt = $query->execute(); + $result = $stmt->fetchAll(); + $stmt->closeCursor(); + + foreach ($result as $row) { + if((int)$row['count'] > 1) { + $this->removeApplicable($row['mount_id'], self::APPLICABLE_TYPE_USER, $uid); + } else { + $this->removeMount($row['mount_id']); + } + } + } + /** * Get admin defined mounts * From b45bdfc7ef992ffba4da2c561444195fee80f08e Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Nov 2019 12:28:23 +0100 Subject: [PATCH 2/3] extend with group deletion handling Signed-off-by: Arthur Schiwon --- .../lib/AppInfo/Application.php | 11 ++++++++++ .../lib/Service/DBConfigService.php | 20 +++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/apps/files_external/lib/AppInfo/Application.php b/apps/files_external/lib/AppInfo/Application.php index 2e614dd366a34..9e07339e068b7 100644 --- a/apps/files_external/lib/AppInfo/Application.php +++ b/apps/files_external/lib/AppInfo/Application.php @@ -64,6 +64,7 @@ use OCA\Files_External\Lib\Backend\FTP; use OCA\Files_External\Lib\Backend\Local; use OCP\Files\Config\IUserMountCache; +use OCP\IGroup; use OCP\IUser; use Symfony\Component\EventDispatcher\GenericEvent; @@ -111,6 +112,16 @@ function (GenericEvent $event) { $config->modifyMountsOnUserDelete($user->getUID()); } ); + $dispatcher->addListener( + IGroup::class . '::postDelete', + function (GenericEvent $event) { + /** @var IGroup $group */ + $group = $event->getSubject(); + /** @var DBConfigService $config */ + $config = $this->getContainer()->query(DBConfigService::class); + $config->modifyMountsOnGroupDelete($group->getGID()); + } + ); } /** diff --git a/apps/files_external/lib/Service/DBConfigService.php b/apps/files_external/lib/Service/DBConfigService.php index 0d8d048fc1d9d..9394924d3968e 100644 --- a/apps/files_external/lib/Service/DBConfigService.php +++ b/apps/files_external/lib/Service/DBConfigService.php @@ -114,15 +114,23 @@ public function getMountsForUser($userId, $groupIds) { return $this->getMountsFromQuery($query); } - public function modifyMountsOnUserDelete(string $uid) { + public function modifyMountsOnUserDelete(string $uid): void { + $this->modifyMountsOnDelete($uid, self::APPLICABLE_TYPE_USER); + } + + public function modifyMountsOnGroupDelete(string $gid): void { + $this->modifyMountsOnDelete($gid, self::APPLICABLE_TYPE_GROUP); + } + + protected function modifyMountsOnDelete(string $applicableId, int $applicableType): void { $builder = $this->connection->getQueryBuilder(); $query = $builder->select(['a.mount_id', $builder->func()->count('a.mount_id', 'count')]) ->from('external_applicable', 'a') ->rightJoin('a', 'external_applicable', 'b', $builder->expr()->eq('a.mount_id', 'b.mount_id')) - ->where($builder->expr()->andX( // mounts for user - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), - $builder->expr()->eq('a.value', $builder->createNamedParameter($uid)) - ) + ->where($builder->expr()->andX( + $builder->expr()->eq('a.type', $builder->createNamedParameter($applicableType, IQueryBuilder::PARAM_INT)), + $builder->expr()->eq('a.value', $builder->createNamedParameter($applicableId)) + ) ) ->groupBy(['a.mount_id']); $stmt = $query->execute(); @@ -131,7 +139,7 @@ public function modifyMountsOnUserDelete(string $uid) { foreach ($result as $row) { if((int)$row['count'] > 1) { - $this->removeApplicable($row['mount_id'], self::APPLICABLE_TYPE_USER, $uid); + $this->removeApplicable($row['mount_id'], $applicableType, $applicableId); } else { $this->removeMount($row['mount_id']); } From 7242057c5ac951ed18a293770500b1a37e0c4b65 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 5 Dec 2019 14:43:58 +0100 Subject: [PATCH 3/3] rejig right to left join for sqlite's satisfaction Signed-off-by: Arthur Schiwon --- apps/files_external/lib/Service/DBConfigService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/files_external/lib/Service/DBConfigService.php b/apps/files_external/lib/Service/DBConfigService.php index 9394924d3968e..2fc23af9168e7 100644 --- a/apps/files_external/lib/Service/DBConfigService.php +++ b/apps/files_external/lib/Service/DBConfigService.php @@ -126,10 +126,10 @@ protected function modifyMountsOnDelete(string $applicableId, int $applicableTyp $builder = $this->connection->getQueryBuilder(); $query = $builder->select(['a.mount_id', $builder->func()->count('a.mount_id', 'count')]) ->from('external_applicable', 'a') - ->rightJoin('a', 'external_applicable', 'b', $builder->expr()->eq('a.mount_id', 'b.mount_id')) + ->leftJoin('a', 'external_applicable', 'b', $builder->expr()->eq('a.mount_id', 'b.mount_id')) ->where($builder->expr()->andX( - $builder->expr()->eq('a.type', $builder->createNamedParameter($applicableType, IQueryBuilder::PARAM_INT)), - $builder->expr()->eq('a.value', $builder->createNamedParameter($applicableId)) + $builder->expr()->eq('b.type', $builder->createNamedParameter($applicableType, IQueryBuilder::PARAM_INT)), + $builder->expr()->eq('b.value', $builder->createNamedParameter($applicableId)) ) ) ->groupBy(['a.mount_id']);