From d558c1dd82be18b8793bbce7c44388489b8158cc Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Sun, 13 Oct 2024 23:14:18 +0200 Subject: [PATCH] added groupfolder ACL manager --- lib/Manager/ACLManager.php | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 lib/Manager/ACLManager.php diff --git a/lib/Manager/ACLManager.php b/lib/Manager/ACLManager.php new file mode 100644 index 0000000..0082e40 --- /dev/null +++ b/lib/Manager/ACLManager.php @@ -0,0 +1,86 @@ +userMappingManager->mappingFromId($data['mapping_type'], $data['mapping_id']); + + if ($mapping) { + return new Rule( + $mapping, + (int)$data['fileid'], + (int)$data['mask'], + (int)$data['permissions'] + ); + } else { + return null; + } + } + + public function getAllRulesForFileId(int $fileId) { + $qb = $this->db->getQueryBuilder(); + + $qb->select(['fileid', 'mapping_type', 'mapping_id', 'mask', 'permissions']) + ->from('group_folders_acl') + ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); + + $rows = $qb->executeQuery()->fetchAll(); + + return array_map($this->createRuleEntityFromRow(...), $rows); + } + + private function ruleMappingComparison(Rule $rule1, Rule $rule2) { + $mapping1 = $rule1->getUserMapping(); + $mapping2 = $rule2->getUserMapping(); + + return $mapping1->getType() <=> $mapping2->getType() ?: $mapping1->getId() <=> $mapping2->getId(); + } + + public function overwriteACLsForFileId(int $fileId, array $rules): array { + $existingRules = $this->getAllRulesForFileId($fileId); + + // new rules to be added + $newRules = array_udiff($rules, $existingRules, $this->ruleMappingComparison(...)); + + // old rules to be deleted + $removedRules = array_udiff($existingRules, $rules, $this->ruleMappingComparison(...)); + + // rules for user or group for which a rule already exists, but it might need to be updated + $potentiallyUpdatedRules = array_uintersect($rules, $existingRules, $this->ruleMappingComparison(...)); + + + foreach($removedRules as $removedRule) { + $this->ruleManager->deleteRule($removedRule); + } + + foreach($newRules as $newRule) { + $this->ruleManager->saveRule($newRule); + } + + foreach($potentiallyUpdatedRules as $potentiallyUpdatedRule) { + $this->ruleManager->saveRule($potentiallyUpdatedRule); + } + + return ["created" => $newRules, "removed" => $removedRules, "updated" => $potentiallyUpdatedRules]; + } + +}