88
99namespace OCA \GroupFolders \ACL ;
1010
11+ use OCA \GroupFolders \ACL \UserMapping \IUserMappingManager ;
1112use OCA \GroupFolders \Trash \TrashManager ;
1213use OCP \Cache \CappedMemoryCache ;
1314use OCP \Constants ;
@@ -22,6 +23,7 @@ class ACLManager {
2223 public function __construct (
2324 private readonly RuleManager $ ruleManager ,
2425 private readonly TrashManager $ trashManager ,
26+ private readonly IUserMappingManager $ userMappingManager ,
2527 private readonly LoggerInterface $ logger ,
2628 private readonly IUser $ user ,
2729 private readonly \Closure $ rootFolderProvider ,
@@ -85,7 +87,7 @@ private function getRelevantPaths(string $path): array {
8587 $ fromTrashbin = str_starts_with ($ path , '__groupfolders/trash/ ' );
8688 if ($ fromTrashbin ) {
8789 /* Exploded path will look like ["__groupfolders", "trash", "1", "folderName.d2345678", "rest/of/the/path.txt"] */
88- [,, $ groupFolderId ,$ rootTrashedItemName ] = explode ('/ ' , $ path , 5 );
90+ [, , $ groupFolderId , $ rootTrashedItemName ] = explode ('/ ' , $ path , 5 );
8991 $ groupFolderId = (int )$ groupFolderId ;
9092 /* Remove the date part */
9193 $ separatorPos = strrpos ($ rootTrashedItemName , '.d ' );
@@ -143,6 +145,20 @@ public function getACLPermissionsForPath(string $path): int {
143145 return $ this ->calculatePermissionsForPath ($ rules );
144146 }
145147
148+ /**
149+ * Check what the effective permissions would be for the current user for a path would be with a new set of rules
150+ *
151+ * @param list<Rule> $newRules
152+ */
153+ public function testACLPermissionsForPath (string $ path , array $ newRules ): int {
154+ $ path = ltrim ($ path , '/ ' );
155+ $ rules = $ this ->getRules ($ this ->getRelevantPaths ($ path ));
156+
157+ $ rules [$ path ] = $ this ->filterApplicableRulesToUser ($ newRules );
158+
159+ return $ this ->calculatePermissionsForPath ($ rules );
160+ }
161+
146162 /**
147163 * @param array<string, Rule[]> $rules list of rules per path
148164 */
@@ -229,4 +245,25 @@ public function getPermissionsForTree(string $path): int {
229245 public function preloadRulesForFolder (string $ path ): void {
230246 $ this ->ruleManager ->getRulesForFilesByParent ($ this ->user , $ this ->getRootStorageId (), $ path );
231247 }
248+
249+ /**
250+ * Filter a list to only the rules applicable to the current user
251+ *
252+ * @param list<Rule> $rules
253+ * @return list<Rule>
254+ */
255+ private function filterApplicableRulesToUser (array $ rules ): array {
256+ $ userMappings = $ this ->userMappingManager ->getMappingsForUser ($ this ->user );
257+ return array_values (array_filter ($ rules , function (Rule $ rule ) use ($ userMappings ): bool {
258+ foreach ($ userMappings as $ userMapping ) {
259+ if (
260+ $ userMapping ->getType () == $ rule ->getUserMapping ()->getType () &&
261+ $ userMapping ->getId () == $ rule ->getUserMapping ()->getId ()
262+ ) {
263+ return true ;
264+ }
265+ }
266+ return false ;
267+ }));
268+ }
232269}
0 commit comments