@php use App\Models\DocumentManagerPermission; use App\Services\DocumentManager\FolderCatalog; $userLevel = $request->input('user_level', 'Admin'); if (!$userLevel) { header('Content-Type: application/json'); echo json_encode(['success' => false, 'message' => 'User level is required']); exit; } try { // Get all permissions for this user level $allPermissions = DocumentManagerPermission::where('user_level', $userLevel)->get(); $catalogService = app(FolderCatalog::class); $definedFolders = $catalogService->flattened(); $definedPaths = $definedFolders->pluck('path')->map(fn($path) => trim($path, '/'))->filter()->unique()->values(); // Start with catalog defined folders (in declared order) $allFoldersCollection = collect($definedPaths->all()); // Get all folders from documents directory (not just from permissions) $documentsPath = storage_path('documents'); $actualFolders = []; // Debug: Check if path exists if (!is_dir($documentsPath)) { header('Content-Type: application/json'); echo json_encode([ 'success' => false, 'message' => 'Documents directory not found at: ' . $documentsPath ]); exit; } // Get system folder patterns from config for filtering when needed $exactMatches = config('document-folders.system_patterns.exact', []); $regexPatterns = config('document-folders.system_patterns.regex', []); $items = scandir($documentsPath); foreach ($items as $item) { if ($item !== '.' && $item !== '..' && is_dir($documentsPath . '/' . $item)) { // Filter system folders for non-admin users if ($userLevel !== 'Admin') { $isSystemFolder = in_array($item, $exactMatches); if (!$isSystemFolder) { foreach ($regexPatterns as $pattern) { if (preg_match($pattern, $item)) { $isSystemFolder = true; break; } } } if ($isSystemFolder) { continue; // Skip system folders for non-admin } } // Add main folder $actualFolders[] = $item; // Check for subfolders (one level deep) $subPath = $documentsPath . '/' . $item; if (is_dir($subPath)) { $subItems = scandir($subPath); foreach ($subItems as $subItem) { if ($subItem !== '.' && $subItem !== '..' && is_dir($subPath . '/' . $subItem)) { if ($userLevel !== 'Admin') { $target = $subItem; $isSystemSub = in_array($target, $exactMatches); if (!$isSystemSub) { foreach ($regexPatterns as $pattern) { if (preg_match($pattern, $target) || preg_match($pattern, $item . '/' . $target)) { $isSystemSub = true; break; } } } if ($isSystemSub) { continue; } } // Add subfolder with parent folder prefix $actualFolders[] = $item . '/' . $subItem; } } } } } $actualCollection = collect($actualFolders)->unique()->values(); $extras = $actualCollection->diff($definedPaths)->sort()->values(); $allFoldersCollection = $allFoldersCollection->merge($extras)->unique(); if ($allFoldersCollection->isEmpty()) { header('Content-Type: application/json'); echo json_encode([ 'success' => false, 'message' => 'No folders are defined in the catalog or found on disk.' ]); exit; } $allFolders = $allFoldersCollection->values()->all(); // Get permission types (2-tier system) $permissionTypes = ['read', 'write']; $permissions = []; foreach ($allFolders as $folder) { $permissions[$folder] = []; foreach ($permissionTypes as $permissionType) { $permission = $allPermissions->where('folder_path', $folder) ->where('permission_type', $permissionType) ->first(); if ($permission && $permission->is_allowed) { $permissions[$folder][] = $permissionType; } } } header('Content-Type: application/json'); echo json_encode([ 'success' => true, 'folders' => $allFolders, 'permissions' => $permissions, 'user_level' => $userLevel ]); } catch (Exception $e) { header('Content-Type: application/json'); echo json_encode([ 'success' => false, 'message' => 'Error loading permissions: ' . $e->getMessage() ]); } @endphp